8000 Allow using the parameter in the consent text for dynamic scopes by rmartinc · Pull Request #48843 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2042,7 +2042,7 @@ requirements.ALTERNATIVE=Alternative
claimFilterValueHelp=Value of the essential claim (with regex support)
credentialResetConfirm=Send Email
permissionsEnabledHelp=Determines if fine grained permissions are enabled for managing this role. Disabling deletes all current permissions.
consentScreenTextHelp=Text that will be shown on the consent screen when this client scope is added to some client with consent required. Defaults to name of client scope if it is not filled.
consentScreenTextHelp=Text that will be shown on the consent screen when this client scope is added to some client with consent required. The placeholder 0 can be used to reference the parameter if the scope is dynamic (between curly brackets). Defaults to name of client scope if it is not filled.
Comment thread
mabartos marked this conversation as resolved.
Resolved
realmRolesList=Realm roles
roleList=Role list
kerberosRealm=Kerberos realm
Expand Down
3 changes: 1 addition & 2 deletions js/apps/admin-ui/src/client-scopes/details/ScopeForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,7 @@ export const ScopeForm = ({ clientScope, save }: ScopeFormProps) => {
rules={{
required: t("required"),
onChange: (e) => {
if (isDynamicScopesEnabled)
setDynamicRegex(e.target.validated, true);
if (isDynamicScopesEnabled) setDynamicRegex(e.target.value, true);
Comment thread
mabartos marked this conversation as resolved.
Resolved
},
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ default String getConsentScreenText() {
String consentScreenText = getAttribute(CONSENT_SCREEN_TEXT);
if (ObjectUtil.isBlank(consentScreenText)) {
consentScreenText = getName();
if (isDynamicScope()) {
consentScreenText += ": {0}";
}
}
return consentScreenText;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public Object exec(List list) throws TemplateModelException {
if (key.startsWith("${") && key.endsWith("}")) {
key = key.substring(2, key.length() - 1);
return new MessageFormat(messages.getProperty(key, key), locale).format(list.subList(1, list.size()).toArray());
} else if (list.size() > 1) {
return new MessageFormat(key, locale).format(list.subList(1, list.size()).toArray());
} else {
return key;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.keycloak.testframework.realm;

import java.util.HashMap;

import org.keycloak.representations.idm.ClientScopeRepresentation;

/**
Expand Down Expand Up @@ -49,4 +51,10 @@ public ClientScopeBuilder protocol(String protocol) {
rep.setProtocol(protocol);
return this;
}

public ClientScopeBuilder attribute(String key, String value) {
rep.setAttributes(Builder.createIfNull(rep.getAttributes(), HashMap::new));
rep.getAttributes().put(key, value);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.keycloak.representations.idm.ClientPolicyRepresentation;
import org.keycloak.representations.idm.ClientProfileRepresentation;
import org.keycloak.representations.idm.ClientProfilesRepresentation;
import org.keycloak.representations.idm.ClientScopeRepresentation;
import org.keycloak.representations.idm.ComponentRepresentation;
import org.keycloak.representations.idm.IdentityProviderRepresentation;
import org.keycloak.representations.idm.RealmRepresentation;
Expand Down Expand Up @@ -163,6 +164,16 @@ public void updateIdentityProvider(String alias, IdentityProviderUpdate update)
cleanup().add(r -> r.identityProviders().get(alias).update(original));
}

public void updateClientScope(String id, ClientScopeUpdate update) {
ClientScopeRepresentation original = realmResource.clientScopes().get(id).toRepresentation();

ClientScopeBuilder updatedRep = ClientScopeBuilder.update(RepresentationUtils.clone(original));
ClientScopeRepresentation updated = update.update(updatedRep).build();
realmResource.clientScopes().get(id).update(updated);

cleanup().add(r -> r.clientScopes().get(id).update(original));
}

public void updateClientProfile(List<ClientProfileRepresentation> profiles) {
ClientProfilesRepresentation oldProfiles = realmResource.clientPoliciesProfilesResource().getProfiles(true);
ClientProfilesRepresentation profilesToUpdate = realmResource.clientPoliciesProfilesResource().getProfiles(true);
Expand Down Expand Up @@ -217,6 +228,12 @@ public interface RealmUpdate {

}

public interface ClientScopeUpdate {

ClientScopeBuilder update(ClientScopeBuilder scope);

}

public interface IdentityProviderUpdate {

void update(IdentityProviderRepresentation rep);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
package org.keycloak.tests.oauth;

import java.util.List;
import java.util.Map;

import org.keycloak.common.Profile;
import org.keycloak.events.Details;
import org.keycloak.events.EventType;
import org.keycloak.models.ClientScopeModel;
import org.keycloak.protocol.oidc.OIDCLoginProtocol;
import org.keycloak.representations.idm.ClientScopeRepresentation;
import org.keycloak.representations.idm.EventRepresentation;
import org.keycloak.testframework.annotations.InjectClient;
import org.keycloak.testframework.annotations.InjectEvents;
import org.keycloak.testframework.annotations.InjectRealm;
import org.keycloak.testframework.annotations.KeycloakIntegrationTest;
import org.keycloak.testframework.annotations.TestSetup;
import org.keycloak.testframework.events.EventAssertion;
import org.keycloak.testframework.events.Events;
import org.keycloak.testframework.oauth.OAuthClient;
import org.keyclo 8 ak.testf 10BC8 ramework.oauth.annotations.InjectOAuthClient;
import org.keycloak.testframework.realm.ClientBuilder;
import org.keycloak.testframework.realm.ClientConfig;
import org.keycloak.testframework.realm.ClientScopeBuilder;
import org.keycloak.testframework.realm.ManagedClient;
import org.keycloak.testframework.realm.ManagedRealm;
import org.keycloak.testframework.realm.RealmBuilder;
import org.keycloak.testframework.realm.RealmConfig;
import org.keycloak.testframework.realm.UserBuilder;
import org.keycloak.testframework.server.KeycloakServerConfig;
import org.keycloak.testframework.server.KeycloakServerConfigBuilder;
import org.keycloak.testframework.ui.annotations.InjectPage;
import org.keycloak.testframework.ui.page.OAuthGrantPage;
import org.keycloak.testframework.util.ApiUtil;
import org.keycloak.testsuite.util.AccountHelper;
import org.keycloak.testsuite.util.oauth.AccessTokenResponse;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
*
* @author rmartinc
*/
@KeycloakIntegrationTest(config = DynamicScopesOAuthGrantTest.DynamicScopesServerConfig.class)
public class DynamicScopesOAuthGrantTest {

private static final String THIRD_PARTY_APP = "third-party";
private static final String DEFAULT_USERNAME = "test-user@localhost";
private static final String DEFAULT_PASSWORD = "password";

private static String DYNAMIC_SCOPE_ID;

@InjectRealm(config = DynamicScopesRealmConfig.class)
ManagedRealm realm;

@InjectClient(config = ThirdPartyClient.class)
ManagedClient thirdParty;

@InjectOAuthClient
OAuthClient oauth;

@InjectEvents
protected Events events;

@InjectPage
protected OAuthGrantPage grantPage;

@TestSetup
public void configureTestRealm() {
ClientScopeRepresentation dynamicScope = ClientScopeBuilder.create()
.name("foo-dynamic-scope")
.protocol(OIDCLoginProtocol.LOGIN_PROTOCOL)
.attribute(ClientScopeModel.IS_DYNAMIC_SCOPE, Boolean.TRUE.toString())
.attribute(ClientScopeModel.DYNAMIC_SCOPE_REGEXP, "foo-dynamic-scope:*")
.build();
DYNAMIC_SCOPE_ID = ApiUtil.getCreatedId(realm.admin().clientScopes().create(dynamicScope));
thirdParty.admin().addOptionalClientScope(DYNAMIC_SCOPE_ID);
}

@BeforeEach
public void beforeEach() {
// logout user and revoke consents if present
AccountHelper.logout(realm.admin(), DEFAULT_USERNAME);
List<Map<String, Object>> userConsents = AccountHelper.getUserConsents(realm.admin(), DEFAULT_USERNAME);
if (userConsents.stream().anyMatch(m -> THIRD_PARTY_APP.equals(m.get("clientId")))) {
AccountHelper.revokeConsents(realm.admin(), DEFAULT_USERNAME, THIRD_PARTY_APP);
}
}

@Test
public void oauthGrantDynamicScopeParamRequired() {
realm.updateClientScope(DYNAMIC_SCOPE_ID, s -> s.attribute(ClientScopeModel.CONSENT_SCREEN_TEXT, ""));

// login using the dynamic scope
oauth.client(THIRD_PARTY_APP, "password");
oauth.scope("foo-dynamic-scope:withparam");
oauth.openLoginForm();
oauth.fillLoginForm(DEFAULT_USERNAME, DEFAULT_PASSWORD);
grantPage.assertCurrent();
List<String> grants = grantPage.getDisplayedGrants();
Assertions.assertTrue(grants.contains("foo-dynamic-scope: withparam"));
grantPage.accept();

EventRepresentation loginEvent = events.poll();
EventAssertion.assertSuccess(loginEvent).type(EventType.LOGIN)
.clientId(THIRD_PARTY_APP)
.details(Details.REDIRECT_URI, oauth.getRedirectUri())
.details(Details.CONSENT, Details.CONSENT_VALUE_CONSENT_GRANTED);

String code = oauth.parseLoginResponse().getCode();
AccessTokenResponse res = oauth.doAccessTokenRequest(code);

EventAssertion.assertSuccess(events.poll()).type(EventType.CODE_TO_TOKEN)
.clientId(THIRD_PARTY_APP)
.sessionId(loginEvent.getSessionId())
.details(Details.CODE_ID, loginEvent.getDetails().get(Details.CODE_ID));

oauth.logoutForm().idTokenHint(res.getIdToken()).open();

EventAssertion.assertSuccess(events.poll()).type(EventType.LOGOUT)
.sessionId(loginEvent.getSessionId())
.clientId(THIRD_PARTY_APP)
.withoutDetails(Details.REDIRECT_URI);

// login again to check whether the Dynamic scope and only the dynamic scope is requested again
oauth.scope("foo-dynamic-scope:withparam");
oauth.openLoginForm();
oauth.fillLoginForm(DEFAULT_USERNAME, DEFAULT_PASSWORD);
grantPage.assertCurrent();
grants = grantPage.getDisplayedGrants();
Assertions.assertEquals(1, grants.size());
Assertions.assertTrue(grants.contains("foo-dynamic-scope: withparam"));
grantPage.accept();

EventAssertion.expectLoginSuccess(events.poll())
.clientId(THIRD_PARTY_APP)
.details(Details.REDIRECT_URI, oauth.getRedirectUri())
.details(Details.CONSENT, Details.CONSENT_VALUE_CONSENT_GRANTED);
}

@Test
public void oauthGrantDynamicScopeParamRequiredWithConsentText() {
realm.updateClientScope(DYNAMIC_SCOPE_ID, s -> s.attribute(
ClientScopeModel.CONSENT_SCREEN_TEXT, "Dynamic scope with parameter {0}"));

oauth.client(THIRD_PARTY_APP, "password");
oauth.scope("foo-dynamic-scope:one");
oauth.openLoginForm();
oauth.fillLoginForm(DEFAULT_USERNAME, DEFAULT_PASSWORD);
grantPage.assertCurrent();
List<String> grants = grantPage.getDisplayedGrants();
Assertions.assertTrue(grants.contains("Dynamic scope with parameter one"));
grantPage.accept();

EventRepresentation loginEvent = events.poll();
EventAssertion.assertSuccess(loginEvent).type(EventType.LOGIN)
.clientId(THIRD_PARTY_APP)
.details(Details.REDIRECT_URI, oauth.getRedirectUri())
.details(Details.CONSENT, Details.CONSENT_VALUE_CONSENT_GRANTED);
}

@Test
public void oauthGrantDynamicScopeParamRequiredWithConsentTextKey() {
realm.admin().localization().saveRealmLocalizationText("en", "dynamicConsentText", "Dynamic scope with parameter {0}");
realm.updateClientScope(DYNAMIC_SCOPE_ID, s -> s.attribute(
ClientScopeModel.CONSENT_SCREEN_TEXT, "${dynamicConsentText}"));

oauth.client(THIRD_PARTY_APP, "password");
oauth.scope("foo-dynamic-scope:two");
oauth.openLoginForm();
oauth.fillLoginForm(DEFAULT_USERNAME, DEFAULT_PASSWORD);
grantPage.assertCurrent();
List<String> grants = grantPage.getDisplayedGrants();
Assertions.assertTrue(grants.contains("Dynamic scope with parameter two"));
grantPage.accept();

EventRepresentation loginEvent = events.poll();
EventAssertion.assertSuccess(loginEvent).type(EventType.LOGIN)
.clientId(THIRD_PARTY_APP)
.details(Details.REDIRECT_URI, oauth.getRedirectUri())
.details(Details.CONSENT, Details.CONSENT_VALUE_CONSENT_GRANTED);

realm.admin().localization().deleteRealmLocalizationText("en", "dynamicConsentText");
}

public static class DynamicScopesServerConfig implements KeycloakServerConfig {

@Override
public KeycloakServerConfigBuilder configure(KeycloakServerConfigBuilder config) {
return config.features(Profile.Feature.DYNAMIC_SCOPES);
}
}

public static class DynamicScopesRealmConfig implements RealmConfig {
@Override
public RealmBuilder configure(RealmBuilder realm) {
realm.users(UserBuilder.create(DEFAULT_USERNAME)
.email(DEFAULT_USERNAME)
.name("Test", "User")
.emailVerified(true)
.password(DEFAULT_PASSWORD)
.enabled(true));
return realm;
}
}

public static class ThirdPartyClient implements ClientConfig {

@Override
public ClientBuilder configure(ClientBuilder client) {
return client.clientId(THIRD_PARTY_APP)
.protocol(OIDCLoginProtocol.LOGIN_PROTOCOL)
.consentRequired(Boolean.TRUE)
.redirectUris("*")
.secret("password");
}
}
}
Loading
Loading
0