Skip to content

Commit

Permalink
chore: updated SecureCredentialsManager to return credentials without…
Browse files Browse the repository at this point in the history
… authenticaton as well, in cases where customers just want to encode and encrypt the credentials
  • Loading branch information
desusai7 committed Jul 22, 2024
1 parent 6824cf6 commit 68102e7
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package com.auth0.android.authentication.storage

import androidx.annotation.VisibleForTesting
import androidx.fragment.app.FragmentActivity
import com.auth0.android.authentication.AuthenticationAPIClient
import com.auth0.android.callback.Callback
import com.auth0.android.result.Credentials
import com.auth0.android.util.Clock
import java.util.*
import java.util.concurrent.Executor
import java.util.concurrent.Executors
import kotlin.math.min

/**
* Base class meant to abstract common logic across Credentials Manager implementations.
* The scope of this class is package-private, as it's not meant to be exposed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public class SecureCredentialsManager @VisibleForTesting(otherwise = VisibleForT
storage: Storage,
private val crypto: CryptoUtil,
jwtDecoder: JWTDecoder,
private val fragmentActivity: WeakReference<FragmentActivity>,
private val localAuthenticationOptions: LocalAuthenticationOptions,
private val localAuthenticationManagerFactory: LocalAuthenticationManagerFactory,
private val serialExecutor: Executor,
private val fragmentActivity: WeakReference<FragmentActivity>? = null,
private val localAuthenticationOptions: LocalAuthenticationOptions? = null,
private val localAuthenticationManagerFactory: LocalAuthenticationManagerFactory? = null,
) : BaseCredentialsManager(apiClient, storage, jwtDecoder) {
private val gson: Gson = GsonProvider.gson

Expand All @@ -45,6 +45,28 @@ public class SecureCredentialsManager @VisibleForTesting(otherwise = VisibleForT
* @param apiClient the Auth0 Authentication API Client to handle token refreshment when needed.
* @param storage the storage implementation to use
*/
public constructor(
context: Context,
apiClient: AuthenticationAPIClient,
storage: Storage,
) : this(
apiClient,
storage,
CryptoUtil(context, storage, KEY_ALIAS),
JWTDecoder(),
Executors.newSingleThreadExecutor(),
)


/**
* Creates a new SecureCredentialsManager to handle Credentials with Authentication
*
* @param context a valid context
* @param apiClient the Auth0 Authentication API Client to handle token refreshment when needed.
* @param storage the storage implementation to use
* @param fragmentActivity the FragmentActivity to use for the biometric authentication
* @param localAuthenticationOptions the options of type [LocalAuthenticationOptions] to use for the biometric authentication
*/
public constructor(
context: Context,
apiClient: AuthenticationAPIClient,
Expand All @@ -56,13 +78,12 @@ public class SecureCredentialsManager @VisibleForTesting(otherwise = VisibleForT
storage,
CryptoUtil(context, storage, KEY_ALIAS),
JWTDecoder(),
Executors.newSingleThreadExecutor(),
WeakReference(fragmentActivity),
localAuthenticationOptions,
DefaultLocalAuthenticationManagerFactory(),
Executors.newSingleThreadExecutor(),
DefaultLocalAuthenticationManagerFactory()
)


/**
* Saves the given credentials in the Storage.
*
Expand Down Expand Up @@ -355,38 +376,47 @@ public class SecureCredentialsManager @VisibleForTesting(otherwise = VisibleForT
* @param callback the callback to receive the result in.
*/
public fun getCredentials(
scope: String? = null,
minTtl: Int = 0,
parameters: Map<String, String> = emptyMap(),
headers: Map<String, String> = emptyMap(),
forceRefresh: Boolean = false,
scope: String?,
minTtl: Int,
parameters: Map<String, String>,
headers: Map<String, String>,
forceRefresh: Boolean,
callback: Callback<Credentials, CredentialsManagerException>
) {
if (!hasValidCredentials(minTtl.toLong())) {
callback.onFailure(CredentialsManagerException.NO_CREDENTIALS)
return
}

fragmentActivity.get()?.let { fragmentActivity ->
val localAuthenticationManager = localAuthenticationManagerFactory.create(
activity = fragmentActivity,
authenticationOptions = localAuthenticationOptions,
resultCallback = localAuthenticationResultCallback(
scope,
minTtl,
parameters,
headers,
forceRefresh,
callback
if (fragmentActivity != null && localAuthenticationOptions != null) {
fragmentActivity.get()?.let { fragmentActivity ->
val localAuthenticationManager = localAuthenticationManagerFactory!!.create(
activity = fragmentActivity,
authenticationOptions = localAuthenticationOptions,
resultCallback = localAuthenticationResultCallback(
scope,
minTtl,
parameters,
headers,
forceRefresh,
callback
)
)
)
localAuthenticationManager.authenticate()
} ?: run {
callback.onFailure(CredentialsManagerException.BIOMETRIC_ERROR_NO_ACTIVITY)
localAuthenticationManager.authenticate()
} ?: run {
callback.onFailure(CredentialsManagerException.BIOMETRIC_ERROR_NO_ACTIVITY)
}
return
}

continueGetCredentials(scope, minTtl, parameters, headers, forceRefresh, callback)
}

private val localAuthenticationResultCallback =
{ scope: String?, minTtl: Int, parameters: Map<String, String>, headers: Map<String, String>, forceRefresh: Boolean, callback: Callback<Credentials, CredentialsManagerException> ->
object : Callback<Boolean, CredentialsManagerException> {
override fun onSuccess(result: Boolean) {
getCredentialsFromStorage(
continueGetCredentials(
scope, minTtl, parameters, headers, forceRefresh,
callback
)
Expand Down Expand Up @@ -441,19 +471,14 @@ public class SecureCredentialsManager @VisibleForTesting(otherwise = VisibleForT
}

@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
internal fun getCredentialsFromStorage(
internal fun continueGetCredentials(
scope: String?,
minTtl: Int,
parameters: Map<String, String>,
headers: Map<String, String>,
forceRefresh: Boolean,
callback: Callback<Credentials, CredentialsManagerException>
) {

if (!hasValidCredentials(minTtl.toLong())) {
callback.onFailure(CredentialsManagerException.NO_CREDENTIALS)
return
}
serialExecutor.execute {
val encryptedEncoded = storage.retrieveString(KEY_CREDENTIALS)
if (encryptedEncoded.isNullOrBlank()) {
Expand Down Expand Up @@ -585,7 +610,7 @@ public class SecureCredentialsManager @VisibleForTesting(otherwise = VisibleForT

@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
internal fun clearFragmentActivity() {
fragmentActivity.clear()
fragmentActivity!!.clear()
}

internal companion object {
Expand Down
Loading

0 comments on commit 68102e7

Please sign in to comment.