Search in sources :

Example 11 with TokenResponse

use of com.microsoft.identity.common.internal.providers.oauth2.TokenResponse in project urlaubsverwaltung by synyx.

the class GoogleCalendarSyncProviderServiceTest method getCalendarEventCount.

private Integer getCalendarEventCount() throws GeneralSecurityException, IOException {
    NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    TokenResponse tokenResponse = new TokenResponse();
    tokenResponse.setRefreshToken(REFRESH_TOKEN);
    Credential credential = createCredentialWithRefreshToken(httpTransport, jsonFactory, tokenResponse);
    Calendar calendar = new com.google.api.services.calendar.Calendar.Builder(httpTransport, jsonFactory, credential).setApplicationName(APPLICATION_NAME).build();
    Calendar.Events.List events = calendar.events().list(CALENDAR_ID);
    return events.execute().getItems().size();
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) TokenResponse(com.google.api.client.auth.oauth2.TokenResponse) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) Calendar(com.google.api.services.calendar.Calendar) JsonFactory(com.google.api.client.json.JsonFactory)

Example 12 with TokenResponse

use of com.microsoft.identity.common.internal.providers.oauth2.TokenResponse 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 13 with TokenResponse

use of com.microsoft.identity.common.internal.providers.oauth2.TokenResponse in project microsoft-authentication-library-common-for-android by AzureAD.

the class MicrosoftStsOAuth2Strategy method createAccount.

@Override
public MicrosoftStsAccount createAccount(@NonNull final MicrosoftStsTokenResponse response) {
    final String methodName = ":createAccount";
    Logger.verbose(TAG + methodName, "Creating account from TokenResponse...");
    IDToken idToken = null;
    ClientInfo clientInfo = null;
    try {
        idToken = new IDToken(response.getIdToken());
        clientInfo = new ClientInfo(response.getClientInfo());
    } catch (ServiceException ccse) {
        Logger.error(TAG + methodName, "Failed to construct IDToken or ClientInfo", null);
        Logger.errorPII(TAG + methodName, "Failed with Exception", ccse);
        throw new RuntimeException();
    }
    MicrosoftStsAccount account = new MicrosoftStsAccount(idToken, clientInfo);
    account.setEnvironment(getIssuerCacheIdentifierFromTokenEndpoint());
    return account;
}
Also used : ServiceException(com.microsoft.identity.common.exception.ServiceException) IDToken(com.microsoft.identity.common.internal.providers.oauth2.IDToken) ClientInfo(com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.ClientInfo)

Example 14 with TokenResponse

use of com.microsoft.identity.common.internal.providers.oauth2.TokenResponse in project microsoft-authentication-library-common-for-android by AzureAD.

the class MockTestStrategy method getTokenResult.

public TokenResult getTokenResult() {
    final TokenResponse tokenResponse = MockTokenResponse.getMockSuccessTokenResponse();
    final TokenResult tokenResult = new TokenResult(tokenResponse);
    return tokenResult;
}
Also used : TokenResponse(com.microsoft.identity.common.internal.providers.oauth2.TokenResponse) MockTokenResponse(com.microsoft.identity.internal.testutils.mocks.MockTokenResponse) TokenResult(com.microsoft.identity.common.internal.providers.oauth2.TokenResult)

Example 15 with TokenResponse

use of com.microsoft.identity.common.internal.providers.oauth2.TokenResponse in project microsoft-authentication-library-common-for-android by AzureAD.

the class MockTestStrategy method performTokenRequest.

@Override
protected HttpResponse performTokenRequest(final MicrosoftStsTokenRequest tokenRequest) {
    final TokenResult tokenResult = getTokenResult();
    final TokenResponse tokenResponse = tokenResult.getTokenResponse();
    final HttpResponse httpResponse = makeHttpResponseFromResponseObject(tokenResponse);
    return httpResponse;
}
Also used : TokenResponse(com.microsoft.identity.common.internal.providers.oauth2.TokenResponse) MockTokenResponse(com.microsoft.identity.internal.testutils.mocks.MockTokenResponse) TokenResult(com.microsoft.identity.common.internal.providers.oauth2.TokenResult) HttpResponse(com.microsoft.identity.common.internal.net.HttpResponse)

Aggregations

TokenResponse (com.google.api.client.auth.oauth2.TokenResponse)48 IOException (java.io.IOException)23 GenericUrl (com.google.api.client.http.GenericUrl)22 Credential (com.google.api.client.auth.oauth2.Credential)20 ClientParametersAuthentication (com.google.api.client.auth.oauth2.ClientParametersAuthentication)16 AuthorizationCodeFlow (com.google.api.client.auth.oauth2.AuthorizationCodeFlow)15 Map (java.util.Map)13 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)12 TokenResponse (com.microsoft.identity.common.internal.providers.oauth2.TokenResponse)11 BearerToken (com.google.api.client.auth.oauth2.BearerToken)9 TokenResult (com.microsoft.identity.common.internal.providers.oauth2.TokenResult)8 Logger (org.slf4j.Logger)8 LoggerFactory (org.slf4j.LoggerFactory)8 Test (org.junit.Test)7 URL (java.net.URL)6 HashMap (java.util.HashMap)6 List (java.util.List)6 Timed (com.codahale.metrics.annotation.Timed)5 AuthorizationCodeRequestUrl (com.google.api.client.auth.oauth2.AuthorizationCodeRequestUrl)5 Collections (java.util.Collections)5