Search in sources :

Example 1 with HttpRequest

use of org.restlet.engine.adapter.HttpRequest in project OpenAM by OpenRock.

the class RestletHeaderAccessTokenVerifierTest method shouldCheckHttpHeader.

@Test
public void shouldCheckHttpHeader() throws Exception {
    // Given
    ServerCall serverCall = mock(ServerCall.class);
    HttpRequest request = mock(HttpRequest.class);
    OAuth2Request req = new RestletOAuth2Request(null, request);
    when(request.getHttpCall()).thenReturn(serverCall);
    Series<Header> requestHeaders = new Series<Header>(Header.class);
    requestHeaders.add(new Header(HeaderConstants.HEADER_AUTHORIZATION, "Bearer freddy"));
    when(request.getHttpCall().getRequestHeaders()).thenReturn(requestHeaders);
    // When
    AccessTokenVerifier.TokenState result = verifier.verify(req);
    // Then
    assertThat(result.isValid()).isFalse();
    verify(tokenStore).readAccessToken(req, "freddy");
}
Also used : HttpRequest(org.restlet.engine.adapter.HttpRequest) Series(org.restlet.util.Series) OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) Header(org.restlet.data.Header) ServerCall(org.restlet.engine.adapter.ServerCall) AccessTokenVerifier(org.forgerock.oauth2.core.AccessTokenVerifier) Test(org.testng.annotations.Test)

Example 2 with HttpRequest

use of org.restlet.engine.adapter.HttpRequest in project OpenAM by OpenRock.

the class RestletRealmRouterTest method shouldHandleQueryParamRealmWithNoLeadingSlash.

@Test
public void shouldHandleQueryParamRealmWithNoLeadingSlash() throws IdRepoException, SSOException {
    //Given
    SSOToken adminToken = mock(SSOToken.class);
    Restlet next = mock(Restlet.class);
    HttpServletRequest httpRequest = mock(HttpServletRequest.class);
    Request request = setUpRequest(httpRequest, adminToken);
    Response response = mock(Response.class);
    setUpServerName(request, adminToken, "/");
    Reference reference = mock(Reference.class);
    given(request.getResourceRef()).willReturn(reference);
    Reference baseRef = mock(Reference.class);
    given(reference.getBaseRef()).willReturn(baseRef);
    given(baseRef.toString()).willReturn("The base url");
    Form queryForm = mock(Form.class);
    given(reference.getQueryAsForm()).willReturn(queryForm);
    given(queryForm.getFirstValue("realm")).willReturn("REALM");
    setUpRealmValidator("REALM", false, adminToken);
    //When
    router.doHandle(next, request, response);
    //Then
    assertThat(request.getAttributes()).containsEntry("realm", "/REALM");
    verify(httpRequest).setAttribute("realm", "/REALM");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Response(org.restlet.Response) SSOToken(com.iplanet.sso.SSOToken) Restlet(org.restlet.Restlet) Form(org.restlet.data.Form) Reference(org.restlet.data.Reference) HttpRequest(org.restlet.engine.adapter.HttpRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(org.restlet.Request) Test(org.testng.annotations.Test)

Example 3 with HttpRequest

use of org.restlet.engine.adapter.HttpRequest in project OpenAM by OpenRock.

the class RestletRealmRouterTest method shouldRouteToRealm.

@Test(dataProvider = "realmRoutingDataProvider")
public void shouldRouteToRealm(String realmLocation, boolean isRealmAlias) throws Exception {
    //Given
    SSOToken adminToken = mock(SSOToken.class);
    Restlet next = mock(Restlet.class);
    HttpServletRequest httpRequest = mock(HttpServletRequest.class);
    Request request = setUpRequest(httpRequest, adminToken);
    Response response = mock(Response.class);
    String realm;
    if (!isRealmAlias) {
        realm = "REALM";
    } else {
        realm = "REALM_ALIAS";
    }
    if ("dns".equalsIgnoreCase(realmLocation)) {
        //set up server name
        setUpServerName(request, adminToken, realm);
    }
    if ("query".equalsIgnoreCase(realmLocation)) {
        //set up query string
        setUpServerName(request, adminToken, "/");
        setUpQueryString(request, realm);
    }
    if ("uri".equalsIgnoreCase(realmLocation)) {
        //set up uri
        setUpServerName(request, adminToken, "/");
        setUpUri(request, realm);
    }
    //set up validate realm
    setUpRealmValidator(realm, isRealmAlias, adminToken);
    //When
    router.doHandle(next, request, response);
    //Then
    assertThat(request.getAttributes()).containsEntry("realm", "/REALM");
    verify(httpRequest).setAttribute("realm", "/REALM");
    assertThat(request.getAttributes()).containsEntry("realmUrl", "The base url");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Response(org.restlet.Response) SSOToken(com.iplanet.sso.SSOToken) Restlet(org.restlet.Restlet) HttpRequest(org.restlet.engine.adapter.HttpRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(org.restlet.Request) Test(org.testng.annotations.Test)

Example 4 with HttpRequest

use of org.restlet.engine.adapter.HttpRequest in project OpenAM by OpenRock.

the class RestletRealmRouterTest method setUpRequest.

private Request setUpRequest(HttpServletRequest httpRequest, SSOToken adminToken) {
    HttpRequest request = generateRequest();
    ServletCall serverCall = mock(ServletCall.class);
    given(request.getHttpCall()).willReturn(serverCall);
    given(serverCall.getRequest()).willReturn(httpRequest);
    Reference reference = mock(Reference.class);
    given(request.getResourceRef()).willReturn(reference);
    Form queryForm = mock(Form.class);
    given(reference.getQueryAsForm()).willReturn(queryForm);
    Reference baseReference = mock(Reference.class);
    given(reference.getBaseRef()).willReturn(baseReference);
    given(baseReference.toString()).willReturn("The base url");
    given(coreWrapper.getAdminToken()).willReturn(adminToken);
    return request;
}
Also used : HttpRequest(org.restlet.engine.adapter.HttpRequest) ServletCall(org.restlet.ext.servlet.internal.ServletCall) Form(org.restlet.data.Form) Reference(org.restlet.data.Reference)

Example 5 with HttpRequest

use of org.restlet.engine.adapter.HttpRequest in project OpenAM by OpenRock.

the class RestletHeaderAccessTokenVerifier method getChallengeResponse.

/**
     * Returns the authentication response sent by a client to an origin server
     * instead of org.restlet.engine.adapter.HttpRequest.
     *
     * @return The authentication response sent by a client to an origin server.
     */
public ChallengeResponse getChallengeResponse(Request request) {
    if (request instanceof HttpRequest) {
        // Extract the header value
        final Series<Header> headers = ((HttpRequest) request).getHttpCall().getRequestHeaders();
        final String authorization = headers.getValues(HeaderConstants.HEADER_AUTHORIZATION);
        if (authorization != null) {
            int space = authorization.indexOf(' ');
            if (space != -1) {
                String scheme = authorization.substring(0, space);
                if (scheme.equalsIgnoreCase("Bearer")) {
                    ChallengeResponse result = new ChallengeResponse(new ChallengeScheme("HTTP_" + scheme, scheme));
                    result.setRawValue(authorization.substring(space + 1));
                    request.setChallengeResponse(result);
                    return result;
                }
            }
        }
    }
    return request.getChallengeResponse();
}
Also used : HttpRequest(org.restlet.engine.adapter.HttpRequest) Header(org.restlet.data.Header) ChallengeScheme(org.restlet.data.ChallengeScheme) ChallengeResponse(org.restlet.data.ChallengeResponse)

Aggregations

HttpRequest (org.restlet.engine.adapter.HttpRequest)6 Test (org.testng.annotations.Test)3 SSOToken (com.iplanet.sso.SSOToken)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 Request (org.restlet.Request)2 Response (org.restlet.Response)2 Restlet (org.restlet.Restlet)2 Form (org.restlet.data.Form)2 Header (org.restlet.data.Header)2 Reference (org.restlet.data.Reference)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AccessTokenVerifier (org.forgerock.oauth2.core.AccessTokenVerifier)1 OAuth2Request (org.forgerock.oauth2.core.OAuth2Request)1 ChallengeResponse (org.restlet.data.ChallengeResponse)1 ChallengeScheme (org.restlet.data.ChallengeScheme)1 ServerCall (org.restlet.engine.adapter.ServerCall)1 ServletCall (org.restlet.ext.servlet.internal.ServletCall)1 Series (org.restlet.util.Series)1