Search in sources :

Example 1 with Context

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

the class AuditRestRouteProvider method addResourceRoutes.

@Override
public void addResourceRoutes(ResourceRouter rootRouter, ResourceRouter realmRouter) {
    rootRouter.route("global-audit").auditAs(AUDIT, AuditEndpointAuditFilter.class).authorizeWith(SpecialOrAdminOrAgentAuthzModule.class).forVersion(1).toRequestHandler(STARTS_WITH, auditServiceProvider.getDefaultAuditService());
    rootRouter.route("realm-audit").auditAs(AUDIT, AuditEndpointAuditFilter.class).authorizeWith(SpecialOrAdminOrAgentAuthzModule.class).forVersion(1).through(RealmContextFilter.class).toRequestHandler(STARTS_WITH, new RequestHandler() {

        @Override
        public Promise<ActionResponse, ResourceException> handleAction(Context context, ActionRequest actionRequest) {
            return getAuditService(context).handleAction(context, actionRequest);
        }

        @Override
        public Promise<ResourceResponse, ResourceException> handleCreate(Context context, CreateRequest createRequest) {
            return getAuditService(context).handleCreate(context, createRequest);
        }

        @Override
        public Promise<ResourceResponse, ResourceException> handleDelete(Context context, DeleteRequest deleteRequest) {
            return getAuditService(context).handleDelete(context, deleteRequest);
        }

        @Override
        public Promise<ResourceResponse, ResourceException> handlePatch(Context context, PatchRequest patchRequest) {
            return getAuditService(context).handlePatch(context, patchRequest);
        }

        @Override
        public Promise<QueryResponse, ResourceException> handleQuery(Context context, QueryRequest queryRequest, QueryResourceHandler queryResourceHandler) {
            return getAuditService(context).handleQuery(context, queryRequest, queryResourceHandler);
        }

        @Override
        public Promise<ResourceResponse, ResourceException> handleRead(Context context, ReadRequest readRequest) {
            return getAuditService(context).handleRead(context, readRequest);
        }

        @Override
        public Promise<ResourceResponse, ResourceException> handleUpdate(Context context, UpdateRequest updateRequest) {
            return getAuditService(context).handleUpdate(context, updateRequest);
        }

        private AMAuditService getAuditService(Context context) {
            String realm = context.asContext(RealmContext.class).getResolvedRealm();
            if (StringUtils.isEmpty(realm)) {
                logger.warn("Context contained RealmContext but had an empty resolved realm");
                return auditServiceProvider.getDefaultAuditService();
            }
            return auditServiceProvider.getAuditService(realm);
        }
    });
}
Also used : RealmContext(org.forgerock.openam.rest.RealmContext) Context(org.forgerock.services.context.Context) QueryRequest(org.forgerock.json.resource.QueryRequest) AuditEndpointAuditFilter(org.forgerock.openam.rest.fluent.AuditEndpointAuditFilter) UpdateRequest(org.forgerock.json.resource.UpdateRequest) CreateRequest(org.forgerock.json.resource.CreateRequest) AMAuditService(org.forgerock.openam.audit.AMAuditService) PatchRequest(org.forgerock.json.resource.PatchRequest) Promise(org.forgerock.util.promise.Promise) RequestHandler(org.forgerock.json.resource.RequestHandler) ActionRequest(org.forgerock.json.resource.ActionRequest) RealmContextFilter(org.forgerock.openam.rest.RealmContextFilter) SpecialOrAdminOrAgentAuthzModule(org.forgerock.openam.rest.authz.SpecialOrAdminOrAgentAuthzModule) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) DeleteRequest(org.forgerock.json.resource.DeleteRequest) ReadRequest(org.forgerock.json.resource.ReadRequest)

Example 2 with Context

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

the class AbstractHttpAccessAuditFilterTest method shouldAuditAccessAttemptAndResult.

@Test(dataProvider = "handlerResponses")
public void shouldAuditAccessAttemptAndResult(Status responseStatus) throws AuditException {
    //Given
    Context context = new RequestAuditContext(mockContext());
    Request request = new Request().setMethod("GET").setUri(URI.create("http://example.com:8080?query=value"));
    request.getHeaders().put(ContentTypeHeader.valueOf("CONTENT_TYPE"));
    enableAccessTopicAuditing();
    Handler handler = mockHandler(context, request, responseStatus);
    //When
    auditFilter.filter(context, request, handler);
    //Then
    ArgumentCaptor<AuditEvent> auditEventCaptor = ArgumentCaptor.forClass(AuditEvent.class);
    verify(eventPublisher, times(2)).tryPublish(eq(AuditConstants.ACCESS_TOPIC), auditEventCaptor.capture());
    verifyAccessAttemptAuditEvent(auditEventCaptor.getAllValues().get(0).getValue());
    if (responseStatus.isSuccessful()) {
        verifyAccessSuccessAuditEvent(auditEventCaptor.getAllValues().get(1).getValue());
    } else {
        verifyAccessFailedAuditEvent(auditEventCaptor.getAllValues().get(1).getValue());
    }
}
Also used : RootContext(org.forgerock.services.context.RootContext) ClientContext(org.forgerock.services.context.ClientContext) RequestAuditContext(org.forgerock.services.context.RequestAuditContext) Context(org.forgerock.services.context.Context) AuditRequestContext(org.forgerock.openam.audit.context.AuditRequestContext) SessionContext(org.forgerock.http.session.SessionContext) AttributesContext(org.forgerock.services.context.AttributesContext) RequestAuditContext(org.forgerock.services.context.RequestAuditContext) Request(org.forgerock.http.protocol.Request) Handler(org.forgerock.http.Handler) AuditEvent(org.forgerock.audit.events.AuditEvent) Test(org.testng.annotations.Test)

Example 3 with Context

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

the class AbstractHttpAccessAuditFilterTest method shouldNotAuditIfAuditingIsNotEnabledForAccessTopic.

@Test(dataProvider = "handlerResponses")
public void shouldNotAuditIfAuditingIsNotEnabledForAccessTopic(Status responseStatus) throws AuditException {
    //Given
    Context context = new RequestAuditContext(mockContext());
    Request request = new Request().setUri(URI.create("http://example.com"));
    disableAccessTopicAuditing();
    Handler handler = mockHandler(context, request, responseStatus);
    //When
    auditFilter.filter(context, request, handler);
    //Then
    verify(eventPublisher, never()).tryPublish(anyString(), any(AuditEvent.class));
}
Also used : RootContext(org.forgerock.services.context.RootContext) ClientContext(org.forgerock.services.context.ClientContext) RequestAuditContext(org.forgerock.services.context.RequestAuditContext) Context(org.forgerock.services.context.Context) AuditRequestContext(org.forgerock.openam.audit.context.AuditRequestContext) SessionContext(org.forgerock.http.session.SessionContext) AttributesContext(org.forgerock.services.context.AttributesContext) RequestAuditContext(org.forgerock.services.context.RequestAuditContext) Request(org.forgerock.http.protocol.Request) Handler(org.forgerock.http.Handler) AuditEvent(org.forgerock.audit.events.AuditEvent) Test(org.testng.annotations.Test)

Example 4 with Context

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

the class TrustedDevicesResourceTest method ctx.

private Context ctx() {
    SSOTokenContext ssoTokenContext = mock(SSOTokenContext.class);
    RealmContext realmContext = new RealmContext(ssoTokenContext);
    Context serverContext = ClientContext.newInternalClientContext(realmContext);
    return serverContext;
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) Context(org.forgerock.services.context.Context) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) RealmContext(org.forgerock.openam.rest.RealmContext)

Example 5 with Context

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

the class SessionResourceAuthzModuleTest method shouldAllowLogoutAction.

@Test
public void shouldAllowLogoutAction() throws ExecutionException, InterruptedException {
    //given
    Context mockContext = mock(Context.class);
    ActionRequest mockRequest = mock(ActionRequest.class);
    given(mockRequest.getAction()).willReturn("logout");
    //when
    Promise<AuthorizationResult, ResourceException> result = testModule.authorizeAction(mockContext, mockRequest);
    //then
    assertTrue(result.get().isAuthorized());
}
Also used : Context(org.forgerock.services.context.Context) RootContext(org.forgerock.services.context.RootContext) ActionRequest(org.forgerock.json.resource.ActionRequest) ResourceException(org.forgerock.json.resource.ResourceException) AuthorizationResult(org.forgerock.authz.filter.api.AuthorizationResult) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

Context (org.forgerock.services.context.Context)250 Test (org.testng.annotations.Test)231 RealmContext (org.forgerock.openam.rest.RealmContext)150 ResourceException (org.forgerock.json.resource.ResourceException)132 ClientContext (org.forgerock.services.context.ClientContext)120 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)101 ResourceResponse (org.forgerock.json.resource.ResourceResponse)82 SubjectContext (org.forgerock.openam.rest.resource.SubjectContext)76 RootContext (org.forgerock.services.context.RootContext)62 JsonValue (org.forgerock.json.JsonValue)56 QueryRequest (org.forgerock.json.resource.QueryRequest)54 ReadRequest (org.forgerock.json.resource.ReadRequest)50 AttributesContext (org.forgerock.services.context.AttributesContext)44 ActionRequest (org.forgerock.json.resource.ActionRequest)43 QueryResponse (org.forgerock.json.resource.QueryResponse)43 Subject (javax.security.auth.Subject)42 Matchers.anyString (org.mockito.Matchers.anyString)38 Request (org.forgerock.http.protocol.Request)37 UpdateRequest (org.forgerock.json.resource.UpdateRequest)36 DeleteRequest (org.forgerock.json.resource.DeleteRequest)35