Search in sources :

Example 21 with OauthTokenIssuer

use of org.wso2.carbon.identity.oauth2.token.OauthTokenIssuer in project identity-inbound-auth-oauth by wso2-extensions.

the class OAuthServerConfiguration method getIdentityOauthTokenIssuer.

/**
 * Returns server level default identity oauth token issuer
 *
 * @return instance of default identity oauth token issuer
 */
public OauthTokenIssuer getIdentityOauthTokenIssuer() {
    if (oauthIdentityTokenGenerator == null) {
        synchronized (this) {
            if (oauthIdentityTokenGenerator == null) {
                try {
                    if (oauthIdentityTokenGeneratorClassName != null) {
                        Class clazz = this.getClass().getClassLoader().loadClass(oauthIdentityTokenGeneratorClassName);
                        oauthIdentityTokenGenerator = (OauthTokenIssuer) clazz.newInstance();
                        log.info("An instance of " + oauthIdentityTokenGeneratorClassName + " is created for Identity OAuth token generation.");
                    } else {
                        oauthIdentityTokenGenerator = new OauthTokenIssuerImpl();
                        log.info("The default Identity OAuth token issuer will be used. No custom token " + "generator is set.");
                    }
                } catch (Exception e) {
                    String errorMsg = "Error when instantiating the OAuthIssuer : " + tokenPersistenceProcessorClassName + ". Defaulting to OAuthIssuerImpl";
                    log.error(errorMsg, e);
                    oauthIdentityTokenGenerator = new OauthTokenIssuerImpl();
                }
            }
        }
    }
    return oauthIdentityTokenGenerator;
}
Also used : OauthTokenIssuerImpl(org.wso2.carbon.identity.oauth2.token.OauthTokenIssuerImpl) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)

Example 22 with OauthTokenIssuer

use of org.wso2.carbon.identity.oauth2.token.OauthTokenIssuer in project identity-inbound-auth-oauth by wso2-extensions.

the class OAuthServerConfiguration method addAndReturnTokenIssuerInstance.

/**
 * Adds oauth token issuer instances used for token generation.
 * @param tokenType registered token type
 * @return token issuer instance
 * @throws IdentityOAuth2Exception
 */
public OauthTokenIssuer addAndReturnTokenIssuerInstance(String tokenType) throws IdentityOAuth2Exception {
    TokenIssuerDO tokenIssuerDO = supportedTokenIssuers.get(tokenType);
    OauthTokenIssuer oauthTokenIssuer = null;
    if (tokenIssuerDO != null && tokenIssuerDO.getTokenImplClass() != null) {
        try {
            if (oauthTokenIssuerMap.get(tokenType) == null) {
                Class clazz = this.getClass().getClassLoader().loadClass(tokenIssuerDO.getTokenImplClass());
                oauthTokenIssuer = (OauthTokenIssuer) clazz.newInstance();
                oauthTokenIssuer.setPersistAccessTokenAlias(supportedTokenIssuers.get(tokenType).isPersistAccessTokenAlias());
                oauthTokenIssuerMap.put(tokenType, oauthTokenIssuer);
                log.info("An instance of " + tokenIssuerDO.getTokenImplClass() + " is created for Identity OAuth token generation.");
            } else {
                oauthTokenIssuer = oauthTokenIssuerMap.get(tokenType);
            }
        } catch (Exception e) {
            String errorMsg = "Error when instantiating the OAuthIssuer : " + tokenIssuerDO.getTokenImplClass() + ". Defaulting to OAuthIssuerImpl";
            throw new IdentityOAuth2Exception(errorMsg, e);
        }
    }
    return oauthTokenIssuer;
}
Also used : OauthTokenIssuer(org.wso2.carbon.identity.oauth2.token.OauthTokenIssuer) TokenIssuerDO(org.wso2.carbon.identity.oauth2.model.TokenIssuerDO) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)

Example 23 with OauthTokenIssuer

use of org.wso2.carbon.identity.oauth2.token.OauthTokenIssuer in project identity-inbound-auth-oauth by wso2-extensions.

the class UserInfoResponseBaseTest method mockAccessTokenDOInOAuth2Util.

protected void mockAccessTokenDOInOAuth2Util(AuthenticatedUser authorizedUser) throws IdentityOAuth2Exception, InvalidOAuthClientException {
    AccessTokenDO accessTokenDO = new AccessTokenDO();
    accessTokenDO.setAuthzUser(authorizedUser);
    when(OAuth2Util.getAccessTokenDOfromTokenIdentifier(accessToken)).thenReturn(accessTokenDO);
    when(OAuth2Util.getAuthenticatedUser(any(AccessTokenDO.class))).thenCallRealMethod();
    OauthTokenIssuer oauthTokenIssuer = new OauthTokenIssuerImpl();
    when(OAuth2Util.getTokenIssuer(accessToken)).thenReturn(oauthTokenIssuer);
}
Also used : AccessTokenDO(org.wso2.carbon.identity.oauth2.model.AccessTokenDO) OauthTokenIssuer(org.wso2.carbon.identity.oauth2.token.OauthTokenIssuer) OauthTokenIssuerImpl(org.wso2.carbon.identity.oauth2.token.OauthTokenIssuerImpl)

Example 24 with OauthTokenIssuer

use of org.wso2.carbon.identity.oauth2.token.OauthTokenIssuer in project identity-inbound-auth-oauth by wso2-extensions.

the class OAuth2Util method findAccessToken.

/**
 * Find access tokenDO from token identifier by chaining through all available token issuers.
 *
 * @param tokenIdentifier access token data object from the validation request.
 * @return AccessTokenDO
 * @throws IdentityOAuth2Exception
 */
public static AccessTokenDO findAccessToken(String tokenIdentifier, boolean includeExpired) throws IdentityOAuth2Exception {
    AccessTokenDO accessTokenDO;
    // Get a copy of the list of token issuers .
    Map<String, OauthTokenIssuer> allOAuthTokenIssuerMap = new HashMap<>(OAuthServerConfiguration.getInstance().getOauthTokenIssuerMap());
    // Differentiate default token issuers and other issuers for better performance.
    Map<String, OauthTokenIssuer> defaultOAuthTokenIssuerMap = new HashMap<>();
    extractDefaultOauthTokenIssuers(allOAuthTokenIssuerMap, defaultOAuthTokenIssuerMap);
    // First try default token issuers.
    accessTokenDO = getAccessTokenDOFromMatchingTokenIssuer(tokenIdentifier, defaultOAuthTokenIssuerMap, includeExpired);
    if (accessTokenDO != null) {
        return accessTokenDO;
    }
    // Loop through other issuer and try to get the hash.
    accessTokenDO = getAccessTokenDOFromMatchingTokenIssuer(tokenIdentifier, allOAuthTokenIssuerMap, includeExpired);
    // IllegalArgumentException to be thrown to identify inactive/invalid tokens.
    if (accessTokenDO == null && !includeExpired) {
        throw new IllegalArgumentException("Invalid Access Token. ACTIVE access token is not found.");
    }
    return accessTokenDO;
}
Also used : AccessTokenDO(org.wso2.carbon.identity.oauth2.model.AccessTokenDO) OauthTokenIssuer(org.wso2.carbon.identity.oauth2.token.OauthTokenIssuer) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 25 with OauthTokenIssuer

use of org.wso2.carbon.identity.oauth2.token.OauthTokenIssuer 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)

Aggregations

OauthTokenIssuer (org.wso2.carbon.identity.oauth2.token.OauthTokenIssuer)18 AccessTokenDO (org.wso2.carbon.identity.oauth2.model.AccessTokenDO)15 IdentityOAuth2Exception (org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)14 InvalidOAuthClientException (org.wso2.carbon.identity.oauth.common.exception.InvalidOAuthClientException)9 HashMap (java.util.HashMap)7 OAuthSystemException (org.apache.oltu.oauth2.common.exception.OAuthSystemException)7 OAuth2AuthorizeReqDTO (org.wso2.carbon.identity.oauth2.dto.OAuth2AuthorizeReqDTO)6 Timestamp (java.sql.Timestamp)5 Date (java.util.Date)5 UserIdNotFoundException (org.wso2.carbon.identity.application.authentication.framework.exception.UserIdNotFoundException)5 OAuthAppDO (org.wso2.carbon.identity.oauth.dao.OAuthAppDO)5 OAuthCacheKey (org.wso2.carbon.identity.oauth.cache.OAuthCacheKey)4 OauthTokenIssuerImpl (org.wso2.carbon.identity.oauth2.token.OauthTokenIssuerImpl)4 JWTTokenIssuer (org.wso2.carbon.identity.oauth2.token.JWTTokenIssuer)3 SQLException (java.sql.SQLException)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Test (org.testng.annotations.Test)2 TokenIssuerDO (org.wso2.carbon.identity.oauth2.model.TokenIssuerDO)2 OAuthTokenReqMessageContext (org.wso2.carbon.identity.oauth2.token.OAuthTokenReqMessageContext)2