use of org.forgerock.oauth2.core.UserInfoClaims in project OpenAM by OpenRock.
the class ScopeImpl method getUserInfo.
/**
* {@inheritDoc}
*/
public UserInfoClaims getUserInfo(CoreToken token) {
Set<String> scopes = token.getScope();
Map<String, Object> response = new HashMap<String, Object>();
AMIdentity id = null;
try {
id = identityManager.getResourceOwnerIdentity(token.getUserID(), token.getRealm());
} catch (UnauthorizedClientException e) {
throw OAuthProblemException.OAuthError.UNAUTHORIZED_CLIENT.handle(null, e.getMessage());
}
//add the subject identifier to the response
response.put("sub", token.getUserID());
for (String scope : scopes) {
if (OPENID_SCOPE.equals(scope)) {
continue;
}
//get the attribute associated with the scope
Object attributes = scopeToUserUserProfileAttributes.get(scope);
if (attributes == null) {
logger.error("ScopeImpl.getUserInfo()::Invalid Scope in token scope=" + scope);
} else if (attributes instanceof String) {
Set<String> attr = null;
//if the attribute is a string get the attribute
try {
attr = id.getAttribute((String) attributes);
} catch (IdRepoException e) {
logger.warning("ScopeImpl.getUserInfo(): Unable to retrieve attribute= " + attributes, e);
} catch (SSOException e) {
logger.warning("ScopeImpl.getUserInfo(): Unable to retrieve attribute= " + attributes, e);
}
//add a single object to the response.
if (attr != null && attr.size() == 1) {
response.put(scope, attr.iterator().next());
} else if (attr != null && attr.size() > 1) {
// add a set to the response
response.put(scope, attr);
} else {
//attr is null or attr is empty
logger.warning("ScopeImpl.getUserInfo(): Got an empty result for attribute=" + attributes + " of scope=" + scope);
}
} else if (attributes instanceof Map) {
//for example profile can be address, email, etc...
if (attributes != null && !((Map<String, String>) attributes).isEmpty()) {
for (Map.Entry<String, String> entry : ((Map<String, String>) attributes).entrySet()) {
String attribute;
attribute = entry.getValue();
Set<String> attr = null;
//get the attribute
try {
attr = id.getAttribute(attribute);
} catch (IdRepoException e) {
logger.warning("ScopeImpl.getUserInfo(): Unable to retrieve attribute", e);
} catch (SSOException e) {
logger.warning("ScopeImpl.getUserInfo(): Unable to retrieve attribute", e);
}
//add the attribute value(s) to the response
if (attr != null && attr.size() == 1) {
response.put(entry.getKey(), attr.iterator().next());
} else if (attr != null && attr.size() > 1) {
response.put(entry.getKey(), attr);
} else {
//attr is null or attr is empty
logger.warning("ScopeImpl.getUserInfo(): Got an empty result for scope=" + scope);
}
}
}
}
}
return new UserInfoClaims(response, Collections.<String, List<String>>emptyMap());
}
use of org.forgerock.oauth2.core.UserInfoClaims in project OpenAM by OpenRock.
the class OidcClaimsExtensionTest method testRequestedClaims.
@Test
public void testRequestedClaims() throws Exception {
// Given
Map<String, Set<String>> requestedClaims = new HashMap<String, Set<String>>();
requestedClaims.put("given_name", asSet("fred"));
requestedClaims.put("family_name", asSet("flintstone"));
Bindings variables = testBindings(asSet("profile"), requestedClaims);
when(identity.getAttribute("cn")).thenReturn(asSet("Joe Bloggs"));
// When
UserInfoClaims result = scriptEvaluator.evaluateScript(script, variables);
// Then
assertThat(result.getValues()).containsOnly(entry("given_name", "fred"), entry("family_name", "flintstone"), entry("name", "Joe Bloggs"));
assertThat(result.getCompositeScopes()).containsOnlyKeys("profile");
ArrayList<String> hashProfile = (ArrayList<String>) result.getCompositeScopes().get("profile");
assertThat(hashProfile).contains("zoneinfo", "name", "locale", "family_name", "given_name");
assertThat(hashProfile).hasSize(5);
verify(identity).getAttribute("cn");
verify(identity).getAttribute("preferredlocale");
verify(identity).getAttribute("preferredtimezone");
verifyNoMoreInteractions(identity);
}
Aggregations