@@ -11,24 +11,19 @@ import com.auth0.android.authentication.AuthenticationAPIClient
1111import com.auth0.android.authentication.AuthenticationException
1212import com.auth0.android.callback.Callback
1313import com.auth0.android.request.internal.GsonProvider
14- import com.auth0.android.request.internal.Jwt
1514import com.auth0.android.result.APICredentials
1615import com.auth0.android.result.Credentials
1716import com.auth0.android.result.OptionalCredentials
1817import com.auth0.android.result.SSOCredentials
1918import com.auth0.android.result.UserProfile
2019import com.auth0.android.result.toAPICredentials
2120import com.google.gson.Gson
22- import kotlinx.coroutines.CoroutineScope
23- import kotlinx.coroutines.GlobalScope
24- import kotlinx.coroutines.launch
2521import kotlinx.coroutines.suspendCancellableCoroutine
2622import java.lang.ref.WeakReference
27- import java.util.*
23+ import java.util.Date
24+ import java.util.Locale
2825import java.util.concurrent.Executor
2926import java.util.concurrent.atomic.AtomicLong
30- import kotlin.collections.component1
31- import kotlin.collections.component2
3227import kotlin.coroutines.resume
3328import kotlin.coroutines.resumeWithException
3429
@@ -143,13 +138,19 @@ public class SecureCredentialsManager @VisibleForTesting(otherwise = VisibleForT
143138 * Stores the given [APICredentials] in the storage for the given audience.
144139 * @param apiCredentials the API Credentials to be stored
145140 * @param audience the audience for which the credentials are stored
141+ * @param scope the scope for which the credentials are stored
146142 */
147- override fun saveApiCredentials (apiCredentials : APICredentials , audience : String ) {
143+ override fun saveApiCredentials (
144+ apiCredentials : APICredentials ,
145+ audience : String ,
146+ scope : String?
147+ ) {
148+ val key = getAPICredentialsKey(audience, scope)
148149 val json = gson.toJson(apiCredentials)
149150 try {
150151 val encrypted = crypto.encrypt(json.toByteArray())
151152 val encryptedEncoded = Base64 .encodeToString(encrypted, Base64 .DEFAULT )
152- storage.store(audience , encryptedEncoded)
153+ storage.store(key , encryptedEncoded)
153154 } catch (e: IncompatibleDeviceException ) {
154155 throw CredentialsManagerException (
155156 CredentialsManagerException .Code .INCOMPATIBLE_DEVICE ,
@@ -270,7 +271,7 @@ public class SecureCredentialsManager @VisibleForTesting(otherwise = VisibleForT
270271 if (credentials == null ) {
271272 return null
272273 }
273- return credentials.user
274+ return credentials.user
274275 }
275276
276277 /* *
@@ -908,7 +909,7 @@ public class SecureCredentialsManager @VisibleForTesting(otherwise = VisibleForT
908909 callback : Callback <APICredentials , CredentialsManagerException >
909910 ) {
910911 serialExecutor.execute {
911- val encryptedEncodedJson = storage.retrieveString(audience)
912+ val encryptedEncodedJson = storage.retrieveString(getAPICredentialsKey( audience, scope) )
912913 // Check if existing api credentials are present and valid
913914 encryptedEncodedJson?.let { encryptedEncoded ->
914915 val encrypted = Base64 .decode(encryptedEncoded, Base64 .DEFAULT )
@@ -938,7 +939,10 @@ public class SecureCredentialsManager @VisibleForTesting(otherwise = VisibleForT
938939
939940 val expiresAt = apiCredentials.expiresAt.time
940941 val willAccessTokenExpire = willExpire(expiresAt, minTtl.toLong())
941- val scopeChanged = hasScopeChanged(apiCredentials.scope, scope)
942+ val scopeChanged = hasScopeChanged(
943+ apiCredentials.scope, scope,
944+ ignoreOpenid = scope?.contains(" openid" ) == false
945+ )
942946 val hasExpired = hasExpired(apiCredentials.expiresAt.time)
943947 if (! hasExpired && ! willAccessTokenExpire && ! scopeChanged) {
944948 callback.onSuccess(apiCredentials)
@@ -993,7 +997,7 @@ public class SecureCredentialsManager @VisibleForTesting(otherwise = VisibleForT
993997 idToken = newCredentials.idToken
994998 )
995999 )
996- saveApiCredentials(newApiCredentials, audience)
1000+ saveApiCredentials(newApiCredentials, audience, scope )
9971001 callback.onSuccess(newApiCredentials)
9981002
9991003 } catch (error: AuthenticationException ) {
@@ -1138,7 +1142,7 @@ public class SecureCredentialsManager @VisibleForTesting(otherwise = VisibleForT
11381142 internal fun isBiometricSessionValid (): Boolean {
11391143 val lastAuth = lastBiometricAuthTime.get()
11401144 if (lastAuth == NO_SESSION ) return false // No session exists
1141-
1145+
11421146 return when (val policy = biometricPolicy) {
11431147 is BiometricPolicy .Session ,
11441148 is BiometricPolicy .AppLifecycle -> {
@@ -1149,6 +1153,7 @@ public class SecureCredentialsManager @VisibleForTesting(otherwise = VisibleForT
11491153 } * 1000L
11501154 System .currentTimeMillis() - lastAuth < timeoutMillis
11511155 }
1156+
11521157 is BiometricPolicy .Always -> false
11531158 }
11541159 }
0 commit comments