Search in sources :

Example 21 with Request

use of org.restlet.Request in project OpenAM by OpenRock.

the class JSONRestStatusServiceTest method shouldReturnThrowableJsonValueIfResourceException.

@Test
public void shouldReturnThrowableJsonValueIfResourceException() throws IOException {
    //Given
    Request request = mock(Request.class);
    Response response = mock(Response.class);
    ResourceException exception = ResourceException.newResourceException(401);
    exception.setDetail(json(object(field("bing", "bong"))));
    Status status = new Status(exception.getCode(), exception);
    //When
    Representation representation = restStatusService.toRepresentation(status, request, response);
    //Then
    assertTrue(representation.getText().contains("\"bing\":\"bong\""));
}
Also used : Response(org.restlet.Response) Status(org.restlet.data.Status) Request(org.restlet.Request) ResourceException(org.forgerock.json.resource.ResourceException) Representation(org.restlet.representation.Representation) Test(org.testng.annotations.Test)

Example 22 with Request

use of org.restlet.Request in project OpenAM by OpenRock.

the class XMLRestStatusServiceTest method shouldGetXmlResourceException.

@Test
public void shouldGetXmlResourceException() throws IOException {
    //Given
    Status status = Status.CLIENT_ERROR_BAD_REQUEST;
    Request request = mock(Request.class);
    Response response = mock(Response.class);
    //When
    Representation representation = restStatusService.toRepresentation(status, request, response);
    //Then
    assertTrue(representation.getText().contains("<code>400</code>"));
}
Also used : Status(org.restlet.data.Status) Response(org.restlet.Response) Request(org.restlet.Request) Representation(org.restlet.representation.Representation) Test(org.testng.annotations.Test)

Example 23 with Request

use of org.restlet.Request in project OpenAM by OpenRock.

the class OAuth2GuiceModule method configure.

/**
     * {@inheritDoc}
     */
@Override
protected void configure() {
    bind(AuthorizationService.class).to(AuthorizationServiceImpl.class);
    bind(new TypeLiteral<OAuth2RequestFactory<?, Request>>() {
    }).to(RestletOAuth2RequestFactory.class);
    bind(ResourceOwnerConsentVerifier.class).to(OpenIdResourceOwnerConsentVerifier.class);
    bind(ClientRegistrationStore.class).to(OpenAMClientRegistrationStore.class);
    bind(OpenIdConnectClientRegistrationStore.class).to(OpenAMClientRegistrationStore.class);
    bind(OAuth2ProviderSettingsFactory.class).to(OpenAMOAuth2ProviderSettingsFactory.class);
    bind(OAuth2ProviderSettingsFactory.class).to(OpenAMOAuth2ProviderSettingsFactory.class);
    bind(ResourceOwnerSessionValidator.class).to(OpenAMResourceOwnerSessionValidator.class);
    bind(ClientAuthenticator.class).to(ClientAuthenticatorImpl.class);
    bind(TokenStore.class).to(OpenAMTokenStore.class);
    bind(OpenIdConnectTokenStore.class).to(OpenAMTokenStore.class);
    bind(AccessTokenService.class).to(AccessTokenServiceImpl.class);
    bind(ResourceOwnerAuthenticator.class).to(OpenAMResourceOwnerAuthenticator.class);
    bind(IdTokenResponseTypeHandler.class).to(OpenAMIdTokenResponseTypeHandler.class);
    bind(UserInfoService.class).to(UserInfoServiceImpl.class);
    bind(TokenInfoService.class).to(TokenInfoServiceImpl.class);
    bind(ClientAuthenticationFailureFactory.class).to(OpenAMClientAuthenticationFailureFactory.class);
    bind(AccessTokenVerifier.class).to(RestletHeaderAccessTokenVerifier.class);
    bind(AccessTokenVerifier.class).annotatedWith(named(HEADER)).to(RestletHeaderAccessTokenVerifier.class);
    bind(AccessTokenVerifier.class).annotatedWith(named(FORM_BODY)).to(RestletFormBodyAccessTokenVerifier.class);
    bind(AccessTokenVerifier.class).annotatedWith(named(QUERY_PARAM)).to(RestletQueryParameterAccessTokenVerifier.class);
    bind(OpenIDConnectProvider.class).to(OpenAMOpenIDConnectProvider.class);
    bind(ClientDAO.class).to(OpenAMClientDAO.class);
    bind(OpenIdConnectClientRegistrationService.class).to(OpenAMOpenIdConnectClientRegistrationService.class);
    bind(OpenAMSettings.class).toProvider(new Provider<OpenAMSettings>() {

        public OpenAMSettings get() {
            return new OpenAMSettingsImpl(OAuth2Constants.OAuth2ProviderService.NAME, OAuth2Constants.OAuth2ProviderService.VERSION);
        }
    });
    bind(OpenIDTokenIssuer.class).to(OpenAMOpenIdTokenIssuer.class);
    final Multibinder<AuthorizeRequestValidator> authorizeRequestValidators = Multibinder.newSetBinder(binder(), AuthorizeRequestValidator.class);
    authorizeRequestValidators.addBinding().to(AuthorizeRequestValidatorImpl.class);
    authorizeRequestValidators.addBinding().to(OpenIdConnectAuthorizeRequestValidator.class);
    authorizeRequestValidators.addBinding().to(ClaimsParameterValidator.class);
    authorizeRequestValidators.addBinding().to(SubjectTypeValidator.class);
    authorizeRequestValidators.addBinding().to(CodeVerifierValidator.class);
    final Multibinder<AuthorizationCodeRequestValidator> authorizationCodeRequestValidators = Multibinder.newSetBinder(binder(), AuthorizationCodeRequestValidator.class);
    authorizationCodeRequestValidators.addBinding().to(AuthorizationCodeRequestValidatorImpl.class);
    final Multibinder<ClientCredentialsRequestValidator> clientCredentialsRequestValidators = Multibinder.newSetBinder(binder(), ClientCredentialsRequestValidator.class);
    clientCredentialsRequestValidators.addBinding().to(ClientCredentialsRequestValidatorImpl.class);
    final Multibinder<PasswordCredentialsRequestValidator> passwordCredentialsRequestValidators = Multibinder.newSetBinder(binder(), PasswordCredentialsRequestValidator.class);
    passwordCredentialsRequestValidators.addBinding().to(PasswordCredentialsRequestValidatorImpl.class);
    final MapBinder<String, GrantTypeHandler> grantTypeHandlers = MapBinder.newMapBinder(binder(), String.class, GrantTypeHandler.class);
    grantTypeHandlers.addBinding(CLIENT_CREDENTIALS).to(ClientCredentialsGrantTypeHandler.class);
    grantTypeHandlers.addBinding(PASSWORD).to(PasswordCredentialsGrantTypeHandler.class);
    grantTypeHandlers.addBinding(AUTHORIZATION_CODE).to(AuthorizationCodeGrantTypeHandler.class);
    grantTypeHandlers.addBinding(DEVICE_CODE).to(DeviceCodeGrantTypeHandler.class);
    grantTypeHandlers.addBinding(JWT_BEARER).to(JwtBearerGrantTypeHandler.class);
    grantTypeHandlers.addBinding(OAuth2Constants.TokenEndpoint.SAML2_BEARER).to(Saml2GrantTypeHandler.class);
    final Multibinder<AuthorizeRequestHook> authorizeRequestHooks = Multibinder.newSetBinder(binder(), AuthorizeRequestHook.class);
    authorizeRequestHooks.addBinding().to(LoginHintHook.class);
    final Multibinder<TokenRequestHook> tokenRequestHooks = Multibinder.newSetBinder(binder(), TokenRequestHook.class);
    tokenRequestHooks.addBinding().to(LoginHintHook.class);
    install(new FactoryModuleBuilder().implement(ResourceSetStore.class, OpenAMResourceSetStore.class).build(ResourceSetStoreFactory.class));
    bind(TokenIdGenerator.class).to(ThreadSafeTokenIdGenerator.class);
    Multibinder.newSetBinder(binder(), TokenIntrospectionHandler.class).addBinding().to(OAuth2TokenIntrospectionHandler.class);
    bind(TokenIntrospectionService.class).to(TokenIntrospectionServiceImpl.class);
    Multibinder.newSetBinder(binder(), ResourceSetRegistrationHook.class);
    bind(OpenIDConnectURLValidator.class).toInstance(OpenIDConnectURLValidator.getInstance());
    install(new LabelsGuiceModule());
    bind(OAuth2UrisFactory.class).to(OpenAMOAuth2UrisFactory.class);
    bind(new TypeLiteral<OAuth2UrisFactory<RealmInfo>>() {
    }).to(OpenAMOAuth2UrisFactory.class);
}
Also used : IdTokenResponseTypeHandler(org.forgerock.openidconnect.IdTokenResponseTypeHandler) OpenAMIdTokenResponseTypeHandler(org.forgerock.openam.openidconnect.OpenAMIdTokenResponseTypeHandler) OpenIdConnectTokenStore(org.forgerock.openidconnect.OpenIdConnectTokenStore) OpenAMOpenIDConnectProvider(org.forgerock.openam.openidconnect.OpenAMOpenIDConnectProvider) OpenIDConnectProvider(org.forgerock.openidconnect.OpenIDConnectProvider) FactoryModuleBuilder(com.google.inject.assistedinject.FactoryModuleBuilder) ResourceOwnerConsentVerifier(org.forgerock.oauth2.core.ResourceOwnerConsentVerifier) OpenIdResourceOwnerConsentVerifier(org.forgerock.openidconnect.OpenIdResourceOwnerConsentVerifier) OpenIDTokenIssuer(org.forgerock.openidconnect.OpenIDTokenIssuer) OpenAMClientRegistrationStore(org.forgerock.openam.oauth2.OpenAMClientRegistrationStore) OpenIdConnectClientRegistrationStore(org.forgerock.openidconnect.OpenIdConnectClientRegistrationStore) ClientRegistrationStore(org.forgerock.oauth2.core.ClientRegistrationStore) AuthorizationCodeRequestValidator(org.forgerock.oauth2.core.AuthorizationCodeRequestValidator) OpenIdConnectClientRegistrationStore(org.forgerock.openidconnect.OpenIdConnectClientRegistrationStore) ClientCredentialsRequestValidator(org.forgerock.oauth2.core.ClientCredentialsRequestValidator) TokenIntrospectionService(org.forgerock.oauth2.core.TokenIntrospectionService) UserInfoService(org.forgerock.openidconnect.UserInfoService) PasswordCredentialsRequestValidator(org.forgerock.oauth2.core.PasswordCredentialsRequestValidator) RealmInfo(org.forgerock.openam.core.RealmInfo) TokenRequestHook(org.forgerock.oauth2.restlet.TokenRequestHook) TypeLiteral(com.google.inject.TypeLiteral) OAuth2ProviderSettingsFactory(org.forgerock.oauth2.core.OAuth2ProviderSettingsFactory) OpenAMOAuth2ProviderSettingsFactory(org.forgerock.openam.oauth2.OpenAMOAuth2ProviderSettingsFactory) OpenAMClientDAO(org.forgerock.openam.oauth2.OpenAMClientDAO) ClientDAO(org.forgerock.openidconnect.ClientDAO) OpenAMSettingsImpl(org.forgerock.openam.utils.OpenAMSettingsImpl) OpenAMResourceOwnerSessionValidator(org.forgerock.openam.oauth2.OpenAMResourceOwnerSessionValidator) ResourceOwnerSessionValidator(org.forgerock.oauth2.core.ResourceOwnerSessionValidator) RestletFormBodyAccessTokenVerifier(org.forgerock.oauth2.restlet.RestletFormBodyAccessTokenVerifier) RestletQueryParameterAccessTokenVerifier(org.forgerock.oauth2.restlet.RestletQueryParameterAccessTokenVerifier) RestletHeaderAccessTokenVerifier(org.forgerock.oauth2.restlet.RestletHeaderAccessTokenVerifier) AccessTokenVerifier(org.forgerock.oauth2.core.AccessTokenVerifier) AuthorizationCodeGrantTypeHandler(org.forgerock.oauth2.core.AuthorizationCodeGrantTypeHandler) JwtBearerGrantTypeHandler(org.forgerock.oauth2.core.JwtBearerGrantTypeHandler) ClientCredentialsGrantTypeHandler(org.forgerock.oauth2.core.ClientCredentialsGrantTypeHandler) DeviceCodeGrantTypeHandler(org.forgerock.oauth2.core.DeviceCodeGrantTypeHandler) Saml2GrantTypeHandler(org.forgerock.openam.oauth2.saml2.core.Saml2GrantTypeHandler) GrantTypeHandler(org.forgerock.oauth2.core.GrantTypeHandler) PasswordCredentialsGrantTypeHandler(org.forgerock.oauth2.core.PasswordCredentialsGrantTypeHandler) OpenAMOAuth2UrisFactory(org.forgerock.openam.oauth2.OpenAMOAuth2UrisFactory) OAuth2UrisFactory(org.forgerock.oauth2.core.OAuth2UrisFactory) AuthorizeRequestValidator(org.forgerock.oauth2.core.AuthorizeRequestValidator) OpenIdConnectAuthorizeRequestValidator(org.forgerock.openidconnect.OpenIdConnectAuthorizeRequestValidator) Request(org.restlet.Request) OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) TokenInfoService(org.forgerock.oauth2.core.TokenInfoService) ClientAuthenticationFailureFactory(org.forgerock.oauth2.core.exceptions.ClientAuthenticationFailureFactory) OpenAMClientAuthenticationFailureFactory(org.forgerock.oauth2.restlet.OpenAMClientAuthenticationFailureFactory) OpenAMSettings(org.forgerock.openam.utils.OpenAMSettings) OpenIDConnectURLValidator(org.forgerock.openam.oauth2.validation.OpenIDConnectURLValidator) ThreadSafeTokenIdGenerator(org.forgerock.openam.sm.datalayer.utils.ThreadSafeTokenIdGenerator) TokenIdGenerator(org.forgerock.openam.cts.api.tokens.TokenIdGenerator) AuthorizationService(org.forgerock.oauth2.core.AuthorizationService) AccessTokenService(org.forgerock.oauth2.core.AccessTokenService) OpenIdConnectClientRegistrationService(org.forgerock.openidconnect.OpenIdConnectClientRegistrationService) OpenAMOpenIdConnectClientRegistrationService(org.forgerock.openam.openidconnect.OpenAMOpenIdConnectClientRegistrationService) ClientAuthenticator(org.forgerock.oauth2.core.ClientAuthenticator) OpenAMResourceOwnerAuthenticator(org.forgerock.openam.oauth2.OpenAMResourceOwnerAuthenticator) ResourceOwnerAuthenticator(org.forgerock.oauth2.core.ResourceOwnerAuthenticator) LabelsGuiceModule(org.forgerock.openam.oauth2.resources.labels.LabelsGuiceModule) TokenStore(org.forgerock.oauth2.core.TokenStore) OpenIdConnectTokenStore(org.forgerock.openidconnect.OpenIdConnectTokenStore) OpenAMTokenStore(org.forgerock.openam.oauth2.OpenAMTokenStore) OAuthTokenStore(org.forgerock.openam.oauth2.OAuthTokenStore) AuthorizeRequestHook(org.forgerock.oauth2.restlet.AuthorizeRequestHook) ResourceSetStoreFactory(org.forgerock.openam.oauth2.resources.ResourceSetStoreFactory)

Example 24 with Request

use of org.restlet.Request in project OpenAM by OpenRock.

the class OpenAMResourceOwnerSessionValidator method authenticationRequired.

private ResourceOwnerAuthenticationRequired authenticationRequired(OAuth2Request request) throws AccessDeniedException, URISyntaxException, ServerException, NotFoundException, UnsupportedEncodingException {
    OAuth2ProviderSettings providerSettings = providerSettingsFactory.get(request);
    Template loginUrlTemplate = providerSettings.getCustomLoginUrlTemplate();
    removeLoginPrompt(request.<Request>getRequest());
    String gotoUrl = request.<Request>getRequest().getResourceRef().toString();
    if (request.getParameter(USER_CODE) != null) {
        gotoUrl += (gotoUrl.indexOf('?') > -1 ? "&" : "?") + USER_CODE + "=" + request.getParameter(USER_CODE);
    }
    String acrValues = request.getParameter(ACR_VALUES);
    String realm = request.getParameter(OAuth2Constants.Custom.REALM);
    String moduleName = request.getParameter(MODULE);
    String serviceName = request.getParameter(SERVICE);
    String locale = getRequestLocale(request);
    URI loginUrl;
    if (loginUrlTemplate != null) {
        loginUrl = buildCustomLoginUrl(loginUrlTemplate, gotoUrl, acrValues, realm, moduleName, serviceName, locale);
    } else {
        loginUrl = buildDefaultLoginUrl(request, gotoUrl, acrValues, realm, moduleName, serviceName, locale);
    }
    return new ResourceOwnerAuthenticationRequired(loginUrl);
}
Also used : ResourceOwnerAuthenticationRequired(org.forgerock.oauth2.core.exceptions.ResourceOwnerAuthenticationRequired) OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(org.restlet.Request) OAuth2ProviderSettings(org.forgerock.oauth2.core.OAuth2ProviderSettings) URI(java.net.URI) Template(freemarker.template.Template)

Example 25 with Request

use of org.restlet.Request in project OpenAM by OpenRock.

the class OpenAMResourceOwnerSessionValidator method setCurrentAcr.

/**
     * If the user is already logged in when the OAuth2 request comes in with an acr_values parameter, we
     * look to see if they've already matched one. If they have, we set the acr value on the request.
     */
private void setCurrentAcr(SSOToken token, OAuth2Request request, String acrValuesStr) throws NotFoundException, ServerException, SSOException, AccessDeniedException, UnsupportedEncodingException, URISyntaxException, ResourceOwnerAuthenticationRequired {
    String serviceUsed = token.getProperty(ISAuthConstants.SERVICE);
    Set<String> acrValues = new HashSet<>(Arrays.asList(acrValuesStr.split("\\s+")));
    OAuth2ProviderSettings settings = providerSettingsFactory.get(request);
    Map<String, AuthenticationMethod> acrMap = settings.getAcrMapping();
    boolean matched = false;
    for (String acr : acrValues) {
        if (acrMap.containsKey(acr)) {
            if (serviceUsed.equals(acrMap.get(acr).getName())) {
                final Request req = request.getRequest();
                req.getResourceRef().addQueryParameter(OAuth2Constants.JWTTokenParams.ACR, acr);
                matched = true;
            }
        }
    }
    if (!matched) {
        throw authenticationRequired(request, token);
    }
}
Also used : OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(org.restlet.Request) OAuth2ProviderSettings(org.forgerock.oauth2.core.OAuth2ProviderSettings) AuthenticationMethod(org.forgerock.oauth2.core.AuthenticationMethod) HashSet(java.util.HashSet)

Aggregations

Request (org.restlet.Request)100 Response (org.restlet.Response)64 Test (org.testng.annotations.Test)38 OAuth2Request (org.forgerock.oauth2.core.OAuth2Request)37 Reference (org.restlet.data.Reference)26 Representation (org.restlet.representation.Representation)24 ChallengeResponse (org.restlet.data.ChallengeResponse)18 StringWriter (java.io.StringWriter)15 Status (org.restlet.data.Status)15 HttpServletRequest (javax.servlet.http.HttpServletRequest)13 HashMap (java.util.HashMap)12 ZNRecord (org.apache.helix.ZNRecord)11 AccessToken (org.forgerock.oauth2.core.AccessToken)11 AccessTokenVerifier (org.forgerock.oauth2.core.AccessTokenVerifier)11 StringReader (java.io.StringReader)10 TypeReference (org.codehaus.jackson.type.TypeReference)10 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)9 Form (org.restlet.data.Form)9 OAuth2ProviderSettings (org.forgerock.oauth2.core.OAuth2ProviderSettings)8 BeforeMethod (org.testng.annotations.BeforeMethod)8