Search in sources :

Example 56 with Credential

use of com.microsoft.identity.common.internal.dto.Credential in project microsoft-authentication-library-common-for-android by AzureAD.

the class SharedPreferencesAccountCredentialCacheTest method testLatestMergedPropertyWins.

@Test
public void testLatestMergedPropertyWins() {
    final RefreshTokenRecord refreshTokenFirst = new RefreshTokenRecord();
    refreshTokenFirst.setCredentialType(CredentialType.RefreshToken.name());
    refreshTokenFirst.setHomeAccountId(HOME_ACCOUNT_ID);
    refreshTokenFirst.setEnvironment(ENVIRONMENT);
    refreshTokenFirst.setClientId(CLIENT_ID);
    refreshTokenFirst.setCachedAt(CACHED_AT);
    refreshTokenFirst.setSecret(SECRET);
    // Create and set some additional field data...
    final String additionalKey = "extra-prop-1";
    final String additionalValue = "extra-value-1";
    final JsonElement additionalValueElement = new JsonPrimitive(additionalValue);
    final Map<String, JsonElement> additionalFields = new HashMap<>();
    additionalFields.put(additionalKey, additionalValueElement);
    refreshTokenFirst.setAdditionalFields(additionalFields);
    // Save the Credential
    mSharedPreferencesAccountCredentialCache.saveCredential(refreshTokenFirst);
    final RefreshTokenRecord refreshTokenSecond = new RefreshTokenRecord();
    refreshTokenSecond.setCredentialType(CredentialType.RefreshToken.name());
    refreshTokenSecond.setHomeAccountId(HOME_ACCOUNT_ID);
    refreshTokenSecond.setEnvironment(ENVIRONMENT);
    refreshTokenSecond.setClientId(CLIENT_ID);
    refreshTokenSecond.setCachedAt(CACHED_AT);
    refreshTokenSecond.setSecret(SECRET);
    // Create and set some additional field data...
    final String additionalKey2 = "extra-prop-1";
    final String additionalValue2 = "extra-value-2";
    final JsonElement additionalValueElement2 = new JsonPrimitive(additionalValue2);
    final Map<String, JsonElement> additionalFields2 = new HashMap<>();
    additionalFields2.put(additionalKey2, additionalValueElement2);
    refreshTokenSecond.setAdditionalFields(additionalFields2);
    // Save the Credential
    mSharedPreferencesAccountCredentialCache.saveCredential(refreshTokenSecond);
    // Synthesize a cache key for it
    final String credentialCacheKey = mDelegate.generateCacheKey(refreshTokenFirst);
    // Resurrect the Credential
    final Credential restoredIdToken = mSharedPreferencesAccountCredentialCache.getCredential(credentialCacheKey);
    assertTrue(refreshTokenFirst.equals(restoredIdToken));
    assertEquals(additionalValue2, restoredIdToken.getAdditionalFields().get(additionalKey).getAsString());
}
Also used : Credential(com.microsoft.identity.common.internal.dto.Credential) JsonPrimitive(com.google.gson.JsonPrimitive) HashMap(java.util.HashMap) JsonElement(com.google.gson.JsonElement) PrimaryRefreshTokenRecord(com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord) RefreshTokenRecord(com.microsoft.identity.common.internal.dto.RefreshTokenRecord) Test(org.junit.Test)

Example 57 with Credential

use of com.microsoft.identity.common.internal.dto.Credential in project microsoft-authentication-library-common-for-android by AzureAD.

the class SharedPreferencesAccountCredentialCacheTest method saveCredentialNoHomeAccountId.

@Test
public void saveCredentialNoHomeAccountId() {
    final RefreshTokenRecord refreshToken = new RefreshTokenRecord();
    refreshToken.setCredentialType(CredentialType.RefreshToken.name());
    refreshToken.setEnvironment(ENVIRONMENT);
    refreshToken.setClientId(CLIENT_ID);
    refreshToken.setSecret(SECRET);
    refreshToken.setTarget(TARGET);
    // Save the Credential
    mSharedPreferencesAccountCredentialCache.saveCredential(refreshToken);
    // Synthesize a cache key for it
    final String credentialCacheKey = mDelegate.generateCacheKey(refreshToken);
    // Resurrect the Credential
    final Credential restoredRefreshToken = mSharedPreferencesAccountCredentialCache.getCredential(credentialCacheKey);
    assertTrue(refreshToken.equals(restoredRefreshToken));
}
Also used : Credential(com.microsoft.identity.common.internal.dto.Credential) PrimaryRefreshTokenRecord(com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord) RefreshTokenRecord(com.microsoft.identity.common.internal.dto.RefreshTokenRecord) Test(org.junit.Test)

Example 58 with Credential

use of com.microsoft.identity.common.internal.dto.Credential in project microsoft-authentication-library-common-for-android by AzureAD.

the class MsalOAuth2TokenCache method load.

@Override
public ICacheRecord load(@NonNull final String clientId, @Nullable final String target, @NonNull final AccountRecord account, @NonNull final AbstractAuthenticationScheme authScheme) {
    Telemetry.emit(new CacheStartEvent());
    final boolean isMultiResourceCapable = MicrosoftAccount.AUTHORITY_TYPE_V1_V2.equals(account.getAuthorityType());
    // 'Preloading' our credentials to avoid repeated expensive cache hits
    final List<Credential> allCredentials = mAccountCredentialCache.getCredentials();
    // Load the AccessTokens
    final List<Credential> accessTokens = mAccountCredentialCache.getCredentialsFilteredBy(account.getHomeAccountId(), account.getEnvironment(), getAccessTokenCredentialTypeForAuthenticationScheme(authScheme), clientId, account.getRealm(), target, authScheme.getName(), allCredentials);
    // Load the RefreshTokens
    List<Credential> refreshTokens = mAccountCredentialCache.getCredentialsFilteredBy(account.getHomeAccountId(), account.getEnvironment(), CredentialType.RefreshToken, clientId, isMultiResourceCapable ? // wildcard (*)
    null : account.getRealm(), isMultiResourceCapable ? // wildcard (*)
    null : target, // not applicable
    null, allCredentials);
    if (refreshTokens.isEmpty()) {
        // If we didn't find an RT in the cache, this could be a "TSL-seed" or "dual-client stack"
        // scenario
        // 
        // Defining these terms:
        // TSL-seed: another 1P TSL integrated app has put a token into our cache so we can
        // pick it up
        // 
        // Dual-Client stack: two FoCI-enabled app registrations are sharing a single binary
        // and accordingly, can share RTs.
        // Examples for this might be TFL/TFW - which uses multiple client ids to enable
        // different scenarios depending on enterprise vs. consumer usage
        // Unlike the broker, where we check if an app is FoCI prior to making a network call
        // with an arbitrary FoCI RT we find in the cache, if we're in standalone mode and find
        // a FoCI RT in the cache, the current app must also be FoCI (!!!)
        // 
        // Making the assumption that the current client id can use any FoCI RT we find in the
        // cache is strictly contingent that app developers NOT mix FoCI/non-FoCI registrations
        // into same binary. If you do this, you'll get confusing errors that the RT used doesn't
        // match the client app registration. This assumption means we don't need to implement
        // "FoCI probing" and/or track FoCI app meta
        final Credential fallbackFrt = getFamilyRefreshTokenForAccount(account);
        if (null != fallbackFrt) {
            refreshTokens = new ArrayList<>();
            refreshTokens.add(fallbackFrt);
        }
    }
    // Load the IdTokens
    final List<Credential> idTokens = mAccountCredentialCache.getCredentialsFilteredBy(account.getHomeAccountId(), account.getEnvironment(), IdToken, clientId, account.getRealm(), // wildcard (*),
    null, // not applicable
    null, allCredentials);
    // Load the v1 IdTokens
    final List<Credential> v1IdTokens = mAccountCredentialCache.getCredentialsFilteredBy(account.getHomeAccountId(), account.getEnvironment(), CredentialType.V1IdToken, clientId, account.getRealm(), // wildcard (*)
    null, // not applicable
    null, allCredentials);
    final CacheRecord.CacheRecordBuilder result = CacheRecord.builder();
    result.account(account);
    result.accessToken(accessTokens.isEmpty() ? null : (AccessTokenRecord) accessTokens.get(0));
    result.refreshToken(refreshTokens.isEmpty() ? null : (RefreshTokenRecord) refreshTokens.get(0));
    result.idToken(idTokens.isEmpty() ? null : (IdTokenRecord) idTokens.get(0));
    result.v1IdToken(v1IdTokens.isEmpty() ? null : (IdTokenRecord) v1IdTokens.get(0));
    Telemetry.emit(new CacheEndEvent().putCacheRecordStatus(result.build()));
    return result.build();
}
Also used : IdTokenRecord(com.microsoft.identity.common.internal.dto.IdTokenRecord) Credential(com.microsoft.identity.common.internal.dto.Credential) CacheEndEvent(com.microsoft.identity.common.internal.telemetry.events.CacheEndEvent) RefreshTokenRecord(com.microsoft.identity.common.internal.dto.RefreshTokenRecord) AccessTokenRecord(com.microsoft.identity.common.internal.dto.AccessTokenRecord) CacheStartEvent(com.microsoft.identity.common.internal.telemetry.events.CacheStartEvent)

Example 59 with Credential

use of com.microsoft.identity.common.internal.dto.Credential in project microsoft-authentication-library-common-for-android by AzureAD.

the class MsalOAuth2TokenCache method deleteAccessTokensWithIntersectingScopes.

private void deleteAccessTokensWithIntersectingScopes(final AccessTokenRecord referenceToken) {
    final String methodName = "deleteAccessTokensWithIntersectingScopes";
    final List<Credential> accessTokens = mAccountCredentialCache.getCredentialsFilteredBy(referenceToken.getHomeAccountId(), referenceToken.getEnvironment(), CredentialType.fromString(referenceToken.getCredentialType()), referenceToken.getClientId(), referenceToken.getRealm(), // Wildcard (*)
    null, referenceToken.getAccessTokenType(), referenceToken.getRequestedClaims());
    Logger.verbose(TAG + ":" + methodName, "Inspecting " + accessTokens.size() + " accessToken[s].");
    for (final Credential accessToken : accessTokens) {
        if (scopesIntersect(referenceToken, (AccessTokenRecord) accessToken, true)) {
            Logger.infoPII(TAG + ":" + methodName, "Removing credential: " + accessToken);
            mAccountCredentialCache.removeCredential(accessToken);
        }
    }
}
Also used : Credential(com.microsoft.identity.common.internal.dto.Credential)

Example 60 with Credential

use of com.microsoft.identity.common.internal.dto.Credential in project microsoft-authentication-library-common-for-android by AzureAD.

the class MsalOAuth2TokenCache method getIdTokensForAccountRecord.

@Override
public List<IdTokenRecord> getIdTokensForAccountRecord(@Nullable String clientId, @NonNull AccountRecord accountRecord) {
    final List<IdTokenRecord> result = new ArrayList<>();
    final List<Credential> allCredentials = mAccountCredentialCache.getCredentials();
    final List<Credential> idTokens = mAccountCredentialCache.getCredentialsFilteredBy(accountRecord.getHomeAccountId(), accountRecord.getEnvironment(), IdToken, // If null, behaves as wildcard
    clientId, accountRecord.getRealm(), // wildcard (*),
    null, // not applicable
    null, allCredentials);
    idTokens.addAll(mAccountCredentialCache.getCredentialsFilteredBy(accountRecord.getHomeAccountId(), accountRecord.getEnvironment(), CredentialType.V1IdToken, clientId, accountRecord.getRealm(), // wildcard (*)
    null, // not applicable
    null, allCredentials));
    for (final Credential credential : idTokens) {
        if (credential instanceof IdTokenRecord) {
            result.add((IdTokenRecord) credential);
        }
    }
    return Collections.unmodifiableList(result);
}
Also used : IdTokenRecord(com.microsoft.identity.common.internal.dto.IdTokenRecord) Credential(com.microsoft.identity.common.internal.dto.Credential) ArrayList(java.util.ArrayList)

Aggregations

Credential (com.microsoft.identity.common.internal.dto.Credential)64 Test (org.junit.Test)45 RefreshTokenRecord (com.microsoft.identity.common.internal.dto.RefreshTokenRecord)33 PrimaryRefreshTokenRecord (com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord)31 AccessTokenRecord (com.microsoft.identity.common.internal.dto.AccessTokenRecord)30 AccountRecord (com.microsoft.identity.common.internal.dto.AccountRecord)10 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)9 JsonElement (com.google.gson.JsonElement)7 JsonPrimitive (com.google.gson.JsonPrimitive)7 IdTokenRecord (com.microsoft.identity.common.internal.dto.IdTokenRecord)7 CredentialType (com.microsoft.identity.common.internal.dto.CredentialType)3 Map (java.util.Map)3 Nullable (androidx.annotation.Nullable)2 HashSet (java.util.HashSet)2 NonNull (androidx.annotation.NonNull)1 ClientException (com.microsoft.identity.common.exception.ClientException)1 SharedPreferencesFileManager (com.microsoft.identity.common.internal.cache.SharedPreferencesFileManager)1 CacheEndEvent (com.microsoft.identity.common.internal.telemetry.events.CacheEndEvent)1 CacheStartEvent (com.microsoft.identity.common.internal.telemetry.events.CacheStartEvent)1