Search in sources :

Example 21 with Claim

use of org.wso2.carbon.identity.application.common.model.xsd.Claim in project product-microgateway by wso2.

the class JWTGenerationTestCase method start.

@BeforeClass
public void start() throws Exception {
    String project = "jwtGeneratorProject";
    // Define application info
    ApplicationDTO application = new ApplicationDTO();
    application.setName("jwtApp");
    application.setTier("Unlimited");
    application.setId((int) (Math.random() * 1000));
    // Create map with custom claims
    Map<String, String> customClaims = new HashMap<>();
    customClaims.put("claim1", "testValue1");
    customClaims.put("claim2", "testValue2");
    jwtTokenProd = TokenUtil.getJwtWithCustomClaims(application, new JSONObject(), TestConstant.KEY_TYPE_PRODUCTION, 3600, customClaims);
    // Create map with backendJwt claim
    Map<String, String> jwtClaim = new HashMap<>();
    jwtClaim.put("backendJwt", jwtTokenProd);
    jwtWithBackendJwtClaim = TokenUtil.getJwtWithCustomClaims(application, new JSONObject(), TestConstant.KEY_TYPE_PRODUCTION, 3600, jwtClaim);
    // generate apis with CLI and start the micro gateway server
    super.init(project, new String[] { "jwtGeneration/jwt_generation.yaml", "mgw-JwtGenerator.jar" }, null, "confs/jwt-generator-test-config.conf");
}
Also used : ApplicationDTO(org.wso2.micro.gateway.tests.common.model.ApplicationDTO) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) BeforeClass(org.testng.annotations.BeforeClass)

Example 22 with Claim

use of org.wso2.carbon.identity.application.common.model.xsd.Claim in project carbon-identity-framework by wso2.

the class JsClaims method getLocalUserClaim.

/**
 * Get the local user claim value specified by the Claim URI.
 *
 * @param claimUri Local claim URI
 * @return Claim value of the given claim URI for the local user if available. Null Otherwise.
 */
private String getLocalUserClaim(String claimUri) {
    int usersTenantId = IdentityTenantUtil.getTenantId(authenticatedUser.getTenantDomain());
    RealmService realmService = FrameworkServiceDataHolder.getInstance().getRealmService();
    try {
        UserRealm userRealm = realmService.getTenantUserRealm(usersTenantId);
        Map<String, String> claimValues = ((AbstractUserStoreManager) userRealm.getUserStoreManager()).getUserClaimValuesWithID(authenticatedUser.getUserId(), new String[] { claimUri }, null);
        return claimValues.get(claimUri);
    } catch (UserStoreException e) {
        LOG.error(String.format("Error when getting claim : %s of user: %s", claimUri, authenticatedUser), e);
    } catch (UserIdNotFoundException e) {
        LOG.error("User id is not available for the user: " + authenticatedUser.getLoggableUserId(), e);
    }
    return null;
}
Also used : UserRealm(org.wso2.carbon.user.api.UserRealm) RealmService(org.wso2.carbon.user.core.service.RealmService) UserStoreException(org.wso2.carbon.user.api.UserStoreException) AbstractUserStoreManager(org.wso2.carbon.user.core.common.AbstractUserStoreManager) UserIdNotFoundException(org.wso2.carbon.identity.application.authentication.framework.exception.UserIdNotFoundException)

Example 23 with Claim

use of org.wso2.carbon.identity.application.common.model.xsd.Claim in project carbon-identity-framework by wso2.

the class JsClaims method hasLocalClaim.

/**
 * Check if there is a local claim by given name.
 *
 * @param claimUri The local claim URI
 * @return Claim value of the user authenticated by the indicated IdP
 */
protected boolean hasLocalClaim(String claimUri) {
    int usersTenantId = IdentityTenantUtil.getTenantId(authenticatedUser.getTenantDomain());
    RealmService realmService = FrameworkServiceDataHolder.getInstance().getRealmService();
    try {
        UserRealm userRealm = realmService.getTenantUserRealm(usersTenantId);
        Claim[] supportedClaims = IdentityClaimManager.getInstance().getAllSupportedClaims((org.wso2.carbon.user.core.UserRealm) userRealm);
        for (Claim claim : supportedClaims) {
            if (claim.getClaimUri().equals(claimUri)) {
                return true;
            }
        }
    } catch (UserStoreException e) {
        LOG.error("Error when retrieving user realm for tenant : " + usersTenantId, e);
    } catch (IdentityException e) {
        LOG.error("Error when initializing identity claim manager.", e);
    }
    return false;
}
Also used : UserRealm(org.wso2.carbon.user.api.UserRealm) RealmService(org.wso2.carbon.user.core.service.RealmService) UserStoreException(org.wso2.carbon.user.api.UserStoreException) IdentityException(org.wso2.carbon.identity.base.IdentityException) Claim(org.wso2.carbon.user.core.claim.Claim)

Example 24 with Claim

use of org.wso2.carbon.identity.application.common.model.xsd.Claim in project carbon-identity-framework by wso2.

the class FrameworkUtils method getFederatedSubjectFromClaims.

/*
     * Find the Subject identifier among federated claims
     */
public static String getFederatedSubjectFromClaims(AuthenticationContext context, String otherDialect) throws FrameworkException {
    String value;
    boolean useLocalClaimDialect = context.getExternalIdP().useDefaultLocalIdpDialect();
    String userIdClaimURI = context.getExternalIdP().getUserIdClaimUri();
    Map<ClaimMapping, String> claimMappings = context.getSubject().getUserAttributes();
    if (useLocalClaimDialect) {
        Map<String, String> extAttributesValueMap = FrameworkUtils.getClaimMappings(claimMappings, false);
        Map<String, String> mappedAttrs = null;
        try {
            mappedAttrs = ClaimMetadataHandler.getInstance().getMappingsMapFromOtherDialectToCarbon(otherDialect, extAttributesValueMap.keySet(), context.getTenantDomain(), true);
        } catch (ClaimMetadataException e) {
            throw new FrameworkException("Error while loading claim mappings.", e);
        }
        String spUserIdClaimURI = mappedAttrs.get(userIdClaimURI);
        value = extAttributesValueMap.get(spUserIdClaimURI);
    } else {
        ClaimMapping claimMapping = new ClaimMapping();
        Claim claim = new Claim();
        claim.setClaimUri(userIdClaimURI);
        claimMapping.setRemoteClaim(claim);
        claimMapping.setLocalClaim(claim);
        value = claimMappings.get(claimMapping);
    }
    return value;
}
Also used : ClaimMapping(org.wso2.carbon.identity.application.common.model.ClaimMapping) ClaimMetadataException(org.wso2.carbon.identity.claim.metadata.mgt.exception.ClaimMetadataException) FrameworkException(org.wso2.carbon.identity.application.authentication.framework.exception.FrameworkException) Claim(org.wso2.carbon.identity.application.common.model.Claim)

Example 25 with Claim

use of org.wso2.carbon.identity.application.common.model.xsd.Claim in project carbon-identity-framework by wso2.

the class DefaultAttributeFinder method getAttributeValues.

/*
     * (non-Javadoc)
	 * 
	 * @see
	 * org.wso2.carbon.identity.entitlement.pip.PIPAttributeFinder#getAttributeValues(java.lang.
	 * String, java.lang.String, java.lang.String)
	 */
public Set<String> getAttributeValues(String subjectId, String resourceId, String actionId, String environmentId, String attributeId, String issuer) throws Exception {
    Set<String> values = new HashSet<String>();
    if (log.isDebugEnabled()) {
        log.debug("Retrieving attribute values of subjectId \'" + subjectId + "\'with attributeId \'" + attributeId + "\'");
    }
    if (StringUtils.isEmpty(subjectId)) {
        if (log.isDebugEnabled()) {
            log.debug("subjectId value is null or empty. Returning empty attribute set");
        }
        return values;
    }
    subjectId = MultitenantUtils.getTenantAwareUsername(subjectId);
    if (UserCoreConstants.ClaimTypeURIs.ROLE.equals(attributeId)) {
        if (log.isDebugEnabled()) {
            log.debug("Looking for roles via DefaultAttributeFinder");
        }
        String[] roles = CarbonContext.getThreadLocalCarbonContext().getUserRealm().getUserStoreManager().getRoleListOfUser(subjectId);
        if (roles != null && roles.length > 0) {
            for (String role : roles) {
                if (log.isDebugEnabled()) {
                    log.debug(String.format("User %1$s belongs to the Role %2$s", subjectId, role));
                }
                values.add(role);
            }
        }
    } else {
        String claimValue = null;
        try {
            claimValue = CarbonContext.getThreadLocalCarbonContext().getUserRealm().getUserStoreManager().getUserClaimValue(subjectId, attributeId, null);
            if (log.isDebugEnabled()) {
                log.debug("Claim \'" + claimValue + "\' retrieved for attributeId \'" + attributeId + "\' " + "for subjectId \'" + subjectId + "\'");
            }
        } catch (UserStoreException e) {
            if (e.getMessage().startsWith(IdentityCoreConstants.USER_NOT_FOUND)) {
                if (log.isDebugEnabled()) {
                    log.debug("User: " + subjectId + " not found in user store");
                }
            } else {
                throw e;
            }
        }
        if (claimValue == null && log.isDebugEnabled()) {
            log.debug(String.format("Request attribute %1$s not found", attributeId));
        }
        // Fix for multiple claim values
        if (claimValue != null) {
            String claimSeparator = CarbonContext.getThreadLocalCarbonContext().getUserRealm().getRealmConfiguration().getUserStoreProperty(IdentityCoreConstants.MULTI_ATTRIBUTE_SEPARATOR);
            if (StringUtils.isBlank(claimSeparator)) {
                claimSeparator = IdentityCoreConstants.MULTI_ATTRIBUTE_SEPARATOR_DEFAULT;
            }
            if (claimValue.contains(claimSeparator)) {
                StringTokenizer st = new StringTokenizer(claimValue, claimSeparator);
                while (st.hasMoreElements()) {
                    String attributeValue = st.nextElement().toString();
                    if (StringUtils.isNotBlank(attributeValue)) {
                        values.add(attributeValue);
                    }
                }
            } else {
                values.add(claimValue);
            }
        }
    }
    return values;
}
Also used : StringTokenizer(java.util.StringTokenizer) UserStoreException(org.wso2.carbon.user.api.UserStoreException) HashSet(java.util.HashSet)

Aggregations

HashMap (java.util.HashMap)112 ArrayList (java.util.ArrayList)89 ClaimMapping (org.wso2.carbon.identity.application.common.model.ClaimMapping)66 UserStoreException (org.wso2.carbon.user.api.UserStoreException)65 Test (org.testng.annotations.Test)63 ClaimMetadataException (org.wso2.carbon.identity.claim.metadata.mgt.exception.ClaimMetadataException)55 Map (java.util.Map)49 PreparedStatement (java.sql.PreparedStatement)48 SQLException (java.sql.SQLException)43 LocalClaim (org.wso2.carbon.identity.claim.metadata.mgt.model.LocalClaim)34 RealmService (org.wso2.carbon.user.core.service.RealmService)30 UserRealm (org.wso2.carbon.user.core.UserRealm)29 Claim (org.wso2.carbon.user.api.Claim)28 UserStoreException (org.wso2.carbon.user.core.UserStoreException)28 UserStoreManager (org.wso2.carbon.user.core.UserStoreManager)28 ResultSet (java.sql.ResultSet)27 Connection (java.sql.Connection)25 ClaimConfig (org.wso2.carbon.identity.application.common.model.ClaimConfig)25 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)24 Claim (org.wso2.carbon.identity.application.common.model.Claim)24