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