Search in sources :

Example 1 with AttributesContext

use of org.forgerock.services.context.AttributesContext in project OpenAM by OpenRock.

the class AuthenticationServiceV1 method getHttpServletRequest.

/**
     * Gets the HttpServletRequest from Restlet and wraps the HttpServletRequest with the URI realm as long as
     * the request does not contain the realm as a query parameter.
     *
     * @return The HttpServletRequest
     */
private HttpServletRequest getHttpServletRequest(Context context, JsonValue jsonValue) {
    AttributesContext requestContext = context.asContext(AttributesContext.class);
    Map<String, Object> requestAttributes = requestContext.getAttributes();
    final HttpServletRequest request = (HttpServletRequest) requestAttributes.get(HttpServletRequest.class.getName());
    // The request contains the realm query param then use that over any realm parsed from the URI
    final String queryParamRealm = request.getParameter(REALM);
    if (queryParamRealm != null && !queryParamRealm.isEmpty()) {
        RealmContext rc = new RealmContext(context);
        rc.setOverrideRealm(queryParamRealm);
        return wrapRequest(request, rc, jsonValue);
    }
    return wrapRequest(request, context.asContext(RealmContext.class), jsonValue);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) AttributesContext(org.forgerock.services.context.AttributesContext) RealmContext(org.forgerock.openam.rest.RealmContext)

Example 2 with AttributesContext

use of org.forgerock.services.context.AttributesContext in project OpenAM by OpenRock.

the class AuthenticationServiceV2Test method shouldFailAuthenticationWithUnsupportedMediaTypeMessage.

@Test
public void shouldFailAuthenticationWithUnsupportedMediaTypeMessage() throws IOException {
    // given
    AttributesContext context = new AttributesContext(new SessionContext(new RootContext(), mock(Session.class)));
    Request httpRequest = new Request();
    httpRequest.setEntity("<xml></xml>");
    httpRequest.getHeaders().put(ContentTypeHeader.NAME, "application/xml");
    // when
    Response response = authServiceV2.authenticate(context, httpRequest);
    // then
    assertThat(response.getStatus()).isEqualTo(Status.UNSUPPORTED_MEDIA_TYPE);
    JsonValue responseBody = json(response.getEntity().getJson());
    assertThat(responseBody).integerAt("code").isEqualTo(415);
    assertThat(responseBody).stringAt("reason").isEqualTo("Unsupported Media Type");
    assertThat(responseBody).stringAt("message").isEqualTo("Unsupported Media Type");
}
Also used : RootContext(org.forgerock.services.context.RootContext) Response(org.forgerock.http.protocol.Response) AttributesContext(org.forgerock.services.context.AttributesContext) Request(org.forgerock.http.protocol.Request) JsonValue(org.forgerock.json.JsonValue) SessionContext(org.forgerock.http.session.SessionContext) Test(org.testng.annotations.Test)

Example 3 with AttributesContext

use of org.forgerock.services.context.AttributesContext in project OpenAM by OpenRock.

the class AuthenticationServiceV1Test method shouldFailAuthenticationWithUnsupportedMediaTypeMessage.

@Test
public void shouldFailAuthenticationWithUnsupportedMediaTypeMessage() throws IOException {
    // given
    AttributesContext context = new AttributesContext(new SessionContext(new RootContext(), mock(Session.class)));
    Request httpRequest = new Request();
    httpRequest.setEntity("<xml></xml>");
    httpRequest.getHeaders().put(ContentTypeHeader.NAME, "application/xml");
    // when
    Response response = authServiceV1.authenticate(context, httpRequest);
    // then
    assertThat(response.getStatus()).isEqualTo(Status.UNSUPPORTED_MEDIA_TYPE);
    assertThat(json(response.getEntity().getJson())).stringAt("errorMessage").isEqualTo("Unsupported Media Type");
}
Also used : RootContext(org.forgerock.services.context.RootContext) Response(org.forgerock.http.protocol.Response) AttributesContext(org.forgerock.services.context.AttributesContext) Request(org.forgerock.http.protocol.Request) SessionContext(org.forgerock.http.session.SessionContext) Test(org.testng.annotations.Test)

Example 4 with AttributesContext

use of org.forgerock.services.context.AttributesContext in project OpenAM by OpenRock.

the class SessionResourceTest method actionCollectionShouldLogoutSessionAndReturnEmptyJsonObjectWhenSSOTokenValid.

@Test
public void actionCollectionShouldLogoutSessionAndReturnEmptyJsonObjectWhenSSOTokenValid() throws SSOException {
    //Given
    cookieResponse = "SSO_TOKEN_ID";
    final AttributesContext attrContext = new AttributesContext(new SessionContext(new RootContext(), mock(Session.class)));
    final AdviceContext adviceContext = new AdviceContext(attrContext, Collections.<String>emptySet());
    final SecurityContext securityContext = new SecurityContext(adviceContext, null, null);
    final Context context = ClientContext.newInternalClientContext(new SSOTokenContext(mock(Debug.class), null, securityContext));
    final ActionRequest request = mock(ActionRequest.class);
    final SSOTokenID ssoTokenId = mock(SSOTokenID.class);
    given(request.getAction()).willReturn(LOGOUT_ACTION_ID);
    given(authUtilsWrapper.logout(ssoTokenId.toString(), null, null)).willReturn(true);
    //When
    Promise<ActionResponse, ResourceException> promise = sessionResource.actionCollection(context, request);
    //Then
    assertThat(promise).succeeded().withContent().stringAt("result").isEqualTo("Successfully logged out");
}
Also used : RootContext(org.forgerock.services.context.RootContext) RootContext(org.forgerock.services.context.RootContext) ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) SessionContext(org.forgerock.http.session.SessionContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) AttributesContext(org.forgerock.services.context.AttributesContext) SecurityContext(org.forgerock.services.context.SecurityContext) Context(org.forgerock.services.context.Context) SSOTokenID(com.iplanet.sso.SSOTokenID) AttributesContext(org.forgerock.services.context.AttributesContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) SecurityContext(org.forgerock.services.context.SecurityContext) SessionContext(org.forgerock.http.session.SessionContext) Test(org.testng.annotations.Test)

Example 5 with AttributesContext

use of org.forgerock.services.context.AttributesContext in project OpenAM by OpenRock.

the class RestRouterIT method mockContext.

private Context mockContext(Context parent) {
    if (parent == null) {
        parent = new RootContext();
    }
    AttributesContext httpRequestContext = new AttributesContext(new SessionContext(parent, mock(Session.class)));
    HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
    httpRequestContext.getAttributes().put(HttpServletRequest.class.getName(), httpServletRequest);
    return new RequestAuditContext(httpRequestContext);
}
Also used : RootContext(org.forgerock.services.context.RootContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) RequestAuditContext(org.forgerock.services.context.RequestAuditContext) AttributesContext(org.forgerock.services.context.AttributesContext) SessionContext(org.forgerock.http.session.SessionContext)

Aggregations

AttributesContext (org.forgerock.services.context.AttributesContext)7 SessionContext (org.forgerock.http.session.SessionContext)4 RootContext (org.forgerock.services.context.RootContext)4 Test (org.testng.annotations.Test)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 SSOTokenID (com.iplanet.sso.SSOTokenID)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 Request (org.forgerock.http.protocol.Request)2 Response (org.forgerock.http.protocol.Response)2 RealmContext (org.forgerock.openam.rest.RealmContext)2 JsonValue (org.forgerock.json.JsonValue)1 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)1 ClientContext (org.forgerock.services.context.ClientContext)1 Context (org.forgerock.services.context.Context)1 RequestAuditContext (org.forgerock.services.context.RequestAuditContext)1 SecurityContext (org.forgerock.services.context.SecurityContext)1