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