use of org.wso2.carbon.identity.oauth2.model.OAuth2ScopeConsentResponse in project identity-inbound-auth-oauth by wso2-extensions.
the class EndpointUtil method setConsentRequiredScopesToOAuthParams.
private static void setConsentRequiredScopesToOAuthParams(AuthenticatedUser user, OAuth2Parameters params) throws OAuthSystemException {
try {
String consentRequiredScopes = StringUtils.EMPTY;
List<String> allowedOAuthScopes = getAllowedOAuthScopes(params);
if (user != null && !isPromptContainsConsent(params)) {
String userId = getUserIdOfAuthenticatedUser(user);
String appId = getAppIdFromClientId(params.getClientId());
OAuth2ScopeConsentResponse existingUserConsent = oAuth2ScopeService.getUserConsentForApp(userId, appId, IdentityTenantUtil.getTenantId(user.getTenantDomain()));
if (existingUserConsent != null) {
if (CollectionUtils.isNotEmpty(existingUserConsent.getApprovedScopes())) {
allowedOAuthScopes.removeAll(existingUserConsent.getApprovedScopes());
}
}
}
if (CollectionUtils.isNotEmpty(allowedOAuthScopes)) {
// Filter out internal scopes to be validated.
String[] requestedScopes = Oauth2ScopeUtils.getRequestedScopes(allowedOAuthScopes.toArray(new String[0]));
if (ArrayUtils.isNotEmpty(requestedScopes)) {
// Remove the filtered internal scopes from the allowedOAuthScopes list.
allowedOAuthScopes.removeAll(Arrays.asList(requestedScopes));
JDBCPermissionBasedInternalScopeValidator scopeValidator = new JDBCPermissionBasedInternalScopeValidator();
String[] validatedScope = scopeValidator.validateScope(requestedScopes, user, params.getClientId());
// Filter out requested scopes from the validated scope array.
for (String scope : requestedScopes) {
if (ArrayUtils.contains(validatedScope, scope)) {
allowedOAuthScopes.add(scope);
}
}
}
params.setConsentRequiredScopes(new HashSet<>(allowedOAuthScopes));
consentRequiredScopes = String.join(" ", allowedOAuthScopes).trim();
}
if (log.isDebugEnabled()) {
log.debug("Consent required scopes : " + consentRequiredScopes + " for request from client : " + params.getClientId());
}
} catch (IdentityOAuth2ScopeException e) {
throw new OAuthSystemException("Error occurred while retrieving user consents OAuth scopes.");
}
}
use of org.wso2.carbon.identity.oauth2.model.OAuth2ScopeConsentResponse in project identity-inbound-auth-oauth by wso2-extensions.
the class OAuth2ScopeServiceTest method testUpdateUserConsentForApplication.
@Test(dataProvider = "userConsentScopesForApplicationProvider")
public void testUpdateUserConsentForApplication(List<String> newApprovedScopes, List<String> newDeniedScopes, int approvedScopeSize, int deniedScopeSize) throws Exception {
List<String> approvedScopes = new ArrayList<>(Arrays.asList("read"));
List<String> deniedScopes = new ArrayList<>(Arrays.asList("write"));
String uuid = UUID.randomUUID().toString();
insertAppId(uuid);
oAuth2ScopeService.addUserConsentForApplication("user_id", uuid, 1, approvedScopes, deniedScopes);
oAuth2ScopeService.updateUserConsentForApplication("user_id", uuid, 1, newApprovedScopes, newDeniedScopes);
OAuth2ScopeConsentResponse oAuth2ScopeConsentResponse = oAuth2ScopeService.getUserConsentForApp("user_id", uuid, 1);
assertEquals(oAuth2ScopeConsentResponse.getApprovedScopes().size(), approvedScopeSize);
assertEquals(oAuth2ScopeConsentResponse.getDeniedScopes().size(), deniedScopeSize);
}
use of org.wso2.carbon.identity.oauth2.model.OAuth2ScopeConsentResponse in project identity-inbound-auth-oauth by wso2-extensions.
the class OAuth2ScopeServiceTest method testGetUserConsentForApp.
@Test
public void testGetUserConsentForApp() throws Exception {
List<String> approvedScopes = new ArrayList<>(Arrays.asList("read", "write"));
List<String> deniedScopes = new ArrayList<>(Arrays.asList("delete"));
String appId = UUID.randomUUID().toString();
insertAppId(appId);
int tenantId = 1;
String userId = "dummyUserId";
oAuth2ScopeService.addUserConsentForApplication(userId, appId, tenantId, approvedScopes, deniedScopes);
OAuth2ScopeConsentResponse oAuth2ScopeConsentResponse = oAuth2ScopeService.getUserConsentForApp(userId, appId, tenantId);
assertEquals(oAuth2ScopeConsentResponse.getAppId(), appId);
assertEquals(oAuth2ScopeConsentResponse.getUserId(), userId);
assertEquals(oAuth2ScopeConsentResponse.getApprovedScopes().size(), approvedScopes.size());
assertEquals(oAuth2ScopeConsentResponse.getDeniedScopes().size(), deniedScopes.size());
}
use of org.wso2.carbon.identity.oauth2.model.OAuth2ScopeConsentResponse in project identity-inbound-auth-oauth by wso2-extensions.
the class EndpointUtilTest method testGetUserConsentURL.
@Test(dataProvider = "provideDataForUserConsentURL")
public void testGetUserConsentURL(Object oAuth2ParamObject, boolean isOIDC, boolean cacheEntryExists, boolean throwError, String queryString, boolean isDebugEnabled) throws Exception {
setMockedLog(isDebugEnabled);
OAuth2Parameters parameters = (OAuth2Parameters) oAuth2ParamObject;
mockStatic(OAuthServerConfiguration.class);
when(OAuthServerConfiguration.getInstance()).thenReturn(mockedOAuthServerConfiguration);
EndpointUtil.setOauthServerConfiguration(mockedOAuthServerConfiguration);
when(mockedOAuthServerConfiguration.isDropUnregisteredScopes()).thenReturn(false);
EndpointUtil.setOAuth2ScopeService(oAuth2ScopeService);
when(oAuth2ScopeService.getUserConsentForApp(anyString(), anyString(), anyInt())).thenReturn(oAuth2ScopeConsentResponse);
mockStatic(OAuth2Util.class);
mockStatic(OAuth2Util.OAuthURL.class);
when(OAuth2Util.OAuthURL.getOIDCConsentPageUrl()).thenReturn(OIDC_CONSENT_PAGE_URL);
when(OAuth2Util.OAuthURL.getOAuth2ConsentPageUrl()).thenReturn(OAUTH2_CONSENT_PAGE_URL);
mockStatic(IdentityTenantUtil.class);
when(IdentityTenantUtil.getTenantId(anyString())).thenReturn(MultitenantConstants.SUPER_TENANT_ID);
mockStatic(FrameworkUtils.class);
when(FrameworkUtils.resolveUserIdFromUsername(anyInt(), anyString(), anyString())).thenReturn("sample");
when(FrameworkUtils.getRedirectURLWithFilteredParams(anyString(), anyMap())).then(i -> i.getArgumentAt(0, String.class));
mockStatic(OAuth2Util.class);
spy(EndpointUtil.class);
doReturn("sampleId").when(EndpointUtil.class, "getAppIdFromClientId", anyString());
mockStatic(SessionDataCache.class);
when(SessionDataCache.getInstance()).thenReturn(mockedSessionDataCache);
if (cacheEntryExists) {
when(mockedSessionDataCache.getValueFromCache(any(SessionDataCacheKey.class))).thenReturn(mockedSessionDataCacheEntry);
when(mockedSessionDataCacheEntry.getQueryString()).thenReturn(queryString);
when(mockedSessionDataCacheEntry.getLoggedInUser()).thenReturn(user);
when(mockedSessionDataCacheEntry.getEndpointParams()).thenReturn(new HashMap<>());
} else {
when(mockedSessionDataCache.getValueFromCache(any(SessionDataCacheKey.class))).thenReturn(null);
}
EndpointUtil.setOAuthAdminService(mockedOAuthAdminService);
when(mockedOAuthAdminService.getScopeNames()).thenReturn(new String[0]);
JDBCPermissionBasedInternalScopeValidator scopeValidatorSpy = PowerMockito.spy(new JDBCPermissionBasedInternalScopeValidator());
doNothing().when(scopeValidatorSpy, method(JDBCPermissionBasedInternalScopeValidator.class, "endTenantFlow")).withNoArguments();
when(scopeValidatorSpy, method(JDBCPermissionBasedInternalScopeValidator.class, "getUserAllowedScopes", AuthenticatedUser.class, String[].class, String.class)).withArguments(any(AuthenticatedUser.class), any(), anyString()).thenReturn(getScopeList());
PowerMockito.whenNew(JDBCPermissionBasedInternalScopeValidator.class).withNoArguments().thenReturn(scopeValidatorSpy);
String consentUrl;
try {
consentUrl = EndpointUtil.getUserConsentURL(parameters, username, sessionDataKey, isOIDC);
if (isOIDC) {
Assert.assertTrue(consentUrl.contains(OIDC_CONSENT_PAGE_URL), "Incorrect consent page url for OIDC");
} else {
Assert.assertTrue(consentUrl.contains(OAUTH2_CONSENT_PAGE_URL), "Incorrect consent page url for OAuth");
}
Assert.assertTrue(consentUrl.contains(URLEncoder.encode(username, "UTF-8")), "loggedInUser parameter value is not found in url");
Assert.assertTrue(consentUrl.contains(URLEncoder.encode("TestApplication", "ISO-8859-1")), "application parameter value is not found in url");
List<NameValuePair> nameValuePairList = URLEncodedUtils.parse(consentUrl, StandardCharsets.UTF_8);
Optional<NameValuePair> optionalScope = nameValuePairList.stream().filter(nameValuePair -> nameValuePair.getName().equals("scope")).findAny();
Assert.assertTrue(optionalScope.isPresent());
NameValuePair scopeNameValuePair = optionalScope.get();
String[] scopeArray = scopeNameValuePair.getValue().split(" ");
Assert.assertTrue(ArrayUtils.contains(scopeArray, "scope2"), "scope parameter value " + "is not found in url");
Assert.assertTrue(ArrayUtils.contains(scopeArray, "internal_login"), "internal_login " + "scope parameter value is not found in url");
Assert.assertFalse(ArrayUtils.contains(scopeArray, "SYSTEM"), "SYSTEM scope" + "parameter should not contain in the url.");
if (queryString != null && cacheEntryExists) {
Assert.assertTrue(consentUrl.contains(queryString), "spQueryParams value is not found in url");
}
} catch (OAuthSystemException e) {
Assert.assertTrue(e.getMessage().contains("Error while retrieving the application name"));
}
}
Aggregations