Search in sources :

Example 11 with UserInfoEndpointException

use of org.wso2.carbon.identity.oauth.user.UserInfoEndpointException in project identity-inbound-auth-oauth by wso2-extensions.

the class ClaimUtil method getUserRealm.

private static UserRealm getUserRealm(String username, String userTenantDomain) throws IdentityException, UserInfoEndpointException {
    UserRealm realm;
    realm = IdentityTenantUtil.getRealm(userTenantDomain, username);
    if (realm == null) {
        throw new UserInfoEndpointException("Invalid User Domain provided: " + userTenantDomain + "Cannot retrieve user claims for user: " + username);
    }
    return realm;
}
Also used : UserRealm(org.wso2.carbon.user.core.UserRealm) UserInfoEndpointException(org.wso2.carbon.identity.oauth.user.UserInfoEndpointException)

Example 12 with UserInfoEndpointException

use of org.wso2.carbon.identity.oauth.user.UserInfoEndpointException in project identity-inbound-auth-oauth by wso2-extensions.

the class ClaimUtil method getUserAttributesFromCache.

private static Map<ClaimMapping, String> getUserAttributesFromCache(OAuth2TokenValidationResponseDTO tokenResponse) throws UserInfoEndpointException {
    AuthorizationGrantCacheKey cacheKey = new AuthorizationGrantCacheKey(OAuth2Util.getAccessTokenIdentifier(tokenResponse));
    AuthorizationGrantCacheEntry cacheEntry = AuthorizationGrantCache.getInstance().getValueFromCacheByToken(cacheKey);
    if (cacheEntry == null) {
        return new HashMap<>();
    }
    return cacheEntry.getUserAttributes();
}
Also used : AuthorizationGrantCacheEntry(org.wso2.carbon.identity.oauth.cache.AuthorizationGrantCacheEntry) HashMap(java.util.HashMap) AuthorizationGrantCacheKey(org.wso2.carbon.identity.oauth.cache.AuthorizationGrantCacheKey)

Example 13 with UserInfoEndpointException

use of org.wso2.carbon.identity.oauth.user.UserInfoEndpointException in project identity-inbound-auth-oauth by wso2-extensions.

the class ClaimUtilTest method testGetClaimsFromUserStore.

@Test(dataProvider = "provideDataForGetClaimsFromUser")
public void testGetClaimsFromUserStore(boolean mockRealm, boolean mockAccessTokenDO, boolean mockServiceProvider, Object claimMappingObject, Map<String, String> spToLocalClaimMappings, Map<String, String> userClaimsMap, String clientId, String subjectClaimUri, String userStoreDomain, String claimSeparator, boolean isFederated, boolean mapFedUsersToLocal, int expectedMapSize) throws Exception {
    ClaimMapping[] claimMappings = (ClaimMapping[]) claimMappingObject;
    mockStatic(IdentityTenantUtil.class);
    if (mockRealm) {
        when(IdentityTenantUtil.getRealm(anyString(), anyString())).thenReturn(mockedUserRealm);
    } else {
        when(IdentityTenantUtil.getRealm(anyString(), anyString())).thenReturn(null);
    }
    mockStatic(OAuthServerConfiguration.class);
    when(OAuthServerConfiguration.getInstance()).thenReturn(mockedOAuthServerConfiguration);
    when(mockedOAuthServerConfiguration.isMapFederatedUsersToLocal()).thenReturn(mapFedUsersToLocal);
    mockOAuth2Util();
    AuthenticatedUser authenticatedUser = getAuthenticatedUser("carbon.super", userStoreDomain, "test-user", isFederated, "4b4414e1-916b-4475-aaee-6b0751c29f11");
    mockStatic(FrameworkUtils.class);
    when(FrameworkUtils.resolveUserIdFromUsername(anyInt(), anyString(), anyString())).thenReturn("4b4414e1-916b-4475-aaee-6b0751c29f11");
    AccessTokenDO accessTokenDO = getAccessTokenDO(clientId, authenticatedUser);
    if (mockAccessTokenDO) {
        when(OAuth2Util.getAccessTokenDOfromTokenIdentifier(anyString())).thenReturn(accessTokenDO);
    }
    mockStatic(OAuth2ServiceComponentHolder.class);
    when(OAuth2ServiceComponentHolder.getApplicationMgtService()).thenReturn(mockedApplicationManagementService);
    when(mockedApplicationManagementService.getServiceProviderNameByClientId(anyString(), anyString(), anyString())).thenReturn("SP1");
    if (mockServiceProvider) {
        when(mockedApplicationManagementService.getApplicationExcludingFileBasedSPs(anyString(), anyString())).thenReturn(mockedServiceProvider);
    }
    when(mockedValidationTokenResponseDTO.getAuthorizedUser()).thenReturn(AUTHORIZED_USER);
    when(mockedValidationTokenResponseDTO.getAuthorizationContextToken()).thenReturn(mockedAuthzContextToken);
    mockedUserStoreManager = mock(AbstractUserStoreManager.class);
    when(mockedUserRealm.getUserStoreManager()).thenReturn(mockedUserStoreManager);
    when(mockedServiceProvider.getClaimConfig()).thenReturn(mockedClaimConfig);
    when(mockedClaimConfig.getClaimMappings()).thenReturn(claimMappings);
    when(mockedServiceProvider.getLocalAndOutBoundAuthenticationConfig()).thenReturn(mockedLocalAndOutboundConfig);
    when(mockedLocalAndOutboundConfig.getSubjectClaimUri()).thenReturn(subjectClaimUri);
    mockStatic(ClaimMetadataHandler.class);
    when(ClaimMetadataHandler.getInstance()).thenReturn(mockedClaimMetadataHandler);
    when(mockedClaimMetadataHandler.getMappingsMapFromOtherDialectToCarbon(anyString(), isNull(Set.class), anyString(), anyBoolean())).thenReturn(spToLocalClaimMappings);
    if (userClaimsMap != null) {
        when(mockedUserStoreManager.getUserClaimValuesWithID(anyString(), any(String[].class), anyString())).thenReturn(userClaimsMap);
    } else {
        when(mockedUserStoreManager.getUserClaimValuesWithID(anyString(), any(String[].class), anyString())).thenThrow(new UserStoreException("UserNotFound"));
    }
    mockStatic(IdentityUtil.class);
    when(IdentityUtil.extractDomainFromName(anyString())).thenReturn(userStoreDomain);
    when(mockedUserRealm.getUserStoreManager()).thenReturn(mockedUserStoreManager);
    when(mockedUserStoreManager.getSecondaryUserStoreManager(anyString())).thenReturn(mockedUserStoreManager);
    when(mockedUserStoreManager.getRealmConfiguration()).thenReturn(mockedRealmConfiguration);
    when(mockedRealmConfiguration.getUserStoreProperty(IdentityCoreConstants.MULTI_ATTRIBUTE_SEPARATOR)).thenReturn(claimSeparator);
    when(mockedServiceProvider.getPermissionAndRoleConfig()).thenReturn(mockedPermissionAndRoleConfig);
    when(mockedPermissionAndRoleConfig.getRoleMappings()).thenReturn(roleMappings);
    Map<String, Object> claimsMap;
    try {
        claimsMap = ClaimUtil.getClaimsFromUserStore(mockedValidationTokenResponseDTO);
        Assert.assertEquals(claimsMap.size(), expectedMapSize);
    } catch (UserInfoEndpointException e) {
        Assert.assertEquals(expectedMapSize, -1, "Unexpected exception thrown");
    }
}
Also used : AccessTokenDO(org.wso2.carbon.identity.oauth2.model.AccessTokenDO) ClaimMapping(org.wso2.carbon.identity.application.common.model.ClaimMapping) Set(java.util.Set) UserInfoEndpointException(org.wso2.carbon.identity.oauth.user.UserInfoEndpointException) UserStoreException(org.wso2.carbon.user.core.UserStoreException) AbstractUserStoreManager(org.wso2.carbon.user.core.common.AbstractUserStoreManager) Matchers.anyString(org.mockito.Matchers.anyString) AuthenticatedUser(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 14 with UserInfoEndpointException

use of org.wso2.carbon.identity.oauth.user.UserInfoEndpointException in project identity-inbound-auth-oauth by wso2-extensions.

the class AbstractUserInfoResponseBuilder method getServiceProvider.

private ServiceProvider getServiceProvider(String tenantDomain, String clientId) throws UserInfoEndpointException {
    ApplicationManagementService applicationMgtService = OAuth2ServiceComponentHolder.getApplicationMgtService();
    ServiceProvider serviceProvider;
    try {
        // Get the Service Provider.
        serviceProvider = applicationMgtService.getServiceProviderByClientId(clientId, IdentityApplicationConstants.OAuth2.NAME, tenantDomain);
    } catch (IdentityApplicationManagementException e) {
        throw new UserInfoEndpointException("Error while obtaining the service provider for client_id: " + clientId + " of tenantDomain: " + tenantDomain, e);
    }
    return serviceProvider;
}
Also used : UserInfoEndpointException(org.wso2.carbon.identity.oauth.user.UserInfoEndpointException) ServiceProvider(org.wso2.carbon.identity.application.common.model.ServiceProvider) IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) ApplicationManagementService(org.wso2.carbon.identity.application.mgt.ApplicationManagementService)

Example 15 with UserInfoEndpointException

use of org.wso2.carbon.identity.oauth.user.UserInfoEndpointException in project identity-inbound-auth-oauth by wso2-extensions.

the class AbstractUserInfoResponseBuilder method getServiceProviderTenantDomain.

private String getServiceProviderTenantDomain(OAuth2TokenValidationResponseDTO tokenResponse) throws UserInfoEndpointException {
    String clientId = null;
    OAuthAppDO oAuthAppDO;
    try {
        clientId = getClientId(OAuth2Util.getAccessTokenIdentifier(tokenResponse));
        oAuthAppDO = OAuth2Util.getAppInformationByClientId(clientId);
    } catch (IdentityOAuth2Exception | InvalidOAuthClientException e) {
        throw new UserInfoEndpointException("Error while retrieving OAuth app information for clientId: " + clientId);
    }
    return OAuth2Util.getTenantDomainOfOauthApp(oAuthAppDO);
}
Also used : OAuthAppDO(org.wso2.carbon.identity.oauth.dao.OAuthAppDO) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) UserInfoEndpointException(org.wso2.carbon.identity.oauth.user.UserInfoEndpointException) InvalidOAuthClientException(org.wso2.carbon.identity.oauth.common.exception.InvalidOAuthClientException)

Aggregations

UserInfoEndpointException (org.wso2.carbon.identity.oauth.user.UserInfoEndpointException)11 IdentityOAuth2Exception (org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)5 HashMap (java.util.HashMap)3 AuthenticatedUser (org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)3 ClaimMapping (org.wso2.carbon.identity.application.common.model.ClaimMapping)3 ServiceProvider (org.wso2.carbon.identity.application.common.model.ServiceProvider)3 InvalidOAuthClientException (org.wso2.carbon.identity.oauth.common.exception.InvalidOAuthClientException)3 AccessTokenDO (org.wso2.carbon.identity.oauth2.model.AccessTokenDO)3 JWSAlgorithm (com.nimbusds.jose.JWSAlgorithm)2 ArrayList (java.util.ArrayList)2 Matchers.anyString (org.mockito.Matchers.anyString)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 BeforeTest (org.testng.annotations.BeforeTest)2 Test (org.testng.annotations.Test)2 IdentityApplicationManagementException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementException)2 ApplicationManagementService (org.wso2.carbon.identity.application.mgt.ApplicationManagementService)2 AuthorizationGrantCacheEntry (org.wso2.carbon.identity.oauth.cache.AuthorizationGrantCacheEntry)2 AuthorizationGrantCacheKey (org.wso2.carbon.identity.oauth.cache.AuthorizationGrantCacheKey)2 OAuthAppDO (org.wso2.carbon.identity.oauth.dao.OAuthAppDO)2 OAuth2TokenValidationResponseDTO (org.wso2.carbon.identity.oauth2.dto.OAuth2TokenValidationResponseDTO)2