Search in sources :

Example 1 with StratosException

use of org.wso2.carbon.stratos.common.exception.StratosException in project carbon-identity-framework by wso2.

the class TenantConsentMgtListener method deleteAllConsents.

/**
 * Delete all consents belongs to a given tenant id.
 *
 * @param tenantId The id of the tenant.
 * @throws StratosException throws when an error occurs in deleting consents.
 */
protected void deleteAllConsents(int tenantId) throws StratosException {
    try {
        PrivilegedConsentManager privilegedConsentManager = IdentityConsentDataHolder.getInstance().getPrivilegedConsentManager();
        privilegedConsentManager.deletePurposeCategories(tenantId);
        privilegedConsentManager.deletePIICategories(tenantId);
        privilegedConsentManager.deletePurposes(tenantId);
        privilegedConsentManager.deleteReceipts(tenantId);
    } catch (ConsentManagementException e) {
        throw new StratosException("Error in deleting consents of tenant:" + tenantId, e);
    }
}
Also used : PrivilegedConsentManager(org.wso2.carbon.consent.mgt.core.PrivilegedConsentManager) ConsentManagementException(org.wso2.carbon.consent.mgt.core.exception.ConsentManagementException) StratosException(org.wso2.carbon.stratos.common.exception.StratosException)

Example 2 with StratosException

use of org.wso2.carbon.stratos.common.exception.StratosException in project carbon-identity-framework by wso2.

the class TenantManagementListener method onTenantCreate.

/**
 * Add the default Resident Identity Provider entry when a new tenant is registered.
 *
 * @param tenantInfo Information about the newly created tenant
 */
@Override
public void onTenantCreate(TenantInfoBean tenantInfo) throws StratosException {
    try {
        String tenantDomain = tenantInfo.getTenantDomain();
        IdentityProvider identityProvider = new IdentityProvider();
        identityProvider.setIdentityProviderName(IdentityApplicationConstants.RESIDENT_IDP_RESERVED_NAME);
        identityProvider.setHomeRealmId("localhost");
        identityProvider.setPrimary(true);
        IdentityProviderManager.getInstance().addResidentIdP(identityProvider, tenantDomain);
    } catch (IdentityProviderManagementException e) {
        String message = "Error when adding Resident Identity Provider entry for tenant " + tenantInfo.getTenantDomain();
        throw new StratosException(message, e);
    }
}
Also used : IdentityProvider(org.wso2.carbon.identity.application.common.model.IdentityProvider) IdentityProviderManagementException(org.wso2.carbon.idp.mgt.IdentityProviderManagementException) StratosException(org.wso2.carbon.stratos.common.exception.StratosException)

Example 3 with StratosException

use of org.wso2.carbon.stratos.common.exception.StratosException in project identity-inbound-auth-oauth by wso2-extensions.

the class OAuthTenantMgtListenerImpl method clearTokenData.

private void clearTokenData(int tenantId) throws StratosException {
    try {
        Set<AccessTokenDO> accessTokenDOs = OAuthTokenPersistenceFactory.getInstance().getAccessTokenDAO().getAccessTokensByTenant(tenantId);
        Map<String, AccessTokenDO> latestAccessTokens = new HashMap<>();
        for (AccessTokenDO accessTokenDO : accessTokenDOs) {
            String keyString = accessTokenDO.getConsumerKey() + ":" + accessTokenDO.getAuthzUser() + ":" + OAuth2Util.buildScopeString(accessTokenDO.getScope()) + ":" + accessTokenDO.getAuthzUser().getFederatedIdPName();
            AccessTokenDO accessTokenDOFromMap = latestAccessTokens.get(keyString);
            if (accessTokenDOFromMap != null) {
                if (accessTokenDOFromMap.getIssuedTime().before(accessTokenDO.getIssuedTime())) {
                    latestAccessTokens.put(keyString, accessTokenDO);
                }
            } else {
                latestAccessTokens.put(keyString, accessTokenDO);
            }
            // Clear cache
            OAuthUtil.clearOAuthCache(accessTokenDO.getConsumerKey(), accessTokenDO.getAuthzUser(), OAuth2Util.buildScopeString(accessTokenDO.getScope()));
            OAuthUtil.clearOAuthCache(accessTokenDO.getConsumerKey(), accessTokenDO.getAuthzUser());
            OAuthUtil.clearOAuthCache(accessTokenDO);
        }
        ArrayList<String> tokensToRevoke = new ArrayList<>();
        for (Map.Entry entry : latestAccessTokens.entrySet()) {
            tokensToRevoke.add(((AccessTokenDO) entry.getValue()).getAccessToken());
        }
        OAuthTokenPersistenceFactory.getInstance().getAccessTokenDAO().revokeAccessTokens(tokensToRevoke.toArray(new String[tokensToRevoke.size()]), OAuth2Util.isHashEnabled());
        List<AuthzCodeDO> latestAuthzCodes = OAuthTokenPersistenceFactory.getInstance().getAuthorizationCodeDAO().getLatestAuthorizationCodesByTenant(tenantId);
        for (AuthzCodeDO authzCodeDO : latestAuthzCodes) {
            // remove the authorization code from the cache
            OAuthUtil.clearOAuthCache(authzCodeDO.getConsumerKey() + ":" + authzCodeDO.getAuthorizationCode());
        }
        OAuthTokenPersistenceFactory.getInstance().getAuthorizationCodeDAO().deactivateAuthorizationCodes(latestAuthzCodes);
    } catch (IdentityOAuth2Exception e) {
        throw new StratosException("Error occurred while revoking the access tokens in tenant " + tenantId, e);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StratosException(org.wso2.carbon.stratos.common.exception.StratosException) AccessTokenDO(org.wso2.carbon.identity.oauth2.model.AccessTokenDO) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) AuthzCodeDO(org.wso2.carbon.identity.oauth2.model.AuthzCodeDO) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with StratosException

use of org.wso2.carbon.stratos.common.exception.StratosException in project identity-inbound-auth-oauth by wso2-extensions.

the class TenantCreationEventListener method revokeTokens.

private void revokeTokens(int tenantId) throws StratosException {
    try {
        Set<AccessTokenDO> accessTokenDOs = OAuthTokenPersistenceFactory.getInstance().getAccessTokenDAO().getAccessTokensByTenant(tenantId);
        Map<String, AccessTokenDO> latestAccessTokens = new HashMap<>();
        for (AccessTokenDO accessTokenDO : accessTokenDOs) {
            String keyString = accessTokenDO.getConsumerKey() + ":" + accessTokenDO.getAuthzUser() + ":" + OAuth2Util.buildScopeString(accessTokenDO.getScope()) + ":" + accessTokenDO.getAuthzUser().getFederatedIdPName();
            AccessTokenDO accessTokenDOFromMap = latestAccessTokens.get(keyString);
            if (accessTokenDOFromMap != null) {
                if (accessTokenDOFromMap.getIssuedTime().before(accessTokenDO.getIssuedTime())) {
                    latestAccessTokens.put(keyString, accessTokenDO);
                }
            } else {
                latestAccessTokens.put(keyString, accessTokenDO);
            }
            OAuthUtil.clearOAuthCache(accessTokenDO.getConsumerKey(), accessTokenDO.getAuthzUser(), OAuth2Util.buildScopeString(accessTokenDO.getScope()));
            OAuthUtil.clearOAuthCache(accessTokenDO.getConsumerKey(), accessTokenDO.getAuthzUser());
            OAuthUtil.clearOAuthCache(accessTokenDO);
        }
        OAuthTokenPersistenceFactory.getInstance().getAccessTokenDAO().revokeAccessTokens(latestAccessTokens.values().stream().map(AccessTokenDO::getAccessToken).toArray(String[]::new), OAuth2Util.isHashEnabled());
        List<AuthzCodeDO> latestAuthzCodes = OAuthTokenPersistenceFactory.getInstance().getAuthorizationCodeDAO().getLatestAuthorizationCodesByTenant(tenantId);
        // Remove the authorization code from the cache.
        latestAuthzCodes.stream().map(authzCodeDO -> authzCodeDO.getConsumerKey() + ":" + authzCodeDO.getAuthorizationCode()).forEach(OAuthUtil::clearOAuthCache);
        OAuthTokenPersistenceFactory.getInstance().getAuthorizationCodeDAO().deactivateAuthorizationCodes(latestAuthzCodes);
    } catch (IdentityOAuth2Exception e) {
        throw new StratosException("Error occurred while revoking Access Token of tenant: " + tenantId, e);
    }
}
Also used : AccessTokenDO(org.wso2.carbon.identity.oauth2.model.AccessTokenDO) StratosException(org.wso2.carbon.stratos.common.exception.StratosException) OAuth2Util(org.wso2.carbon.identity.oauth2.util.OAuth2Util) TenantMgtListener(org.wso2.carbon.stratos.common.listeners.TenantMgtListener) Set(java.util.Set) HashMap(java.util.HashMap) TenantInfoBean(org.wso2.carbon.stratos.common.beans.TenantInfoBean) List(java.util.List) IdentityOAuthAdminException(org.wso2.carbon.identity.oauth.IdentityOAuthAdminException) OAuthTokenPersistenceFactory(org.wso2.carbon.identity.oauth2.dao.OAuthTokenPersistenceFactory) AccessTokenDO(org.wso2.carbon.identity.oauth2.model.AccessTokenDO) Map(java.util.Map) OAuthUtil(org.wso2.carbon.identity.oauth.OAuthUtil) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) OAuth2ServiceComponentHolder(org.wso2.carbon.identity.oauth2.internal.OAuth2ServiceComponentHolder) AuthzCodeDO(org.wso2.carbon.identity.oauth2.model.AuthzCodeDO) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) HashMap(java.util.HashMap) AuthzCodeDO(org.wso2.carbon.identity.oauth2.model.AuthzCodeDO) OAuthUtil(org.wso2.carbon.identity.oauth.OAuthUtil) StratosException(org.wso2.carbon.stratos.common.exception.StratosException)

Example 5 with StratosException

use of org.wso2.carbon.stratos.common.exception.StratosException in project identity-governance by wso2-extensions.

the class TenantManagementListener method onPreDelete.

@Override
public void onPreDelete(int tenantId) throws StratosException {
    try {
        UserRecoveryDataStore userRecoveryDataStore = JDBCRecoveryDataStore.getInstance();
        userRecoveryDataStore.deleteRecoveryDataByTenantId(tenantId);
    } catch (IdentityRecoveryException e) {
        throw new StratosException("Error in deleting recovery data of the tenant:" + tenantId, e);
    }
}
Also used : UserRecoveryDataStore(org.wso2.carbon.identity.recovery.store.UserRecoveryDataStore) IdentityRecoveryException(org.wso2.carbon.identity.recovery.IdentityRecoveryException) StratosException(org.wso2.carbon.stratos.common.exception.StratosException)

Aggregations

StratosException (org.wso2.carbon.stratos.common.exception.StratosException)6 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ConsentManagementException (org.wso2.carbon.consent.mgt.core.exception.ConsentManagementException)2 IdentityOAuth2Exception (org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)2 AccessTokenDO (org.wso2.carbon.identity.oauth2.model.AccessTokenDO)2 AuthzCodeDO (org.wso2.carbon.identity.oauth2.model.AuthzCodeDO)2 IdentityRecoveryException (org.wso2.carbon.identity.recovery.IdentityRecoveryException)2 TenantInfoBean (org.wso2.carbon.stratos.common.beans.TenantInfoBean)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Set (java.util.Set)1 Test (org.testng.annotations.Test)1 PrivilegedConsentManager (org.wso2.carbon.consent.mgt.core.PrivilegedConsentManager)1 PurposeCategory (org.wso2.carbon.consent.mgt.core.model.PurposeCategory)1 IdentityProvider (org.wso2.carbon.identity.application.common.model.IdentityProvider)1 IdentityEventListenerConfig (org.wso2.carbon.identity.core.model.IdentityEventListenerConfig)1 IdentityMgtConfigException (org.wso2.carbon.identity.mgt.IdentityMgtConfigException)1 IdentityMgtEventListener (org.wso2.carbon.identity.mgt.IdentityMgtEventListener)1 Config (org.wso2.carbon.identity.mgt.config.Config)1