use of org.wso2.carbon.identity.oauth.cache.AuthorizationGrantCacheEntry in project identity-inbound-auth-oauth by wso2-extensions.
the class OAuth2AuthzEndpoint method addToBCLogoutSessionToOAuthMessage.
/**
* Store Authorization Code and SessionID for back-channel logout in the cache.
*
* @param oAuthMessage
* @param sessionId
*/
private void addToBCLogoutSessionToOAuthMessage(OAuthMessage oAuthMessage, String sessionId) {
AuthorizationGrantCacheEntry entry = oAuthMessage.getAuthorizationGrantCacheEntry();
if (entry == null) {
log.debug("Authorization code is not found in the redirect URL");
return;
}
entry.setOidcSessionId(sessionId);
}
use of org.wso2.carbon.identity.oauth.cache.AuthorizationGrantCacheEntry in project identity-inbound-auth-oauth by wso2-extensions.
the class AbstractTokenBinder method getTokenBindingValue.
@Override
public Optional<String> getTokenBindingValue(OAuth2AccessTokenReqDTO oAuth2AccessTokenReqDTO) {
if (AUTHORIZATION_CODE.equals(oAuth2AccessTokenReqDTO.getGrantType()) && StringUtils.isNotBlank(oAuth2AccessTokenReqDTO.getAuthorizationCode())) {
AuthorizationGrantCacheKey cacheKey = new AuthorizationGrantCacheKey(oAuth2AccessTokenReqDTO.getAuthorizationCode());
AuthorizationGrantCacheEntry authorizationGrantCacheEntry = AuthorizationGrantCache.getInstance().getValueFromCacheByCode(cacheKey);
if (authorizationGrantCacheEntry != null && StringUtils.isNotBlank(authorizationGrantCacheEntry.getTokenBindingValue())) {
return Optional.of(authorizationGrantCacheEntry.getTokenBindingValue());
}
}
return Optional.empty();
}
use of org.wso2.carbon.identity.oauth.cache.AuthorizationGrantCacheEntry in project identity-inbound-auth-oauth by wso2-extensions.
the class DefaultOIDCClaimsCallbackHandlerTest method testHandleClaimsForOAuthTokenReqMessageContextWithAuthorizationCode.
@Test
public void testHandleClaimsForOAuthTokenReqMessageContextWithAuthorizationCode() throws Exception {
JWTClaimsSet.Builder jwtClaimsSetBuilder = new JWTClaimsSet.Builder();
Map<ClaimMapping, String> userAttributes = new HashMap<>();
userAttributes.put(SAML2BearerGrantHandlerTest.buildClaimMapping(COUNTRY), TestConstants.CLAIM_VALUE1);
userAttributes.put(SAML2BearerGrantHandlerTest.buildClaimMapping(EMAIL), TestConstants.CLAIM_VALUE2);
OAuthTokenReqMessageContext requestMsgCtx = getTokenReqMessageContextForFederatedUser(userAttributes);
requestMsgCtx.addProperty("AuthorizationCode", "dummyAuthorizationCode");
AuthorizationGrantCacheEntry authorizationGrantCacheEntry = mock(AuthorizationGrantCacheEntry.class);
mockAuthorizationGrantCache(authorizationGrantCacheEntry);
UserRealm userRealm = getUserRealmWithUserClaims(USER_CLAIMS_MAP);
mockUserRealm(requestMsgCtx.getAuthorizedUser().toString(), userRealm);
JWTClaimsSet jwtClaimsSet = getJwtClaimSet(jwtClaimsSetBuilder, requestMsgCtx);
assertNotNull(jwtClaimsSet, "JWT Custom claim handling failed.");
assertFalse(jwtClaimsSet.getClaims().isEmpty(), "JWT custom claim handling failed");
Assert.assertEquals(jwtClaimsSet.getClaims().size(), 3, "Expected custom claims are not set.");
Assert.assertEquals(jwtClaimsSet.getClaim(EMAIL), TestConstants.CLAIM_VALUE2, "OIDC claim " + EMAIL + " is not added with the JWT token");
}
use of org.wso2.carbon.identity.oauth.cache.AuthorizationGrantCacheEntry in project identity-inbound-auth-oauth by wso2-extensions.
the class DefaultOIDCClaimsCallbackHandlerTest method testHandleCustomClaimsWithoutClaimsInRefreshFlow.
@Test(description = "This method tests the handle custom claims when there is no user attributes in cache as well" + " as in authenticates user object")
public void testHandleCustomClaimsWithoutClaimsInRefreshFlow() throws Exception {
JWTClaimsSet.Builder jwtClaimsSetBuilder = new JWTClaimsSet.Builder();
OAuthTokenReqMessageContext requestMsgCtx = getTokenReqMessageContextForFederatedUser(null);
// Add the relevant oidc claims to scop resource.
Properties oidcProperties = new Properties();
String[] oidcScopeClaims = new String[] { USERNAME, EMAIL };
oidcProperties.setProperty(OIDC_SCOPE, StringUtils.join(oidcScopeClaims, ","));
Map<ClaimMapping, String> userAttributes = new HashMap<>();
userAttributes.put(SAML2BearerGrantHandlerTest.buildClaimMapping(USERNAME), TestConstants.CLAIM_VALUE1);
userAttributes.put(SAML2BearerGrantHandlerTest.buildClaimMapping(EMAIL), TestConstants.CLAIM_VALUE2);
userAttributes.put(SAML2BearerGrantHandlerTest.buildClaimMapping(PHONE_NUMBER_VERIFIED), TestConstants.CLAIM_VALUE2);
AuthorizationGrantCacheEntry authorizationGrantCacheEntry = new AuthorizationGrantCacheEntry(userAttributes);
authorizationGrantCacheEntry.setSubjectClaim(requestMsgCtx.getAuthorizedUser().getUserName());
mockAuthorizationGrantCache(authorizationGrantCacheEntry);
RefreshTokenValidationDataDO refreshTokenValidationDataDO = Mockito.mock(RefreshTokenValidationDataDO.class);
Mockito.doReturn(SAMPLE_ACCESS_TOKEN).when(refreshTokenValidationDataDO).getAccessToken();
requestMsgCtx.addProperty(PREV_ACCESS_TOKEN, refreshTokenValidationDataDO);
UserRealm userRealm = getUserRealmWithUserClaims(USER_CLAIMS_MAP);
mockUserRealm(requestMsgCtx.getAuthorizedUser().toString(), userRealm);
JWTClaimsSet jwtClaimsSet = getJwtClaimSet(jwtClaimsSetBuilder, requestMsgCtx);
Assert.assertFalse(jwtClaimsSet.getClaims().isEmpty(), "JWT custom claim list is empty. Custom claim handling failed in refresh flow");
Assert.assertEquals(jwtClaimsSet.getClaim(USERNAME), TestConstants.CLAIM_VALUE1, "Incomplete list of custom claims returned.");
jwtClaimsSetBuilder = new JWTClaimsSet.Builder();
jwtClaimsSet = defaultOIDCClaimsCallbackHandler.handleCustomClaims(jwtClaimsSetBuilder, requestMsgCtx);
Assert.assertFalse(jwtClaimsSet.getClaims().isEmpty(), "JWT custom claim list is empty. Custom claim handling failed in refresh flow");
}
use of org.wso2.carbon.identity.oauth.cache.AuthorizationGrantCacheEntry in project identity-inbound-auth-oauth by wso2-extensions.
the class AccessTokenIssuer method addUserAttributesAgainstAccessToken.
/**
* Copies the cache entry against the authorization code and adds an entry against the access token. This is done to
* reuse the calculated user claims for subsequent usages such as user info calls.
*
* @param tokenReqDTO
* @param tokenRespDTO
*/
private void addUserAttributesAgainstAccessToken(OAuth2AccessTokenReqDTO tokenReqDTO, OAuth2AccessTokenRespDTO tokenRespDTO) {
AuthorizationGrantCacheKey oldCacheKey = new AuthorizationGrantCacheKey(getAuthorizationCode(tokenReqDTO));
// checking getUserAttributesId value of cacheKey before retrieve entry from cache as it causes to NPE
if (oldCacheKey.getUserAttributesId() != null) {
AuthorizationGrantCacheEntry authorizationGrantCacheEntry = AuthorizationGrantCache.getInstance().getValueFromCacheByCode(oldCacheKey);
AuthorizationGrantCacheKey newCacheKey = new AuthorizationGrantCacheKey(tokenRespDTO.getAccessToken());
if (authorizationGrantCacheEntry != null) {
authorizationGrantCacheEntry.setTokenId(tokenRespDTO.getTokenId());
if (log.isDebugEnabled()) {
if (IdentityUtil.isTokenLoggable(IdentityConstants.IdentityTokens.ACCESS_TOKEN)) {
log.debug("Adding AuthorizationGrantCache entry for the access token(hashed):" + DigestUtils.sha256Hex(newCacheKey.getUserAttributesId()));
} else {
log.debug("Adding AuthorizationGrantCache entry for the access token");
}
}
authorizationGrantCacheEntry.setValidityPeriod(TimeUnit.MILLISECONDS.toNanos(tokenRespDTO.getExpiresInMillis()));
AuthorizationGrantCache.getInstance().addToCacheByToken(newCacheKey, authorizationGrantCacheEntry);
}
}
}
Aggregations