Search in sources :

Example 1 with UserInfoResponseBuilder

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

the class OpenIDConnectUserEndpointTest method testGetUserClaims.

/**
 * Here handleError & setServiceProviderTenantId private methods also covered by this method.
 *
 * @param authResponse
 * @param errorMessage
 * @param errorCode
 * @param expectedStatus
 * @throws Exception
 */
@Test(dataProvider = "provideDataForGetUserClaims")
public void testGetUserClaims(String authResponse, String errorMessage, String errorCode, int expectedStatus) throws Exception {
    String clientID = "rgfKVdnMQnJlSSr_pKFTxj3apiwYa";
    UserInfoEndpointException ex = new UserInfoEndpointException(errorCode, errorMessage);
    Class<?> clazz = OpenIDConnectUserEndpoint.class;
    Object setHandleError = clazz.newInstance();
    Method handleError = setHandleError.getClass().getDeclaredMethod("handleError", UserInfoEndpointException.class);
    handleError.setAccessible(true);
    Response errorResponse = (Response) handleError.invoke(setHandleError, ex);
    assertEquals(errorResponse.getStatus(), expectedStatus, "Error response values are not same");
    mockStatic(OAuthServerConfiguration.class);
    when(OAuthServerConfiguration.getInstance()).thenReturn(oauthServerConfigurationMock);
    when(oauthServerConfigurationMock.getTimeStampSkewInSeconds()).thenReturn(3600L);
    when(userInfoResponseBuilder.getResponseString(tokenResponse)).thenReturn(authResponse);
    when(userInfoEndpointConfig.getUserInfoResponseBuilder()).thenReturn(userInfoResponseBuilder);
    mockStatic(OAuth2Util.class);
    when(OAuth2Util.getTenantDomainOfOauthApp(appDO)).thenReturn("test");
    when(OAuth2Util.getTenantId(anyString())).thenReturn(-1234);
    when(OAuth2Util.getAppInformationByClientId(anyString())).thenReturn(appDO);
    when(OAuth2Util.getClientIdForAccessToken(anyString())).thenReturn(clientID);
    when(tokenValidator.validateToken(anyString(), anyObject())).thenReturn(tokenResponse);
    when(userInfoEndpointConfig.getUserInfoAccessTokenValidator()).thenReturn(tokenValidator);
    when(userInfoEndpointConfig.getUserInfoRequestValidator()).thenReturn(requestValidator);
    mockStatic(UserInfoEndpointConfig.class);
    when(UserInfoEndpointConfig.getInstance()).thenReturn(userInfoEndpointConfig);
    Response response = openIDConnectUserEndpoint.getUserClaims(httpServletRequest);
    assertNotNull(response.getStatus());
    assertEquals(response.getStatus(), HttpServletResponse.SC_OK);
    MultivaluedMap<String, Object> metadata = response.getMetadata();
    String metadataValue1 = metadata.get(OAuthConstants.HTTP_RESP_HEADER_CACHE_CONTROL).toString();
    String metadataValue2 = metadata.get(OAuthConstants.HTTP_RESP_HEADER_PRAGMA).toString();
    assertEquals(metadataValue1, "[no-store]", "Values are not equal");
    assertEquals(metadataValue2, "[no-cache]", "Values are not equal");
    assertNotNull(response);
    assertEquals(response.getEntity().toString(), authResponse, "Response values are not same");
    when(httpServletRequest.getParameterNames()).thenReturn(new Enumeration<String>() {

        @Override
        public boolean hasMoreElements() {
            return false;
        }

        @Override
        public String nextElement() {
            return null;
        }
    });
    openIDConnectUserEndpoint.getUserClaimsPost(httpServletRequest, paramMap);
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) OAuthASResponse(org.apache.oltu.oauth2.as.response.OAuthASResponse) Response(javax.ws.rs.core.Response) UserInfoEndpointException(org.wso2.carbon.identity.oauth.user.UserInfoEndpointException) Matchers.anyObject(org.mockito.Matchers.anyObject) Matchers.anyString(org.mockito.Matchers.anyString) Method(java.lang.reflect.Method) 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 2 with UserInfoResponseBuilder

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

the class UserInfoEndpointConfigTest method testGetUserInfoClaimRetriever.

@Test(dataProvider = "getUserUserInfoClaimRetriever")
public void testGetUserInfoClaimRetriever(String validatorClass, Class validatorClassType, boolean isClassExisting) throws Exception {
    mockStatic(EndpointUtil.class);
    when(EndpointUtil.getUserInfoClaimRetriever()).thenReturn(validatorClass);
    UserInfoClaimRetriever userInfoClaimRetriever = UserInfoEndpointConfig.getInstance().getUserInfoClaimRetriever();
    if (isClassExisting) {
        assertNotNull(userInfoClaimRetriever, "UserInfoResponseBuilder should not be null for class " + validatorClass);
        assertEquals(validatorClassType, userInfoClaimRetriever.getClass(), "Expected type of " + "UserInfoClaimRetriever was not found");
    } else {
        assertNull(userInfoClaimRetriever, "Non-existing or invalid class passed. Hence validator should be " + "null");
    }
}
Also used : UserInfoClaimRetriever(org.wso2.carbon.identity.oauth.user.UserInfoClaimRetriever) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with UserInfoResponseBuilder

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

the class UserInfoEndpointConfigTest method testGetUserInfoResponseBuilder.

@Test(dataProvider = "getUserInfoResponseBuilder")
public void testGetUserInfoResponseBuilder(String validatorClass, Class validatorClassType, boolean isClassExisting) throws Exception {
    mockStatic(EndpointUtil.class);
    when(EndpointUtil.getUserInfoResponseBuilder()).thenReturn(validatorClass);
    UserInfoResponseBuilder userInfoResponseBuilder = UserInfoEndpointConfig.getInstance().getUserInfoResponseBuilder();
    if (isClassExisting) {
        assertNotNull(userInfoResponseBuilder, "UserInfoResponseBuilder should not be null for class " + validatorClass);
        assertEquals(validatorClassType, userInfoResponseBuilder.getClass(), "Expected type of " + "UserInfoResponseBuilder was not found");
    } else {
        assertNull(userInfoResponseBuilder, "Non-existing or invalid class passed. Hence validator should be " + "null");
    }
}
Also used : UserInfoResponseBuilder(org.wso2.carbon.identity.oauth.user.UserInfoResponseBuilder) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with UserInfoResponseBuilder

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

the class OpenIDConnectUserEndpoint method getUserClaims.

@GET
@Path("/")
@Consumes("application/x-www-form-urlencoded")
public Response getUserClaims(@Context HttpServletRequest request) throws OAuthSystemException {
    String userInfoResponse;
    String userInfoResponseContentType;
    try {
        // validate the request
        UserInfoRequestValidator requestValidator = UserInfoEndpointConfig.getInstance().getUserInfoRequestValidator();
        String accessToken = requestValidator.validateRequest(request);
        // validate the access token
        UserInfoAccessTokenValidator tokenValidator = UserInfoEndpointConfig.getInstance().getUserInfoAccessTokenValidator();
        OAuth2TokenValidationResponseDTO tokenResponse = tokenValidator.validateToken(accessToken, request);
        // build the claims
        // ToDO - Validate the grant type to be implicit or authorization_code before retrieving claims
        UserInfoResponseBuilder userInfoResponseBuilder = UserInfoEndpointConfig.getInstance().getUserInfoResponseBuilder();
        userInfoResponse = userInfoResponseBuilder.getResponseString(tokenResponse);
        userInfoResponseContentType = getUserInfoResponseMediaType(userInfoResponseBuilder);
    } catch (UserInfoEndpointException e) {
        return handleError(e);
    } catch (OAuthSystemException e) {
        log.error("UserInfoEndpoint Failed", e);
        throw new OAuthSystemException("UserInfoEndpoint Failed");
    }
    ResponseBuilder respBuilder = getResponseBuilderWithCacheControlHeaders();
    if (userInfoResponse != null) {
        return respBuilder.type(userInfoResponseContentType).entity(userInfoResponse).build();
    }
    return respBuilder.build();
}
Also used : UserInfoEndpointException(org.wso2.carbon.identity.oauth.user.UserInfoEndpointException) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) UserInfoRequestValidator(org.wso2.carbon.identity.oauth.user.UserInfoRequestValidator) UserInfoResponseBuilder(org.wso2.carbon.identity.oauth.user.UserInfoResponseBuilder) UserInfoResponseBuilder(org.wso2.carbon.identity.oauth.user.UserInfoResponseBuilder) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) UserInfoAccessTokenValidator(org.wso2.carbon.identity.oauth.user.UserInfoAccessTokenValidator) OAuth2TokenValidationResponseDTO(org.wso2.carbon.identity.oauth2.dto.OAuth2TokenValidationResponseDTO) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) GET(javax.ws.rs.GET)

Aggregations

PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 Test (org.testng.annotations.Test)3 UserInfoEndpointException (org.wso2.carbon.identity.oauth.user.UserInfoEndpointException)2 UserInfoResponseBuilder (org.wso2.carbon.identity.oauth.user.UserInfoResponseBuilder)2 Method (java.lang.reflect.Method)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Consumes (javax.ws.rs.Consumes)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Response (javax.ws.rs.core.Response)1 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)1 OAuthASResponse (org.apache.oltu.oauth2.as.response.OAuthASResponse)1 OAuthSystemException (org.apache.oltu.oauth2.common.exception.OAuthSystemException)1 Matchers.anyObject (org.mockito.Matchers.anyObject)1 Matchers.anyString (org.mockito.Matchers.anyString)1 BeforeTest (org.testng.annotations.BeforeTest)1 UserInfoAccessTokenValidator (org.wso2.carbon.identity.oauth.user.UserInfoAccessTokenValidator)1 UserInfoClaimRetriever (org.wso2.carbon.identity.oauth.user.UserInfoClaimRetriever)1 UserInfoRequestValidator (org.wso2.carbon.identity.oauth.user.UserInfoRequestValidator)1 OAuth2TokenValidationResponseDTO (org.wso2.carbon.identity.oauth2.dto.OAuth2TokenValidationResponseDTO)1