Search in sources :

Example 1 with CacheEndEvent

use of com.microsoft.identity.common.internal.telemetry.events.CacheEndEvent in project microsoft-authentication-library-common-for-android by AzureAD.

the class BaseController method renewAccessToken.

protected void renewAccessToken(@NonNull final SilentTokenCommandParameters parameters, @NonNull final AcquireTokenResult acquireTokenSilentResult, @SuppressWarnings(WarningType.rawtype_warning) @NonNull final OAuth2TokenCache tokenCache, @SuppressWarnings(WarningType.rawtype_warning) @NonNull final OAuth2Strategy strategy, @NonNull final ICacheRecord cacheRecord) throws IOException, ClientException {
    final String methodName = ":renewAccessToken";
    Logger.info(TAG + methodName, "Renewing access token...");
    RefreshTokenRecord refreshTokenRecord = cacheRecord.getRefreshToken();
    logParameters(TAG, parameters);
    final TokenResult tokenResult = performSilentTokenRequest(strategy, refreshTokenRecord, parameters);
    acquireTokenSilentResult.setTokenResult(tokenResult);
    logResult(TAG + methodName, tokenResult);
    if (tokenResult.getSuccess()) {
        Logger.info(TAG + methodName, "Token request was successful");
        // Suppressing unchecked warnings due to casting of rawtypes to generic types of OAuth2TokenCache's instance tokenCache while calling method saveAndLoadAggregatedAccountData
        @SuppressWarnings(WarningType.unchecked_warning) final List<ICacheRecord> savedRecords = tokenCache.saveAndLoadAggregatedAccountData(strategy, getAuthorizationRequest(strategy, parameters), tokenResult.getTokenResponse());
        final ICacheRecord savedRecord = savedRecords.get(0);
        // Create a new AuthenticationResult to hold the saved record
        final LocalAuthenticationResult authenticationResult = new LocalAuthenticationResult(finalizeCacheRecordForResult(savedRecord, parameters.getAuthenticationScheme()), savedRecords, parameters.getSdkType(), false);
        // Set the client telemetry...
        if (null != tokenResult.getCliTelemInfo()) {
            final CliTelemInfo cliTelemInfo = tokenResult.getCliTelemInfo();
            authenticationResult.setSpeRing(cliTelemInfo.getSpeRing());
            authenticationResult.setRefreshTokenAge(cliTelemInfo.getRefreshTokenAge());
            Telemetry.emit(new CacheEndEvent().putSpeInfo(tokenResult.getCliTelemInfo().getSpeRing()));
        } else {
            // we can't put SpeInfo as the CliTelemInfo is null
            Telemetry.emit(new CacheEndEvent());
        }
        // Set the AuthenticationResult on the final result object
        acquireTokenSilentResult.setLocalAuthenticationResult(authenticationResult);
    } else {
        if (tokenResult.getErrorResponse() != null) {
            final String errorCode = tokenResult.getErrorResponse().getError();
            final String subErrorCode = tokenResult.getErrorResponse().getSubError();
            Logger.info(TAG, "Error: " + errorCode + " Suberror: " + subErrorCode);
            if (INVALID_GRANT.equals(errorCode) && BAD_TOKEN.equals(subErrorCode)) {
                boolean isRemoved = tokenCache.removeCredential(cacheRecord.getRefreshToken());
                Logger.info(TAG, "Refresh token is invalid, " + "attempting to delete the RT from cache, result:" + isRemoved);
            }
        } else {
            Logger.warn(TAG, "Invalid state, No token success or error response on the token result");
        }
    }
}
Also used : CliTelemInfo(com.microsoft.identity.common.internal.telemetry.CliTelemInfo) ICacheRecord(com.microsoft.identity.common.internal.cache.ICacheRecord) TokenResult(com.microsoft.identity.common.internal.providers.oauth2.TokenResult) AcquireTokenResult(com.microsoft.identity.common.internal.result.AcquireTokenResult) CacheEndEvent(com.microsoft.identity.common.internal.telemetry.events.CacheEndEvent) RefreshTokenRecord(com.microsoft.identity.common.internal.dto.RefreshTokenRecord) LocalAuthenticationResult(com.microsoft.identity.common.internal.result.LocalAuthenticationResult)

Example 2 with CacheEndEvent

use of com.microsoft.identity.common.internal.telemetry.events.CacheEndEvent 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)

Aggregations

RefreshTokenRecord (com.microsoft.identity.common.internal.dto.RefreshTokenRecord)2 CacheEndEvent (com.microsoft.identity.common.internal.telemetry.events.CacheEndEvent)2 ICacheRecord (com.microsoft.identity.common.internal.cache.ICacheRecord)1 AccessTokenRecord (com.microsoft.identity.common.internal.dto.AccessTokenRecord)1 Credential (com.microsoft.identity.common.internal.dto.Credential)1 IdTokenRecord (com.microsoft.identity.common.internal.dto.IdTokenRecord)1 TokenResult (com.microsoft.identity.common.internal.providers.oauth2.TokenResult)1 AcquireTokenResult (com.microsoft.identity.common.internal.result.AcquireTokenResult)1 LocalAuthenticationResult (com.microsoft.identity.common.internal.result.LocalAuthenticationResult)1 CliTelemInfo (com.microsoft.identity.common.internal.telemetry.CliTelemInfo)1 CacheStartEvent (com.microsoft.identity.common.internal.telemetry.events.CacheStartEvent)1