Search in sources :

Example 26 with UserIdNotFoundException

use of org.wso2.carbon.identity.application.authentication.framework.exception.UserIdNotFoundException in project identity-inbound-auth-oauth by wso2-extensions.

the class AccessTokenIssuer method getSubjectClaim.

private String getSubjectClaim(ServiceProvider serviceProvider, AuthenticatedUser authenticatedUser) throws IdentityOAuth2Exception {
    String userTenantDomain = authenticatedUser.getTenantDomain();
    String subject;
    String userStoreDomain = authenticatedUser.getUserStoreDomain();
    String subjectClaimUri = getSubjectClaimUriInLocalDialect(serviceProvider);
    if (StringUtils.isNotBlank(subjectClaimUri)) {
        try {
            subject = getSubjectClaimFromUserStore(subjectClaimUri, authenticatedUser);
            if (StringUtils.isBlank(subject)) {
                // Set username as the subject claim since we have no other option
                subject = getDefaultSubject(serviceProvider, authenticatedUser);
                log.warn("Cannot find subject claim: " + subjectClaimUri + " for user:" + authenticatedUser.getLoggableUserId() + ". Defaulting to username: " + subject + " as the subject identifier.");
            }
            // Get the subject claim in the correct format (ie. tenantDomain or userStoreDomain appended)
            subject = getFormattedSubjectClaim(serviceProvider, subject, userStoreDomain, userTenantDomain);
        } catch (IdentityException e) {
            String error = "Error occurred while getting user claim for user: " + authenticatedUser.getLoggableUserId() + ", claim" + ": " + subjectClaimUri;
            throw new IdentityOAuth2Exception(error, e);
        } catch (org.wso2.carbon.user.core.UserStoreException e) {
            String error = "Error occurred while getting subject claim: " + subjectClaimUri + " for user: " + authenticatedUser.getLoggableUserId();
            throw new IdentityOAuth2Exception(error, e);
        }
    } else {
        try {
            subject = getDefaultSubject(serviceProvider, authenticatedUser);
            subject = getFormattedSubjectClaim(serviceProvider, subject, userStoreDomain, userTenantDomain);
        } catch (UserIdNotFoundException e) {
            throw new IdentityOAuth2Exception("User id not found for user: " + authenticatedUser.getLoggableUserId(), e);
        }
        if (log.isDebugEnabled()) {
            log.debug("No subject claim defined for service provider: " + serviceProvider.getApplicationName() + ". Using username as the subject claim.");
        }
    }
    return subject;
}
Also used : IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) UserIdNotFoundException(org.wso2.carbon.identity.application.authentication.framework.exception.UserIdNotFoundException) IdentityException(org.wso2.carbon.identity.base.IdentityException)

Example 27 with UserIdNotFoundException

use of org.wso2.carbon.identity.application.authentication.framework.exception.UserIdNotFoundException in project identity-inbound-auth-oauth by wso2-extensions.

the class RefreshGrantHandler method removeIfCached.

private void removeIfCached(OAuth2AccessTokenReqDTO tokenReq, RefreshTokenValidationDataDO validationBean) throws IdentityOAuth2Exception {
    if (cacheEnabled) {
        String userId;
        try {
            userId = validationBean.getAuthorizedUser().getUserId();
        } catch (UserIdNotFoundException e) {
            throw new IdentityOAuth2Exception("User id not found for user:" + validationBean.getAuthorizedUser().getLoggableUserId(), e);
        }
        clearCache(tokenReq.getClientId(), userId, validationBean.getScope(), validationBean.getAccessToken(), validationBean.getAuthorizedUser().getFederatedIdPName(), validationBean.getTokenBindingReference(), validationBean.getAuthorizedUser().getTenantDomain());
    }
}
Also used : IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) UserIdNotFoundException(org.wso2.carbon.identity.application.authentication.framework.exception.UserIdNotFoundException)

Example 28 with UserIdNotFoundException

use of org.wso2.carbon.identity.application.authentication.framework.exception.UserIdNotFoundException in project identity-inbound-auth-oauth by wso2-extensions.

the class AbstractAuthorizationGrantHandler method updateCacheIfEnabled.

private void updateCacheIfEnabled(AccessTokenDO newTokenBean, String scope, OauthTokenIssuer oauthTokenIssuer) throws IdentityOAuth2Exception {
    if (isHashDisabled && cacheEnabled) {
        AccessTokenDO tokenToCache = AccessTokenDO.clone(newTokenBean);
        // method is set as the token.
        if (oauthTokenIssuer.usePersistedAccessTokenAlias()) {
            try {
                String persistedTokenIdentifier = oauthTokenIssuer.getAccessTokenHash(newTokenBean.getAccessToken());
                tokenToCache.setAccessToken(persistedTokenIdentifier);
            } catch (OAuthSystemException e) {
                if (log.isDebugEnabled()) {
                    if (IdentityUtil.isTokenLoggable(IdentityConstants.IdentityTokens.ACCESS_TOKEN)) {
                        log.debug("Token issuer: " + oauthTokenIssuer.getClass() + " was tried and" + " failed to parse the received token: " + tokenToCache.getAccessToken(), e);
                    } else {
                        log.debug("Token issuer: " + oauthTokenIssuer.getClass() + " was tried and" + " failed to parse the received token.", e);
                    }
                }
            }
        }
        String userId;
        try {
            userId = tokenToCache.getAuthzUser().getUserId();
        } catch (UserIdNotFoundException e) {
            throw new IdentityOAuth2Exception("User id is not available for user: " + tokenToCache.getAuthzUser().getLoggableUserId(), e);
        }
        String authenticatedIDP = OAuth2Util.getAuthenticatedIDP(tokenToCache.getAuthzUser());
        OAuthCacheKey cacheKey = getOAuthCacheKey(scope, tokenToCache.getConsumerKey(), userId, authenticatedIDP, getTokenBindingReference(tokenToCache));
        oauthCache.addToCache(cacheKey, tokenToCache);
        if (log.isDebugEnabled()) {
            log.debug("Access token was added to OAuthCache with cache key : " + cacheKey.getCacheKeyString());
        }
        // Adding AccessTokenDO to improve validation performance
        OAuth2Util.addTokenDOtoCache(newTokenBean);
    }
}
Also used : AccessTokenDO(org.wso2.carbon.identity.oauth2.model.AccessTokenDO) OAuthCacheKey(org.wso2.carbon.identity.oauth.cache.OAuthCacheKey) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) UserIdNotFoundException(org.wso2.carbon.identity.application.authentication.framework.exception.UserIdNotFoundException)

Example 29 with UserIdNotFoundException

use of org.wso2.carbon.identity.application.authentication.framework.exception.UserIdNotFoundException in project identity-inbound-auth-oauth by wso2-extensions.

the class AuthorizationCodeGrantHandler method revokeExistingAccessTokens.

private void revokeExistingAccessTokens(String tokenId, AuthzCodeDO authzCodeDO) throws IdentityOAuth2Exception {
    String userId = null;
    try {
        userId = authzCodeDO.getAuthorizedUser().getUserId();
    } catch (UserIdNotFoundException e) {
        throw new IdentityOAuth2Exception("User id not found for user: " + authzCodeDO.getAuthorizedUser().getLoggableUserId(), e);
    }
    OAuthTokenPersistenceFactory.getInstance().getAccessTokenDAO().revokeAccessToken(tokenId, userId);
    if (log.isDebugEnabled()) {
        if (IdentityUtil.isTokenLoggable(IdentityConstants.IdentityTokens.AUTHORIZATION_CODE)) {
            log.debug("Validated authorization code(hashed): " + DigestUtils.sha256Hex(authzCodeDO.getAuthorizationCode()) + " for client: " + authzCodeDO.getConsumerKey() + " is not active. " + "So revoking the access tokens issued for the authorization code.");
        } else {
            log.debug("Validated authorization code for client: " + authzCodeDO.getConsumerKey() + " is not " + "active. So revoking the access tokens issued for the authorization code.");
        }
    }
}
Also used : IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) UserIdNotFoundException(org.wso2.carbon.identity.application.authentication.framework.exception.UserIdNotFoundException)

Example 30 with UserIdNotFoundException

use of org.wso2.carbon.identity.application.authentication.framework.exception.UserIdNotFoundException in project identity-inbound-auth-oauth by wso2-extensions.

the class TokenBindingExpiryEventHandler method revokeTokensOfBindingRef.

/**
 * Revoke all the access tokens issued for the given user with the given token binding reference if the token
 * revocation token after logout is enabled for the application.
 *
 * @param user                  authenticated user
 * @param tokenBindingReference token binding reference
 * @throws IdentityOAuth2Exception     if an exception occurs while revoking tokens
 * @throws InvalidOAuthClientException if an exception occurs while revoking tokens
 */
private void revokeTokensOfBindingRef(AuthenticatedUser user, String tokenBindingReference) throws IdentityOAuth2Exception, InvalidOAuthClientException {
    if (StringUtils.isBlank(tokenBindingReference) || user == null) {
        return;
    }
    String userId;
    try {
        userId = user.getUserId();
    } catch (UserIdNotFoundException e) {
        log.error("User id cannot be found for user: " + user.getLoggableUserId() + ". Hence skip revoking " + "relevant tokens");
        throw new IdentityOAuth2Exception("Unable to revoke tokens for the token binding reference: " + tokenBindingReference);
    }
    Set<AccessTokenDO> boundTokens = OAuthTokenPersistenceFactory.getInstance().getAccessTokenDAO().getAccessTokensByBindingRef(tokenBindingReference);
    if (log.isDebugEnabled() && CollectionUtils.isEmpty(boundTokens)) {
        log.debug("No bound tokens found for the the provided binding reference: " + tokenBindingReference);
    }
    for (AccessTokenDO accessTokenDO : boundTokens) {
        String consumerKey = accessTokenDO.getConsumerKey();
        if (OAuth2Util.getAppInformationByClientId(consumerKey).isTokenRevocationWithIDPSessionTerminationEnabled() && accessTokenDO.getAuthzUser() != null) {
            AuthenticatedUser authenticatedUser = new AuthenticatedUser(accessTokenDO.getAuthzUser());
            try {
                if (StringUtils.equalsIgnoreCase(userId, authenticatedUser.getUserId())) {
                    revokeTokens(consumerKey, accessTokenDO, tokenBindingReference);
                }
            } catch (UserIdNotFoundException e) {
                log.error("User id cannot be found for user: " + authenticatedUser.getLoggableUserId());
                throw new IdentityOAuth2Exception("Unable to revoke tokens of the app: " + consumerKey + " for the token binding reference: " + tokenBindingReference);
            }
        }
    }
}
Also used : AccessTokenDO(org.wso2.carbon.identity.oauth2.model.AccessTokenDO) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) UserIdNotFoundException(org.wso2.carbon.identity.application.authentication.framework.exception.UserIdNotFoundException) AuthenticatedUser(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)

Aggregations

UserIdNotFoundException (org.wso2.carbon.identity.application.authentication.framework.exception.UserIdNotFoundException)29 IdentityOAuth2Exception (org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)14 AccessTokenDO (org.wso2.carbon.identity.oauth2.model.AccessTokenDO)9 HashMap (java.util.HashMap)8 AuthenticatedUser (org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)7 UserStoreException (org.wso2.carbon.user.api.UserStoreException)7 OAuthCacheKey (org.wso2.carbon.identity.oauth.cache.OAuthCacheKey)6 AbstractUserStoreManager (org.wso2.carbon.user.core.common.AbstractUserStoreManager)6 OAuth2AuthorizeReqDTO (org.wso2.carbon.identity.oauth2.dto.OAuth2AuthorizeReqDTO)5 ArrayList (java.util.ArrayList)4 FrameworkException (org.wso2.carbon.identity.application.authentication.framework.exception.FrameworkException)4 RealmService (org.wso2.carbon.user.core.service.RealmService)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 OAuthSystemException (org.apache.oltu.oauth2.common.exception.OAuthSystemException)3 JSONObject (org.json.JSONObject)3 ClaimMapping (org.wso2.carbon.identity.application.common.model.ClaimMapping)3 Timestamp (java.sql.Timestamp)2 Date (java.util.Date)2 SequenceConfig (org.wso2.carbon.identity.application.authentication.framework.config.model.SequenceConfig)2 StepConfig (org.wso2.carbon.identity.application.authentication.framework.config.model.StepConfig)2