use of org.wso2.carbon.identity.oauth2.OAuth2Service in project identity-inbound-auth-oauth by wso2-extensions.
the class OAuth2AuthzEndpoint method getTokenBinder.
private Optional<TokenBinder> getTokenBinder(String clientId) throws OAuthSystemException {
OAuthAppDO oAuthAppDO;
try {
oAuthAppDO = OAuth2Util.getAppInformationByClientId(clientId);
} catch (IdentityOAuth2Exception | InvalidOAuthClientException e) {
throw new OAuthSystemException("Failed to retrieve OAuth application with client id: " + clientId, e);
}
if (oAuthAppDO == null || StringUtils.isBlank(oAuthAppDO.getTokenBindingType())) {
return Optional.empty();
}
OAuth2Service oAuth2Service = getOAuth2Service();
List<TokenBinder> supportedTokenBinders = oAuth2Service.getSupportedTokenBinders();
if (supportedTokenBinders == null || supportedTokenBinders.isEmpty()) {
return Optional.empty();
}
return supportedTokenBinders.stream().filter(t -> t.getBindingType().equals(oAuthAppDO.getTokenBindingType())).findAny();
}
use of org.wso2.carbon.identity.oauth2.OAuth2Service in project identity-inbound-auth-oauth by wso2-extensions.
the class OAuth2AuthzEndpointTest method mockEndpointUtil.
private void mockEndpointUtil(boolean isConsentMgtEnabled) throws Exception {
spy(EndpointUtil.class);
doReturn(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME).when(EndpointUtil.class, "getSPTenantDomainFromClientId", anyString());
doReturn(oAuth2Service).when(EndpointUtil.class, "getOAuth2Service");
doReturn(oAuthServerConfiguration).when(EndpointUtil.class, "getOAuthServerConfiguration");
doReturn(USER_CONSENT_URL).when(EndpointUtil.class, "getUserConsentURL", any(OAuth2Parameters.class), anyString(), anyString(), anyBoolean(), any(OAuthMessage.class));
doReturn(LOGIN_PAGE_URL).when(EndpointUtil.class, "getLoginPageURL", anyString(), anyString(), anyBoolean(), anyBoolean(), anySet(), anyMap(), any());
doReturn(requestObjectService).when(EndpointUtil.class, "getRequestObjectService");
EndpointUtil.setOAuthAdminService(oAuthAdminService);
EndpointUtil.setOAuth2ScopeService(oAuth2ScopeService);
// TODO: Remove mocking consentUtil and test the consent flow as well
// https://github.com/wso2/product-is/issues/2679
SSOConsentService ssoConsentService = mock(SSOConsentService.class);
when(ssoConsentService.getConsentRequiredClaimsWithExistingConsents(any(ServiceProvider.class), any(AuthenticatedUser.class))).thenReturn(new ConsentClaimsData());
when(ssoConsentService.getConsentRequiredClaimsWithoutExistingConsents(any(ServiceProvider.class), any(AuthenticatedUser.class))).thenReturn(new ConsentClaimsData());
when(ssoConsentService.isSSOConsentManagementEnabled(any())).thenReturn(isConsentMgtEnabled);
doReturn(ssoConsentService).when(EndpointUtil.class, "getSSOConsentService");
}
use of org.wso2.carbon.identity.oauth2.OAuth2Service in project identity-inbound-auth-oauth by wso2-extensions.
the class OAuth2TokenEndpointTest method testTokenErrorResponse.
@Test(dataProvider = "testTokenErrorResponseDataProvider", groups = "testWithConnection")
public void testTokenErrorResponse(String errorCode, Object headerObj, int expectedStatus, String expectedErrorCode) throws Exception {
ResponseHeader[] responseHeaders = (ResponseHeader[]) headerObj;
Map<String, String[]> requestParams = new HashMap<>();
requestParams.put(OAuth.OAUTH_GRANT_TYPE, new String[] { GrantType.PASSWORD.toString() });
requestParams.put(OAuth.OAUTH_USERNAME, new String[] { USERNAME });
requestParams.put(OAuth.OAUTH_PASSWORD, new String[] { "password" });
mockStatic(LoggerUtils.class);
when(LoggerUtils.isDiagnosticLogsEnabled()).thenReturn(true);
mockStatic(IdentityTenantUtil.class);
when(IdentityTenantUtil.getTenantId(anyString())).thenReturn(-1234);
HttpServletRequest request = mockHttpRequest(requestParams, new HashMap<String, Object>());
when(request.getHeader(OAuthConstants.HTTP_REQ_HEADER_AUTHZ)).thenReturn(AUTHORIZATION_HEADER);
when(request.getHeaderNames()).thenReturn(Collections.enumeration(new ArrayList<String>() {
{
add(OAuthConstants.HTTP_REQ_HEADER_AUTHZ);
}
}));
spy(EndpointUtil.class);
doReturn(REALM).when(EndpointUtil.class, "getRealmInfo");
doReturn(oAuth2Service).when(EndpointUtil.class, "getOAuth2Service");
when(oAuth2Service.issueAccessToken(any(OAuth2AccessTokenReqDTO.class))).thenReturn(oAuth2AccessTokenRespDTO);
when(oAuth2AccessTokenRespDTO.getErrorMsg()).thenReturn("Token Response error");
when(oAuth2AccessTokenRespDTO.getErrorCode()).thenReturn(errorCode);
when(oAuth2AccessTokenRespDTO.getResponseHeaders()).thenReturn(responseHeaders);
mockOAuthServerConfiguration();
mockStatic(IdentityDatabaseUtil.class);
when(IdentityDatabaseUtil.getDBConnection()).thenReturn(connection);
Map<String, Class<? extends OAuthValidator<HttpServletRequest>>> grantTypeValidators = new Hashtable<>();
grantTypeValidators.put(GrantType.PASSWORD.toString(), PasswordValidator.class);
when(oAuthServerConfiguration.getSupportedGrantTypeValidators()).thenReturn(grantTypeValidators);
when(oAuth2Service.getOauthApplicationState(CLIENT_ID_VALUE)).thenReturn("ACTIVE");
Response response;
try {
response = oAuth2TokenEndpoint.issueAccessToken(request, new MultivaluedHashMap<String, String>());
} catch (InvalidRequestParentException ire) {
InvalidRequestExceptionMapper invalidRequestExceptionMapper = new InvalidRequestExceptionMapper();
response = invalidRequestExceptionMapper.toResponse(ire);
}
assertNotNull(response, "Token response is null");
assertEquals(response.getStatus(), expectedStatus, "Unexpected HTTP response status");
assertNotNull(response.getEntity(), "Response entity is null");
assertTrue(response.getEntity().toString().contains(expectedErrorCode), "Expected error code not found");
}
use of org.wso2.carbon.identity.oauth2.OAuth2Service in project identity-inbound-auth-oauth by wso2-extensions.
the class EndpointUtilTest method mockPrivilegedCarbonContext.
private void mockPrivilegedCarbonContext() {
mockStatic(PrivilegedCarbonContext.class);
when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(mockedPrivilegedCarbonContext);
when(mockedPrivilegedCarbonContext.getOSGiService(OAuthServerConfiguration.class, null)).thenReturn(mockedOAuthServerConfiguration);
when(mockedPrivilegedCarbonContext.getOSGiService(WebFingerProcessor.class, null)).thenReturn(DefaultWebFingerProcessor.getInstance());
when(mockedPrivilegedCarbonContext.getOSGiService(OIDCProviderRequestBuilder.class, null)).thenReturn(new DefaultOIDCProviderRequestBuilder());
when(mockedPrivilegedCarbonContext.getOSGiService(OIDCProcessor.class, null)).thenReturn(DefaultOIDCProcessor.getInstance());
when(mockedPrivilegedCarbonContext.getOSGiService(OAuth2Service.class, null)).thenReturn(new OAuth2Service());
when(mockedPrivilegedCarbonContext.getOSGiService(OAuth2TokenValidationService.class, null)).thenReturn(new OAuth2TokenValidationService());
}
Aggregations