Search in sources :

Example 1 with TokenResult

use of com.microsoft.identity.common.internal.providers.oauth2.TokenResult in project jersey by eclipse-ee4j.

the class OAuth2Test method testFlow.

private void testFlow(final boolean isArray) {
    ClientIdentifier clientId = new ClientIdentifier(CLIENT_PUBLIC, CLIENT_SECRET);
    final String authUri = UriBuilder.fromUri(getBaseUri()).path("oauth").path("authorization").build().toString();
    final String accessTokenUri = UriBuilder.fromUri(getBaseUri()).path("oauth").path("access-token").build().toString();
    final String refreshTokenUri = UriBuilder.fromUri(getBaseUri()).path("oauth").path("refresh-token").build().toString();
    final String state = STATE;
    final Client client = ClientBuilder.newClient();
    if (isArray) {
        client.register(new ClientRequestFilter() {

            @Override
            public void filter(final ClientRequestContext requestContext) throws IOException {
                requestContext.getHeaders().putSingle("isArray", true);
            }
        });
    }
    final OAuth2CodeGrantFlow.Builder builder = OAuth2ClientSupport.authorizationCodeGrantFlowBuilder(clientId, authUri, accessTokenUri);
    final OAuth2CodeGrantFlow flow = builder.client(client).refreshTokenUri(refreshTokenUri).property(OAuth2CodeGrantFlow.Phase.AUTHORIZATION, "readOnly", "true").property(OAuth2CodeGrantFlow.Phase.AUTHORIZATION, OAuth2Parameters.STATE, state).scope("contact").build();
    final String finalAuthorizationUri = flow.start();
    final Response response = ClientBuilder.newClient().target(finalAuthorizationUri).request().get();
    assertEquals(200, response.getStatus());
    final String code = response.readEntity(String.class);
    assertEquals(CODE, code);
    final TokenResult result = flow.finish(code, state);
    assertEquals("access-token-aab999f", result.getAccessToken());
    assertEquals(new Long(3600), result.getExpiresIn());
    assertEquals("access-token", result.getTokenType());
    final TokenResult refreshResult = flow.refreshAccessToken(result.getRefreshToken());
    assertEquals("access-token-new", refreshResult.getAccessToken());
    assertEquals(new Long(3600), refreshResult.getExpiresIn());
    assertEquals("access-token", refreshResult.getTokenType());
    if (isArray) {
        final Collection<String> array = (Collection<String>) refreshResult.getAllProperties().get("access_token");
        assertThat(array.size(), is(1));
        assertThat(array, hasItem("access-token-new"));
    }
}
Also used : ClientRequestFilter(javax.ws.rs.client.ClientRequestFilter) ClientRequestContext(javax.ws.rs.client.ClientRequestContext) ClientIdentifier(org.glassfish.jersey.client.oauth2.ClientIdentifier) TokenResult(org.glassfish.jersey.client.oauth2.TokenResult) OAuth2CodeGrantFlow(org.glassfish.jersey.client.oauth2.OAuth2CodeGrantFlow) IOException(java.io.IOException) Response(javax.ws.rs.core.Response) Collection(java.util.Collection) Client(javax.ws.rs.client.Client)

Example 2 with TokenResult

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

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

the class AzureActiveDirectoryClientCredentialsGrantTest method test_ClientCredentials.

@Test
public void test_ClientCredentials() throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, NoSuchProviderException, IOException {
    final CertificateCredential credential = new CertificateCredential.CertificateCredentialBuilder(CLIENT_ID).clientCertificateMetadata(new ClientCertificateMetadata(CERTIFICATE_ALIAS, null)).keyStoreConfiguration(new KeyStoreConfiguration(KEYSTORE_TYPE, KEYSTORE_PROVIDER, null)).build();
    final String audience = AAD_CLIENT_ASSERTION_AUDIENCE;
    final MicrosoftClientAssertion assertion = new MicrosoftClientAssertion(audience, credential);
    final AzureActiveDirectoryTokenRequest tr = new AzureActiveDirectoryTokenRequest();
    tr.setClientAssertionType(assertion.getClientAssertionType());
    tr.setClientAssertion(assertion.getClientAssertion());
    tr.setClientId(CLIENT_ID);
    tr.setResourceId(RESOURCE);
    tr.setGrantType(GRANT_TYPE);
    final OAuth2StrategyParameters options = new OAuth2StrategyParameters();
    final OAuth2Strategy strategy = new AzureActiveDirectoryOAuth2Strategy(new AzureActiveDirectoryOAuth2Configuration(), options);
    try {
        final TokenResult tokenResult = strategy.requestToken(tr);
        assertEquals(true, tokenResult.getSuccess());
    } catch (final ClientException exception) {
        fail("Unexpected exception.");
    }
}
Also used : AzureActiveDirectoryOAuth2Strategy(com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryOAuth2Strategy) MicrosoftClientAssertion(com.microsoft.identity.common.internal.providers.microsoft.MicrosoftClientAssertion) TokenResult(com.microsoft.identity.common.internal.providers.oauth2.TokenResult) AzureActiveDirectoryTokenRequest(com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryTokenRequest) ClientCertificateMetadata(com.microsoft.identity.common.internal.providers.keys.ClientCertificateMetadata) OAuth2StrategyParameters(com.microsoft.identity.common.internal.providers.oauth2.OAuth2StrategyParameters) KeyStoreConfiguration(com.microsoft.identity.common.internal.providers.keys.KeyStoreConfiguration) OAuth2Strategy(com.microsoft.identity.common.internal.providers.oauth2.OAuth2Strategy) AzureActiveDirectoryOAuth2Strategy(com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryOAuth2Strategy) CertificateCredential(com.microsoft.identity.common.internal.providers.keys.CertificateCredential) AzureActiveDirectoryOAuth2Configuration(com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryOAuth2Configuration) ClientException(com.microsoft.identity.common.exception.ClientException) Test(org.junit.Test)

Example 4 with TokenResult

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

the class MicrosoftSTSClientCredentialsGrantTest method test_ClientCredentials.

@Test
public void test_ClientCredentials() throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, NoSuchProviderException, IOException {
    final CertificateCredential credential = new CertificateCredential.CertificateCredentialBuilder(CLIENT_ID).clientCertificateMetadata(new ClientCertificateMetadata(CERTIFICATE_ALIAS, null)).keyStoreConfiguration(new KeyStoreConfiguration(KEYSTORE_TYPE, KEYSTORE_PROVIDER, null)).build();
    final String audience = MSSTS_CLIENT_ASSERTION_AUDIENCE;
    final MicrosoftClientAssertion assertion = new MicrosoftClientAssertion(audience, credential);
    final TokenRequest tr = new MicrosoftStsTokenRequest();
    tr.setClientAssertionType(assertion.getClientAssertionType());
    tr.setClientAssertion(assertion.getClientAssertion());
    tr.setClientId(CLIENT_ID);
    tr.setScope(SCOPE);
    tr.setGrantType(GRANT_TYPE);
    final OAuth2StrategyParameters options = new OAuth2StrategyParameters();
    final OAuth2Strategy strategy = new MicrosoftStsOAuth2Strategy(new MicrosoftStsOAuth2Configuration(), options);
    try {
        final TokenResult tokenResult = strategy.requestToken(tr);
        assertEquals(true, tokenResult.getSuccess());
    } catch (final ClientException exception) {
        fail("Unexpected exception.");
    }
}
Also used : MicrosoftClientAssertion(com.microsoft.identity.common.internal.providers.microsoft.MicrosoftClientAssertion) TokenResult(com.microsoft.identity.common.internal.providers.oauth2.TokenResult) ClientCertificateMetadata(com.microsoft.identity.common.internal.providers.keys.ClientCertificateMetadata) OAuth2StrategyParameters(com.microsoft.identity.common.internal.providers.oauth2.OAuth2StrategyParameters) MicrosoftStsOAuth2Strategy(com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsOAuth2Strategy) KeyStoreConfiguration(com.microsoft.identity.common.internal.providers.keys.KeyStoreConfiguration) MicrosoftStsOAuth2Strategy(com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsOAuth2Strategy) OAuth2Strategy(com.microsoft.identity.common.internal.providers.oauth2.OAuth2Strategy) CertificateCredential(com.microsoft.identity.common.internal.providers.keys.CertificateCredential) MicrosoftStsTokenRequest(com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsTokenRequest) MicrosoftStsOAuth2Configuration(com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsOAuth2Configuration) TokenRequest(com.microsoft.identity.common.internal.providers.oauth2.TokenRequest) MicrosoftStsTokenRequest(com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsTokenRequest) ClientException(com.microsoft.identity.common.exception.ClientException) Test(org.junit.Test)

Example 5 with TokenResult

use of com.microsoft.identity.common.internal.providers.oauth2.TokenResult 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)

Aggregations

TokenResult (com.microsoft.identity.common.internal.providers.oauth2.TokenResult)15 OAuth2StrategyParameters (com.microsoft.identity.common.internal.providers.oauth2.OAuth2StrategyParameters)7 TokenResponse (com.microsoft.identity.common.internal.providers.oauth2.TokenResponse)6 ClientException (com.microsoft.identity.common.exception.ClientException)5 AcquireTokenResult (com.microsoft.identity.common.internal.result.AcquireTokenResult)5 MicrosoftStsTokenRequest (com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsTokenRequest)4 OAuth2Strategy (com.microsoft.identity.common.internal.providers.oauth2.OAuth2Strategy)4 MockTokenResponse (com.microsoft.identity.internal.testutils.mocks.MockTokenResponse)4 IOException (java.io.IOException)4 ICacheRecord (com.microsoft.identity.common.internal.cache.ICacheRecord)3 MicrosoftStsOAuth2Configuration (com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsOAuth2Configuration)3 MicrosoftStsOAuth2Strategy (com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsOAuth2Strategy)3 AuthorizationResult (com.microsoft.identity.common.internal.providers.oauth2.AuthorizationResult)3 TokenRequest (com.microsoft.identity.common.internal.providers.oauth2.TokenRequest)3 LocalAuthenticationResult (com.microsoft.identity.common.internal.result.LocalAuthenticationResult)3 OAuth2CodeGrantFlow (org.glassfish.jersey.client.oauth2.OAuth2CodeGrantFlow)3 TokenResult (org.glassfish.jersey.client.oauth2.TokenResult)3 Authority (com.microsoft.identity.common.internal.authorities.Authority)2 AzureActiveDirectoryAuthority (com.microsoft.identity.common.internal.authorities.AzureActiveDirectoryAuthority)2 HttpResponse (com.microsoft.identity.common.internal.net.HttpResponse)2