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"));
}
}
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();
}
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;
}
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);
}
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;
}
Aggregations