use of org.gluu.model.GluuAttribute in project oxAuth by GluuFederation.
the class UserInfoRestWebServiceImpl method getJSonResponse.
/**
* Builds a JSon String with the response parameters.
*/
public String getJSonResponse(User user, AuthorizationGrant authorizationGrant, Collection<String> scopes) throws Exception {
log.trace("Building JSON reponse with next scopes {0} for user {1} and user custom attributes {0}", scopes, user.getUserId(), user.getCustomAttributes());
JsonWebResponse jsonWebResponse = new JsonWebResponse();
// Claims
List<Scope> dynamicScopes = new ArrayList<Scope>();
for (String scopeName : scopes) {
org.oxauth.persistence.model.Scope scope = scopeService.getScopeById(scopeName);
if ((scope != null) && (org.gluu.oxauth.model.common.ScopeType.DYNAMIC == scope.getScopeType())) {
dynamicScopes.add(scope);
continue;
}
Map<String, Object> claims = scopeService.getClaims(user, scope);
if (claims == null) {
continue;
}
if (scope != null && Boolean.TRUE.equals(scope.isOxAuthGroupClaims())) {
JwtSubClaimObject groupClaim = new JwtSubClaimObject();
groupClaim.setName(scope.getId());
for (Map.Entry<String, Object> entry : claims.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (value instanceof List) {
groupClaim.setClaim(key, (List<String>) value);
} else {
groupClaim.setClaim(key, String.valueOf(value));
}
}
jsonWebResponse.getClaims().setClaim(scope.getId(), groupClaim);
} else {
for (Map.Entry<String, Object> entry : claims.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (value instanceof List) {
jsonWebResponse.getClaims().setClaim(key, (List<String>) value);
} else if (value instanceof Boolean) {
jsonWebResponse.getClaims().setClaim(key, (Boolean) value);
} else if (value instanceof Date) {
jsonWebResponse.getClaims().setClaim(key, ((Date) value).getTime() / 1000);
} else {
jsonWebResponse.getClaims().setClaim(key, String.valueOf(value));
}
}
}
}
if (authorizationGrant.getClaims() != null) {
JSONObject claimsObj = new JSONObject(authorizationGrant.getClaims());
if (claimsObj.has("userinfo")) {
JSONObject userInfoObj = claimsObj.getJSONObject("userinfo");
for (Iterator<String> it = userInfoObj.keys(); it.hasNext(); ) {
String claimName = it.next();
// ClaimValueType.OPTIONAL.equals(claim.getClaimValue().getClaimValueType());
boolean optional = true;
GluuAttribute gluuAttribute = attributeService.getByClaimName(claimName);
if (gluuAttribute != null) {
String ldapClaimName = gluuAttribute.getName();
Object attribute = user.getAttribute(ldapClaimName, optional, gluuAttribute.getOxMultiValuedAttribute());
jsonWebResponse.getClaims().setClaimFromJsonObject(claimName, attribute);
}
}
}
}
if (authorizationGrant.getJwtAuthorizationRequest() != null && authorizationGrant.getJwtAuthorizationRequest().getUserInfoMember() != null) {
for (Claim claim : authorizationGrant.getJwtAuthorizationRequest().getUserInfoMember().getClaims()) {
// ClaimValueType.OPTIONAL.equals(claim.getClaimValue().getClaimValueType());
boolean optional = true;
GluuAttribute gluuAttribute = attributeService.getByClaimName(claim.getName());
if (gluuAttribute != null) {
Client client = authorizationGrant.getClient();
if (validateRequesteClaim(gluuAttribute, client.getClaims(), scopes)) {
String ldapClaimName = gluuAttribute.getName();
Object attribute = user.getAttribute(ldapClaimName, optional, gluuAttribute.getOxMultiValuedAttribute());
jsonWebResponse.getClaims().setClaimFromJsonObject(claim.getName(), attribute);
}
}
}
}
jsonWebResponse.getClaims().setSubjectIdentifier(authorizationGrant.getSub());
if ((dynamicScopes.size() > 0) && externalDynamicScopeService.isEnabled()) {
final UnmodifiableAuthorizationGrant unmodifiableAuthorizationGrant = new UnmodifiableAuthorizationGrant(authorizationGrant);
DynamicScopeExternalContext dynamicScopeContext = new DynamicScopeExternalContext(dynamicScopes, jsonWebResponse, unmodifiableAuthorizationGrant);
externalDynamicScopeService.executeExternalUpdateMethods(dynamicScopeContext);
}
return jsonWebResponse.toString();
}
use of org.gluu.model.GluuAttribute in project oxAuth by GluuFederation.
the class ScopeServiceTest method mockRegularGluuAttributesMapping.
private void mockRegularGluuAttributesMapping() {
GluuAttribute attributeUid = new GluuAttribute();
attributeUid.setName("uid");
attributeUid.setOxAuthClaimName("uid");
GluuAttribute attributeUpdatedAt = new GluuAttribute();
attributeUpdatedAt.setName("updatedAt");
attributeUpdatedAt.setOxAuthClaimName("updated_at");
GluuAttribute attributeCreatedAt = new GluuAttribute();
attributeCreatedAt.setName("createdAt");
attributeCreatedAt.setOxAuthClaimName("created_at");
GluuAttribute attributeBoolean = new GluuAttribute();
attributeBoolean.setDataType(AttributeDataType.BOOLEAN);
attributeBoolean.setName("emailVerified");
attributeBoolean.setOxAuthClaimName("email_verified");
GluuAttribute attributeDate = new GluuAttribute();
attributeDate.setDataType(AttributeDataType.DATE);
attributeDate.setName("lastLogon");
attributeDate.setOxAuthClaimName("last_logon");
GluuAttribute attributeJson = new GluuAttribute();
attributeJson.setDataType(AttributeDataType.BINARY);
attributeJson.setName("metadata");
attributeJson.setOxAuthClaimName("metadata");
GluuAttribute attributeTmp = new GluuAttribute();
attributeTmp.setDataType(AttributeDataType.STRING);
attributeTmp.setName("tmp");
attributeTmp.setOxAuthClaimName("tmp");
when(attributeService.getAttributeByDn(anyString())).thenReturn(attributeUid, attributeUpdatedAt, attributeCreatedAt, attributeBoolean, attributeDate, attributeJson, attributeTmp);
}
use of org.gluu.model.GluuAttribute in project oxAuth by GluuFederation.
the class ScopeServiceTest method getClaims_GluuAttributeLdapNameBlank_EmptyResult.
@Test
public void getClaims_GluuAttributeLdapNameBlank_EmptyResult() throws Exception {
User user = new User();
Scope scope = new Scope();
scope.setOxAuthClaims(Lists.newArrayList("claim1", "claim2"));
GluuAttribute gluuAttribute = new GluuAttribute();
gluuAttribute.setOxAuthClaimName("CLAIM_NAME");
when(attributeService.getAttributeByDn(anyString())).thenReturn(gluuAttribute);
Map<String, Object> result = scopeService.getClaims(user, scope);
assertNotNull(result);
assertEquals(result.size(), 0);
verify(log, times(2)).error(startsWith("Failed to get claim because name is not set for attribute"), (Object) isNull());
verifyNoMoreInteractions(log);
verifyNoMoreInteractions(attributeService);
}
Aggregations