Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove callbacks #157

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions descopesdk/src/main/java/com/descope/internal/routes/Auth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,28 @@ import com.descope.types.DescopeTenant
import com.descope.types.DescopeUser
import com.descope.types.RevokeType
import com.descope.types.RefreshResponse
import com.descope.types.Result

internal class Auth(private val client: DescopeClient) : DescopeAuth {

override suspend fun me(refreshJwt: String): DescopeUser =
client.me(refreshJwt).convert()

override fun me(refreshJwt: String, callback: (Result<DescopeUser>) -> Unit) = wrapCoroutine(callback) {
me(refreshJwt)
}

override suspend fun tenants(dct: Boolean, tenantIds: List<String>, refreshJwt: String): List<DescopeTenant> =
client.tenants(dct, tenantIds, refreshJwt).convert()

override fun tenants(dct: Boolean, tenantIds: List<String>, refreshJwt: String, callback: (Result<List<DescopeTenant>>) -> Unit) = wrapCoroutine(callback) {
tenants(dct, tenantIds, refreshJwt)
}

override suspend fun refreshSession(refreshJwt: String): RefreshResponse =
client.refresh(refreshJwt).toRefreshResponse()

override fun refreshSession(refreshJwt: String, callback: (Result<RefreshResponse>) -> Unit) = wrapCoroutine(callback) {
refreshSession(refreshJwt)
}

override suspend fun revokeSessions(revokeType: RevokeType, refreshJwt: String) {
client.logout(refreshJwt, revokeType)
}

override fun revokeSessions(revoke: RevokeType, refreshJwt: String, callback: (Result<Unit>) -> Unit) = wrapCoroutine(callback) {
revokeSessions(revoke, refreshJwt)
}

// Deprecated

@Deprecated(message = "Use revokeSessions instead", replaceWith = ReplaceWith("revokeSessions(RevokeType.CurrentSession, refreshJwt)"))
override suspend fun logout(refreshJwt: String) =
revokeSessions(RevokeType.CurrentSession, refreshJwt)

@Deprecated(message = "Use revokeSessions instead", replaceWith = ReplaceWith("revokeSessions(RevokeType.CurrentSession, refreshJwt, callback)"))
override fun logout(refreshJwt: String, callback: (Result<Unit>) -> Unit) =
revokeSessions(RevokeType.CurrentSession, refreshJwt, callback)

}

internal fun TenantsResponse.convert(): List<DescopeTenant> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.descope.sdk.DescopeLogger.Level.Info
import com.descope.types.AuthenticationResponse
import com.descope.types.DescopeException
import com.descope.types.EnchantedLinkResponse
import com.descope.types.Result
import com.descope.types.SignInOptions
import com.descope.types.SignUpDetails
import com.descope.types.UpdateOptions
Expand All @@ -21,38 +20,18 @@ internal class EnchantedLink(override val client: DescopeClient) : Route, Descop
override suspend fun signUp(loginId: String, details: SignUpDetails?, uri: String?): EnchantedLinkResponse =
client.enchantedLinkSignUp(loginId, details, uri).convert()

override fun signUp(loginId: String, details: SignUpDetails?, uri: String?, callback: (Result<EnchantedLinkResponse>) -> Unit) = wrapCoroutine(callback) {
signUp(loginId, details, uri)
}

override suspend fun signIn(loginId: String, uri: String?, options: List<SignInOptions>?): EnchantedLinkResponse =
client.enchantedLinkSignIn(loginId, uri, options).convert()

override fun signIn(loginId: String, uri: String?, options: List<SignInOptions>?, callback: (Result<EnchantedLinkResponse>) -> Unit) = wrapCoroutine(callback) {
signIn(loginId, uri, options)
}

override suspend fun signUpOrIn(loginId: String, uri: String?, options: List<SignInOptions>?): EnchantedLinkResponse =
client.enchantedLinkSignUpOrIn(loginId, uri, options).convert()

override fun signUpOrIn(loginId: String, uri: String?, options: List<SignInOptions>?, callback: (Result<EnchantedLinkResponse>) -> Unit) = wrapCoroutine(callback) {
signUpOrIn(loginId, uri, options)
}

override suspend fun updateEmail(email: String, loginId: String, uri: String?, refreshJwt: String, options: UpdateOptions?): EnchantedLinkResponse =
client.enchantedLinkUpdateEmail(email, loginId, uri, refreshJwt, options).convert()

override fun updateEmail(email: String, loginId: String, uri: String?, refreshJwt: String, options: UpdateOptions?, callback: (Result<EnchantedLinkResponse>) -> Unit) = wrapCoroutine(callback) {
updateEmail(email, loginId, uri, refreshJwt, options)
}

override suspend fun checkForSession(pendingRef: String): AuthenticationResponse =
client.enchantedLinkCheckForSession(pendingRef).convert()

override fun checkForSession(pendingRef: String, callback: (Result<AuthenticationResponse>) -> Unit) = wrapCoroutine(callback) {
checkForSession(pendingRef)
}

override suspend fun pollForSession(pendingRef: String, timeoutMilliseconds: Long?): AuthenticationResponse {
val pollingEndsAt = System.currentTimeMillis() + (timeoutMilliseconds ?: DEFAULT_POLL_DURATION)
log(Info, "Polling for enchanted link", timeoutMilliseconds ?: DEFAULT_POLL_DURATION)
Expand All @@ -78,10 +57,6 @@ internal class EnchantedLink(override val client: DescopeClient) : Route, Descop
}
}
}

override fun pollForSession(pendingRef: String, timeoutMilliseconds: Long?, callback: (Result<AuthenticationResponse>) -> Unit) = wrapCoroutine(callback) {
pollForSession(pendingRef, timeoutMilliseconds)
}
}

private fun EnchantedLinkServerResponse.convert() = EnchantedLinkResponse(
Expand Down
9 changes: 0 additions & 9 deletions descopesdk/src/main/java/com/descope/internal/routes/Flow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.descope.sdk.DescopeFlow
import com.descope.sdk.DescopeLogger.Level.Info
import com.descope.types.AuthenticationResponse
import com.descope.types.DescopeException
import com.descope.types.Result
import java.security.MessageDigest
import kotlin.random.Random

Expand Down Expand Up @@ -45,10 +44,6 @@ internal class Flow(
startFlowViaBrowser(codeChallenge, context)
}

override fun start(context: Context, callback: (Result<Unit>) -> Unit) = wrapCoroutine(callback) {
start(context)
}

override fun resume(context: Context, incomingUriString: String) {
// create the redirect flow URL by copying all url parameters received from the incoming URI
val incomingUri = Uri.parse(incomingUriString)
Expand All @@ -71,10 +66,6 @@ internal class Flow(
return client.flowExchange(authorizationCode, codeVerifier).convert()
}

override fun exchange(incomingUri: Uri, callback: (Result<AuthenticationResponse>) -> Unit) = wrapCoroutine(callback) {
exchange(incomingUri)
}

// Internal

private fun initVerifierAndChallenge(): String {
Expand Down
25 changes: 0 additions & 25 deletions descopesdk/src/main/java/com/descope/internal/routes/MagicLink.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.descope.internal.http.DescopeClient
import com.descope.sdk.DescopeMagicLink
import com.descope.types.AuthenticationResponse
import com.descope.types.DeliveryMethod
import com.descope.types.Result
import com.descope.types.SignInOptions
import com.descope.types.SignUpDetails
import com.descope.types.UpdateOptions
Expand All @@ -14,42 +13,18 @@ internal class MagicLink(private val client: DescopeClient): DescopeMagicLink {
override suspend fun signUp(method: DeliveryMethod, loginId: String, details: SignUpDetails?, uri: String?): String =
client.magicLinkSignUp(method, loginId, details, uri).convert(method)

override fun signUp(method: DeliveryMethod, loginId: String, details: SignUpDetails?, uri: String?, callback: (Result<String>) -> Unit) = wrapCoroutine(callback) {
signUp(method, loginId, details, uri)
}

override suspend fun signIn(method: DeliveryMethod, loginId: String, uri: String?, options: List<SignInOptions>?): String =
client.magicLinkSignIn(method, loginId, uri, options).convert(method)

override fun signIn(method: DeliveryMethod, loginId: String, uri: String?, options: List<SignInOptions>?, callback: (Result<String>) -> Unit) = wrapCoroutine(callback) {
signIn(method, loginId, uri, options)
}

override suspend fun signUpOrIn(method: DeliveryMethod, loginId: String, uri: String?, options: List<SignInOptions>?): String =
client.magicLinkSignUpOrIn(method, loginId, uri, options).convert(method)

override fun signUpOrIn(method: DeliveryMethod, loginId: String, uri: String?, options: List<SignInOptions>?, callback: (Result<String>) -> Unit) = wrapCoroutine(callback) {
signUpOrIn(method, loginId, uri, options)
}

override suspend fun updateEmail(email: String, loginId: String, uri: String?, refreshJwt: String, options: UpdateOptions?): String =
client.magicLinkUpdateEmail(email, loginId, uri, refreshJwt, options).convert(DeliveryMethod.Email)

override fun updateEmail(email: String, loginId: String, uri: String?, refreshJwt: String, options: UpdateOptions?, callback: (Result<String>) -> Unit) = wrapCoroutine(callback) {
updateEmail(email, loginId, uri, refreshJwt, options)
}

override suspend fun updatePhone(phone: String, method: DeliveryMethod, loginId: String, uri: String?, refreshJwt: String, options: UpdateOptions?): String =
client.magicLinkUpdatePhone(phone, method, loginId, uri, refreshJwt, options).convert(method)

override fun updatePhone(phone: String, method: DeliveryMethod, loginId: String, uri: String?, refreshJwt: String, options: UpdateOptions?, callback: (Result<String>) -> Unit) = wrapCoroutine(callback) {
updatePhone(phone, method, loginId, uri, refreshJwt, options)
}

override suspend fun verify(token: String): AuthenticationResponse = client.magicLinkVerify(token).convert()

override fun verify(token: String, callback: (Result<AuthenticationResponse>) -> Unit) = wrapCoroutine(callback) {
verify(token)
}

}
25 changes: 0 additions & 25 deletions descopesdk/src/main/java/com/descope/internal/routes/OAuth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import com.descope.sdk.DescopeOAuth
import com.descope.types.AuthenticationResponse
import com.descope.types.DescopeException
import com.descope.types.OAuthProvider
import com.descope.types.Result
import com.descope.types.SignInOptions
import com.google.android.libraries.identity.googleid.GetGoogleIdOption
import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential
Expand All @@ -28,50 +27,26 @@ internal class OAuth(override val client: DescopeClient) : Route, DescopeOAuth {
override suspend fun signUp(provider: OAuthProvider, redirectUrl: String?, options: List<SignInOptions>?): String =
client.oauthWebStart(provider, redirectUrl, options, OAuthMethod.SignUp).url

override fun signUp(provider: OAuthProvider, redirectUrl: String?, options: List<SignInOptions>?, callback: (Result<String>) -> Unit) = wrapCoroutine(callback) {
signUp(provider, redirectUrl, options)
}

override suspend fun signIn(provider: OAuthProvider, redirectUrl: String?, options: List<SignInOptions>?): String =
client.oauthWebStart(provider, redirectUrl, options, OAuthMethod.SignIn).url

override fun signIn(provider: OAuthProvider, redirectUrl: String?, options: List<SignInOptions>?, callback: (Result<String>) -> Unit) = wrapCoroutine(callback) {
signIn(provider, redirectUrl, options)
}

override suspend fun signUpOrIn(provider: OAuthProvider, redirectUrl: String?, options: List<SignInOptions>?): String =
client.oauthWebStart(provider, redirectUrl, options, OAuthMethod.SignUpOrIn).url

override fun signUpOrIn(provider: OAuthProvider, redirectUrl: String?, options: List<SignInOptions>?, callback: (Result<String>) -> Unit) = wrapCoroutine(callback) {
signUpOrIn(provider, redirectUrl, options)
}

override suspend fun exchange(code: String): AuthenticationResponse =
client.oauthWebExchange(code).convert()

override fun exchange(code: String, callback: (Result<AuthenticationResponse>) -> Unit) = wrapCoroutine(callback) {
exchange(code)
}

override suspend fun native(context: Context, provider: OAuthProvider, options: List<SignInOptions>?): AuthenticationResponse {
val startResponse = client.oauthNativeStart(provider, options)
val authorizationResponse = nativeAuthorization(context, startResponse)
return client.oauthNativeFinish(provider, authorizationResponse.stateId, authorizationResponse.identityToken).convert()
}

override fun native(context: Context, provider: OAuthProvider, options: List<SignInOptions>?, callback: (Result<AuthenticationResponse>) -> Unit) = wrapCoroutine(callback) {
native(context, provider, options)
}

// Deprecated

@Deprecated(message = "Use signUpOrIn instead", replaceWith = ReplaceWith("signUpOrIn(provider, redirectUrl, options)"))
override suspend fun start(provider: OAuthProvider, redirectUrl: String?, options: List<SignInOptions>?): String =
signUpOrIn(provider, redirectUrl, options)

@Deprecated(message = "Use signUpOrIn instead", replaceWith = ReplaceWith("signUpOrIn(provider, redirectUrl, options, callback)"))
override fun start(provider: OAuthProvider, redirectUrl: String?, options: List<SignInOptions>?, callback: (Result<String>) -> Unit) =
signUpOrIn(provider, redirectUrl, options, callback)
}

internal suspend fun nativeAuthorization(context: Context, responseJson: JSONObject): NativeAuthorizationResponse {
Expand Down
25 changes: 0 additions & 25 deletions descopesdk/src/main/java/com/descope/internal/routes/Otp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.descope.internal.http.DescopeClient
import com.descope.sdk.DescopeOtp
import com.descope.types.AuthenticationResponse
import com.descope.types.DeliveryMethod
import com.descope.types.Result
import com.descope.types.SignInOptions
import com.descope.types.SignUpDetails
import com.descope.types.UpdateOptions
Expand All @@ -14,43 +13,19 @@ internal class Otp(private val client: DescopeClient) : DescopeOtp {
override suspend fun signUp(method: DeliveryMethod, loginId: String, details: SignUpDetails?): String =
client.otpSignUp(method, loginId, details).convert(method)

override fun signUp(method: DeliveryMethod, loginId: String, details: SignUpDetails?, callback: (Result<String>) -> Unit) = wrapCoroutine(callback) {
signUp(method, loginId, details)
}

override suspend fun signIn(method: DeliveryMethod, loginId: String, options: List<SignInOptions>?): String =
client.otpSignIn(method, loginId, options).convert(method)

override fun signIn(method: DeliveryMethod, loginId: String, options: List<SignInOptions>?, callback: (Result<String>) -> Unit) = wrapCoroutine(callback) {
signIn(method, loginId, options)
}

override suspend fun signUpOrIn(method: DeliveryMethod, loginId: String, options: List<SignInOptions>?): String =
client.otpSignUpIn(method, loginId, options).convert(method)

override fun signUpOrIn(method: DeliveryMethod, loginId: String, options: List<SignInOptions>?, callback: (Result<String>) -> Unit) = wrapCoroutine(callback) {
signUpOrIn(method, loginId, options)
}

override suspend fun verify(method: DeliveryMethod, loginId: String, code: String): AuthenticationResponse =
client.otpVerify(method, loginId, code).convert()

override fun verify(method: DeliveryMethod, loginId: String, code: String, callback: (Result<AuthenticationResponse>) -> Unit) = wrapCoroutine(callback) {
verify(method, loginId, code)
}

override suspend fun updateEmail(email: String, loginId: String, refreshJwt: String, options: UpdateOptions?): String =
client.otpUpdateEmail(email, loginId, refreshJwt, options).convert(DeliveryMethod.Email)

override fun updateEmail(email: String, loginId: String, refreshJwt: String, options: UpdateOptions?, callback: (Result<String>) -> Unit) = wrapCoroutine(callback) {
updateEmail(email, loginId, refreshJwt, options)
}

override suspend fun updatePhone(phone: String, method: DeliveryMethod, loginId: String, refreshJwt: String, options: UpdateOptions?): String =
client.otpUpdatePhone(phone, method, loginId, refreshJwt, options).convert(method)

override fun updatePhone(phone: String, method: DeliveryMethod, loginId: String, refreshJwt: String, options: UpdateOptions?, callback: (Result<String>) -> Unit) = wrapCoroutine(callback) {
updatePhone(phone, method, loginId, refreshJwt, options)
}
}

25 changes: 0 additions & 25 deletions descopesdk/src/main/java/com/descope/internal/routes/Password.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,27 @@ import com.descope.internal.http.PasswordPolicyServerResponse
import com.descope.sdk.DescopePassword
import com.descope.types.AuthenticationResponse
import com.descope.types.PasswordPolicy
import com.descope.types.Result
import com.descope.types.SignUpDetails

internal class Password(private val client: DescopeClient) : DescopePassword {

override suspend fun signUp(loginId: String, password: String, details: SignUpDetails?): AuthenticationResponse =
client.passwordSignUp(loginId, password, details).convert()

override fun signUp(loginId: String, password: String, details: SignUpDetails?, callback: (Result<AuthenticationResponse>) -> Unit) = wrapCoroutine(callback) {
signUp(loginId, password, details)
}

override suspend fun signIn(loginId: String, password: String): AuthenticationResponse =
client.passwordSignIn(loginId, password).convert()

override fun signIn(loginId: String, password: String, callback: (Result<AuthenticationResponse>) -> Unit) = wrapCoroutine(callback) {
signIn(loginId, password)
}

override suspend fun update(loginId: String, newPassword: String, refreshJwt: String) =
client.passwordUpdate(loginId, newPassword, refreshJwt)

override fun update(loginId: String, newPassword: String, refreshJwt: String, callback: (Result<Unit>) -> Unit) = wrapCoroutine(callback) {
update(loginId, newPassword, refreshJwt)
}

override suspend fun replace(loginId: String, oldPassword: String, newPassword: String): AuthenticationResponse =
client.passwordReplace(loginId, oldPassword, newPassword).convert()

override fun replace(loginId: String, oldPassword: String, newPassword: String, callback: (Result<AuthenticationResponse>) -> Unit) = wrapCoroutine(callback) {
replace(loginId, oldPassword, newPassword)
}

override suspend fun sendReset(loginId: String, redirectUrl: String?) =
client.passwordSendReset(loginId, redirectUrl)

override fun sendReset(loginId: String, redirectUrl: String?, callback: (Result<Unit>) -> Unit) = wrapCoroutine(callback) {
sendReset(loginId, redirectUrl)
}

override suspend fun getPolicy(): PasswordPolicy =
client.passwordGetPolicy().convert()

override fun getPolicy(callback: (Result<PasswordPolicy>) -> Unit) = wrapCoroutine(callback) {
getPolicy()
}
}

private fun PasswordPolicyServerResponse.convert() = PasswordPolicy(
Expand Down
17 changes: 0 additions & 17 deletions descopesdk/src/main/java/com/descope/internal/routes/Shared.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ import com.descope.types.DeliveryMethod
import com.descope.types.DescopeException
import com.descope.types.DescopeUser
import com.descope.types.RefreshResponse
import com.descope.types.Result
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

internal interface Route {
val client: DescopeClient
Expand Down Expand Up @@ -66,16 +62,3 @@ internal fun MaskedAddressServerResponse.convert(method: DeliveryMethod) = when
DeliveryMethod.Email -> maskedEmail ?: throw DescopeException.decodeError.with(message = "masked email not received")
DeliveryMethod.Sms, DeliveryMethod.Whatsapp -> maskedPhone ?: throw DescopeException.decodeError.with(message = "masked phone not received")
}

@Suppress("OPT_IN_USAGE")
internal fun <T> wrapCoroutine(callback: (Result<T>) -> Unit, coroutine: suspend () -> T) {
GlobalScope.launch(Dispatchers.Main) {
val result = try {
val result = coroutine()
Result.Success(result)
} catch (e: Exception) {
Result.Failure(e)
}
callback(result)
}
}
Loading
Loading