Search in sources :

Example 31 with Request

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

the class ClientCredentialsReader method extractCredentials.

/**
     * Extracts the client's credentials from the OAuth2 request.
     *
     * @param request The OAuth2 request.
     * @param endpoint The endpoint this request should be for, or null to disable audience verification.
     * @return The client's credentials.
     * @throws InvalidRequestException If the request contains multiple client credentials.
     * @throws InvalidClientException If the request does not contain the client's id.
     */
public ClientCredentials extractCredentials(OAuth2Request request, String endpoint) throws InvalidRequestException, InvalidClientException, NotFoundException {
    final Request req = request.getRequest();
    boolean basicAuth = false;
    if (req.getChallengeResponse() != null) {
        basicAuth = true;
    }
    final ClientCredentials client;
    Client.TokenEndpointAuthMethod method = CLIENT_SECRET_POST;
    //jwt type first
    if (JWT_PROFILE_CLIENT_ASSERTION_TYPE.equalsIgnoreCase(request.<String>getParameter(CLIENT_ASSERTION_TYPE))) {
        client = verifyJwtBearer(request, basicAuth, endpoint);
        method = PRIVATE_KEY_JWT;
    } else {
        String clientId = request.getParameter(OAuth2Constants.Params.CLIENT_ID);
        String clientSecret = request.getParameter(OAuth2Constants.Params.CLIENT_SECRET);
        if (basicAuth && clientId != null) {
            logger.error("Client (" + clientId + ") using multiple authentication methods");
            throw new InvalidRequestException("Client authentication failed");
        }
        if (req.getChallengeResponse() != null) {
            final ChallengeResponse challengeResponse = req.getChallengeResponse();
            clientId = challengeResponse.getIdentifier();
            clientSecret = "";
            if (challengeResponse.getSecret() != null && challengeResponse.getSecret().length > 0) {
                clientSecret = String.valueOf(req.getChallengeResponse().getSecret());
            }
            method = CLIENT_SECRET_BASIC;
        }
        if (clientId == null || clientId.isEmpty()) {
            logger.error("Client Id is not set");
            throw failureFactory.getException(request, "Client authentication failed");
        }
        client = new ClientCredentials(clientId, clientSecret == null ? null : clientSecret.toCharArray(), false, basicAuth);
    }
    final OpenIdConnectClientRegistration cr = clientRegistrationStore.get(client.getClientId(), request);
    final Set<String> scopes = cr.getAllowedScopes();
    //if we're accessing the token endpoint, check we're authenticating using the appropriate method
    if (scopes.contains(OAuth2Constants.Params.OPENID) && req.getResourceRef().getLastSegment().equals(OAuth2Constants.Params.ACCESS_TOKEN) && !cr.getTokenEndpointAuthMethod().equals(method.getType())) {
        throw failureFactory.getException(request, "Invalid authentication method for accessing this endpoint.");
    }
    return client;
}
Also used : OpenIdConnectClientRegistration(org.forgerock.openidconnect.OpenIdConnectClientRegistration) TokenEndpointAuthMethod(org.forgerock.openidconnect.Client.TokenEndpointAuthMethod) Request(org.restlet.Request) OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) InvalidRequestException(org.forgerock.oauth2.core.exceptions.InvalidRequestException) Client(org.forgerock.openidconnect.Client) ChallengeResponse(org.restlet.data.ChallengeResponse)

Example 32 with Request

use of org.restlet.Request in project lucene-solr by apache.

the class TestRestManager method testResolveResourceId.

@Test
public void testResolveResourceId() throws Exception {
    Request testRequest = new Request();
    Reference rootRef = new Reference("http://solr.apache.org/");
    testRequest.setRootRef(rootRef);
    Reference resourceRef = new Reference("http://solr.apache.org/schema/analysis/synonyms/de");
    testRequest.setResourceRef(resourceRef);
    String resourceId = RestManager.ManagedEndpoint.resolveResourceId(testRequest);
    assertEquals(resourceId, "/schema/analysis/synonyms/de");
}
Also used : Reference(org.restlet.data.Reference) Request(org.restlet.Request) Test(org.junit.Test)

Example 33 with Request

use of org.restlet.Request in project camel by apache.

the class RestletConsumer method createRestlet.

protected Restlet createRestlet() {
    return new Restlet() {

        @Override
        public void handle(Request request, Response response) {
            // must call super according to restlet documentation
            super.handle(request, response);
            LOG.debug("Consumer restlet handle request method: {}", request.getMethod());
            Exchange exchange = null;
            try {
                // we want to handle the UoW
                exchange = getEndpoint().createExchange();
                createUoW(exchange);
                RestletBinding binding = getEndpoint().getRestletBinding();
                binding.populateExchangeFromRestletRequest(request, response, exchange);
                try {
                    getProcessor().process(exchange);
                } catch (Exception e) {
                    exchange.setException(e);
                }
                binding.populateRestletResponseFromExchange(exchange, response);
                // resetlet will call the callback when its done sending where it would be safe
                // to call doneUoW
                Uniform callback = newResponseUniform(exchange);
                response.setOnError(callback);
                response.setOnSent(callback);
            } catch (Throwable e) {
                getExceptionHandler().handleException("Error processing request", exchange, e);
                if (exchange != null) {
                    doneUoW(exchange);
                }
            }
        }
    };
}
Also used : Response(org.restlet.Response) Exchange(org.apache.camel.Exchange) Restlet(org.restlet.Restlet) Request(org.restlet.Request) Uniform(org.restlet.Uniform)

Example 34 with Request

use of org.restlet.Request in project camel by apache.

the class RestletProducer method process.

@Override
public boolean process(final Exchange exchange, final AsyncCallback callback) {
    RestletEndpoint endpoint = (RestletEndpoint) getEndpoint();
    // force processing synchronously using different api
    if (endpoint.isSynchronous()) {
        try {
            process(exchange);
        } catch (Throwable e) {
            exchange.setException(e);
        }
        callback.done(true);
        return true;
    }
    LOG.trace("Processing asynchronously");
    final RestletBinding binding = endpoint.getRestletBinding();
    Request request;
    try {
        String resourceUri = buildUri(endpoint, exchange);
        URI uri = new URI(resourceUri);
        request = new Request(endpoint.getRestletMethod(), resourceUri);
        binding.populateRestletRequestFromExchange(request, exchange);
        loadCookies(exchange, uri, request);
    } catch (Throwable e) {
        // break out in case of exception
        exchange.setException(e);
        callback.done(true);
        return true;
    }
    // process the request asynchronously
    LOG.debug("Sending request asynchronously: {} for exchangeId: {}", request, exchange.getExchangeId());
    client.handle(request, new Uniform() {

        @Override
        public void handle(Request request, Response response) {
            LOG.debug("Received response asynchronously: {} for exchangeId: {}", response, exchange.getExchangeId());
            try {
                if (response != null) {
                    String resourceUri = buildUri(endpoint, exchange);
                    URI uri = new URI(resourceUri);
                    Integer respCode = response.getStatus().getCode();
                    storeCookies(exchange, uri, response);
                    if (respCode > 207 && throwException) {
                        exchange.setException(populateRestletProducerException(exchange, response, respCode));
                    } else {
                        binding.populateExchangeFromRestletResponse(exchange, response);
                    }
                }
            } catch (Throwable e) {
                exchange.setException(e);
            } finally {
                callback.done(false);
            }
        }
    });
    // we continue routing async
    return false;
}
Also used : Response(org.restlet.Response) Request(org.restlet.Request) Uniform(org.restlet.Uniform) URI(java.net.URI)

Example 35 with Request

use of org.restlet.Request in project camel by apache.

the class RestletRequestAndResponseAPITest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start").to("restlet:http://localhost:" + portNum + "/users/{id}/like/{beverage.beer}");
            // START SNIPPET: e1
            from("restlet:http://localhost:" + portNum + "/users/{id}/like/{beer}").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    // the Restlet request should be available if needed
                    Request request = exchange.getIn().getHeader(RestletConstants.RESTLET_REQUEST, Request.class);
                    assertNotNull("Restlet Request", request);
                    // use Restlet API to create the response
                    Response response = exchange.getIn().getHeader(RestletConstants.RESTLET_RESPONSE, Response.class);
                    assertNotNull("Restlet Response", response);
                    response.setStatus(Status.SUCCESS_OK);
                    response.setEntity("<response>Beer is Good</response>", MediaType.TEXT_XML);
                    exchange.getOut().setBody(response);
                }
            });
        // END SNIPPET: e1
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Response(org.restlet.Response) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) Request(org.restlet.Request)

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