Search in sources :

Example 1 with UserClaims

use of org.wso2.carbon.apimgt.impl.utils.UserClaims in project carbon-apimgt by wso2.

the class JWTGenerator method convertClaimMap.

protected Map<String, String> convertClaimMap(Map<ClaimMapping, String> userAttributes, String username) throws APIManagementException {
    Map<String, String> userClaims = new HashMap<>();
    Map<String, String> userClaimsCopy = new HashMap<>();
    for (Map.Entry<ClaimMapping, String> entry : userAttributes.entrySet()) {
        Claim claimObject = entry.getKey().getLocalClaim();
        if (claimObject == null) {
            claimObject = entry.getKey().getRemoteClaim();
        }
        userClaims.put(claimObject.getClaimUri(), entry.getValue());
        userClaimsCopy.put(claimObject.getClaimUri(), entry.getValue());
    }
    String convertClaimsFromOIDCtoConsumerDialect = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration().getFirstProperty(APIConstants.CONVERT_CLAIMS_TO_CONSUMER_DIALECT);
    if (convertClaimsFromOIDCtoConsumerDialect != null && !Boolean.parseBoolean(convertClaimsFromOIDCtoConsumerDialect)) {
        return userClaims;
    }
    int tenantId = APIUtil.getTenantId(username);
    String tenantDomain = APIUtil.getTenantDomainFromTenantId(tenantId);
    String dialect;
    ClaimsRetriever claimsRetriever = getClaimsRetriever();
    if (claimsRetriever != null) {
        dialect = claimsRetriever.getDialectURI(username);
    } else {
        dialect = getDialectURI();
    }
    // (key) configuredDialectClaimURI -> (value)
    Map<String, String> configuredDialectToCarbonClaimMapping = null;
    // carbonClaimURI
    // (key) carbonClaimURI ->  value (oidcClaimURI)
    Map<String, String> carbonToOIDCclaimMapping = null;
    Set<String> claimUris = new HashSet<String>(userClaims.keySet());
    try {
        carbonToOIDCclaimMapping = new ClaimMetadataHandler().getMappingsMapFromOtherDialectToCarbon(OIDC_DIALECT_URI, claimUris, tenantDomain, true);
        configuredDialectToCarbonClaimMapping = ClaimManagerHandler.getInstance().getMappingsMapFromCarbonDialectToOther(dialect, carbonToOIDCclaimMapping.keySet(), tenantDomain);
    } catch (ClaimMetadataException e) {
        String error = "Error while mapping claims from Carbon dialect to " + OIDC_DIALECT_URI + " dialect";
        throw new APIManagementException(error, e);
    } catch (ClaimManagementException e) {
        String error = "Error while mapping claims from configured dialect to Carbon dialect";
        throw new APIManagementException(error, e);
    }
    for (Map.Entry<String, String> oidcClaimValEntry : userClaims.entrySet()) {
        for (Map.Entry<String, String> carbonToOIDCEntry : carbonToOIDCclaimMapping.entrySet()) {
            if (oidcClaimValEntry.getKey().equals(carbonToOIDCEntry.getValue())) {
                for (Map.Entry<String, String> configuredToCarbonEntry : configuredDialectToCarbonClaimMapping.entrySet()) {
                    if (configuredToCarbonEntry.getValue().equals(carbonToOIDCEntry.getKey())) {
                        userClaimsCopy.remove(oidcClaimValEntry.getKey());
                        userClaimsCopy.put(configuredToCarbonEntry.getKey(), oidcClaimValEntry.getValue());
                    }
                }
            }
        }
    }
    return userClaimsCopy;
}
Also used : ClaimMetadataException(org.wso2.carbon.identity.claim.metadata.mgt.exception.ClaimMetadataException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ClaimMetadataHandler(org.wso2.carbon.identity.claim.metadata.mgt.ClaimMetadataHandler) ClaimsRetriever(org.wso2.carbon.apimgt.impl.token.ClaimsRetriever) ClaimMapping(org.wso2.carbon.identity.application.common.model.ClaimMapping) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ClaimManagementException(org.wso2.carbon.claim.mgt.ClaimManagementException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Claim(org.wso2.carbon.identity.application.common.model.Claim) HashSet(java.util.HashSet)

Example 2 with UserClaims

use of org.wso2.carbon.apimgt.impl.utils.UserClaims in project carbon-apimgt by wso2.

the class DefaultClaimsRetrieverTestCase method testGetClaimsWhenCacheNonEmpty.

@Test
public void testGetClaimsWhenCacheNonEmpty() throws Exception {
    DefaultClaimsRetriever defaultClaimsRetriever = new DefaultClaimsRetriever();
    Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.JWT_CLAIM_CACHE_EXPIRY)).thenReturn("3600");
    CacheBuilder cacheBuilder = Mockito.mock(CacheBuilder.class);
    Mockito.when(cacheManager.createCacheBuilder(APIConstants.CLAIMS_APIM_CACHE)).thenReturn(cacheBuilder);
    Cache cache = Mockito.mock(Cache.class);
    Mockito.when(cacheBuilder.setStoreByValue(false)).thenReturn(cacheBuilder);
    Mockito.when(cacheBuilder.setExpiry(Matchers.any(CacheConfiguration.ExpiryType.class), Matchers.any(CacheConfiguration.Duration.class))).thenReturn(cacheBuilder);
    Mockito.when(cacheBuilder.build()).thenReturn(cache);
    PowerMockito.mockStatic(APIUtil.class);
    PowerMockito.when(APIUtil.getTenantId(USER_NAME)).thenReturn(TENANT_ID);
    SortedMap<String, String> claimValues = new TreeMap<String, String>();
    claimValues.put("claim1", "http://wso2.org/claim1");
    claimValues.put("claim2", "http://wso2.org/claim2");
    UserClaims userClaims = new UserClaims(claimValues);
    Mockito.when(cache.get(Matchers.any(ClaimCacheKey.class))).thenReturn(userClaims);
    SortedMap<String, String> claims = defaultClaimsRetriever.getClaims(USER_NAME);
    Assert.assertNotNull(claims);
    Assert.assertEquals(claimValues, claims);
}
Also used : UserClaims(org.wso2.carbon.apimgt.impl.utils.UserClaims) ClaimCacheKey(org.wso2.carbon.apimgt.impl.utils.ClaimCacheKey) CacheBuilder(javax.cache.CacheBuilder) TreeMap(java.util.TreeMap) Cache(javax.cache.Cache) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with UserClaims

use of org.wso2.carbon.apimgt.impl.utils.UserClaims in project carbon-apimgt by wso2.

the class CommentMappingUtil method fromCommentToDTOWithUserInfo.

/**
 * Converts a Comment object into corresponding REST API CommentDTO object with User Info.
 *
 * @param comment comment object
 * @return CommentDTO
 */
public static CommentDTO fromCommentToDTOWithUserInfo(Comment comment, Map<String, Map<String, String>> userClaimsMap) throws APIManagementException {
    CommentDTO commentDTO = fromCommentToDTO(comment);
    if (userClaimsMap.get(comment.getUser()) != null) {
        Map userClaims = userClaimsMap.get(comment.getUser());
        CommenterInfoDTO commenterInfoDTO = new CommenterInfoDTO();
        commenterInfoDTO.setFullName((String) userClaims.get(APIConstants.FULL_NAME));
        commenterInfoDTO.setFirstName((String) userClaims.get(APIConstants.FIRST_NAME));
        commenterInfoDTO.setLastName((String) userClaims.get(APIConstants.LAST_NAME));
        commentDTO.setCommenterInfo(commenterInfoDTO);
    }
    return commentDTO;
}
Also used : CommenterInfoDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommenterInfoDTO) CommentDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommentDTO) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with UserClaims

use of org.wso2.carbon.apimgt.impl.utils.UserClaims in project carbon-apimgt by wso2.

the class CommentMappingUtil method fromCommentToDTOWithUserInfo.

/**
 * Converts a Comment object into corresponding REST API CommentDTO object with User Info
 *
 * @param comment comment object
 * @return CommentDTO
 */
public static CommentDTO fromCommentToDTOWithUserInfo(Comment comment, Map<String, Map<String, String>> userClaimsMap) throws APIManagementException {
    CommentDTO commentDTO = fromCommentToDTO(comment);
    if (userClaimsMap.get(comment.getUser()) != null) {
        Map userClaims = userClaimsMap.get(comment.getUser());
        CommenterInfoDTO commenterInfoDTO = new CommenterInfoDTO();
        commenterInfoDTO.setFullName((String) userClaims.get(APIConstants.FULL_NAME));
        commenterInfoDTO.setFirstName((String) userClaims.get(APIConstants.FIRST_NAME));
        commenterInfoDTO.setLastName((String) userClaims.get(APIConstants.LAST_NAME));
        commentDTO.setCommenterInfo(commenterInfoDTO);
    }
    return commentDTO;
}
Also used : CommenterInfoDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.CommenterInfoDTO) CommentDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.CommentDTO) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with UserClaims

use of org.wso2.carbon.apimgt.impl.utils.UserClaims in project carbon-apimgt by wso2.

the class DefaultClaimsRetriever method getClaims.

public SortedMap<String, String> getClaims(String endUserName) throws APIManagementException {
    String strEnabledJWTClaimCache = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration().getFirstProperty(APIConstants.ENABLED_JWT_CLAIM_CACHE);
    boolean enabledJWTClaimCache = true;
    if (strEnabledJWTClaimCache != null) {
        enabledJWTClaimCache = Boolean.valueOf(strEnabledJWTClaimCache);
    }
    SortedMap<String, String> claimValues;
    if (endUserName != null) {
        int tenantId = APIUtil.getTenantId(endUserName);
        String tenantAwareUserName = MultitenantUtils.getTenantAwareUsername(endUserName);
        // check in local cache
        String key = endUserName + ':' + tenantId;
        ClaimCacheKey cacheKey = new ClaimCacheKey(key);
        Object result = null;
        if (enabledJWTClaimCache) {
            result = getClaimsLocalCache().get(cacheKey);
        }
        if (result != null) {
            return ((UserClaims) result).getClaimValues();
        } else {
            claimValues = APIUtil.getClaims(endUserName, tenantId, dialectURI);
            UserClaims userClaims = new UserClaims(claimValues);
            // add to cache
            if (enabledJWTClaimCache) {
                getClaimsLocalCache().put(cacheKey, userClaims);
            }
            return claimValues;
        }
    }
    return null;
}
Also used : ClaimCacheKey(org.wso2.carbon.apimgt.impl.utils.ClaimCacheKey) UserClaims(org.wso2.carbon.apimgt.impl.utils.UserClaims)

Aggregations

HashMap (java.util.HashMap)3 Map (java.util.Map)3 ClaimCacheKey (org.wso2.carbon.apimgt.impl.utils.ClaimCacheKey)2 UserClaims (org.wso2.carbon.apimgt.impl.utils.UserClaims)2 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 TreeMap (java.util.TreeMap)1 Cache (javax.cache.Cache)1 CacheBuilder (javax.cache.CacheBuilder)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 ClaimsRetriever (org.wso2.carbon.apimgt.impl.token.ClaimsRetriever)1 CommentDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommentDTO)1 CommenterInfoDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommenterInfoDTO)1 CommentDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.CommentDTO)1 CommenterInfoDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.CommenterInfoDTO)1 ClaimManagementException (org.wso2.carbon.claim.mgt.ClaimManagementException)1 Claim (org.wso2.carbon.identity.application.common.model.Claim)1 ClaimMapping (org.wso2.carbon.identity.application.common.model.ClaimMapping)1