Search in sources :

Example 26 with Reference

use of org.restlet.data.Reference in project OpenAM by OpenRock.

the class ConsentRequiredResource method getDataModel.

/**
     * Gets the data model to use when rendering the error page.
     *
     * @param consentRequired The details for requesting consent.
     * @param request The OAuth2 request.
     * @return The data model.
     */
protected Map<String, Object> getDataModel(ResourceOwnerConsentRequired consentRequired, OAuth2Request request) {
    String displayName = consentRequired.getClientName();
    String displayDescription = consentRequired.getClientDescription();
    String userDisplayName = consentRequired.getUserDisplayName();
    Map<String, Object> data = new HashMap<>(getRequest().getAttributes());
    data.putAll(getQuery().getValuesMap());
    Reference resRef = getRequest().getResourceRef();
    String target = resRef.getPath();
    String query = resRef.getQuery();
    if (!StringUtils.isBlank(query)) {
        target = target + "?" + query;
    }
    data.put("target", target);
    data.put("display_name", ESAPI.encoder().encodeForHTML(displayName));
    data.put("display_description", ESAPI.encoder().encodeForHTML(displayDescription));
    addDisplayScopesAndClaims(consentRequired, data);
    data.put("user_name", userDisplayName);
    data.put("xui", xuiState.isXUIEnabled());
    data.put("user_code", request.getParameter(OAuth2Constants.DeviceCode.USER_CODE));
    data.put("baseUrl", baseURLProviderFactory.get(request.<String>getParameter("realm")).getRootURL(ServletUtils.getRequest(getRequest())));
    return data;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Reference(org.restlet.data.Reference)

Example 27 with Reference

use of org.restlet.data.Reference in project OpenAM by OpenRock.

the class ExceptionHandler method handle.

/**
     * Handles a OAuth2RestletException that is thrown when processing a OAuth2 authorization request.
     * <br/>
     * If the OAuth2RestletException has a status of {@link Status#REDIRECTION_TEMPORARY} the user agent will be
     * redirected to the redirect uri set on the exception.
     * <br/>
     * If the OAuth2RestletException does not have a redirect status but still has a redirect uri set, the user
     * agent will be redrected to the redirect uri with the exception message in the redirect uri.
     * <br/>
     * In all other cases the OAuth2 error page will be presented.
     *
     * @param exception The OAuth2RestletException.
     * @param context The Restlet context.
     * @param request The Restlet request.
     * @param response The Restlet response.
     */
private void handle(OAuth2RestletException exception, Context context, Request request, Response response) {
    if (exception.getStatus().equals(Status.REDIRECTION_TEMPORARY)) {
        Redirector redirector = new Redirector(new Context(), exception.getRedirectUri(), Redirector.MODE_CLIENT_PERMANENT);
        redirector.handle(request, response);
        return;
    } else {
        response.setStatus(exception.getStatus());
    }
    if (!isEmpty(exception.getRedirectUri())) {
        Reference ref = new Reference(exception.getRedirectUri());
        if (UrlLocation.FRAGMENT.equals(exception.getParameterLocation())) {
            ref.setFragment(representation.toForm(exception.asMap()).getQueryString());
        } else {
            ref.addQueryParameters(representation.toForm(exception.asMap()));
        }
        final Redirector redirector = new Redirector(context, ref.toString(), Redirector.MODE_CLIENT_FOUND);
        redirector.handle(request, response);
        return;
    }
    final Map<String, String> data = new HashMap<>(exception.asMap());
    final String realm = requestFactory.create(request).getParameter("realm");
    data.put("realm", realm);
    data.put("baseUrl", baseURLProviderFactory.get(realm).getRootURL(ServletUtils.getRequest(request)));
    response.setEntity(representation.getRepresentation(context, "page", "error.ftl", data));
}
Also used : Context(org.restlet.Context) HashMap(java.util.HashMap) Reference(org.restlet.data.Reference) Redirector(org.restlet.routing.Redirector)

Example 28 with Reference

use of org.restlet.data.Reference in project OpenAM by OpenRock.

the class OpenAMResourceOwnerSessionValidator method buildDefaultLoginUrl.

private URI buildDefaultLoginUrl(OAuth2Request request, String gotoUrl, String acrValues, String realm, String moduleName, String serviceName, String locale) throws URISyntaxException, ServerException, NotFoundException {
    final Request req = request.getRequest();
    final String authURL = getAuthURL(getHttpServletRequest(req));
    final URI authURI = new URI(authURL);
    final Reference loginRef = new Reference(authURI);
    if (!isEmpty(realm)) {
        loginRef.addQueryParameter(OAuth2Constants.Custom.REALM, realm);
    }
    if (!isEmpty(locale)) {
        loginRef.addQueryParameter(LOCALE, locale);
    }
    // Prefer standard acr_values, then module, then service
    if (!isEmpty(acrValues)) {
        final ACRValue chosen = chooseBestAcrValue(request, acrValues.split("\\s+"));
        if (chosen != null) {
            loginRef.addQueryParameter(chosen.method.getIndexType().toString(), chosen.method.getName());
            // Adjust the GOTO url to indicate which acr value was actually chosen
            req.getResourceRef().addQueryParameter(OAuth2Constants.JWTTokenParams.ACR, chosen.acr);
        }
    } else if (!isEmpty(moduleName)) {
        loginRef.addQueryParameter(MODULE, moduleName);
    } else if (!isEmpty(serviceName)) {
        loginRef.addQueryParameter(SERVICE, serviceName);
    }
    loginRef.addQueryParameter(GOTO, gotoUrl);
    return loginRef.toUri();
}
Also used : Reference(org.restlet.data.Reference) OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(org.restlet.Request) URI(java.net.URI)

Example 29 with Reference

use of org.restlet.data.Reference in project OpenAM by OpenRock.

the class RestletRealmRouterTest method setUpServerName.

private void setUpServerName(Request request, SSOToken adminToken, String realm) throws IdRepoException, SSOException {
    Reference reference = request.getResourceRef();
    given(request.getHostRef()).willReturn(reference);
    given(reference.getHostDomain()).willReturn("HOST_DOMAIN");
    given(coreWrapper.getOrganization(adminToken, "HOST_DOMAIN")).willReturn("REALM_HOST_DN");
    given(coreWrapper.convertOrgNameToRealmName("REALM_HOST_DN")).willReturn(realm.equals("/") ? realm : "/" + realm);
    request.getAttributes().put(RestletRealmRouter.REALM_INFO, new RealmInfo("/"));
}
Also used : RealmInfo(org.forgerock.openam.core.RealmInfo) Reference(org.restlet.data.Reference)

Example 30 with Reference

use of org.restlet.data.Reference in project OpenAM by OpenRock.

the class RestletOAuth2RequestTest method theSetUp.

@BeforeMethod
private void theSetUp() {
    //you need this
    request = new Request();
    Reference reference = new Reference("http://127.0.0.1:8080/test");
    request.setResourceRef(reference);
    request.setMethod(Method.POST);
    JacksonRepresentationFactory jacksonRepresentationFactory = new JacksonRepresentationFactory(new ObjectMapper());
    requestUnderTest = new RestletOAuth2Request(jacksonRepresentationFactory, request);
}
Also used : Reference(org.restlet.data.Reference) JacksonRepresentationFactory(org.forgerock.openam.rest.representations.JacksonRepresentationFactory) Request(org.restlet.Request) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

Reference (org.restlet.data.Reference)46 Request (org.restlet.Request)26 Response (org.restlet.Response)26 Representation (org.restlet.representation.Representation)19 StringWriter (java.io.StringWriter)15 HashMap (java.util.HashMap)11 ZNRecord (org.apache.helix.ZNRecord)11 StringReader (java.io.StringReader)10 TypeReference (org.codehaus.jackson.type.TypeReference)10 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)9 ResourceException (org.restlet.resource.ResourceException)8 Form (org.restlet.data.Form)5 IOException (java.io.IOException)3 Date (java.util.Date)3 Test (org.junit.Test)3 ContextResourceClient (org.qi4j.library.rest.client.api.ContextResourceClient)3 ContextResourceClientFactory (org.qi4j.library.rest.client.api.ContextResourceClientFactory)3 ErrorHandler (org.qi4j.library.rest.client.api.ErrorHandler)3 HandlerCommand (org.qi4j.library.rest.client.api.HandlerCommand)3 ResponseHandler (org.qi4j.library.rest.client.spi.ResponseHandler)3