Search in sources :

Example 1 with OAuthUtil

use of org.wso2.carbon.identity.oauth.OAuthUtil 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)

Aggregations

HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 IdentityOAuthAdminException (org.wso2.carbon.identity.oauth.IdentityOAuthAdminException)1 OAuthUtil (org.wso2.carbon.identity.oauth.OAuthUtil)1 IdentityOAuth2Exception (org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)1 OAuthTokenPersistenceFactory (org.wso2.carbon.identity.oauth2.dao.OAuthTokenPersistenceFactory)1 OAuth2ServiceComponentHolder (org.wso2.carbon.identity.oauth2.internal.OAuth2ServiceComponentHolder)1 AccessTokenDO (org.wso2.carbon.identity.oauth2.model.AccessTokenDO)1 AuthzCodeDO (org.wso2.carbon.identity.oauth2.model.AuthzCodeDO)1 OAuth2Util (org.wso2.carbon.identity.oauth2.util.OAuth2Util)1 TenantInfoBean (org.wso2.carbon.stratos.common.beans.TenantInfoBean)1 StratosException (org.wso2.carbon.stratos.common.exception.StratosException)1 TenantMgtListener (org.wso2.carbon.stratos.common.listeners.TenantMgtListener)1