Search in sources :

Example 11 with TokenResult

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

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 12 with TokenResult

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

the class AuthorizationResource method authorize.

@GET
@Path("authorize")
public Response authorize(@QueryParam("code") String code, @QueryParam("state") String state) {
    final OAuth2CodeGrantFlow flow = SimpleOAuthService.getFlow();
    final TokenResult tokenResult = flow.finish(code, state);
    SimpleOAuthService.setAccessToken(tokenResult.getAccessToken());
    // authorization is finished -> now redirect back to the task resource
    final URI uri = UriBuilder.fromUri(uriInfo.getBaseUri()).path("tasks").build();
    return Response.seeOther(uri).build();
}
Also used : TokenResult(org.glassfish.jersey.client.oauth2.TokenResult) OAuth2CodeGrantFlow(org.glassfish.jersey.client.oauth2.OAuth2CodeGrantFlow) URI(java.net.URI) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 13 with TokenResult

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

the class AzureActiveDirectoryOAuth2Strategy method getTokenResultFromHttpResponse.

@Override
protected TokenResult getTokenResultFromHttpResponse(final HttpResponse response) {
    final String methodName = "getTokenResultFromHttpResponse";
    TokenResponse tokenResponse = null;
    TokenErrorResponse tokenErrorResponse = null;
    if (response.getStatusCode() >= HttpURLConnection.HTTP_BAD_REQUEST) {
        // An error occurred
        Logger.warn(TAG + ":" + methodName, "Status code was: " + response.getStatusCode());
        tokenErrorResponse = ObjectMapper.deserializeJsonStringToObject(response.getBody(), MicrosoftTokenErrorResponse.class);
    } else {
        tokenResponse = ObjectMapper.deserializeJsonStringToObject(response.getBody(), AzureActiveDirectoryTokenResponse.class);
    }
    final TokenResult result = new TokenResult(tokenResponse, tokenErrorResponse);
    return result;
}
Also used : 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) TokenResponse(com.microsoft.identity.common.internal.providers.oauth2.TokenResponse) TokenResult(com.microsoft.identity.common.internal.providers.oauth2.TokenResult)

Example 14 with TokenResult

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

the class BaseController method performSilentTokenRequest.

protected TokenResult performSilentTokenRequest(@SuppressWarnings(WarningType.rawtype_warning) @NonNull final OAuth2Strategy strategy, @NonNull final RefreshTokenRecord refreshToken, @NonNull final SilentTokenCommandParameters parameters) throws ClientException, IOException {
    final String methodName = ":performSilentTokenRequest";
    Logger.info(TAG + methodName, "Requesting tokens...");
    HttpWebRequest.throwIfNetworkNotAvailable(parameters.getAndroidApplicationContext(), parameters.isPowerOptCheckEnabled());
    // Check that the authority is known
    final Authority.KnownAuthorityResult authorityResult = Authority.getKnownAuthorityResult(parameters.getAuthority());
    if (!authorityResult.getKnown()) {
        throw authorityResult.getClientException();
    }
    final TokenRequest refreshTokenRequest = strategy.createRefreshTokenRequest(parameters.getAuthenticationScheme());
    refreshTokenRequest.setClientId(parameters.getClientId());
    refreshTokenRequest.setScope(TextUtils.join(" ", parameters.getScopes()));
    refreshTokenRequest.setRefreshToken(refreshToken.getSecret());
    if (refreshTokenRequest instanceof MicrosoftTokenRequest) {
        ((MicrosoftTokenRequest) refreshTokenRequest).setClaims(parameters.getClaimsRequestJson());
        ((MicrosoftTokenRequest) refreshTokenRequest).setClientAppName(parameters.getApplicationName());
        ((MicrosoftTokenRequest) refreshTokenRequest).setClientAppVersion(parameters.getApplicationVersion());
    }
    // NOTE: this should be moved to the strategy; however requires a larger refactor
    if (parameters.getSdkType() == SdkType.ADAL) {
        ((MicrosoftTokenRequest) refreshTokenRequest).setIdTokenVersion("1");
    }
    // Set Broker version to Token Request if it's a brokered request.
    if (parameters instanceof BrokerSilentTokenCommandParameters) {
        ((MicrosoftTokenRequest) refreshTokenRequest).setBrokerVersion(((BrokerSilentTokenCommandParameters) parameters).getBrokerVersion());
    }
    if (!StringExtensions.isNullOrBlank(refreshTokenRequest.getScope())) {
        Logger.infoPII(TAG + methodName, "Scopes: [" + refreshTokenRequest.getScope() + "]");
    }
    return strategyRequestToken(strategy, refreshTokenRequest);
}
Also used : BrokerSilentTokenCommandParameters(com.microsoft.identity.common.internal.commands.parameters.BrokerSilentTokenCommandParameters) AzureActiveDirectoryAuthority(com.microsoft.identity.common.internal.authorities.AzureActiveDirectoryAuthority) Authority(com.microsoft.identity.common.internal.authorities.Authority) MicrosoftTokenRequest(com.microsoft.identity.common.internal.providers.microsoft.MicrosoftTokenRequest) TokenRequest(com.microsoft.identity.common.internal.providers.oauth2.TokenRequest) MicrosoftTokenRequest(com.microsoft.identity.common.internal.providers.microsoft.MicrosoftTokenRequest)

Example 15 with TokenResult

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

the class BaseController method performTokenRequest.

protected TokenResult performTokenRequest(@SuppressWarnings(WarningType.rawtype_warning) @NonNull final OAuth2Strategy strategy, @SuppressWarnings(WarningType.rawtype_warning) @NonNull final AuthorizationRequest request, @NonNull final AuthorizationResponse response, @NonNull final InteractiveTokenCommandParameters parameters) throws IOException, ClientException {
    final String methodName = ":performTokenRequest";
    HttpWebRequest.throwIfNetworkNotAvailable(parameters.getAndroidApplicationContext(), parameters.isPowerOptCheckEnabled());
    // Suppressing unchecked warnings due to casting of type AuthorizationRequest to GenericAuthorizationRequest and AuthorizationResponse to GenericAuthorizationResponse in arguments of method call to createTokenRequest
    @SuppressWarnings(WarningType.unchecked_warning) final TokenRequest tokenRequest = strategy.createTokenRequest(request, response, parameters.getAuthenticationScheme());
    if (tokenRequest instanceof MicrosoftTokenRequest) {
        ((MicrosoftTokenRequest) tokenRequest).setClientAppName(parameters.getApplicationName());
        ((MicrosoftTokenRequest) tokenRequest).setClientAppVersion(parameters.getApplicationVersion());
    }
    if (tokenRequest instanceof IHasExtraParameters && parameters instanceof IHasExtraParameters) {
        ((IHasExtraParameters) tokenRequest).setExtraParameters(((IHasExtraParameters) parameters).getExtraParameters());
    }
    logExposedFieldsOfObject(TAG + methodName, tokenRequest);
    // Suppressing unchecked warnings due to casting of type TokenRequest to GenericTokenRequest in argument of method call to requestToken
    @SuppressWarnings(WarningType.unchecked_warning) final TokenResult tokenResult = strategy.requestToken(tokenRequest);
    logResult(TAG, tokenResult);
    return tokenResult;
}
Also used : TokenResult(com.microsoft.identity.common.internal.providers.oauth2.TokenResult) AcquireTokenResult(com.microsoft.identity.common.internal.result.AcquireTokenResult) MicrosoftTokenRequest(com.microsoft.identity.common.internal.providers.microsoft.MicrosoftTokenRequest) TokenRequest(com.microsoft.identity.common.internal.providers.oauth2.TokenRequest) MicrosoftTokenRequest(com.microsoft.identity.common.internal.providers.microsoft.MicrosoftTokenRequest) IHasExtraParameters(com.microsoft.identity.common.internal.commands.parameters.IHasExtraParameters)

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