Search in sources :

Example 56 with JsonValue

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

the class UmaPolicyServiceImpl method resolveUsernameToUID.

private JsonValue resolveUsernameToUID(final Context context, JsonValue policy) throws BadRequestException {
    final String resourceOwnerName = contextHelper.getUserId(context);
    final String resourceOwnerUserUid = contextHelper.getUserUid(context);
    for (JsonValue permission : policy.get("permissions")) {
        final String userName = permission.get("subject").asString();
        if (StringUtils.isBlank(userName)) {
            throw new BadRequestException("Subject cannot be a blank string");
        }
        String userUid = contextHelper.getUserUid(context, userName);
        if (userUid != null) {
            permission.put("subject", userUid);
        } else if (resourceOwnerUserUid.contains(resourceOwnerName)) {
            final String derivedUserUid = resourceOwnerUserUid.replace(resourceOwnerName, userName);
            permission.put("subject", derivedUserUid);
        }
    }
    return policy;
}
Also used : JsonValue(org.forgerock.json.JsonValue) BadRequestException(org.forgerock.json.resource.BadRequestException)

Example 57 with JsonValue

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

the class UmaPolicyServiceImpl method resolveUIDToUsername.

private JsonValue resolveUIDToUsername(JsonValue policy) {
    for (JsonValue permission : policy.get("permissions")) {
        try {
            String username = new AMIdentity(null, permission.get("subject").asString()).getName();
            permission.put("subject", username);
        } catch (IdRepoException e) {
        //Cannot happen in this use case
        }
    }
    return policy;
}
Also used : AMIdentity(com.sun.identity.idm.AMIdentity) JsonValue(org.forgerock.json.JsonValue) IdRepoException(com.sun.identity.idm.IdRepoException)

Example 58 with JsonValue

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

the class PendingRequestsServiceTest method shouldSendEmailOnPendingRequestApproval.

@Test
public void shouldSendEmailOnPendingRequestApproval() throws Exception {
    //Given
    Context context = mock(Context.class);
    createPendingRequest(PENDING_REQUEST_ID, RESOURCE_SET_ID, RESOURCE_SET_NAME, RESOURCE_OWNER_ID, REALM, REQUESTING_PARTY_ID, Collections.singleton(SCOPE));
    given(settings.isEmailRequestingPartyOnPendingRequestApprovalEnabled()).willReturn(true);
    mockPendingRequestApprovalEmailTemplate(REQUESTING_PARTY_ID, REALM);
    mockSuccessfulPolicyCreationForPendingRequest();
    JsonValue content = json(object());
    //When
    service.approvePendingRequest(context, PENDING_REQUEST_ID, content, REALM);
    //Then
    verify(policyService).createPolicy(eq(context), any(JsonValue.class));
    verify(emailService).email(REALM, REQUESTING_PARTY_ID, "APPROVAL_SUBJECT", "APPROVAL_BODY " + RESOURCE_OWNER_ID + " " + RESOURCE_SET_NAME + " " + SCOPE);
    verify(store).delete(PENDING_REQUEST_ID);
    verify(auditLogger).log(RESOURCE_SET_ID, RESOURCE_SET_NAME, resourceOwnerIdentity, UmaAuditType.REQUEST_APPROVED, REQUESTING_PARTY_ID);
}
Also used : Context(org.forgerock.services.context.Context) JsonValue(org.forgerock.json.JsonValue) Test(org.testng.annotations.Test)

Example 59 with JsonValue

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

the class PendingRequestsServiceTest method shouldApprovePendingRequestUsingScopesFromRequestContent.

@Test
public void shouldApprovePendingRequestUsingScopesFromRequestContent() throws Exception {
    //Given
    Context context = mock(Context.class);
    createPendingRequest(PENDING_REQUEST_ID, RESOURCE_SET_ID, RESOURCE_SET_NAME, RESOURCE_OWNER_ID, REALM, REQUESTING_PARTY_ID, Collections.singleton(SCOPE));
    mockSuccessfulPolicyCreationForPendingRequest();
    JsonValue content = json(object(field("scopes", array("SCOPE_A", "SCOPE_B"))));
    //When
    service.approvePendingRequest(context, PENDING_REQUEST_ID, content, REALM);
    //Then
    ArgumentCaptor<JsonValue> policyCaptor = ArgumentCaptor.forClass(JsonValue.class);
    verify(policyService).createPolicy(eq(context), policyCaptor.capture());
    JsonValue policy = policyCaptor.getValue();
    assertThat(policy).stringAt("policyId").isEqualTo(RESOURCE_SET_ID);
    assertThat(policy).hasArray("permissions").hasSize(1);
    assertThat(policy).stringAt("permissions/0/subject").isEqualTo(REQUESTING_PARTY_ID);
    assertThat(policy).hasArray("permissions/0/scopes").containsOnly("SCOPE_A", "SCOPE_B");
    verify(store).delete(PENDING_REQUEST_ID);
    verify(auditLogger).log(RESOURCE_SET_ID, RESOURCE_SET_NAME, resourceOwnerIdentity, UmaAuditType.REQUEST_APPROVED, REQUESTING_PARTY_ID);
}
Also used : Context(org.forgerock.services.context.Context) JsonValue(org.forgerock.json.JsonValue) Test(org.testng.annotations.Test)

Example 60 with JsonValue

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

the class PrincipalFromSessionImpl method parsePrincipalFromResponse.

private Principal parsePrincipalFromResponse(String response) throws TokenValidationException {
    JsonValue responseJson;
    try {
        responseJson = JsonValueBuilder.toJsonValue(response);
    } catch (JsonException e) {
        String message = "Exception caught getting the text of the json principal from session response: " + e;
        throw new TokenValidationException(ResourceException.INTERNAL_ERROR, message, e);
    }
    JsonValue principalIdJsonValue = responseJson.get(ID);
    if (!principalIdJsonValue.isString()) {
        String message = "Principal from session response does not contain " + ID + " string entry. The obtained entry: " + principalIdJsonValue.toString() + "; The response: " + responseJson.toString();
        throw new TokenValidationException(ResourceException.INTERNAL_ERROR, message);
    }
    return new STSPrincipal(principalIdJsonValue.asString());
}
Also used : JsonException(org.forgerock.json.JsonException) JsonValue(org.forgerock.json.JsonValue) TokenValidationException(org.forgerock.openam.sts.TokenValidationException) STSPrincipal(org.forgerock.openam.sts.STSPrincipal)

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