Search in sources :

Example 66 with ClaimMapping

use of org.wso2.carbon.identity.application.common.model.xsd.ClaimMapping in project identity-inbound-auth-oauth by wso2-extensions.

the class DefaultClaimsRetrieverTest method getSampleClaimMapping.

private ClaimMapping[] getSampleClaimMapping() {
    String coreClaimUri1;
    String testMappedAttributesCore1;
    String coreClaimUri2;
    String testMappedAttributesCore2;
    String userClaimUri1;
    String testMappedAttributesUser1;
    String userClaimUri2;
    String testMappedAttributesUser2;
    Claim claim1 = new Claim();
    Claim claim2 = new Claim();
    Claim claim3 = new Claim();
    Claim claim4 = new Claim();
    coreClaimUri1 = "testCoreClaimURI1";
    claim1.setClaimUri(coreClaimUri1);
    testMappedAttributesCore1 = "MappedAttributesCore1";
    coreClaimUri2 = "testCoreClaimURI2";
    claim2.setClaimUri(coreClaimUri2);
    testMappedAttributesCore2 = "MappedAttributesCore2";
    userClaimUri1 = "testUserClaimURI1";
    claim3.setClaimUri(userClaimUri1);
    testMappedAttributesUser1 = "MappedAttributesUser1";
    userClaimUri2 = "testUserClaimURI2";
    claim4.setClaimUri(userClaimUri2);
    testMappedAttributesUser2 = "MappedAttributesUser2";
    ClaimMapping cMap1 = new ClaimMapping(claim1, testMappedAttributesCore1);
    ClaimMapping cMap2 = new ClaimMapping(claim2, testMappedAttributesCore2);
    ClaimMapping cMap3 = new ClaimMapping(claim3, testMappedAttributesUser1);
    ClaimMapping cMap4 = new ClaimMapping(claim4, testMappedAttributesUser2);
    return new ClaimMapping[] { cMap1, cMap2, cMap3, cMap4 };
}
Also used : ClaimMapping(org.wso2.carbon.user.api.ClaimMapping) Matchers.anyString(org.mockito.Matchers.anyString) Claim(org.wso2.carbon.user.api.Claim)

Example 67 with ClaimMapping

use of org.wso2.carbon.identity.application.common.model.xsd.ClaimMapping in project identity-inbound-auth-oauth by wso2-extensions.

the class DefaultOIDCClaimsCallbackHandlerTest method testHandleClaimsForOAuthAuthzReqMessageContextNullAccessToken.

@Test
public void testHandleClaimsForOAuthAuthzReqMessageContextNullAccessToken() throws Exception {
    JWTClaimsSet.Builder jwtClaimsSetBuilder = new JWTClaimsSet.Builder();
    AuthenticatedUser authenticatedUser = getDefaultAuthenticatedUserFederatedUser();
    OAuth2AuthorizeReqDTO authorizeReqDTO = new OAuth2AuthorizeReqDTO();
    authorizeReqDTO.setUser(authenticatedUser);
    authorizeReqDTO.setTenantDomain(TENANT_DOMAIN);
    OAuthAuthzReqMessageContext authzReqMessageContext = new OAuthAuthzReqMessageContext(authorizeReqDTO);
    authzReqMessageContext.setApprovedScope(APPROVED_SCOPES);
    ServiceProvider serviceProvider = new ServiceProvider();
    serviceProvider.setApplicationName(SERVICE_PROVIDER_NAME);
    ClaimMapping claimMap1 = ClaimMapping.build("http://www.wso2.org/claims/email", "email", "sample@abc.com", true);
    ClaimMapping claimMap2 = ClaimMapping.build("http://www.wso2.org/claims/username", "username", "user123", true);
    ClaimMapping[] requestedLocalClaimMap = { claimMap1, claimMap2 };
    ClaimConfig claimConfig = new ClaimConfig();
    claimConfig.setClaimMappings(requestedLocalClaimMap);
    serviceProvider.setClaimConfig(claimConfig);
    serviceProvider.setSpProperties(new ServiceProviderProperty[] {});
    OAuthServerConfiguration mockOAuthServerConfiguration = PowerMockito.mock(OAuthServerConfiguration.class);
    mockStatic(OAuthServerConfiguration.class);
    when(OAuthServerConfiguration.getInstance()).thenReturn(mockOAuthServerConfiguration);
    when(mockOAuthServerConfiguration.getOpenIDConnectSkipeUserConsentConfig()).thenReturn(true);
    mockApplicationManagementService(serviceProvider);
    JWTClaimsSet jwtClaimsSet = defaultOIDCClaimsCallbackHandler.handleCustomClaims(jwtClaimsSetBuilder, authzReqMessageContext);
    assertEquals(jwtClaimsSet.getClaims().size(), 0, "Claims are not successfully set.");
}
Also used : ClaimMapping(org.wso2.carbon.identity.application.common.model.ClaimMapping) ClaimConfig(org.wso2.carbon.identity.application.common.model.ClaimConfig) JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) AttributeBuilder(org.opensaml.saml.saml2.core.impl.AttributeBuilder) ServiceProvider(org.wso2.carbon.identity.application.common.model.ServiceProvider) OAuthAuthzReqMessageContext(org.wso2.carbon.identity.oauth2.authz.OAuthAuthzReqMessageContext) OAuthServerConfiguration(org.wso2.carbon.identity.oauth.config.OAuthServerConfiguration) OAuth2AuthorizeReqDTO(org.wso2.carbon.identity.oauth2.dto.OAuth2AuthorizeReqDTO) AuthenticatedUser(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser) SAML2BearerGrantHandlerTest(org.wso2.carbon.identity.oauth2.token.handlers.grant.saml.SAML2BearerGrantHandlerTest) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 68 with ClaimMapping

use of org.wso2.carbon.identity.application.common.model.xsd.ClaimMapping 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");
}
Also used : ClaimMapping(org.wso2.carbon.identity.application.common.model.ClaimMapping) AuthorizationGrantCacheEntry(org.wso2.carbon.identity.oauth.cache.AuthorizationGrantCacheEntry) UserRealm(org.wso2.carbon.user.core.UserRealm) HashMap(java.util.HashMap) JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) AttributeBuilder(org.opensaml.saml.saml2.core.impl.AttributeBuilder) OAuthTokenReqMessageContext(org.wso2.carbon.identity.oauth2.token.OAuthTokenReqMessageContext) Matchers.anyString(org.mockito.Matchers.anyString) SAML2BearerGrantHandlerTest(org.wso2.carbon.identity.oauth2.token.handlers.grant.saml.SAML2BearerGrantHandlerTest) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 69 with ClaimMapping

use of org.wso2.carbon.identity.application.common.model.xsd.ClaimMapping 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");
}
Also used : HashMap(java.util.HashMap) AttributeBuilder(org.opensaml.saml.saml2.core.impl.AttributeBuilder) OAuthTokenReqMessageContext(org.wso2.carbon.identity.oauth2.token.OAuthTokenReqMessageContext) Matchers.anyString(org.mockito.Matchers.anyString) Properties(java.util.Properties) ClaimMapping(org.wso2.carbon.identity.application.common.model.ClaimMapping) AuthorizationGrantCacheEntry(org.wso2.carbon.identity.oauth.cache.AuthorizationGrantCacheEntry) RefreshTokenValidationDataDO(org.wso2.carbon.identity.oauth2.model.RefreshTokenValidationDataDO) UserRealm(org.wso2.carbon.user.core.UserRealm) JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) SAML2BearerGrantHandlerTest(org.wso2.carbon.identity.oauth2.token.handlers.grant.saml.SAML2BearerGrantHandlerTest) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 70 with ClaimMapping

use of org.wso2.carbon.identity.application.common.model.xsd.ClaimMapping in project identity-inbound-auth-oauth by wso2-extensions.

the class DefaultOIDCClaimsCallbackHandlerTest method getTokenReqMessageContextForFederatedUser.

/**
 * To get token request message context for federates user.
 *
 * @param userAttributes Relevant user attributes need to be added to authenticates user.
 * @return relevant token request context for federated authenticated user.
 */
private OAuthTokenReqMessageContext getTokenReqMessageContextForFederatedUser(Map<ClaimMapping, String> userAttributes) {
    OAuth2AccessTokenReqDTO accessTokenReqDTO = new OAuth2AccessTokenReqDTO();
    accessTokenReqDTO.setTenantDomain(TENANT_DOMAIN);
    accessTokenReqDTO.setClientId(DUMMY_CLIENT_ID);
    OAuthTokenReqMessageContext requestMsgCtx = new OAuthTokenReqMessageContext(accessTokenReqDTO);
    requestMsgCtx.setScope(APPROVED_SCOPES);
    AuthenticatedUser authenticatedUser = getDefaultAuthenticatedUserFederatedUser();
    if (userAttributes != null) {
        authenticatedUser.setUserAttributes(userAttributes);
    }
    requestMsgCtx.setAuthorizedUser(authenticatedUser);
    return requestMsgCtx;
}
Also used : OAuthTokenReqMessageContext(org.wso2.carbon.identity.oauth2.token.OAuthTokenReqMessageContext) OAuth2AccessTokenReqDTO(org.wso2.carbon.identity.oauth2.dto.OAuth2AccessTokenReqDTO) AuthenticatedUser(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)

Aggregations

ClaimMapping (org.wso2.carbon.identity.application.common.model.ClaimMapping)109 HashMap (java.util.HashMap)60 ArrayList (java.util.ArrayList)52 ClaimConfig (org.wso2.carbon.identity.application.common.model.ClaimConfig)28 Map (java.util.Map)26 ServiceProvider (org.wso2.carbon.identity.application.common.model.ServiceProvider)24 ClaimMapping (org.wso2.carbon.identity.application.common.model.xsd.ClaimMapping)24 ClaimMapping (org.wso2.carbon.user.api.ClaimMapping)24 Claim (org.wso2.carbon.identity.application.common.model.Claim)23 Test (org.testng.annotations.Test)22 Claim (org.wso2.carbon.identity.application.common.model.xsd.Claim)21 LocalClaim (org.wso2.carbon.identity.claim.metadata.mgt.model.LocalClaim)21 AuthenticatedUser (org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)20 IdentityApplicationManagementException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementException)20 List (java.util.List)17 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)17 UserStoreException (org.wso2.carbon.user.api.UserStoreException)17 Matchers.anyString (org.mockito.Matchers.anyString)16 IdentityProvider (org.wso2.carbon.identity.application.common.model.IdentityProvider)14 ClaimMetadataException (org.wso2.carbon.identity.claim.metadata.mgt.exception.ClaimMetadataException)13