Search in sources :

Example 71 with JsonValue

use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.

the class ApplicationV1FilterTest method updateFailsWhenApplicationMissing.

/**
     * Verifies that update fails when the selected application cannot be found.
     */
@Test(expectedExceptions = NotFoundException.class)
public void updateFailsWhenApplicationMissing() throws Exception {
    // Given
    given(contextHelper.getRealm(context)).willReturn("/abc");
    given(contextHelper.getSubject(context)).willReturn(subject);
    // Build application JSON representation.
    JsonValue jsonValue = json(object(TestData.DATA_SET_1.getResources().asJson(), TestData.DATA_SET_1.getActions().asJson(), field("realm", "/abc")));
    UpdateRequest updateRequest = mock(UpdateRequest.class);
    given(updateRequest.getContent()).willReturn(jsonValue);
    given(updateRequest.getResourcePath()).willReturn("testApplication");
    given(applicationServiceFactory.create(subject, "/abc")).willReturn(applicationService);
    given(applicationService.getApplication("testApplication")).willReturn(null);
    // When
    Promise<ResourceResponse, ResourceException> result = filter.filterUpdate(context, updateRequest, requestHandler);
    // Then
    result.getOrThrowUninterruptibly();
}
Also used : ResourceResponse(org.forgerock.json.resource.ResourceResponse) UpdateRequest(org.forgerock.json.resource.UpdateRequest) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.forgerock.json.resource.ResourceException) Test(org.testng.annotations.Test)

Example 72 with JsonValue

use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.

the class ApplicationV1FilterTest method resourceTypeCreationOnCreate.

/**
     * Verifies that the appropriate resource type is created for the application being created.
     */
@Test
public void resourceTypeCreationOnCreate() throws Exception {
    // Given
    given(contextHelper.getRealm(context)).willReturn("/abc");
    given(contextHelper.getSubject(context)).willReturn(subject);
    // Build application JSON representation.
    JsonValue jsonValue = json(object(field("name", "testApplication"), TestData.DATA_SET_1.getResources().asJson(), TestData.DATA_SET_1.getActions().asJson(), field("realm", "/abc")));
    CreateRequest createRequest = mock(CreateRequest.class);
    given(createRequest.getContent()).willReturn(jsonValue);
    Set<ResourceType> resourceTypes = Collections.emptySet();
    given(resourceTypeService.getResourceTypes(queryFilterCaptor.capture(), eq(subject), eq("/abc"))).willReturn(resourceTypes);
    ResourceType resourceType = ResourceType.builder().setName("test").setUUID("some-test-uuid").setActions(TestData.DATA_SET_1.getActions().getUnderlyingMap()).setPatterns(TestData.DATA_SET_1.getResources().getUnderlyingSet()).build();
    given(resourceTypeService.saveResourceType(eq(subject), eq("/abc"), resourceTypeCaptor.capture())).willReturn(resourceType);
    // When
    filter.filterCreate(context, createRequest, requestHandler);
    // Then
    assertThat(jsonValue.get("resourceTypeUuids").asSet(String.class)).containsOnly("some-test-uuid");
    verify(requestHandler).handleCreate(eq(context), eq(createRequest));
    ResourceType capturedResourceType = resourceTypeCaptor.getValue();
    assertThat(capturedResourceType.getName()).startsWith("testApplicationResourceType");
    assertThat(capturedResourceType.getActions()).isEqualTo(TestData.DATA_SET_1.getActions().getUnderlyingMap());
    assertThat(capturedResourceType.getPatterns()).isEqualTo(TestData.DATA_SET_1.getResources().getUnderlyingSet());
}
Also used : CreateRequest(org.forgerock.json.resource.CreateRequest) JsonValue(org.forgerock.json.JsonValue) ResourceType(org.forgerock.openam.entitlement.ResourceType) Test(org.testng.annotations.Test)

Example 73 with JsonValue

use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.

the class OpenAMTokenStore method readRefreshToken.

/**
     * {@inheritDoc}
     */
public RefreshToken readRefreshToken(OAuth2Request request, String tokenId) throws ServerException, InvalidGrantException, NotFoundException {
    RefreshToken loaded = request.getToken(RefreshToken.class);
    if (loaded != null) {
        return loaded;
    }
    logger.message("Read refresh token");
    JsonValue token;
    try {
        token = tokenStore.read(tokenId);
    } catch (CoreTokenException e) {
        logger.error("Unable to read refresh token corresponding to id: " + tokenId, e);
        throw new ServerException("Could not read token in CTS: " + e.getMessage());
    }
    if (token == null) {
        logger.error("Unable to read refresh token corresponding to id: " + tokenId);
        throw new InvalidGrantException("grant is invalid");
    }
    OpenAMRefreshToken refreshToken = new OpenAMRefreshToken(token);
    validateTokenRealm(refreshToken.getRealm(), request);
    request.setToken(RefreshToken.class, refreshToken);
    return refreshToken;
}
Also used : RefreshToken(org.forgerock.oauth2.core.RefreshToken) ServerException(org.forgerock.oauth2.core.exceptions.ServerException) JsonValue(org.forgerock.json.JsonValue) CoreTokenException(org.forgerock.openam.cts.exceptions.CoreTokenException) InvalidGrantException(org.forgerock.oauth2.core.exceptions.InvalidGrantException)

Example 74 with JsonValue

use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.

the class OpenAMTokenStore method readAuthorizationCode.

/**
     * {@inheritDoc}
     */
public AuthorizationCode readAuthorizationCode(OAuth2Request request, String code) throws InvalidGrantException, ServerException, NotFoundException {
    AuthorizationCode loaded = request.getToken(AuthorizationCode.class);
    if (loaded != null) {
        return loaded;
    }
    logger.message("Reading Authorization code: {}", code);
    final JsonValue token;
    // Read from CTS
    try {
        token = tokenStore.read(code);
    } catch (CoreTokenException e) {
        logger.error("Unable to read authorization code corresponding to id: " + code, e);
        throw new ServerException("Could not read token from CTS: " + e.getMessage());
    }
    if (token == null) {
        logger.error("Unable to read authorization code corresponding to id: " + code);
        throw new InvalidGrantException("The provided access grant is invalid, expired, or revoked.");
    }
    OpenAMAuthorizationCode authorizationCode = new OpenAMAuthorizationCode(token);
    validateTokenRealm(authorizationCode.getRealm(), request);
    request.setToken(AuthorizationCode.class, authorizationCode);
    return authorizationCode;
}
Also used : AuthorizationCode(org.forgerock.oauth2.core.AuthorizationCode) ServerException(org.forgerock.oauth2.core.exceptions.ServerException) JsonValue(org.forgerock.json.JsonValue) CoreTokenException(org.forgerock.openam.cts.exceptions.CoreTokenException) InvalidGrantException(org.forgerock.oauth2.core.exceptions.InvalidGrantException)

Example 75 with JsonValue

use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.

the class OpenAMTokenStore method readAccessToken.

/**
     * {@inheritDoc}
     */
public AccessToken readAccessToken(OAuth2Request request, String tokenId) throws ServerException, InvalidGrantException, NotFoundException {
    AccessToken loaded = request.getToken(AccessToken.class);
    if (loaded != null) {
        return loaded;
    }
    logger.message("Reading access token");
    JsonValue token;
    // Read from CTS
    try {
        token = tokenStore.read(tokenId);
    } catch (CoreTokenException e) {
        logger.error("Unable to read access token corresponding to id: " + tokenId, e);
        throw new ServerException("Could not read token in CTS: " + e.getMessage());
    }
    if (token == null) {
        logger.error("Unable to read access token corresponding to id: " + tokenId);
        throw new InvalidGrantException("Could not read token in CTS");
    }
    OpenAMAccessToken accessToken = new OpenAMAccessToken(token);
    validateTokenRealm(accessToken.getRealm(), request);
    request.setToken(AccessToken.class, accessToken);
    return accessToken;
}
Also used : ServerException(org.forgerock.oauth2.core.exceptions.ServerException) AccessToken(org.forgerock.oauth2.core.AccessToken) JsonValue(org.forgerock.json.JsonValue) CoreTokenException(org.forgerock.openam.cts.exceptions.CoreTokenException) InvalidGrantException(org.forgerock.oauth2.core.exceptions.InvalidGrantException)

Aggregations

JsonValue (org.forgerock.json.JsonValue)575 Test (org.testng.annotations.Test)333 ResourceException (org.forgerock.json.resource.ResourceException)144 ResourceResponse (org.forgerock.json.resource.ResourceResponse)123 RealmContext (org.forgerock.openam.rest.RealmContext)70 Context (org.forgerock.services.context.Context)63 HashSet (java.util.HashSet)56 SSOException (com.iplanet.sso.SSOException)54 ArrayList (java.util.ArrayList)51 BadRequestException (org.forgerock.json.resource.BadRequestException)47 Privilege (com.sun.identity.entitlement.Privilege)46 InternalServerErrorException (org.forgerock.json.resource.InternalServerErrorException)46 SSOToken (com.iplanet.sso.SSOToken)43 SMSException (com.sun.identity.sm.SMSException)42 HashMap (java.util.HashMap)42 NotFoundException (org.forgerock.json.resource.NotFoundException)41 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)41 CreateRequest (org.forgerock.json.resource.CreateRequest)40 OpenSSOPrivilege (com.sun.identity.entitlement.opensso.OpenSSOPrivilege)39 Subject (javax.security.auth.Subject)32