Search in sources :

Example 1 with SerializeException

use of com.nimbusds.oauth2.sdk.SerializeException in project microsoft-authentication-library-for-java by AzureAD.

the class CustomJWTAuthentication method toParameters.

public Map<String, List<String>> toParameters() {
    HashMap<String, List<String>> params = new HashMap<>();
    try {
        params.put("client_assertion", Collections.singletonList(this.clientAssertion.assertion()));
    } catch (IllegalStateException var3) {
        throw new SerializeException("Couldn't serialize JWT to a client assertion string: " + var3.getMessage(), var3);
    }
    params.put("client_assertion_type", Collections.singletonList(JWTAuthentication.CLIENT_ASSERTION_TYPE));
    params.put("client_id", Collections.singletonList(getClientID().getValue()));
    return params;
}
Also used : HashMap(java.util.HashMap) SerializeException(com.nimbusds.oauth2.sdk.SerializeException) List(java.util.List)

Example 2 with SerializeException

use of com.nimbusds.oauth2.sdk.SerializeException in project microsoft-authentication-library-for-java by AzureAD.

the class TokenRequestExecutorTest method testExecuteOAuth_Success.

@Test
public void testExecuteOAuth_Success() throws SerializeException, ParseException, MsalException, IOException, URISyntaxException {
    PublicClientApplication app = PublicClientApplication.builder("id").correlationId("corr-id").build();
    AuthorizationCodeParameters parameters = AuthorizationCodeParameters.builder("code", new URI("http://my.redirect.com")).scopes(Collections.singleton("default-scope")).build();
    final AuthorizationCodeRequest acr = new AuthorizationCodeRequest(parameters, app, new RequestContext(app, PublicApi.ACQUIRE_TOKEN_BY_AUTHORIZATION_CODE, parameters));
    ServiceBundle serviceBundle = new ServiceBundle(null, null, new TelemetryManager(null, false));
    final TokenRequestExecutor request = PowerMock.createPartialMock(TokenRequestExecutor.class, new String[] { "createOauthHttpRequest" }, new AADAuthority(new URL(TestConstants.ORGANIZATIONS_AUTHORITY)), acr, serviceBundle);
    final OAuthHttpRequest msalOAuthHttpRequest = PowerMock.createMock(OAuthHttpRequest.class);
    final HTTPResponse httpResponse = PowerMock.createMock(HTTPResponse.class);
    EasyMock.expect(request.createOauthHttpRequest()).andReturn(msalOAuthHttpRequest).times(1);
    EasyMock.expect(msalOAuthHttpRequest.send()).andReturn(httpResponse).times(1);
    EasyMock.expect(httpResponse.getContentAsJSONObject()).andReturn(JSONObjectUtils.parse(TestConfiguration.TOKEN_ENDPOINT_OK_RESPONSE)).times(1);
    httpResponse.ensureStatusCode(200);
    EasyMock.expectLastCall();
    EasyMock.expect(httpResponse.getStatusCode()).andReturn(200).times(1);
    PowerMock.replay(request, msalOAuthHttpRequest, httpResponse);
    final AuthenticationResult result = request.executeTokenRequest();
    PowerMock.verifyAll();
    Assert.assertNotNull(result.account());
    Assert.assertNotNull(result.account().homeAccountId());
    Assert.assertEquals(result.account().username(), "idlab@msidlab4.onmicrosoft.com");
    Assert.assertFalse(StringHelper.isBlank(result.accessToken()));
    Assert.assertFalse(StringHelper.isBlank(result.refreshToken()));
}
Also used : HTTPResponse(com.nimbusds.oauth2.sdk.http.HTTPResponse) URI(java.net.URI) URL(java.net.URL) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with SerializeException

use of com.nimbusds.oauth2.sdk.SerializeException in project microsoft-authentication-library-for-java by AzureAD.

the class TokenRequestExecutorTest method testExecuteOAuth_Failure.

@Test(expectedExceptions = MsalException.class)
public void testExecuteOAuth_Failure() throws SerializeException, ParseException, MsalException, IOException, URISyntaxException {
    PublicClientApplication app = PublicClientApplication.builder("id").correlationId("corr-id").build();
    AuthorizationCodeParameters parameters = AuthorizationCodeParameters.builder("code", new URI("http://my.redirect.com")).scopes(Collections.singleton("default-scope")).build();
    final AuthorizationCodeRequest acr = new AuthorizationCodeRequest(parameters, app, new RequestContext(app, PublicApi.ACQUIRE_TOKEN_BY_AUTHORIZATION_CODE, parameters));
    ServiceBundle serviceBundle = new ServiceBundle(null, null, new TelemetryManager(null, false));
    final TokenRequestExecutor request = PowerMock.createPartialMock(TokenRequestExecutor.class, new String[] { "createOauthHttpRequest" }, new AADAuthority(new URL(TestConstants.ORGANIZATIONS_AUTHORITY)), acr, serviceBundle);
    final OAuthHttpRequest msalOAuthHttpRequest = PowerMock.createMock(OAuthHttpRequest.class);
    final HTTPResponse httpResponse = PowerMock.createMock(HTTPResponse.class);
    EasyMock.expect(request.createOauthHttpRequest()).andReturn(msalOAuthHttpRequest).times(1);
    EasyMock.expect(msalOAuthHttpRequest.send()).andReturn(httpResponse).times(1);
    EasyMock.expect(httpResponse.getStatusCode()).andReturn(402).times(3);
    EasyMock.expect(httpResponse.getStatusMessage()).andReturn("403 Forbidden");
    EasyMock.expect(httpResponse.getHeaderMap()).andReturn(new HashMap<>());
    EasyMock.expect(httpResponse.getContent()).andReturn(TestConfiguration.HTTP_ERROR_RESPONSE);
    final ErrorResponse errorResponse = PowerMock.createMock(ErrorResponse.class);
    EasyMock.expect(errorResponse.error()).andReturn("invalid_request");
    EasyMock.expect(httpResponse.getHeaderValue("User-Agent")).andReturn(null);
    EasyMock.expect(httpResponse.getHeaderValue("x-ms-request-id")).andReturn(null);
    EasyMock.expect(httpResponse.getHeaderValue("x-ms-clitelem")).andReturn(null);
    EasyMock.expect(httpResponse.getStatusCode()).andReturn(402).times(1);
    PowerMock.replay(request, msalOAuthHttpRequest, httpResponse, TokenErrorResponse.class, errorResponse);
    try {
        request.executeTokenRequest();
        PowerMock.verifyAll();
    } finally {
        PowerMock.reset(request, msalOAuthHttpRequest, httpResponse, TokenErrorResponse.class, errorResponse);
    }
}
Also used : HTTPResponse(com.nimbusds.oauth2.sdk.http.HTTPResponse) URI(java.net.URI) URL(java.net.URL) TokenErrorResponse(com.nimbusds.oauth2.sdk.TokenErrorResponse) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with SerializeException

use of com.nimbusds.oauth2.sdk.SerializeException in project microsoft-authentication-library-for-java by AzureAD.

the class TokenRequestExecutorTest method executeOAuthRequest_SCBadRequestErrorInvalidGrant_InteractionRequiredException.

@Test
public void executeOAuthRequest_SCBadRequestErrorInvalidGrant_InteractionRequiredException() throws SerializeException, ParseException, MsalException, IOException, URISyntaxException {
    TokenRequestExecutor request = createMockedTokenRequest();
    OAuthHttpRequest msalOAuthHttpRequest = PowerMock.createMock(OAuthHttpRequest.class);
    HTTPResponse httpResponse = new HTTPResponse(HTTPResponse.SC_BAD_REQUEST);
    String claims = "{\\\"access_token\\\":{\\\"polids\\\":{\\\"essential\\\":true,\\\"values\\\":[\\\"5ce770ea-8690-4747-aa73-c5b3cd509cd4\\\"]}}}";
    String content = "{\"error\":\"invalid_grant\"," + "\"error_description\":\"AADSTS65001: description\\r\\nCorrelation ID: 3a...5a\\r\\nTimestamp:2017-07-15 02:35:05Z\"," + "\"error_codes\":[50076]," + "\"timestamp\":\"2017-07-15 02:35:05Z\"," + "\"trace_id\":\"0788...000\"," + "\"correlation_id\":\"3a...95a\"," + "\"suberror\":\"basic_action\"," + "\"claims\":\"" + claims + "\"}";
    httpResponse.setContent(content);
    httpResponse.setContentType(HTTPContentType.ApplicationJSON.contentType);
    EasyMock.expect(request.createOauthHttpRequest()).andReturn(msalOAuthHttpRequest).times(1);
    EasyMock.expect(msalOAuthHttpRequest.send()).andReturn(httpResponse).times(1);
    PowerMock.replay(request, msalOAuthHttpRequest);
    try {
        request.executeTokenRequest();
        Assert.fail("Expected MsalServiceException was not thrown");
    } catch (MsalInteractionRequiredException ex) {
        Assert.assertEquals(claims.replace("\\", ""), ex.claims());
        Assert.assertEquals(ex.reason(), InteractionRequiredExceptionReason.BASIC_ACTION);
    }
    PowerMock.verifyAll();
}
Also used : HTTPResponse(com.nimbusds.oauth2.sdk.http.HTTPResponse) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with SerializeException

use of com.nimbusds.oauth2.sdk.SerializeException in project microsoft-authentication-library-for-java by AzureAD.

the class TokenRequestExecutorTest method executeOAuthRequest_SCBadRequestErrorInvalidGrant_SubErrorFilteredServiceExceptionThrown.

@Test
public void executeOAuthRequest_SCBadRequestErrorInvalidGrant_SubErrorFilteredServiceExceptionThrown() throws SerializeException, ParseException, MsalException, IOException, URISyntaxException {
    TokenRequestExecutor request = createMockedTokenRequest();
    OAuthHttpRequest msalOAuthHttpRequest = PowerMock.createMock(OAuthHttpRequest.class);
    HTTPResponse httpResponse = new HTTPResponse(HTTPResponse.SC_BAD_REQUEST);
    String claims = "{\\\"access_token\\\":{\\\"polids\\\":{\\\"essential\\\":true,\\\"values\\\":[\\\"5ce770ea-8690-4747-aa73-c5b3cd509cd4\\\"]}}}";
    String content = "{\"error\":\"invalid_grant\"," + "\"error_description\":\"AADSTS65001: description\\r\\nCorrelation ID: 3a...5a\\r\\nTimestamp:2017-07-15 02:35:05Z\"," + "\"error_codes\":[50076]," + "\"timestamp\":\"2017-07-15 02:35:05Z\"," + "\"trace_id\":\"0788...000\"," + "\"correlation_id\":\"3a...95a\"," + "\"suberror\":\"client_mismatch\"," + "\"claims\":\"" + claims + "\"}";
    httpResponse.setContent(content);
    httpResponse.setContentType(HTTPContentType.ApplicationJSON.contentType);
    EasyMock.expect(request.createOauthHttpRequest()).andReturn(msalOAuthHttpRequest).times(1);
    EasyMock.expect(msalOAuthHttpRequest.send()).andReturn(httpResponse).times(1);
    PowerMock.replay(request, msalOAuthHttpRequest);
    try {
        request.executeTokenRequest();
        Assert.fail("Expected MsalServiceException was not thrown");
    } catch (MsalServiceException ex) {
        Assert.assertEquals(claims.replace("\\", ""), ex.claims());
        Assert.assertTrue(!(ex instanceof MsalInteractionRequiredException));
    }
    PowerMock.verifyAll();
}
Also used : HTTPResponse(com.nimbusds.oauth2.sdk.http.HTTPResponse) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

HTTPResponse (com.nimbusds.oauth2.sdk.http.HTTPResponse)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 Test (org.testng.annotations.Test)4 SerializeException (com.nimbusds.oauth2.sdk.SerializeException)2 URI (java.net.URI)2 URL (java.net.URL)2 List (java.util.List)2 TokenErrorResponse (com.nimbusds.oauth2.sdk.TokenErrorResponse)1 HashMap (java.util.HashMap)1