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;
}
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;
}
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);
}
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;
}
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);
}
}
Aggregations