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;
}
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;
}
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);
}
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);
}
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());
}
Aggregations