Java Code Examples for org.wso2.carbon.identity.core.util.IdentityTenantUtil#getTenantId()

The following examples show how to use org.wso2.carbon.identity.core.util.IdentityTenantUtil#getTenantId() . 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: OpenIDProviderService.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * Returns RP DTO for the given OpenID and RP
 *
 * @param openID
 * @param rpUrl
 * @return openIDUserRPDTO
 * @throws IdentityProviderException
 */
public OpenIDUserRPDTO getOpenIDUserRPInfo(String openID, String rpUrl) throws IdentityProviderException {

    String userName = null;
    try {
        userName = OpenIDUtil.getUserName(openID);
    } catch (MalformedURLException e) {
        throw new IdentityProviderException("Failed to get username from OpenID " + openID, e);
    }
    String domainName = MultitenantUtils.getTenantDomain(userName);
    int tenantId = IdentityTenantUtil.getTenantId(domainName);

    OpenIDUserRPDO rpdo = null;
    OpenIDUserRPDAO dao = new OpenIDUserRPDAO();
    rpdo = dao.getOpenIDUserRP(userName, rpUrl, tenantId);
    if (rpdo == null) {
        return null;
    }
    return new OpenIDUserRPDTO(rpdo);
}
 
Example 2
Source File: IdentityProviderManager.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves Identity provider information about a given tenant by realm identifier
 *
 * @param realmId      Unique realm identifier of the Identity provider of whose information is
 *                     requested
 * @param tenantDomain Tenant domain whose information is requested
 * @throws IdentityProviderManagementException Error when getting Identity Provider
 *                                             information by IdP home realm identifier
 */
@Override
public IdentityProvider getIdPByRealmId(String realmId, String tenantDomain)
        throws IdentityProviderManagementException {

    int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
    if (StringUtils.isEmpty(realmId)) {
        String msg = "Invalid argument: Identity Provider Home Realm Identifier value is empty";
        throw new IdentityProviderManagementException(msg);
    }
    IdentityProvider identityProvider = dao.getIdPByRealmId(realmId, tenantId, tenantDomain);

    if (identityProvider == null) {
        identityProvider = new FileBasedIdPMgtDAO().getIdPByRealmId(realmId, tenantDomain);
    }

    return identityProvider;
}
 
Example 3
Source File: IdentityProviderNameResolverListener.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
public boolean doPreUpdateIdPByResourceId(String resourceId, IdentityProvider identityProvider, String
        tenantDomain) throws IdentityProviderManagementException {

    int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
    IdentityProvider idp = dao.getIdPByResourceId(resourceId, tenantId, tenantDomain);
    if (idp != null) {
        String oldIdPName = idp.getIdentityProviderName();

        // invoking the pre listeners
        Collection<IdentityProviderMgtListener> listeners = IdPManagementServiceComponent.getIdpMgtListeners();
        for (IdentityProviderMgtListener listener : listeners) {
            if (listener.isEnable() && !listener.doPreUpdateIdP(oldIdPName, identityProvider, tenantDomain)) {
                return false;
            }
        }
    }
    return true;
}
 
Example 4
Source File: IdentityProviderManager.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
/**
 * @param property      IDP authenticator property (E.g.: IdPEntityId)
 * @param value         Value associated with given Property
 * @param tenantDomain
 * @param authenticator
 * @return <code>IdentityProvider</code> Identity Provider information
 * @throws IdentityProviderManagementException Error when getting Identity Provider
 *                                             information by authenticator property value
 */
public IdentityProvider getIdPByAuthenticatorPropertyValue(String property, String value, String tenantDomain,
                                                           String authenticator, boolean ignoreFileBasedIdps)
        throws IdentityProviderManagementException {

    int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);

    if (StringUtils.isEmpty(property) || StringUtils.isEmpty(value) || StringUtils.isEmpty(authenticator)) {
        String msg = "Invalid argument: Authenticator property, property value or authenticator name is empty";
        throw new IdentityProviderManagementException(msg);
    }

    IdentityProvider identityProvider = dao.getIdPByAuthenticatorPropertyValue(
            null, property, value, authenticator, tenantId, tenantDomain);

    if (identityProvider == null && !ignoreFileBasedIdps) {
        identityProvider = new FileBasedIdPMgtDAO()
                .getIdPByAuthenticatorPropertyValue(property, value, tenantDomain, authenticator);
    }

    return identityProvider;
}
 
Example 5
Source File: ClaimMetadataManagementServiceImpl.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
@Override
public void removeClaimDialect(ClaimDialect claimDialect, String tenantDomain) throws ClaimMetadataException {

    if (claimDialect == null || StringUtils.isBlank(claimDialect.getClaimDialectURI())) {
        throw new ClaimMetadataClientException(ERROR_CODE_EMPTY_CLAIM_DIALECT.getCode(),
                "Claim dialect URI cannot be empty");
    }

    // TODO : validate claim dialect already exists?

    // TODO : validate tenant domain?
    int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);

    // Add listener

    this.claimDialectDAO.removeClaimDialect(claimDialect, tenantId);
    // When deleting a claim dialect the relevant external claim deletion is handled by the DB through
    // ON DELETE CASCADE. Here we are removing the relevant cache entry.
    externalClaimDAO.removeExternalClaimCache(claimDialect.getClaimDialectURI(), tenantId);
    // Add listener

}
 
Example 6
Source File: ClaimMetadataManagementServiceImpl.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
@Override
public void renameClaimDialect(ClaimDialect oldClaimDialect, ClaimDialect newClaimDialect, String tenantDomain)
        throws ClaimMetadataException {

    if (oldClaimDialect == null || StringUtils.isBlank(oldClaimDialect.getClaimDialectURI())
            || newClaimDialect == null || StringUtils.isBlank(newClaimDialect.getClaimDialectURI())) {
        throw new ClaimMetadataClientException(ERROR_CODE_EMPTY_CLAIM_DIALECT);
    }

    // TODO : Validate oldClaimDialectURI is valid????

    // TODO : validate tenant domain?
    int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);

    // Add listener

    this.claimDialectDAO.renameClaimDialect(oldClaimDialect, newClaimDialect, tenantId);
    externalClaimDAO.removeExternalClaimCache(oldClaimDialect.getClaimDialectURI(), tenantId);
    // Add listener

}
 
Example 7
Source File: JsClaims.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
/**
 * 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());
    String usernameWithDomain = UserCoreUtil.addDomainToName(authenticatedUser.getUserName(), authenticatedUser
        .getUserStoreDomain());
    RealmService realmService = FrameworkServiceDataHolder.getInstance().getRealmService();
    try {
        UserRealm userRealm = realmService.getTenantUserRealm(usersTenantId);
        Map<String, String> claimValues = userRealm.getUserStoreManager().getUserClaimValues(usernameWithDomain, 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);
    }
    return null;
}
 
Example 8
Source File: JsAuthenticatedUser.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
private String[] getLocalRoles() {

        if (idp == null || FrameworkConstants.LOCAL.equals(idp)) {
            RealmService realmService = FrameworkServiceDataHolder.getInstance().getRealmService();
            int usersTenantId = IdentityTenantUtil.getTenantId(getWrapped().getTenantDomain());

            try {
                String usernameWithDomain = UserCoreUtil.addDomainToName(getWrapped().getUserName(), getWrapped()
                    .getUserStoreDomain());
                UserRealm userRealm = realmService.getTenantUserRealm(usersTenantId);
                return userRealm.getUserStoreManager().getRoleListOfUser(usernameWithDomain);
            } catch (UserStoreException e) {
                LOG.error("Error when getting role list of user: " + getWrapped(), e);
            }
        }
        return ArrayUtils.EMPTY_STRING_ARRAY;
    }
 
Example 9
Source File: IdentityProviderManager.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves Identity provider information about a given tenant by realm identifier
 *
 * @param realmId      Unique realm identifier of the Identity provider of whose information is
 *                     requested
 * @param tenantDomain Tenant domain whose information is requested
 * @throws IdentityProviderManagementException Error when getting Identity Provider
 *                                                information by IdP home realm identifier
 */
public IdentityProvider getIdPByRealmId(String realmId, String tenantDomain)
        throws IdentityProviderManagementException {

    int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
    if (StringUtils.isEmpty(realmId)) {
        String msg = "Invalid argument: Identity Provider Home Realm Identifier value is empty";
        throw new IdentityProviderManagementException(msg);
    }
    IdentityProvider identityProvider = dao.getIdPByRealmId(realmId, tenantId, tenantDomain);

    if (identityProvider == null) {
        identityProvider = new FileBasedIdPMgtDAO().getIdPByRealmId(realmId, tenantDomain);
    }

    return identityProvider;
}
 
Example 10
Source File: IdentityProviderManager.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
private void deleteIDP(String resourceId, String idpName, String tenantDomain) throws
        IdentityProviderManagementException {

    int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
    for (MetadataConverter metadataConverter : IdpMgtServiceComponentHolder.getInstance().getMetadataConverters()) {
        if (metadataConverter.canDelete(tenantId, idpName)) {
            metadataConverter.deleteMetadataString(tenantId, idpName);
        }
    }

    dao.deleteIdPByResourceId(resourceId, tenantId, tenantDomain);
}
 
Example 11
Source File: UserSessionStore.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
/**
 * Method to get session Id list of a given user.
 *
 * @param user  user object
 * @param idpId id of the user's idp
 * @return the list of session ids
 * @throws UserSessionException if an error occurs when retrieving the session id list from the database
 */
public List<String> getSessionId(User user, int idpId) throws UserSessionException {

    List<String> sessionIdList = new ArrayList<>();
    int tenantId = IdentityTenantUtil.getTenantId(user.getTenantDomain());
    try (Connection connection = IdentityDatabaseUtil.getDBConnection(false)) {
        try (PreparedStatement preparedStatement = connection
                .prepareStatement(SQLQueries.SQL_GET_SESSIONS_BY_USER)) {
            preparedStatement.setString(1, user.getUserName());
            preparedStatement.setInt(2, tenantId);
            preparedStatement.setString(3, (user.getUserStoreDomain() == null) ? FEDERATED_USER_DOMAIN :
                    user.getUserStoreDomain().toUpperCase());
            preparedStatement.setInt(4, idpId);
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                while (resultSet.next()) {
                    sessionIdList.add(resultSet.getString(1));
                }
            }
        } catch (SQLException ex) {
            throw new UserSessionException("Error while retrieving session IDs of user: " +
                    user.getUserName() + ".", ex);
        }
    } catch (SQLException e) {
        throw new UserSessionException("Error while retrieving session IDs of user: " +
                user.getUserName() + ".", e);
    }
    return sessionIdList;
}
 
Example 12
Source File: AuthenticationContextCache.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * Add a cache entry.
 *
 * @param key   Key which cache entry is indexed.
 * @param entry Actual object where cache entry is placed.
 */
public void addToCache(AuthenticationContextCacheKey key, AuthenticationContextCacheEntry entry) {
    super.addToCache(key, entry);
    if (isTemporarySessionDataPersistEnabled) {
        int tenantId = MultitenantConstants.INVALID_TENANT_ID;
        String tenantDomain = entry.getContext().getTenantDomain();
        if (tenantDomain != null) {
            tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
        }
        SessionDataStore.getInstance().storeSessionData(key.getContextId(), AUTHENTICATION_CONTEXT_CACHE_NAME,
                entry, tenantId);
    }
}
 
Example 13
Source File: SessionContextCache.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
public void addToCache(SessionContextCacheKey key, SessionContextCacheEntry entry) {
    entry.setAccessedTime();
    super.addToCache(key, entry);
    Object authUser = entry.getContext().getProperty(FrameworkConstants.AUTHENTICATED_USER);
    if (authUser != null && authUser instanceof AuthenticatedUser) {
        String tenantDomain = ((AuthenticatedUser) authUser).getTenantDomain();
        int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
        SessionDataStore.getInstance()
                .storeSessionData(key.getContextId(), SESSION_CONTEXT_CACHE_NAME, entry, tenantId);
    } else {
        SessionDataStore.getInstance().storeSessionData(key.getContextId(), SESSION_CONTEXT_CACHE_NAME, entry);
    }
}
 
Example 14
Source File: ClaimMetadataManagementServiceImpl.java    From carbon-identity-framework with Apache License 2.0 4 votes vote down vote up
@Override
public void addClaimDialect(ClaimDialect claimDialect, String tenantDomain) throws ClaimMetadataException {

    if (claimDialect == null || StringUtils.isBlank(claimDialect.getClaimDialectURI())) {
        throw new ClaimMetadataClientException(ERROR_CODE_EMPTY_CLAIM_DIALECT);
    }

    // TODO : validate claim dialect already exists?

    // TODO : validate tenant domain?
    int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);

    // Add listener

    this.claimDialectDAO.addClaimDialect(claimDialect, tenantId);

    // Add listener

}
 
Example 15
Source File: ClaimMetadataManagementServiceImpl.java    From carbon-identity-framework with Apache License 2.0 4 votes vote down vote up
@Override
public List<ExternalClaim> getExternalClaims(String externalClaimDialectURI, String tenantDomain) throws
        ClaimMetadataException {

    if (StringUtils.isBlank(externalClaimDialectURI)) {
        throw new ClaimMetadataClientException(ERROR_CODE_EMPTY_EXTERNAL_CLAIM_URI);
    }

    if (ClaimConstants.LOCAL_CLAIM_DIALECT_URI.equalsIgnoreCase(externalClaimDialectURI)) {
        throw new ClaimMetadataClientException(ERROR_CODE_INVALID_EXTERNAL_CLAIM_DIALECT);
    }

    int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);

    // Add listener

    List<ExternalClaim> externalClaims = this.externalClaimDAO.getExternalClaims(externalClaimDialectURI, tenantId);

    // Add listener

    return externalClaims;
}
 
Example 16
Source File: IdentityProviderManager.java    From carbon-identity-framework with Apache License 2.0 4 votes vote down vote up
/**
 * Retrieves Identity provider information about a given tenant
 *
 * @param idPName        Unique Name of the IdP to which the given local claim URIs need to be mapped
 * @param tenantDomain   The tenant domain of whose local claim URIs to be mapped
 * @param localClaimURIs Local claim URIs which need to be mapped to IdP's claim URIs
 * @throws IdentityProviderManagementException Error when getting claim mappings
 */
@Override
public Set<ClaimMapping> getMappedIdPClaims(String idPName, String tenantDomain,
                                            List<String> localClaimURIs) throws
        IdentityProviderManagementException {

    int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
    if (StringUtils.isEmpty(idPName)) {
        String msg = "Invalid argument: Identity Provider Name value is empty";
        throw new IdentityProviderManagementException(msg);
    }

    IdentityProvider identityProvider = dao.getIdPByName(null, idPName, tenantId, tenantDomain);

    if (identityProvider == null) {
        identityProvider = new FileBasedIdPMgtDAO().getIdPByName(idPName, tenantDomain);
    }

    if (identityProvider == null) {
        identityProvider = IdPManagementServiceComponent.getFileBasedIdPs().get(
                IdentityApplicationConstants.DEFAULT_IDP_CONFIG);
    }

    ClaimConfig claimConfiguration = identityProvider.getClaimConfig();

    if (claimConfiguration != null) {

        ClaimMapping[] claimMappings = claimConfiguration.getClaimMappings();

        if (claimMappings != null && claimMappings.length > 0 && localClaimURIs != null) {
            Set<ClaimMapping> returnSet = new HashSet<ClaimMapping>();
            for (String localClaimURI : localClaimURIs) {
                for (ClaimMapping claimMapping : claimMappings) {
                    if (claimMapping.getLocalClaim().getClaimUri().equals(localClaimURI)) {
                        returnSet.add(claimMapping);
                        break;
                    }
                }
            }
            return returnSet;
        }
    }
    return new HashSet<ClaimMapping>();
}
 
Example 17
Source File: IdentityProviderManager.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
/**
 * Retrieves Identity provider information about a given tenant
 *
 * @param idPName        Unique Name of the IdP to which the given local claim URIs need to be mapped
 * @param tenantDomain   The tenant domain of whose local claim URIs to be mapped
 * @param localClaimURIs Local claim URIs which need to be mapped to IdP's claim URIs
 * @throws IdentityProviderManagementException Error when getting claim mappings
 */
public Set<ClaimMapping> getMappedIdPClaims(String idPName, String tenantDomain,
                                            List<String> localClaimURIs) throws
        IdentityProviderManagementException {

    int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
    if (StringUtils.isEmpty(idPName)) {
        String msg = "Invalid argument: Identity Provider Name value is empty";
        throw new IdentityProviderManagementException(msg);
    }

    IdentityProvider identityProvider = dao.getIdPByName(null, idPName, tenantId, tenantDomain);

    if (identityProvider == null) {
        identityProvider = new FileBasedIdPMgtDAO().getIdPByName(idPName, tenantDomain);
    }

    if (identityProvider == null) {
        identityProvider = IdPManagementServiceComponent.getFileBasedIdPs().get(
                IdentityApplicationConstants.DEFAULT_IDP_CONFIG);
    }

    ClaimConfig claimConfiguration = identityProvider.getClaimConfig();

    if (claimConfiguration != null) {

        ClaimMapping[] claimMappings = claimConfiguration.getClaimMappings();

        if (claimMappings != null && claimMappings.length > 0 && localClaimURIs != null) {
            Set<ClaimMapping> returnSet = new HashSet<ClaimMapping>();
            for (String localClaimURI : localClaimURIs) {
                for (ClaimMapping claimMapping : claimMappings) {
                    if (claimMapping.equals(localClaimURI)) {
                        returnSet.add(claimMapping);
                        break;
                    }
                }
            }
            return returnSet;
        }
    }
    return new HashSet<ClaimMapping>();
}
 
Example 18
Source File: IdentityProviderManager.java    From carbon-identity-framework with Apache License 2.0 3 votes vote down vote up
/**
 * Retrieves registered Identity finally {
 * break;
 * }providers for a given tenant
 *
 * @param tenantDomain Tenant domain whose IdP names are requested
 * @return Set of <code>IdentityProvider</code>. IdP names, primary IdP and home realm
 * identifiers of each IdP
 * @throws IdentityProviderManagementException Error when getting list of Identity Providers
 */
@Override
public List<IdentityProvider> getIdPs(String tenantDomain)
        throws IdentityProviderManagementException {

    int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
    return dao.getIdPs(null, tenantId, tenantDomain);

}
 
Example 19
Source File: IdentityProviderManager.java    From carbon-identity-framework with Apache License 2.0 3 votes vote down vote up
/**
 * Retrieves registered Identity finally {
 * break;
 * }providers for a given tenant
 *
 * @param tenantDomain Tenant domain whose IdP names are requested
 * @return Set of <code>IdentityProvider</code>. IdP names, primary IdP and home realm
 * identifiers of each IdP
 * @throws IdentityProviderManagementException Error when getting list of Identity Providers
 */
@Override
public List<IdentityProvider> getIdPsSearch(String tenantDomain, String filter)
        throws IdentityProviderManagementException {

    int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
    return dao.getIdPsSearch(null, tenantId, tenantDomain, filter);
}
 
Example 20
Source File: IdentityProviderManager.java    From carbon-identity-framework with Apache License 2.0 3 votes vote down vote up
/**
 * Get all identity provider's Basic information along with additionally requested information depending on the
 * requiredAttributes.
 *
 * @param limit              Limit per page.
 * @param offset             Offset value.
 * @param filter             Filter value for IdP search.
 * @param sortOrder          Order of IdP ASC/DESC.
 * @param sortBy             The column value need to sort.
 * @param tenantDomain       TenantDomain of the user.
 * @param requiredAttributes Required attributes which needs to be return.
 * @return Identity Provider's Basic Information array along with requested attribute
 * information{@link IdpSearchResult}.
 * @throws IdentityProviderManagementException Server/client related error when getting list of Identity Providers.
 */
@Override
public IdpSearchResult getIdPs(Integer limit, Integer offset, String filter, String sortOrder, String sortBy,
                               String tenantDomain, List<String> requiredAttributes)
        throws IdentityProviderManagementException {

    IdpSearchResult result = new IdpSearchResult();
    List<ExpressionNode> expressionNodes = getExpressionNodes(filter);
    setParameters(limit, offset, sortOrder, sortBy, filter, result);
    int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
    result.setTotalIDPCount(dao.getTotalIdPCount(tenantId, expressionNodes));
    result.setIdpList(dao.getPaginatedIdPsSearch(tenantId, expressionNodes, result.getLimit(), result.getOffSet(),
            result.getSortOrder(), result.getSortBy(), requiredAttributes));
    return result;
}