Search in sources :

Example 26 with ReadRequest

use of org.forgerock.json.resource.ReadRequest in project OpenAM by OpenRock.

the class SubjectTypesResourceTest method testSuccessfulJsonificationAndReadAndSubjectNamePropertyRemoved.

@Test
public void testSuccessfulJsonificationAndReadAndSubjectNamePropertyRemoved() throws Exception {
    //given
    SSOTokenContext mockSubjectContext = mock(SSOTokenContext.class);
    RealmContext realmContext = new RealmContext(mockSubjectContext);
    Context mockServerContext = ClientContext.newInternalClientContext(realmContext);
    Subject mockSubject = new Subject();
    given(mockSubjectContext.getCallerSubject()).willReturn(mockSubject);
    ReadRequest mockRequest = mock(ReadRequest.class);
    JsonSchema mockSchema = mock(JsonSchema.class);
    given(mockMapper.generateJsonSchema((Class<?>) any(Class.class))).willReturn(mockSchema);
    //when
    Promise<ResourceResponse, ResourceException> promise = testResource.readInstance(mockServerContext, TEST_CONDITION_WITH_NAME, mockRequest);
    //then
    assertThat(promise).succeeded().withContent().hasString("title");
    assertThat(promise).succeeded().withContent().stringAt("title").isEqualTo(TEST_CONDITION_WITH_NAME);
    assertThat(promise).succeeded().withContent().hasBoolean("logical");
    assertThat(promise).succeeded().withContent().booleanAt("logical").isFalse();
    assertThat(promise.get().getContent().get("config").getObject().toString()).isEqualTo("{\"type\":\"object\",\"properties\":{}}");
}
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) ResourceResponse(org.forgerock.json.resource.ResourceResponse) JsonSchema(com.fasterxml.jackson.databind.jsonschema.JsonSchema) ResourceException(org.forgerock.json.resource.ResourceException) EntitlementSubject(com.sun.identity.entitlement.EntitlementSubject) Subject(javax.security.auth.Subject) LogicalSubject(com.sun.identity.entitlement.LogicalSubject) ReadRequest(org.forgerock.json.resource.ReadRequest) Test(org.testng.annotations.Test)

Example 27 with ReadRequest

use of org.forgerock.json.resource.ReadRequest in project OpenAM by OpenRock.

the class SmsRouteTreeTest method shouldUseOtherAuthModuleForMatchingPath.

@Test
public void shouldUseOtherAuthModuleForMatchingPath() throws Exception {
    //Given
    RequestHandler requestHandler = mock(RequestHandler.class);
    given(requestHandler.handleRead(any(Context.class), any(ReadRequest.class))).willReturn(newResourceResponse("id", "1", json(object())).asPromise());
    Promise<AuthorizationResult, ResourceException> successResult = newResultPromise(accessPermitted());
    given(defaultAuthModule.authorizeRead(any(Context.class), any(ReadRequest.class))).willReturn(successResult);
    Context context = mock(Context.class);
    ReadRequest request = Requests.newReadRequest("/service");
    //When
    routeTree.handles("OTHERSERVICE").addRoute(RoutingMode.STARTS_WITH, "/service", requestHandler);
    Promise<ResourceResponse, ResourceException> result = routeTree.handleRead(context, request);
    //Then
    assertThat(result).succeeded();
    verify(defaultAuthModule).authorizeRead(any(Context.class), any(ReadRequest.class));
    verifyNoMoreInteractions(authModule);
}
Also used : Context(org.forgerock.services.context.Context) RequestHandler(org.forgerock.json.resource.RequestHandler) ResourceResponse(org.forgerock.json.resource.ResourceResponse) ResourceException(org.forgerock.json.resource.ResourceException) AuthorizationResult(org.forgerock.authz.filter.api.AuthorizationResult) ReadRequest(org.forgerock.json.resource.ReadRequest) Test(org.testng.annotations.Test)

Example 28 with ReadRequest

use of org.forgerock.json.resource.ReadRequest in project OpenAM by OpenRock.

the class SmsRouteTreeTest method shouldHandleAddingRoutes.

@Test(dataProvider = "handleRoutes")
public void shouldHandleAddingRoutes(String serviceName, String resourcePath) {
    //Given
    RequestHandler requestHandler = mock(RequestHandler.class);
    Context context = mock(Context.class);
    ReadRequest request = Requests.newReadRequest(resourcePath + "/handler");
    Promise<AuthorizationResult, ResourceException> successResult = newResultPromise(accessPermitted());
    given(defaultAuthModule.authorizeRead(any(Context.class), any(ReadRequest.class))).willReturn(successResult);
    //When
    SmsRouteTree handlerTree = routeTree.handles(serviceName);
    handlerTree.addRoute(RoutingMode.STARTS_WITH, "/handler", requestHandler);
    routeTree.handleRead(context, request);
    //Then
    verify(requestHandler).handleRead(any(Context.class), any(ReadRequest.class));
}
Also used : Context(org.forgerock.services.context.Context) RequestHandler(org.forgerock.json.resource.RequestHandler) SmsRouteTree(org.forgerock.openam.core.rest.sms.SmsRouteTree) ResourceException(org.forgerock.json.resource.ResourceException) AuthorizationResult(org.forgerock.authz.filter.api.AuthorizationResult) ReadRequest(org.forgerock.json.resource.ReadRequest) Test(org.testng.annotations.Test)

Example 29 with ReadRequest

use of org.forgerock.json.resource.ReadRequest in project OpenAM by OpenRock.

the class RealmContextFilter method filterRead.

@Override
public Promise<ResourceResponse, ResourceException> filterRead(Context context, ReadRequest request, RequestHandler next) {
    try {
        Context newContext = evaluate(context, request);
        ReadRequest evaluatedRequest = wasEvaluated(context, newContext) ? copyOfReadRequest(request).setResourcePath(newContext.asContext(UriRouterContext.class).getRemainingUri()) : request;
        return next.handleRead(newContext, evaluatedRequest);
    } catch (ResourceException e) {
        return newExceptionPromise(e);
    }
}
Also used : UriRouterContext(org.forgerock.http.routing.UriRouterContext) Context(org.forgerock.services.context.Context) HttpContext(org.forgerock.json.resource.http.HttpContext) UriRouterContext(org.forgerock.http.routing.UriRouterContext) ResourceException(org.forgerock.json.resource.ResourceException) ReadRequest(org.forgerock.json.resource.ReadRequest)

Example 30 with ReadRequest

use of org.forgerock.json.resource.ReadRequest in project OpenAM by OpenRock.

the class PrivilegeAuthzModuleTest method crestReadIsAllowed.

@Test
public void crestReadIsAllowed() throws SSOException, DelegationException {
    // Given...
    final Set<String> actions = new HashSet<>(Arrays.asList("READ"));
    final DelegationPermission permission = new DelegationPermission("/abc", "rest", "1.0", "policies", "read", actions, EXTENSIONS, DUMB_FUNC);
    given(factory.newInstance("/abc", "rest", "1.0", "policies", "read", actions, EXTENSIONS)).willReturn(permission);
    given(subjectContext.getCallerSSOToken()).willReturn(token);
    given(evaluator.isAllowed(token, permission, ENVIRONMENT)).willReturn(true);
    JsonValue jsonValue = json(object(field("someKey", "someValue")));
    Promise<ResourceResponse, ResourceException> promise = Promises.newResultPromise(Responses.newResourceResponse("1", "1.0", jsonValue));
    given(provider.readInstance(isA(Context.class), eq("123"), isA(ReadRequest.class))).willReturn(promise);
    // When...
    final FilterChain chain = AuthorizationFilters.createAuthorizationFilter(provider, module);
    final Router router = new Router();
    router.addRoute(RoutingMode.STARTS_WITH, Router.uriTemplate("/policies"), chain);
    final RealmContext context = new RealmContext(subjectContext);
    final ReadRequest request = Requests.newReadRequest("/policies/123");
    context.setSubRealm("abc", "abc");
    Promise<ResourceResponse, ResourceException> result = router.handleRead(context, request);
    // Then...
    assertThat(result).succeeded().withContent().stringAt("someKey").isEqualTo("someValue");
}
Also used : RealmContext(org.forgerock.openam.rest.RealmContext) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) Context(org.forgerock.services.context.Context) RealmContext(org.forgerock.openam.rest.RealmContext) FilterChain(org.forgerock.json.resource.FilterChain) JsonValue(org.forgerock.json.JsonValue) Router(org.forgerock.json.resource.Router) Matchers.anyString(org.mockito.Matchers.anyString) DelegationPermission(com.sun.identity.delegation.DelegationPermission) ResourceResponse(org.forgerock.json.resource.ResourceResponse) ResourceException(org.forgerock.json.resource.ResourceException) HashSet(java.util.HashSet) ReadRequest(org.forgerock.json.resource.ReadRequest) Test(org.testng.annotations.Test)

Aggregations

ReadRequest (org.forgerock.json.resource.ReadRequest)35 Test (org.testng.annotations.Test)31 ResourceException (org.forgerock.json.resource.ResourceException)28 ResourceResponse (org.forgerock.json.resource.ResourceResponse)27 Context (org.forgerock.services.context.Context)23 RealmContext (org.forgerock.openam.rest.RealmContext)15 Subject (javax.security.auth.Subject)13 ClientContext (org.forgerock.services.context.ClientContext)11 JsonSchema (com.fasterxml.jackson.databind.jsonschema.JsonSchema)8 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)7 JsonValue (org.forgerock.json.JsonValue)6 SubjectContext (org.forgerock.openam.rest.resource.SubjectContext)6 RequestHandler (org.forgerock.json.resource.RequestHandler)4 EntitlementSubject (com.sun.identity.entitlement.EntitlementSubject)3 LogicalSubject (com.sun.identity.entitlement.LogicalSubject)3 Map (java.util.Map)3 AuthorizationResult (org.forgerock.authz.filter.api.AuthorizationResult)3 Router (org.forgerock.json.resource.Router)3 EntitlementException (com.sun.identity.entitlement.EntitlementException)2 CreateRequest (org.forgerock.json.resource.CreateRequest)2