Search in sources :

Example 66 with GluuAttribute

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();
}
Also used : JsonWebResponse(org.gluu.oxauth.model.token.JsonWebResponse) DynamicScopeExternalContext(org.gluu.oxauth.service.external.context.DynamicScopeExternalContext) JwtSubClaimObject(org.gluu.oxauth.model.jwt.JwtSubClaimObject) GluuAttribute(org.gluu.model.GluuAttribute) Scope(org.oxauth.persistence.model.Scope) JSONObject(org.json.JSONObject) JwtSubClaimObject(org.gluu.oxauth.model.jwt.JwtSubClaimObject) JSONObject(org.json.JSONObject) Client(org.gluu.oxauth.model.registration.Client) Claim(org.gluu.oxauth.model.authorize.Claim) Scope(org.oxauth.persistence.model.Scope)

Example 67 with GluuAttribute

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);
}
Also used : GluuAttribute(org.gluu.model.GluuAttribute)

Example 68 with GluuAttribute

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);
}
Also used : User(org.gluu.oxauth.model.common.User) Scope(org.oxauth.persistence.model.Scope) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) GluuAttribute(org.gluu.model.GluuAttribute) Test(org.testng.annotations.Test)

Aggregations

GluuAttribute (org.gluu.model.GluuAttribute)68 ArrayList (java.util.ArrayList)21 GluuCustomAttribute (org.gluu.oxtrust.model.GluuCustomAttribute)10 IOException (java.io.IOException)8 Scope (org.oxauth.persistence.model.Scope)8 HttpEntity (org.apache.http.HttpEntity)7 HttpResponse (org.apache.http.HttpResponse)7 ParseException (org.apache.http.ParseException)7 Test (org.junit.Test)7 HttpGet (org.apache.http.client.methods.HttpGet)6 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)6 Filter (org.gluu.search.filter.Filter)5 JSONObject (org.json.JSONObject)4 Operation (io.swagger.v3.oas.annotations.Operation)3 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 FacesMessage (javax.faces.application.FacesMessage)3 UIInput (javax.faces.component.UIInput)3 AttributeValidation (org.gluu.model.attribute.AttributeValidation)3