Search in sources :

Example 1 with CliTelemInfo

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

the class MicrosoftStsOAuth2Strategy method getTokenResultFromHttpResponse.

@Override
@NonNull
protected TokenResult getTokenResultFromHttpResponse(@NonNull final HttpResponse response) throws ClientException {
    final String methodName = ":getTokenResultFromHttpResponse";
    Logger.verbose(TAG + methodName, "Getting TokenResult from HttpResponse...");
    MicrosoftStsTokenResponse tokenResponse = null;
    TokenErrorResponse tokenErrorResponse = null;
    if (response.getStatusCode() >= HttpURLConnection.HTTP_BAD_REQUEST) {
        // An error occurred
        tokenErrorResponse = ObjectMapper.deserializeJsonStringToObject(response.getBody(), MicrosoftTokenErrorResponse.class);
        tokenErrorResponse.setStatusCode(response.getStatusCode());
        if (null != response.getHeaders()) {
            tokenErrorResponse.setResponseHeadersJson(HeaderSerializationUtil.toJson(response.getHeaders()));
        }
        tokenErrorResponse.setResponseBody(response.getBody());
    } else {
        tokenResponse = ObjectMapper.deserializeJsonStringToObject(getBodyFromSuccessfulResponse(response.getBody()), MicrosoftStsTokenResponse.class);
    }
    final TokenResult result = new TokenResult(tokenResponse, tokenErrorResponse);
    logResult(TAG, result);
    if (null != response.getHeaders()) {
        final Map<String, List<String>> responseHeaders = response.getHeaders();
        final List<String> cliTelemValues;
        if (null != (cliTelemValues = responseHeaders.get(X_MS_CLITELEM)) && !cliTelemValues.isEmpty()) {
            // Element should only contain 1 value...
            final String cliTelemHeader = cliTelemValues.get(0);
            final CliTelemInfo cliTelemInfo = CliTelemInfo.fromXMsCliTelemHeader(cliTelemHeader);
            // Parse and set the result...
            result.setCliTelemInfo(cliTelemInfo);
            if (null != tokenResponse && null != cliTelemInfo) {
                tokenResponse.setSpeRing(cliTelemInfo.getSpeRing());
                tokenResponse.setRefreshTokenAge(cliTelemInfo.getRefreshTokenAge());
                tokenResponse.setCliTelemErrorCode(cliTelemInfo.getServerErrorCode());
                tokenResponse.setCliTelemSubErrorCode(cliTelemInfo.getServerSubErrorCode());
            }
        }
    }
    return result;
}
Also used : CliTelemInfo(com.microsoft.identity.common.internal.telemetry.CliTelemInfo) MicrosoftTokenErrorResponse(com.microsoft.identity.common.internal.providers.microsoft.MicrosoftTokenErrorResponse) TokenErrorResponse(com.microsoft.identity.common.internal.providers.oauth2.TokenErrorResponse) MicrosoftTokenErrorResponse(com.microsoft.identity.common.internal.providers.microsoft.MicrosoftTokenErrorResponse) TokenResult(com.microsoft.identity.common.internal.providers.oauth2.TokenResult) List(java.util.List) NonNull(androidx.annotation.NonNull)

Example 2 with CliTelemInfo

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

Aggregations

TokenResult (com.microsoft.identity.common.internal.providers.oauth2.TokenResult)2 CliTelemInfo (com.microsoft.identity.common.internal.telemetry.CliTelemInfo)2 NonNull (androidx.annotation.NonNull)1 ICacheRecord (com.microsoft.identity.common.internal.cache.ICacheRecord)1 RefreshTokenRecord (com.microsoft.identity.common.internal.dto.RefreshTokenRecord)1 MicrosoftTokenErrorResponse (com.microsoft.identity.common.internal.providers.microsoft.MicrosoftTokenErrorResponse)1 TokenErrorResponse (com.microsoft.identity.common.internal.providers.oauth2.TokenErrorResponse)1 AcquireTokenResult (com.microsoft.identity.common.internal.result.AcquireTokenResult)1 LocalAuthenticationResult (com.microsoft.identity.common.internal.result.LocalAuthenticationResult)1 CacheEndEvent (com.microsoft.identity.common.internal.telemetry.events.CacheEndEvent)1 List (java.util.List)1