use of org.wso2.carbon.claim.mgt.ClaimManagementException 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;
}
Aggregations