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