Java Code Examples for org.wso2.carbon.user.core.UserCoreConstants#DOMAIN_SEPARATOR

The following examples show how to use org.wso2.carbon.user.core.UserCoreConstants#DOMAIN_SEPARATOR . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SelfSignUpUtil.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * get the full role name list (ex: internal/subscriber)
 * 
 * @param config - A UserRegistrationConfigDTO instance
 * @return - A list object containing role names
 */
public static List<String> getRoleNames(UserRegistrationConfigDTO config) {

	ArrayList<String> roleNamesArr = new ArrayList<String>();
	Map<String, Boolean> roles = config.getRoles();
	for (Map.Entry<String, Boolean> entry : roles.entrySet()) {
		String roleName;
		if (entry.getValue()) {
			// external role
			roleName =
					config.getSignUpDomain().toUpperCase() +
					UserCoreConstants.DOMAIN_SEPARATOR + entry.getKey();
		} else {
			// internal role
			roleName =
					UserCoreConstants.INTERNAL_DOMAIN + UserCoreConstants.DOMAIN_SEPARATOR +
					entry.getKey();
		}
		roleNamesArr.add(roleName);
	}
	return roleNamesArr;

}
 
Example 2
Source File: ApplicationManagementAdminService.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
/**
 * Method to retrieve all the application roles of a user.
 *
 * @param username User name.
 * @return Application role list.
 * @throws IdentityApplicationManagementException Error in retrieving roles of a user.
 */
private List<String> getApplicationRolesOfUser(String username) throws IdentityApplicationManagementException {

    try {
        String[] userRoles = CarbonContext.getThreadLocalCarbonContext().getUserRealm().
                getUserStoreManager().getRoleListOfUser(username);
        List<String> applicationRoles = new ArrayList<>();
        if (userRoles != null) {
            String applicationRoleDomain =
                    ApplicationConstants.APPLICATION_DOMAIN + UserCoreConstants.DOMAIN_SEPARATOR;
            for (String role : userRoles) {
                if (role.startsWith(applicationRoleDomain)) {
                    applicationRoles.add(role);
                }
            }
        }
        return applicationRoles;
    } catch (UserStoreException e) {
        throw new IdentityApplicationManagementException("Error while retrieving application roles for user: " +
                username, e);
    }
}
 
Example 3
Source File: IdentityUtil.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
/**
 * Appends domain name to the user/role name
 *
 * @param name       user/role name
 * @param domainName domain name
 * @return application name with domain name
 */
public static String addDomainToName(String name, String domainName) {

    if (domainName != null && name != null && !name.contains(UserCoreConstants.DOMAIN_SEPARATOR)) {
        if (!UserCoreConstants.PRIMARY_DEFAULT_DOMAIN_NAME.equalsIgnoreCase(domainName)) {
            if (UserCoreConstants.INTERNAL_DOMAIN.equalsIgnoreCase(domainName) ||
                    WORKFLOW_DOMAIN.equalsIgnoreCase(domainName) || APPLICATION_DOMAIN.equalsIgnoreCase(domainName)) {
                name = domainName.substring(0, 1).toUpperCase() + domainName.substring(1).toLowerCase() +
                        UserCoreConstants.DOMAIN_SEPARATOR + name;
            } else {
                name = domainName.toUpperCase() + UserCoreConstants.DOMAIN_SEPARATOR + name;
            }
        }
    }
    return name;
}
 
Example 4
Source File: UserStoreCountService.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
/**
 * Get the count of roles having a matching role name for the filter
 *
 * @param filter the filter for the role name. Use '*' to have all.
 * @return the number of roles matching the filter by each domain
 */
public PairDTO[] countRoles(String filter) throws UserStoreCounterException {

    Set<String> userStoreDomains = UserStoreCountUtils.getCountEnabledUserStores();
    // Add 2 more for the counts of Internal, Application domains.
    PairDTO[] roleCounts = new PairDTO[userStoreDomains.size() + 2];
    int i = 0;

    for (String userStoreDomain : userStoreDomains) {
        long count = -1L;
        String filterWithDomain = getFilterWithDomain(userStoreDomain, filter);
        count = getRoleCount(filterWithDomain);
        roleCounts[i] = new PairDTO(userStoreDomain, Long.toString(count));
        i++;
    }
    String internalDomainFilter = UserCoreConstants.INTERNAL_DOMAIN + UserCoreConstants.DOMAIN_SEPARATOR + filter;
    String applicationDomainFilter = InternalStoreCountConstants.APPLICATION_DOMAIN +
            UserCoreConstants.DOMAIN_SEPARATOR + filter;
    roleCounts[i] = new PairDTO(UserCoreConstants.INTERNAL_DOMAIN, String.valueOf(
            getRoleCount(internalDomainFilter)));
    roleCounts[++i] = new PairDTO(InternalStoreCountConstants.APPLICATION_DOMAIN, String.valueOf(
            getRoleCount(applicationDomainFilter)));

    return roleCounts;
}
 
Example 5
Source File: AbstractApplicationAuthenticatorTest.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
@DataProvider(name = "usernameProvider")
public Object[][] getUsernames() {

    String userStoreDomainAppendedName = USER_STORE_NAME + UserCoreConstants.DOMAIN_SEPARATOR + USER_NAME;

    return new Object[][]{
            {
                    // username already has a domain appended
                    userStoreDomainAppendedName, "WSO2.COM", userStoreDomainAppendedName
            },
            {
                    // setting domain from threadlocal
                    USER_NAME, USER_STORE_NAME, userStoreDomainAppendedName
            },
            {
                    // username doesn't have domain, thread local domain is empty too
                    USER_NAME, null, USER_NAME
            },
            {
                    // username doesn't have domain, thread local domain is empty too
                    USER_NAME, "", USER_NAME
            },

    };
}
 
Example 6
Source File: SelfSignUpUtil.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * modify user name with user storeage information. 
 * @param username - The user name
 * @param signupConfig - The sign up configuration
 * @return - The modified user name
 */
public static String getDomainSpecificUserName(String username, UserRegistrationConfigDTO signupConfig) {
	String modifiedUsername = null;	
	// set tenant specific sign up user storage
	if (signupConfig != null && !signupConfig.getSignUpDomain().equals("")) {
		
		int index = username.indexOf(UserCoreConstants.DOMAIN_SEPARATOR);
		/*
		 * if there is a different domain provided by the user other than one 
		 * given in the configuration, add the correct signup domain. Here signup
		 * domain refers to the user storage
		 */
	
		if (index > 0) {
			modifiedUsername =
					signupConfig.getSignUpDomain().toUpperCase() +
					UserCoreConstants.DOMAIN_SEPARATOR +
					username.substring(index + 1);
		} else {
			modifiedUsername =
					signupConfig.getSignUpDomain().toUpperCase() +
					UserCoreConstants.DOMAIN_SEPARATOR + username;
		}
	}
	
	return modifiedUsername;
}
 
Example 7
Source File: ApplicationManagementAdminService.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
/**
 * Sanitize the filter to fetch application roles.
 *
 * @param filter Application name filter.
 * @return Sanitized filter string.
 */
private String getSanitizedFilter(String filter) {

    if (StringUtils.isNotBlank(filter)) {
        filter = filter.replace("*", ".*");
        filter = ApplicationConstants.APPLICATION_DOMAIN + UserCoreConstants.DOMAIN_SEPARATOR + filter;
    } else {
        filter = ".*";
    }

    return filter;
}
 
Example 8
Source File: IdentityUtil.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * Appends domain name to the user/role name
 *
 * @param name       user/role name
 * @param domainName domain name
 * @return application name with domain name
 */
public static String addDomainToName(String name, String domainName) {
    if (domainName != null && name != null && name.indexOf(UserCoreConstants.DOMAIN_SEPARATOR) < 0) {
        if (!UserCoreConstants.PRIMARY_DEFAULT_DOMAIN_NAME.equalsIgnoreCase(domainName)) {
            if (UserCoreConstants.INTERNAL_DOMAIN.equalsIgnoreCase(domainName) ||
                    "Workflow".equalsIgnoreCase(domainName) || "Application".equalsIgnoreCase(domainName)) {
                name = domainName + UserCoreConstants.DOMAIN_SEPARATOR + name;
            } else {
                name = domainName.toUpperCase() + UserCoreConstants.DOMAIN_SEPARATOR + name;
            }
        }
    }
    return name;
}
 
Example 9
Source File: UserProfileAdmin.java    From carbon-identity-framework with Apache License 2.0 4 votes vote down vote up
/**
 * Retrieve a claim of the authorized user.
 *
 * @param claimUri    Claim URI in wso2 dialect.
 * @param profileName User profile name.
 * @return Claim value.
 * @throws UserProfileException
 */
public String getUserClaim(String claimUri, String profileName) throws UserProfileException {

    if (StringUtils.isBlank(claimUri)) {
        throw new UserProfileException("Invalid input parameter. Claim URI cannot be null.");
    }
    if (StringUtils.isBlank(profileName)) {
        throw new UserProfileException("Invalid input parameter. Profile name cannot be null.");
    }
    String loggedInUsername = CarbonContext.getThreadLocalCarbonContext().getUsername();
    if (StringUtils.isBlank(loggedInUsername)) {
        throw new UserProfileException("Could not find a logged in user in the current carbon context.");
    }

    String claimValue = null;
    try {
        UserStoreManager userStoreManager = getUserRealm().getUserStoreManager();
        int index = loggedInUsername.indexOf(UserCoreConstants.DOMAIN_SEPARATOR);

        if (index < 0) {
            if (log.isDebugEnabled()) {
                log.debug("Logged in username : '" + loggedInUsername + "' does not contain domain name.");
            }
            /* if domain is not provided, this can be the scenario where user from a secondary user store
            logs in without domain name and tries to view his own profile. */
            MessageContext messageContext = MessageContext.getCurrentMessageContext();
            HttpServletRequest request = (HttpServletRequest) messageContext
                    .getProperty(TRANSPORT_HTTP_SERVLET_REQUEST);
            String domainName = (String) request.getSession().getAttribute(LOGGED_IN_DOMAIN);
            if (StringUtils.isNotBlank(domainName)) {
                loggedInUsername = domainName + UserCoreConstants.DOMAIN_SEPARATOR + loggedInUsername;
            }
        }
        index = loggedInUsername.indexOf(UserCoreConstants.DOMAIN_SEPARATOR);
        UserStoreManager secUserStoreManager = null;

        // Check whether we have a secondary UserStoreManager setup.
        if (index > 0) {
            // Using the short-circuit. User name comes with the domain name.
            String domain = loggedInUsername.substring(0, index);
            if (log.isDebugEnabled()) {
                log.debug("Domain name found in the logged in username. Domain name: " + domain);
            }
            if (userStoreManager instanceof AbstractUserStoreManager) {
                secUserStoreManager = ((AbstractUserStoreManager) userStoreManager)
                        .getSecondaryUserStoreManager(domain);
            }
        }
        Map<String, String> claimValues;
        if (secUserStoreManager != null) {
            claimValues = secUserStoreManager.getUserClaimValues(loggedInUsername, new String[]{claimUri},
                    profileName);
        } else {
            claimValues = userStoreManager.getUserClaimValues(loggedInUsername, new String[]{claimUri},
                    profileName);
        }
        if (claimValues != null) {
            claimValue = claimValues.get(claimUri);
        }
    } catch (UserStoreException e) {
        String message = String.format("An error occurred while getting the user claim '%s' in '%s' profile of " +
                "the user '%s'", claimUri, profileName, loggedInUsername);
        log.error(message, e);
        throw new UserProfileException(message, e);
    }
    return claimValue;
}
 
Example 10
Source File: ExtendedJWTBearerGrantHandler.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
/**
 * Check the retireved roles against the role mappings in the IDP and return the updated roles
 * @param identityProvider used to retrieve the role mappings
 * @param currentRoleClaimValue current roles received through the token
 * @return updated roles
 */
private String getUpdatedRoleClaimValue(IdentityProvider identityProvider, String currentRoleClaimValue) {

    if (StringUtils.equalsIgnoreCase(IdentityApplicationConstants.RESIDENT_IDP_RESERVED_NAME,
            identityProvider.getIdentityProviderName())) {
        return currentRoleClaimValue;
    }
    currentRoleClaimValue = currentRoleClaimValue.replace("\\/", "/").
            replace("[", "").replace("]", "").replace("\"", "");

    PermissionsAndRoleConfig permissionAndRoleConfig = identityProvider.getPermissionAndRoleConfig();
    if (permissionAndRoleConfig != null && ArrayUtils.isNotEmpty(permissionAndRoleConfig.getRoleMappings())) {
        String[] receivedRoles = currentRoleClaimValue.split(FrameworkUtils.getMultiAttributeSeparator());
        List<String> updatedRoleClaimValues = new ArrayList<>();
        String updatedLocalRole;
        loop:
        for (String receivedRole : receivedRoles) {
            for (RoleMapping roleMapping : permissionAndRoleConfig.getRoleMappings()) {
                if (roleMapping.getRemoteRole().equals(receivedRole)) {
                    updatedLocalRole = StringUtils.isEmpty(roleMapping.getLocalRole().getUserStoreId())
                            ? roleMapping.getLocalRole().getLocalRoleName()
                            : roleMapping.getLocalRole().getUserStoreId() + UserCoreConstants.DOMAIN_SEPARATOR
                                    + roleMapping.getLocalRole().getLocalRoleName();
                    updatedRoleClaimValues.add(updatedLocalRole);
                    continue loop;
                }
            }
            if (!OAuthServerConfiguration.getInstance().isReturnOnlyMappedLocalRoles()) {
                updatedRoleClaimValues.add(receivedRole);
            }
        }
        if (!updatedRoleClaimValues.isEmpty()) {
            return StringUtils.join(updatedRoleClaimValues, FrameworkUtils.getMultiAttributeSeparator());
        }
        return null;
    }
    if (!OAuthServerConfiguration.getInstance().isReturnOnlyMappedLocalRoles()) {
        return currentRoleClaimValue;
    }
    return null;
}
 
Example 11
Source File: UserRegistrationService.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
private void addUser(String userName, String password, Map<String, String> claimList,
                     String profileName, UserRealm realm) throws IdentityException {
    UserStoreManager admin = null;
    Permission permission = null;
    try {
        // get config from tenant registry
        TenantRegistrationConfig tenantConfig = getTenantSignUpConfig(realm.getUserStoreManager().getTenantId());
        // set tenant config specific sign up domain
        if (tenantConfig != null && !"".equals(tenantConfig.getSignUpDomain())) {
            int index = userName.indexOf(UserCoreConstants.DOMAIN_SEPARATOR);
            if (index > 0) {
                userName = tenantConfig.getSignUpDomain().toUpperCase() + UserCoreConstants.DOMAIN_SEPARATOR
                        + userName.substring(index + 1);
            } else {
                userName = tenantConfig.getSignUpDomain().toUpperCase() + UserCoreConstants.DOMAIN_SEPARATOR
                        + userName;
            }
        }

        // add user to the relevant user store

        admin = realm.getUserStoreManager();
        if (!isUserNameWithAllowedDomainName(userName, realm)) {
            throw IdentityException.error("Domain does not permit self registration");
        }
        // add user
        admin.addUser(userName, password, null, claimList, profileName);

        // after adding the user, assign specif roles
        List<String> roleNamesArr = getRoleName(userName, tenantConfig);
        if (claimList.get(SelfRegistrationConstants.SIGN_UP_ROLE_CLAIM_URI) != null) {
            // check is a user role is specified as a claim by the client, if so add it to the roles list
            if (tenantConfig != null) {
                roleNamesArr.add(tenantConfig.getSignUpDomain().toUpperCase()
                        + UserCoreConstants.DOMAIN_SEPARATOR
                        + claimList.get(SelfRegistrationConstants.SIGN_UP_ROLE_CLAIM_URI));
            } else {
                roleNamesArr.add(UserCoreConstants.INTERNAL_DOMAIN
                        + UserCoreConstants.DOMAIN_SEPARATOR
                        + claimList.get(SelfRegistrationConstants.SIGN_UP_ROLE_CLAIM_URI));
            }
        }
        String[] identityRoleNames = roleNamesArr.toArray(new String[roleNamesArr.size()]);

        for (int i = 0; i < identityRoleNames.length; i++) {
            // if this is the first time a user signs up, needs to create role
            doAddUser(i,admin, identityRoleNames,userName,permission);
        }
    } catch (UserStoreException e) {
        throw IdentityException.error("Error occurred while adding user : " + userName + ". " + e.getMessage(), e);
    }
}
 
Example 12
Source File: ApplicationMgtUtil.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
private static String getAppRoleName(String applicationName) {
    return ApplicationConstants.APPLICATION_DOMAIN + UserCoreConstants.DOMAIN_SEPARATOR + applicationName;
}
 
Example 13
Source File: UserRegistrationService.java    From carbon-identity-framework with Apache License 2.0 4 votes vote down vote up
private void addUser(String userName, String password, Map<String, String> claimList,
                     String profileName, UserRealm realm) throws IdentityException {
    UserStoreManager admin = null;
    Permission permission = null;
    try {
        // get config from tenant registry
        TenantRegistrationConfig tenantConfig = getTenantSignUpConfig(realm.getUserStoreManager().getTenantId());
        // set tenant config specific sign up domain
        if (tenantConfig != null && !"".equals(tenantConfig.getSignUpDomain())) {
            int index = userName.indexOf(UserCoreConstants.DOMAIN_SEPARATOR);
            if (index > 0) {
                userName = tenantConfig.getSignUpDomain().toUpperCase() + UserCoreConstants.DOMAIN_SEPARATOR
                        + userName.substring(index + 1);
            } else {
                userName = tenantConfig.getSignUpDomain().toUpperCase() + UserCoreConstants.DOMAIN_SEPARATOR
                        + userName;
            }
        }

        // add user to the relevant user store

        admin = realm.getUserStoreManager();
        if (!isUserNameWithAllowedDomainName(userName, realm)) {
            throw IdentityException.error("Domain does not permit self registration");
        }
        // add user
        admin.addUser(userName, password, null, claimList, profileName);

        // after adding the user, assign specif roles
        List<String> roleNamesArr = getRoleName(userName, tenantConfig);
        if (claimList.get(SelfRegistrationConstants.SIGN_UP_ROLE_CLAIM_URI) != null) {
            // check is a user role is specified as a claim by the client, if so add it to the roles list
            if (tenantConfig != null) {
                roleNamesArr.add(tenantConfig.getSignUpDomain().toUpperCase()
                        + UserCoreConstants.DOMAIN_SEPARATOR
                        + claimList.get(SelfRegistrationConstants.SIGN_UP_ROLE_CLAIM_URI));
            } else {
                roleNamesArr.add(UserCoreConstants.INTERNAL_DOMAIN
                        + UserCoreConstants.DOMAIN_SEPARATOR
                        + claimList.get(SelfRegistrationConstants.SIGN_UP_ROLE_CLAIM_URI));
            }
        }
        String[] identityRoleNames = roleNamesArr.toArray(new String[roleNamesArr.size()]);

        for (int i = 0; i < identityRoleNames.length; i++) {
            // if this is the first time a user signs up, needs to create role
            doAddUser(i, admin, identityRoleNames, userName, permission);
        }
    } catch (UserStoreException e) {
        throw IdentityException.error("Error occurred while adding user : " + userName + ". " + e.getMessage(), e);
    }
}
 
Example 14
Source File: WorkflowManagementUtil.java    From carbon-identity-framework with Apache License 2.0 2 votes vote down vote up
/**
 * Generate owner role name for workflow.
 *
 * @param workflowName Workflow name
 * @return
 */
public static String createWorkflowRoleName(String workflowName) {
    return UserCoreConstants.INTERNAL_DOMAIN + UserCoreConstants.DOMAIN_SEPARATOR + workflowName;
}
 
Example 15
Source File: WorkflowManagementUtil.java    From carbon-identity with Apache License 2.0 2 votes vote down vote up
/**
 * Generate owner role name for workflow.
 *
 * @param workflowName Workflow name
 * @return
 */
public static String createWorkflowRoleName(String workflowName) {
    return UserCoreConstants.INTERNAL_DOMAIN + UserCoreConstants.DOMAIN_SEPARATOR + workflowName;
}
 
Example 16
Source File: UserStoreCountService.java    From carbon-identity-framework with Apache License 2.0 2 votes vote down vote up
private String getFilterWithDomain(String domain, String filter) {

        return domain + UserCoreConstants.DOMAIN_SEPARATOR + filter;
    }
 
Example 17
Source File: ApplicationManagementServiceImpl.java    From carbon-identity-framework with Apache License 2.0 2 votes vote down vote up
private static String getAppRoleName(String applicationName) {

        return ApplicationConstants.APPLICATION_DOMAIN + UserCoreConstants.DOMAIN_SEPARATOR + applicationName;
    }
 
Example 18
Source File: ApplicationMgtUtil.java    From carbon-identity-framework with Apache License 2.0 2 votes vote down vote up
private static String getAppRoleName(String applicationName) {

        return ApplicationConstants.APPLICATION_DOMAIN + UserCoreConstants.DOMAIN_SEPARATOR + applicationName;
    }