Search in sources :

Example 61 with Representation

use of org.restlet.representation.Representation in project OpenAM by OpenRock.

the class RestletFormBodyAccessTokenVerifier method obtainTokenId.

/**
     * {@inheritDoc}
     */
protected String obtainTokenId(OAuth2Request request) {
    final Request req = request.getRequest();
    final Representation body = req.getEntity();
    if (body == null || !MediaType.APPLICATION_WWW_FORM.equals(body.getMediaType())) {
        logger.debug("Request does not contain form.");
        return null;
    }
    Form formBody = new Form(body);
    if (!formBody.getNames().contains(OAuth2Constants.Params.ACCESS_TOKEN)) {
        logger.debug("Request form does not contain access_token.");
        return null;
    }
    return formBody.getFirstValue(OAuth2Constants.Params.ACCESS_TOKEN);
}
Also used : Form(org.restlet.data.Form) Request(org.restlet.Request) OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) Representation(org.restlet.representation.Representation)

Example 62 with Representation

use of org.restlet.representation.Representation in project OpenAM by OpenRock.

the class EndSessionTest method shouldAttemptEndSessionAndNotRedirect.

@Test
public void shouldAttemptEndSessionAndNotRedirect() throws Exception {
    // given
    String requestedUri = "";
    String registeredUri = "http://www.example.com";
    when(oAuth2Request.getParameter(OAuth2Constants.Params.POST_LOGOUT_REDIRECT_URI)).thenReturn(requestedUri);
    when(client.getPostLogoutRedirectUris()).thenReturn(Collections.singleton(new URI(registeredUri)));
    // when
    Representation result = endSession.endSession();
    // then
    verify(openIDConnectEndSession, times(1)).endSession(any(String.class));
    assertThat(result).isNull();
}
Also used : Representation(org.restlet.representation.Representation) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 63 with Representation

use of org.restlet.representation.Representation in project OpenAM by OpenRock.

the class ResourceSetRegistrationEndpointTest method shouldUpdateResourceSetDescription.

@Test
@SuppressWarnings("unchecked")
public void shouldUpdateResourceSetDescription() throws Exception {
    //Given
    JsonRepresentation entity = createUpdateRequestRepresentation();
    ResourceSetDescription resourceSetDescription = new ResourceSetDescription("RESOURCE_SET_ID", "CLIENT_ID", "RESOURCE_OWNER_ID", RESOURCE_SET_DESCRIPTION_CONTENT.asMap());
    setUriResourceSetId();
    addCondition();
    given(store.read("RESOURCE_SET_ID", "RESOURCE_OWNER_ID")).willReturn(resourceSetDescription);
    //When
    Representation responseRep = endpoint.updateResourceSet(entity);
    //Then
    ArgumentCaptor<ResourceSetDescription> resourceSetCaptor = ArgumentCaptor.forClass(ResourceSetDescription.class);
    verify(store).update(resourceSetCaptor.capture());
    assertThat(resourceSetCaptor.getValue().getId()).isEqualTo("RESOURCE_SET_ID");
    assertThat(resourceSetCaptor.getValue().getClientId()).isEqualTo("CLIENT_ID");
    assertThat(resourceSetCaptor.getValue().getName()).isEqualTo("NEW_NAME");
    assertThat(resourceSetCaptor.getValue().getUri()).isEqualTo(URI.create("NEW_URI"));
    assertThat(resourceSetCaptor.getValue().getType()).isEqualTo("NEW_TYPE");
    assertThat(resourceSetCaptor.getValue().getScopes()).containsExactly("NEW_SCOPE");
    assertThat(resourceSetCaptor.getValue().getIconUri()).isEqualTo(URI.create("NEW_ICON_URI"));
    Map<String, Object> responseBody = (Map<String, Object>) new ObjectMapper().readValue(responseRep.getText(), Map.class);
    assertThat(responseBody).containsKey("_id");
    verify(labelRegistration).updateLabelsForExistingResourceSet(any(ResourceSetDescription.class));
}
Also used : JacksonRepresentation(org.restlet.ext.jackson.JacksonRepresentation) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) Representation(org.restlet.representation.Representation) JSONObject(org.json.JSONObject) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Example 64 with Representation

use of org.restlet.representation.Representation in project OpenAM by OpenRock.

the class ResourceSetRegistrationEndpointTest method shouldCreateResourceSetDescription.

@Test
@SuppressWarnings("unchecked")
public void shouldCreateResourceSetDescription() throws Exception {
    //Given
    JsonRepresentation entity = createCreateRequestRepresentation();
    doAnswer(new Answer<Void>() {

        public Void answer(InvocationOnMock invocation) throws Throwable {
            ResourceSetDescription resourceSetDescription = (ResourceSetDescription) invocation.getArguments()[1];
            resourceSetDescription.setId("123");
            return null;
        }
    }).when(store).create(any(OAuth2Request.class), any(ResourceSetDescription.class));
    setUriResourceSetId();
    noConditions();
    //When
    Representation response = endpoint.createResourceSet(entity);
    //Then
    ArgumentCaptor<ResourceSetDescription> resourceSetCaptor = ArgumentCaptor.forClass(ResourceSetDescription.class);
    InOrder inOrder = inOrder(resourceRegistrationFilter, store, resourceRegistrationFilter);
    inOrder.verify(resourceRegistrationFilter).beforeResourceRegistration(any(ResourceSetDescription.class));
    inOrder.verify(store).create(Matchers.<OAuth2Request>anyObject(), resourceSetCaptor.capture());
    inOrder.verify(resourceRegistrationFilter).afterResourceRegistration(any(ResourceSetDescription.class));
    assertThat(resourceSetCaptor.getValue().getId()).isNotNull().isNotEmpty();
    assertThat(resourceSetCaptor.getValue().getClientId()).isEqualTo("CLIENT_ID");
    assertThat(resourceSetCaptor.getValue().getName()).isEqualTo("NAME");
    assertThat(resourceSetCaptor.getValue().getUri()).isEqualTo(URI.create("URI"));
    assertThat(resourceSetCaptor.getValue().getType()).isEqualTo("TYPE");
    assertThat(resourceSetCaptor.getValue().getScopes()).containsExactly("SCOPE");
    assertThat(resourceSetCaptor.getValue().getIconUri()).isEqualTo(URI.create("ICON_URI"));
    Map<String, Object> responseBody = (Map<String, Object>) new ObjectMapper().readValue(response.getText(), Map.class);
    assertThat(responseBody).containsKey("_id");
    verify(hook).resourceSetCreated(anyString(), Matchers.<ResourceSetDescription>anyObject());
    verify(labelRegistration).updateLabelsForNewResourceSet(any(ResourceSetDescription.class));
}
Also used : InOrder(org.mockito.InOrder) JacksonRepresentation(org.restlet.ext.jackson.JacksonRepresentation) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) Representation(org.restlet.representation.Representation) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) InvocationOnMock(org.mockito.invocation.InvocationOnMock) JSONObject(org.json.JSONObject) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Example 65 with Representation

use of org.restlet.representation.Representation in project OpenAM by OpenRock.

the class AbstractRestletAccessAuditFilter method auditAccessSuccess.

private void auditAccessSuccess(Request request, Response response) {
    String realm = getRealmFromRequest(request);
    if (auditEventPublisher.isAuditing(realm, ACCESS_TOPIC, EventName.AM_ACCESS_OUTCOME)) {
        long endTime = System.currentTimeMillis();
        long elapsedTime = endTime - request.getDate().getTime();
        final Representation entity = response.getEntity();
        AMAccessAuditEventBuilder builder = auditEventFactory.accessEvent(realm).timestamp(endTime).transactionId(AuditRequestContext.getTransactionIdValue()).eventName(EventName.AM_ACCESS_OUTCOME).component(component).userId(getUserIdForAccessOutcome(request, response)).trackingIds(getTrackingIdsForAccessOutcome(request, response));
        JsonValue detail = null;
        if (responseDetailCreator != null) {
            try {
                detail = responseDetailCreator.apply(entity);
            } catch (AuditException e) {
                debug.warning("An error occurred when fetching response body details for audit", e);
            }
        }
        if (detail == null) {
            builder.response(SUCCESSFUL, "", elapsedTime, MILLISECONDS);
        } else {
            builder.responseWithDetail(SUCCESSFUL, "", elapsedTime, MILLISECONDS, detail);
        }
        addHttpData(request, builder);
        auditEventPublisher.tryPublish(ACCESS_TOPIC, builder.toEvent());
    }
}
Also used : JsonValue(org.forgerock.json.JsonValue) AuditException(org.forgerock.audit.AuditException) BufferingRepresentation(org.restlet.representation.BufferingRepresentation) Representation(org.restlet.representation.Representation) AMAccessAuditEventBuilder(org.forgerock.openam.audit.AMAccessAuditEventBuilder)

Aggregations

Representation (org.restlet.representation.Representation)101 HashMap (java.util.HashMap)28 Test (org.testng.annotations.Test)27 StringRepresentation (org.restlet.representation.StringRepresentation)24 Request (org.restlet.Request)23 Response (org.restlet.Response)23 JacksonRepresentation (org.restlet.ext.jackson.JacksonRepresentation)23 ResourceException (org.restlet.resource.ResourceException)21 Reference (org.restlet.data.Reference)19 StringWriter (java.io.StringWriter)17 JsonRepresentation (org.restlet.ext.json.JsonRepresentation)16 IOException (java.io.IOException)14 Map (java.util.Map)14 Form (org.restlet.data.Form)14 VCellApiApplication (org.vcell.rest.VCellApiApplication)14 User (org.vcell.util.document.User)13 Configuration (freemarker.template.Configuration)10 StringReader (java.io.StringReader)10 ZNRecord (org.apache.helix.ZNRecord)10 TypeReference (org.codehaus.jackson.type.TypeReference)10