Search in sources :

Example 26 with Claim

use of org.wso2.carbon.user.core.claim.Claim in project carbon-apimgt by wso2.

the class APIUtil method getClaims.

/**
 * Returns the user claims for the given user.
 *
 * @param endUserName name of the user whose claims needs to be returned
 * @param tenantId    tenant id of the user
 * @param dialectURI  claim dialect URI
 * @return claims map
 * @throws APIManagementException
 */
public static SortedMap<String, String> getClaims(String endUserName, int tenantId, String dialectURI) throws APIManagementException {
    SortedMap<String, String> claimValues;
    try {
        ClaimManager claimManager = ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantId).getClaimManager();
        ClaimMapping[] claims = claimManager.getAllClaimMappings(dialectURI);
        String[] claimURIs = claimMappingtoClaimURIString(claims);
        UserStoreManager userStoreManager = ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantId).getUserStoreManager();
        String tenantAwareUserName = MultitenantUtils.getTenantAwareUsername(endUserName);
        claimValues = new TreeMap(userStoreManager.getUserClaimValues(tenantAwareUserName, claimURIs, null));
        return claimValues;
    } catch (UserStoreException e) {
        throw new APIManagementException("Error while retrieving user claim values from user store", e);
    }
}
Also used : ClaimManager(org.wso2.carbon.user.api.ClaimManager) ClaimMapping(org.wso2.carbon.user.api.ClaimMapping) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) UserStoreManager(org.wso2.carbon.user.api.UserStoreManager) AbstractUserStoreManager(org.wso2.carbon.user.core.common.AbstractUserStoreManager) TreeMap(java.util.TreeMap)

Example 27 with Claim

use of org.wso2.carbon.user.core.claim.Claim in project carbon-apimgt by wso2.

the class APIUtil method getClaimDisplayName.

/**
 * Returns the display name of the given claim URI.
 *
 * @param claimURI
 * @param subscriber
 * @return display name of the claim
 * @throws APIManagementException
 */
public static String getClaimDisplayName(String claimURI, String subscriber) throws APIManagementException {
    String tenantDomain = MultitenantUtils.getTenantDomain(subscriber);
    int tenantId;
    String displayName;
    try {
        tenantId = getTenantId(tenantDomain);
        ClaimManager claimManager = ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantId).getClaimManager();
        displayName = claimManager.getClaim(claimURI).getDisplayTag();
    } catch (UserStoreException e) {
        throw new APIManagementException("Error while retrieving claim values from user store", e);
    }
    return displayName;
}
Also used : ClaimManager(org.wso2.carbon.user.api.ClaimManager) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) Endpoint(org.wso2.carbon.governance.api.endpoints.dataobjects.Endpoint)

Example 28 with Claim

use of org.wso2.carbon.user.core.claim.Claim in project identity-outbound-auth-sms-otp by wso2-extensions.

the class SMSOTPAuthenticator method checkWithBackUpCodes.

/**
 * If user forgets the mobile, then user can use the back up codes to authenticate the user.
 *
 * @param context           the AuthenticationContext
 * @param userToken         the userToken
 * @param authenticatedUser the name of authenticatedUser
 * @throws AuthenticationFailedException
 */
private void checkWithBackUpCodes(AuthenticationContext context, String userToken, AuthenticatedUser authenticatedUser) throws AuthenticationFailedException {
    String savedOTPString = null;
    String username = context.getProperty(SMSOTPConstants.USER_NAME).toString();
    String tenantAwareUsername = MultitenantUtils.getTenantAwareUsername(username);
    UserRealm userRealm = getUserRealm(username);
    try {
        if (userRealm != null) {
            savedOTPString = userRealm.getUserStoreManager().getUserClaimValue(tenantAwareUsername, SMSOTPConstants.SAVED_OTP_LIST, null);
        }
        if (StringUtils.isEmpty(savedOTPString)) {
            if (log.isDebugEnabled()) {
                log.debug("The claim " + SMSOTPConstants.SAVED_OTP_LIST + " does not contain any values");
            }
            throw new AuthenticationFailedException("The claim " + SMSOTPConstants.SAVED_OTP_LIST + " does not contain any values");
        } else if (savedOTPString.contains(userToken)) {
            if (log.isDebugEnabled()) {
                log.debug("Found saved backup SMS OTP for user :" + authenticatedUser);
            }
            context.setSubject(authenticatedUser);
            savedOTPString = savedOTPString.replaceAll(userToken, "").replaceAll(",,", ",");
            userRealm.getUserStoreManager().setUserClaimValue(tenantAwareUsername, SMSOTPConstants.SAVED_OTP_LIST, savedOTPString, null);
        } else {
            if (log.isDebugEnabled()) {
                log.debug("User entered OTP :" + userToken + " does not match with any of the saved backup codes");
            }
            throw new AuthenticationFailedException("Verification Error due to Code " + userToken + " mismatch.");
        }
    } catch (UserStoreException e) {
        throw new AuthenticationFailedException("Cannot find the user claim for OTP list for user : " + authenticatedUser, e);
    }
}
Also used : UserRealm(org.wso2.carbon.user.api.UserRealm) AuthenticationFailedException(org.wso2.carbon.identity.application.authentication.framework.exception.AuthenticationFailedException) UserStoreException(org.wso2.carbon.user.api.UserStoreException)

Aggregations

UserRealm (org.wso2.carbon.user.core.UserRealm)5 UserStoreManager (org.wso2.carbon.user.core.UserStoreManager)5 ArrayList (java.util.ArrayList)4 XSString (org.opensaml.core.xml.schema.XSString)4 RealmService (org.wso2.carbon.user.core.service.RealmService)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Assertion (org.opensaml.saml.saml2.core.Assertion)3 Response (org.opensaml.saml.saml2.core.Response)3 Subject (org.opensaml.saml.saml2.core.Subject)3 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)3 AuthenticatorsConfiguration (org.wso2.carbon.core.security.AuthenticatorsConfiguration)3 UserStoreException (org.wso2.carbon.user.api.UserStoreException)3 UserStoreException (org.wso2.carbon.user.core.UserStoreException)3 Claim (org.wso2.carbon.user.core.claim.Claim)3 RemoteException (java.rmi.RemoteException)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2