Search in sources :

Example 26 with AccountRecord

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

the class MsalOAuth2TokenCache method setSingleSignOnState.

@Override
public void setSingleSignOnState(final GenericAccount account, final GenericRefreshToken refreshToken) throws ClientException {
    Logger.info(TAG + ":setSingleSignOnState", "Set SSO state called.");
    final AccountRecord accountDto = mAccountCredentialAdapter.asAccount(account);
    final RefreshTokenRecord rt = mAccountCredentialAdapter.asRefreshToken(refreshToken);
    final IdTokenRecord idToken = mAccountCredentialAdapter.asIdToken(account, refreshToken);
    validateCacheArtifacts(accountDto, null, rt, idToken);
    saveAccounts(accountDto);
    saveCredentialsInternal(idToken, rt);
    removeAllRefreshTokensExcept(accountDto, rt);
}
Also used : IdTokenRecord(com.microsoft.identity.common.internal.dto.IdTokenRecord) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) RefreshTokenRecord(com.microsoft.identity.common.internal.dto.RefreshTokenRecord)

Example 27 with AccountRecord

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

the class MsalOAuth2TokenCache method getAccountWithAggregatedAccountDataByLocalAccountId.

@Override
@Nullable
public ICacheRecord getAccountWithAggregatedAccountDataByLocalAccountId(@Nullable String environment, @NonNull String clientId, @NonNull String localAccountId) {
    CacheRecord.CacheRecordBuilder result = null;
    final AccountRecord acct = getAccountByLocalAccountId(environment, clientId, localAccountId);
    if (null != acct) {
        final List<IdTokenRecord> acctIdTokens = getIdTokensForAccountRecord(clientId, acct);
        result = CacheRecord.builder();
        result.account(acct);
        for (final IdTokenRecord idTokenRecord : acctIdTokens) {
            setToCacheRecord(result, idTokenRecord);
        }
        return result.build();
    }
    return null;
}
Also used : IdTokenRecord(com.microsoft.identity.common.internal.dto.IdTokenRecord) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) Nullable(androidx.annotation.Nullable)

Example 28 with AccountRecord

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

the class SharedPreferencesAccountCredentialCache method removeAccount.

@Override
public boolean removeAccount(@NonNull final AccountRecord accountToRemove) {
    Logger.info(TAG, "Removing Account...");
    if (null == accountToRemove) {
        throw new IllegalArgumentException("Param [accountToRemove] cannot be null.");
    }
    final Map<String, AccountRecord> accounts = getAccountsWithKeys();
    boolean accountRemoved = false;
    for (final Map.Entry<String, AccountRecord> entry : accounts.entrySet()) {
        Logger.verbosePII(TAG, "Inspecting: [" + entry.getKey() + "]");
        final IAccountRecord currentAccount = entry.getValue();
        if (currentAccount.equals(accountToRemove)) {
            mSharedPreferencesFileManager.remove(entry.getKey());
            accountRemoved = true;
            break;
        }
    }
    Logger.info(TAG, "Account was removed? [" + accountRemoved + "]");
    return accountRemoved;
}
Also used : IAccountRecord(com.microsoft.identity.common.internal.dto.IAccountRecord) IAccountRecord(com.microsoft.identity.common.internal.dto.IAccountRecord) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) HashMap(java.util.HashMap) Map(java.util.Map)

Example 29 with AccountRecord

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

the class MsalCppOAuth2TokenCache method forceRemoveAccount.

/**
 * Force remove an AccountRecord matching the supplied criteria.
 *
 * @param homeAccountId HomeAccountId of the Account.
 * @param environment   The Environment of the Account.
 * @param realm         The Realm of the Account.
 * @return An {@link AccountDeletionRecord} containing a receipt of the removed Accounts.
 * @throws ClientException
 */
// private by default for production code
@VisibleForTesting
public synchronized AccountDeletionRecord forceRemoveAccount(@NonNull final String homeAccountId, @Nullable final String environment, @Nullable final String realm) throws ClientException {
    validateNonNull(homeAccountId, "homeAccountId");
    final boolean mustMatchOnEnvironment = !StringExtensions.isNullOrBlank(environment);
    final boolean mustMatchOnRealm = !StringExtensions.isNullOrBlank(realm);
    final List<AccountRecord> removedAccounts = new ArrayList<>();
    for (final AccountRecord accountRecord : getAllAccounts()) {
        boolean matches = accountRecord.getHomeAccountId().equals(homeAccountId);
        if (mustMatchOnEnvironment) {
            matches = matches && accountRecord.getEnvironment().equals(environment);
        }
        if (mustMatchOnRealm) {
            matches = matches && accountRecord.getRealm().equals(realm);
        }
        if (matches) {
            // Delete the AccountRecord...
            final boolean accountRemoved = getAccountCredentialCache().removeAccount(accountRecord);
            if (accountRemoved) {
                removedAccounts.add(accountRecord);
            }
        }
    }
    return new AccountDeletionRecord(removedAccounts);
}
Also used : AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) ArrayList(java.util.ArrayList) VisibleForTesting(androidx.annotation.VisibleForTesting)

Example 30 with AccountRecord

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

the class MsalOAuth2TokenCache method save.

@Override
public ICacheRecord save(@NonNull final GenericOAuth2Strategy oAuth2Strategy, @NonNull final GenericAuthorizationRequest request, @NonNull final GenericTokenResponse response) throws ClientException {
    // Create the Account
    final AccountRecord accountToSave = mAccountCredentialAdapter.createAccount(oAuth2Strategy, request, response);
    // Create the AccessToken
    final AccessTokenRecord accessTokenToSave = mAccountCredentialAdapter.createAccessToken(oAuth2Strategy, request, response);
    // Create the RefreshToken
    final RefreshTokenRecord refreshTokenToSave = mAccountCredentialAdapter.createRefreshToken(oAuth2Strategy, request, response);
    // Create the IdToken
    final IdTokenRecord idTokenToSave = mAccountCredentialAdapter.createIdToken(oAuth2Strategy, request, response);
    // Check that everything we're about to save is schema-compliant...
    validateCacheArtifacts(accountToSave, accessTokenToSave, refreshTokenToSave, idTokenToSave);
    // Save the Account and Credentials...
    saveAccounts(accountToSave);
    saveCredentialsInternal(accessTokenToSave, refreshTokenToSave, idTokenToSave);
    // Remove old refresh tokens (except for the one we just saved) if it's MRRT or FRT
    removeAllRefreshTokensExcept(accountToSave, refreshTokenToSave);
    final CacheRecord.CacheRecordBuilder result = CacheRecord.builder();
    result.account(accountToSave);
    result.accessToken(accessTokenToSave);
    result.refreshToken(refreshTokenToSave);
    setToCacheRecord(result, idTokenToSave);
    return result.build();
}
Also used : IdTokenRecord(com.microsoft.identity.common.internal.dto.IdTokenRecord) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) RefreshTokenRecord(com.microsoft.identity.common.internal.dto.RefreshTokenRecord) AccessTokenRecord(com.microsoft.identity.common.internal.dto.AccessTokenRecord)

Aggregations

AccountRecord (com.microsoft.identity.common.internal.dto.AccountRecord)92 Test (org.junit.Test)61 ArrayList (java.util.ArrayList)20 ICacheRecord (com.microsoft.identity.common.internal.cache.ICacheRecord)11 AccessTokenRecord (com.microsoft.identity.common.internal.dto.AccessTokenRecord)11 OAuth2TokenCache (com.microsoft.identity.common.internal.providers.oauth2.OAuth2TokenCache)11 Credential (com.microsoft.identity.common.internal.dto.Credential)10 IdTokenRecord (com.microsoft.identity.common.internal.dto.IdTokenRecord)10 RefreshTokenRecord (com.microsoft.identity.common.internal.dto.RefreshTokenRecord)10 AccountDeletionRecord (com.microsoft.identity.common.internal.cache.AccountDeletionRecord)9 PrimaryRefreshTokenRecord (com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord)7 Nullable (androidx.annotation.Nullable)6 MsalOAuth2TokenCache (com.microsoft.identity.common.internal.cache.MsalOAuth2TokenCache)5 HashMap (java.util.HashMap)5 IAccountRecord (com.microsoft.identity.common.internal.dto.IAccountRecord)4 Context (android.content.Context)3 NonNull (androidx.annotation.NonNull)3 JsonElement (com.google.gson.JsonElement)3 JsonPrimitive (com.google.gson.JsonPrimitive)3 BearerAuthenticationSchemeInternal (com.microsoft.identity.common.internal.authscheme.BearerAuthenticationSchemeInternal)3