Search in sources :

Example 6 with RealmService

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

the class SMSOTPAuthenticator method getUserRealm.

/**
 * Get the user realm of the logged in user.
 *
 * @param username the Username
 * @return the userRealm
 * @throws AuthenticationFailedException
 */
private UserRealm getUserRealm(String username) throws AuthenticationFailedException {
    UserRealm userRealm = null;
    try {
        if (StringUtils.isNotEmpty(username)) {
            String tenantDomain = MultitenantUtils.getTenantDomain(username);
            int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
            RealmService realmService = IdentityTenantUtil.getRealmService();
            userRealm = realmService.getTenantUserRealm(tenantId);
        }
    } catch (UserStoreException e) {
        throw new AuthenticationFailedException("Cannot find the user realm. ", e);
    }
    return userRealm;
}
Also used : UserRealm(org.wso2.carbon.user.api.UserRealm) AuthenticationFailedException(org.wso2.carbon.identity.application.authentication.framework.exception.AuthenticationFailedException) RealmService(org.wso2.carbon.user.core.service.RealmService) UserStoreException(org.wso2.carbon.user.api.UserStoreException)

Example 7 with RealmService

use of org.wso2.carbon.user.core.service.RealmService in project core-util by WSO2Telco.

the class UserAuthorizationValidator method isAuthorizedRole.

public boolean isAuthorizedRole(String userName, Set<String> allowedRolesSet) {
    PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
    RealmService realmService = (RealmService) carbonContext.getOSGiService(RealmService.class, null);
    try {
        RealmConfiguration realmConfiguration = new RealmConfiguration();
        String[] currentUserRoles = realmService.getUserRealm(realmConfiguration).getUserStoreManager().getRoleListOfUser(userName);
        List<String> currentUserRolesList = Arrays.asList(currentUserRoles);
        Iterator<String> iterator = allowedRolesSet.iterator();
        while (iterator.hasNext()) {
            String allowedRole = iterator.next();
            if (currentUserRolesList.contains(allowedRole)) {
                return true;
            }
        }
    } catch (org.wso2.carbon.user.api.UserStoreException e) {
        log.error("authorization failed for user : " + userName, e);
        return false;
    }
    log.error("authorization failed for user : " + userName);
    return false;
}
Also used : RealmConfiguration(org.wso2.carbon.user.api.RealmConfiguration) RealmService(org.wso2.carbon.user.core.service.RealmService) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext)

Example 8 with RealmService

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

the class SMSOTPUtils method getUserRealm.

/**
 * Get the user realm of the logged in user.
 *
 * @param tenantDomain the tenantDomain
 * @return th user realm
 * @throws AuthenticationFailedException
 */
public static UserRealm getUserRealm(String tenantDomain) throws AuthenticationFailedException {
    UserRealm userRealm;
    try {
        int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
        RealmService realmService = IdentityTenantUtil.getRealmService();
        userRealm = realmService.getTenantUserRealm(tenantId);
    } catch (Exception e) {
        throw new AuthenticationFailedException("Cannot find the user realm for the tenant domain " + tenantDomain, e);
    }
    return userRealm;
}
Also used : UserRealm(org.wso2.carbon.user.api.UserRealm) AuthenticationFailedException(org.wso2.carbon.identity.application.authentication.framework.exception.AuthenticationFailedException) RealmService(org.wso2.carbon.user.core.service.RealmService) UserStoreException(org.wso2.carbon.user.api.UserStoreException) AuthenticationFailedException(org.wso2.carbon.identity.application.authentication.framework.exception.AuthenticationFailedException) SMSOTPException(org.wso2.carbon.identity.authenticator.smsotp.exception.SMSOTPException)

Example 9 with RealmService

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

the class SMSOTPUtils method isSMSOTPDisableForLocalUser.

/**
 * Check whether SMSOTP is disable by user.
 *
 * @param username the Username
 * @param context  the AuthenticationContext
 * @return true or false
 * @throws SMSOTPException
 */
public static boolean isSMSOTPDisableForLocalUser(String username, AuthenticationContext context, String authenticatorName) throws SMSOTPException, AuthenticationFailedException {
    UserRealm userRealm;
    try {
        String tenantDomain = MultitenantUtils.getTenantDomain(username);
        int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
        RealmService realmService = IdentityTenantUtil.getRealmService();
        userRealm = realmService.getTenantUserRealm(tenantId);
        username = MultitenantUtils.getTenantAwareUsername(String.valueOf(username));
        boolean isEnablingControlledByUser = isSMSOTPEnableOrDisableByUser(context, authenticatorName);
        if (userRealm != null) {
            if (isEnablingControlledByUser) {
                Map<String, String> claimValues = userRealm.getUserStoreManager().getUserClaimValues(username, new String[] { SMSOTPConstants.USER_SMSOTP_DISABLED_CLAIM_URI }, null);
                return Boolean.parseBoolean(claimValues.get(SMSOTPConstants.USER_SMSOTP_DISABLED_CLAIM_URI));
            }
        } else {
            throw new SMSOTPException("Cannot find the user realm for the given tenant domain : " + CarbonContext.getThreadLocalCarbonContext().getTenantDomain());
        }
    } catch (UserStoreException e) {
        throw new SMSOTPException("Failed while trying to access userRealm of the user : " + username, 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) SMSOTPException(org.wso2.carbon.identity.authenticator.smsotp.exception.SMSOTPException)

Example 10 with RealmService

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

the class SMSOTPAuthenticatorTest method testCheckWithBackUpCodes.

@Test
public void testCheckWithBackUpCodes() throws Exception {
    mockStatic(IdentityTenantUtil.class);
    context.setProperty(SMSOTPConstants.USER_NAME, "admin");
    when(IdentityTenantUtil.getTenantId("carbon.super")).thenReturn(-1234);
    when(IdentityTenantUtil.getRealmService()).thenReturn(realmService);
    when(realmService.getTenantUserRealm(-1234)).thenReturn(userRealm);
    when(userRealm.getUserStoreManager()).thenReturn(userStoreManager);
    when((AuthenticatedUser) context.getProperty(SMSOTPConstants.AUTHENTICATED_USER)).thenReturn(AuthenticatedUser.createLocalAuthenticatedUserFromSubjectIdentifier("admin"));
    when(userRealm.getUserStoreManager().getUserClaimValue(MultitenantUtils.getTenantAwareUsername("admin"), SMSOTPConstants.SAVED_OTP_LIST, null)).thenReturn("12345,4568,1234,7896");
    AuthenticatedUser user = (AuthenticatedUser) context.getProperty(SMSOTPConstants.AUTHENTICATED_USER);
    Whitebox.invokeMethod(smsotpAuthenticator, "checkWithBackUpCodes", context, "1234", user);
}
Also used : AuthenticatedUser(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

RealmService (org.wso2.carbon.user.core.service.RealmService)9 UserStoreException (org.wso2.carbon.user.api.UserStoreException)6 PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)4 UserRealm (org.wso2.carbon.user.api.UserRealm)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 Test (org.testng.annotations.Test)2 BPMNAuthenticationException (org.wso2.carbon.bpmn.core.exception.BPMNAuthenticationException)2 AuthenticationFailedException (org.wso2.carbon.identity.application.authentication.framework.exception.AuthenticationFailedException)2 AuthenticatedUser (org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)2 SMSOTPException (org.wso2.carbon.identity.authenticator.smsotp.exception.SMSOTPException)2 RealmConfiguration (org.wso2.carbon.user.api.RealmConfiguration)2 TenantManager (org.wso2.carbon.user.core.tenant.TenantManager)2 IOException (java.io.IOException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 IdentityService (org.activiti.engine.IdentityService)1 OMElement (org.apache.axiom.om.OMElement)1 Header (org.apache.http.Header)1