Search in sources :

Example 11 with InvalidRequestException

use of org.forgerock.oauth2.core.exceptions.InvalidRequestException in project OpenAM by OpenRock.

the class ClaimsParameterValidatorTest method shouldErrorValidatingJson.

@Test(expectedExceptions = BadRequestException.class)
public void shouldErrorValidatingJson() throws NotFoundException, BadRequestException, RedirectUriMismatchException, InvalidScopeException, InvalidRequestException, InvalidClientException, ServerException, UnsupportedResponseTypeException {
    //given
    OAuth2Request mockRequest = mock(OAuth2Request.class);
    OAuth2ProviderSettings mockProviderSettings = mock(OAuth2ProviderSettings.class);
    String responseTypes = "id_token";
    given(mockProviderSettingsFactory.get(mockRequest)).willReturn(mockProviderSettings);
    given(mockProviderSettings.getClaimsParameterSupported()).willReturn(true);
    given(mockRequest.getParameter(OAuth2Constants.Custom.CLAIMS)).willReturn(invalidClaimsString);
    given(mockRequest.getParameter(OAuth2Constants.Params.RESPONSE_TYPE)).willReturn(responseTypes);
    //when
    claimsParameterValidator.validateRequest(mockRequest);
//then
}
Also used : OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) OAuth2ProviderSettings(org.forgerock.oauth2.core.OAuth2ProviderSettings) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 12 with InvalidRequestException

use of org.forgerock.oauth2.core.exceptions.InvalidRequestException in project OpenAM by OpenRock.

the class ClaimsParameterValidatorTest method shouldErrorValidatingResponseType.

@Test(expectedExceptions = BadRequestException.class)
public void shouldErrorValidatingResponseType() throws NotFoundException, BadRequestException, RedirectUriMismatchException, InvalidScopeException, InvalidRequestException, InvalidClientException, ServerException, UnsupportedResponseTypeException {
    //given
    OAuth2Request mockRequest = mock(OAuth2Request.class);
    OAuth2ProviderSettings mockProviderSettings = mock(OAuth2ProviderSettings.class);
    String responseTypes = "id_token";
    given(mockProviderSettingsFactory.get(mockRequest)).willReturn(mockProviderSettings);
    given(mockProviderSettings.getClaimsParameterSupported()).willReturn(true);
    given(mockRequest.getParameter(OAuth2Constants.Custom.CLAIMS)).willReturn(validClaimsString);
    given(mockRequest.getParameter(OAuth2Constants.Params.RESPONSE_TYPE)).willReturn(responseTypes);
    //when
    claimsParameterValidator.validateRequest(mockRequest);
//then
}
Also used : OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) OAuth2ProviderSettings(org.forgerock.oauth2.core.OAuth2ProviderSettings) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 13 with InvalidRequestException

use of org.forgerock.oauth2.core.exceptions.InvalidRequestException in project OpenAM by OpenRock.

the class OpenIdConnectAuthorizeRequestValidatorTest method validateShouldFailWithInvalidRequestExceptionAndQueryParameters.

@Test
public void validateShouldFailWithInvalidRequestExceptionAndQueryParameters() throws Exception {
    //Given
    OAuth2Request request = mock(OAuth2Request.class);
    given(clientRegistration.getAllowedScopes()).willReturn(Collections.singleton("openid"));
    given(request.getParameter("client_id")).willReturn("CLIENT_ID");
    given(request.getParameter("scope")).willReturn("nothing");
    given(request.getParameter("response_type")).willReturn("code");
    //When
    try {
        requestValidator.validateRequest(request);
        fail();
    } catch (InvalidRequestException e) {
        //Then
        assertEquals(e.getParameterLocation(), OAuth2Constants.UrlLocation.QUERY);
    }
}
Also used : OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) InvalidRequestException(org.forgerock.oauth2.core.exceptions.InvalidRequestException) Test(org.testng.annotations.Test)

Example 14 with InvalidRequestException

use of org.forgerock.oauth2.core.exceptions.InvalidRequestException in project OpenAM by OpenRock.

the class AuthorizationCodeGrantTypeHandler method checkCodeVerifier.

private void checkCodeVerifier(AuthorizationCode authorizationCode, String codeVerifier) throws InvalidGrantException, InvalidRequestException {
    final String codeChallenge = authorizationCode.getCodeChallenge();
    final String codeChallengeMethod = authorizationCode.getCodeChallengeMethod();
    if (OAuth2Constants.Custom.CODE_CHALLENGE_METHOD_PLAIN.equals(codeChallengeMethod)) {
        checkCodeChallenge(codeChallenge, codeVerifier);
    } else if (OAuth2Constants.Custom.CODE_CHALLENGE_METHOD_S_256.equals(codeChallengeMethod)) {
        String encodedCodeVerifier = null;
        try {
            encodedCodeVerifier = Base64url.encode(MessageDigest.getInstance("SHA-256").digest(codeVerifier.getBytes(StandardCharsets.US_ASCII)));
            checkCodeChallenge(codeChallenge, encodedCodeVerifier);
        } catch (NoSuchAlgorithmException e) {
            logger.error("Error encoding code verifier.");
            throw new InvalidGrantException();
        }
    } else {
        throw new InvalidRequestException("Invalid code challenge method specified.");
    }
}
Also used : InvalidRequestException(org.forgerock.oauth2.core.exceptions.InvalidRequestException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidGrantException(org.forgerock.oauth2.core.exceptions.InvalidGrantException)

Example 15 with InvalidRequestException

use of org.forgerock.oauth2.core.exceptions.InvalidRequestException in project OpenAM by OpenRock.

the class OAuth2FlowFinder method create.

/**
     * Creates a new instance of the handler for the correct OAuth2 endpoint based from the grant type specified in
     * the requests query parameters.
     *
     * @param request {@inheritDoc}
     * @param response {@inheritDoc}
     * @return {@inheritDoc}
     */
public ServerResource create(Request request, Response response) {
    final OAuth2Request oAuth2Request = requestFactory.create(request);
    final String grantType = oAuth2Request.getParameter("grant_type");
    if (isEmpty(grantType)) {
        logger.error("Type is not set");
        return new ErrorResource(exceptionHandler, new InvalidRequestException("Grant type is not set"));
    }
    Finder finder = endpointClasses.get(grantType);
    if (finder == null) {
        logger.error("Unsupported grant type: Type is not supported: " + grantType);
        return new ErrorResource(exceptionHandler, new UnsupportedGrantTypeException("Grant type is not supported: " + grantType));
    }
    try {
        return finder.create(request, response);
    } catch (Exception e) {
        logger.warn("Exception while instantiating the target server resource.", e);
        return new ErrorResource(exceptionHandler, new ServerException(e.getMessage()));
    }
}
Also used : OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) ServerException(org.forgerock.oauth2.core.exceptions.ServerException) Finder(org.restlet.resource.Finder) InvalidRequestException(org.forgerock.oauth2.core.exceptions.InvalidRequestException) UnsupportedGrantTypeException(org.forgerock.oauth2.core.exceptions.UnsupportedGrantTypeException) ServerException(org.forgerock.oauth2.core.exceptions.ServerException) InvalidRequestException(org.forgerock.oauth2.core.exceptions.InvalidRequestException) UnsupportedGrantTypeException(org.forgerock.oauth2.core.exceptions.UnsupportedGrantTypeException)

Aggregations

InvalidRequestException (org.forgerock.oauth2.core.exceptions.InvalidRequestException)10 OAuth2Request (org.forgerock.oauth2.core.OAuth2Request)7 OAuth2ProviderSettings (org.forgerock.oauth2.core.OAuth2ProviderSettings)6 Test (org.testng.annotations.Test)5 ClientRegistration (org.forgerock.oauth2.core.ClientRegistration)4 BeforeTest (org.testng.annotations.BeforeTest)3 JsonValue (org.forgerock.json.JsonValue)2 BadRequestException (org.forgerock.oauth2.core.exceptions.BadRequestException)2 InvalidGrantException (org.forgerock.oauth2.core.exceptions.InvalidGrantException)2 ServerException (org.forgerock.oauth2.core.exceptions.ServerException)2 UnauthorizedClientException (org.forgerock.oauth2.core.exceptions.UnauthorizedClientException)2 Client (org.forgerock.openidconnect.Client)2 OpenIdConnectClientRegistration (org.forgerock.openidconnect.OpenIdConnectClientRegistration)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 Assertion (com.sun.identity.saml2.assertion.Assertion)1 AssertionFactory (com.sun.identity.saml2.assertion.AssertionFactory)1 SAML2Exception (com.sun.identity.saml2.common.SAML2Exception)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 HashMap (java.util.HashMap)1