Repository navigation
[OID4VCI] Add a client policy to require a credential offer #47286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5109bf3
[OID4VCI] Add a client policy to require a credential offer
tdiesler 126a49d
-- remove a few inconsistencies in the tests base
tdiesler a19893f
-- fix a few inconsistencies with API naming
tdiesler 80b4c45
-- address reviewer feedback
tdiesler 03c09f6
-- remove oid4vci-client-profile from pre-installed defaults
tdiesler 6fa77f0
-- fix merge conflicts
tdiesler 3a06a27
-- add AuthorizationEndpointCheckProvider abstraction
tdiesler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...ces/src/main/java/org/keycloak/protocol/oid4vc/clientpolicy/CredentialClientPolicies.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package org.keycloak.protocol.oid4vc.clientpolicy; | ||
|
|
||
| import org.keycloak.models.KeycloakSession; | ||
| import org.keycloak.models.RealmModel; | ||
| import org.keycloak.representations.idm.ClientPolicyRepresentation; | ||
| import org.keycloak.services.clientpolicy.ClientPolicyException; | ||
|
|
||
| public abstract class CredentialClientPolicies { | ||
|
|
||
| /** | ||
| * Governs whether the given `credential_configuration_id` requires a Credential Offer | ||
| */ | ||
| public static PredicateCredentialClientPolicy VC_POLICY_CREDENTIAL_OFFER_REQUIRED = new PredicateCredentialClientPolicy( | ||
| "oid4vci-offer-required", "vc.policy.offer.required", true, false); | ||
|
|
||
| public static ClientPolicyRepresentation findClientPolicyByName(KeycloakSession session, String policyName) { | ||
| try { | ||
| RealmModel realm = session.getContext().getRealm(); | ||
| return session.clientPolicy().getClientPolicies(realm, false).getPolicies().stream() | ||
| .filter(cp -> cp.getName().equals(policyName)) | ||
| .findFirst().orElse(null); | ||
| } catch (ClientPolicyException ex) { | ||
| throw new RuntimeException("Cannot access client policies", ex); | ||
| } | ||
| } | ||
| } |
43 changes: 43 additions & 0 deletions
43
services/src/main/java/org/keycloak/protocol/oid4vc/clientpolicy/CredentialClientPolicy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package org.keycloak.protocol.oid4vc.clientpolicy; | ||
|
|
||
| import org.keycloak.protocol.oid4vc.model.CredentialScopeRepresentation; | ||
|
|
||
|
|
||
| public abstract class CredentialClientPolicy<T> { | ||
|
|
||
| private final String name; | ||
| private final String attrName; | ||
| private final Class<T> type; | ||
| private final T expectedValue; | ||
| private final T defaultValue; | ||
|
|
||
| public CredentialClientPolicy(String name, String attrName, Class<T> type, T expectedValue, T defaultValue) { | ||
| this.name = name; | ||
| this.attrName = attrName; | ||
| this.expectedValue = expectedValue; | ||
| this.defaultValue = defaultValue; | ||
| this.type = type; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public String getAttrName() { | ||
| return attrName; | ||
| } | ||
|
|
||
| public Class<T> getType() { | ||
| return type; | ||
| } | ||
|
|
||
| public T getExpectedValue() { | ||
| return expectedValue; | ||
| } | ||
|
|
||
| public T getDefaultValue() { | ||
| return defaultValue; | ||
| } | ||
|
|
||
| public abstract T getCurrentValue(CredentialScopeRepresentation credScope); | ||
| } |
108 changes: 108 additions & 0 deletions
108
...c/main/java/org/keycloak/protocol/oid4vc/clientpolicy/CredentialClientPolicyExecutor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| package org.keycloak.protocol.oid4vc.clientpolicy; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| import org.keycloak.OAuthErrorException; | ||
| import org.keycloak.events.Errors; | ||
| import org.keycloak.models.ClientModel; | ||
| import org.keycloak.models.KeycloakSession; | ||
| import org.keycloak.models.oid4vci.CredentialScopeModel; | ||
| import org.keycloak.protocol.oid4vc.issuance.credentialoffer.CredentialOfferState; | ||
| import org.keycloak.protocol.oid4vc.issuance.credentialoffer.CredentialOfferStorage; | ||
| import org.keycloak.protocol.oid4vc.model.CredentialsOffer; | ||
| import org.keycloak.protocol.oid4vc.model.IssuerState; | ||
| import org.keycloak.protocol.oid4vc.utils.CredentialScopeUtils; | ||
| import org.keycloak.protocol.oidc.endpoints.request.AuthorizationEndpointRequest; | ||
| import org.keycloak.representations.idm.ClientPolicyExecutorConfigurationRepresentation; | ||
| import org.keycloak.services.clientpolicy.ClientPolicyContext; | ||
| import org.keycloak.services.clientpolicy.ClientPolicyException; | ||
| import org.keycloak.services.clientpolicy.context.AuthorizationRequestContext; | ||
| import org.keycloak.services.clientpolicy.executor.ClientPolicyExecutorProvider; | ||
|
|
||
| import static org.keycloak.OAuth2Constants.ISSUER_STATE; | ||
| import static org.keycloak.protocol.oid4vc.clientpolicy.CredentialClientPolicies.VC_POLICY_CREDENTIAL_OFFER_REQUIRED; | ||
| import static org.keycloak.services.clientpolicy.ClientPolicyEvent.AUTHORIZATION_REQUEST; | ||
|
|
||
| /** | ||
| * This client policy executor can be reference in a client profile definition like this, | ||
| * which we currently don't add to the defaults client profile definitions. | ||
| * | ||
| * { | ||
| * "name": "oid4vci-client-profile", | ||
| * "description": "Client profile, which enforces various policies on oid4vci clients.", | ||
| * "executors": [ | ||
| * { | ||
| * "executor": "oid4vci-policy-executor", | ||
| * "configuration": {} | ||
| * } | ||
| * ] | ||
| * } | ||
| */ | ||
| public class CredentialClientPolicyExecutor implements ClientPolicyExecutorProvider<ClientPolicyExecutorConfigurationRepresentation> { | ||
|
|
||
| protected final KeycloakSession session; | ||
|
|
||
| public CredentialClientPolicyExecutor(KeycloakSession session) { | ||
| this.session = session; | ||
| } | ||
|
|
||
| @Override | ||
| public String getProviderId() { | ||
| return CredentialClientPolicyExecutorFactory.PROVIDER_ID; | ||
| } | ||
|
|
||
| @Override | ||
| public void executeOnEvent(ClientPolicyContext context) throws ClientPolicyException { | ||
| if (AUTHORIZATION_REQUEST.equals(context.getEvent())) { | ||
| AuthorizationRequestContext authRequestContext = (AuthorizationRequestContext) context; | ||
| checkCredentialPolicies(authRequestContext); | ||
| } | ||
| } | ||
|
|
||
| private void checkCredentialPolicies(AuthorizationRequestContext context) throws ClientPolicyException { | ||
|
|
||
| ClientModel client = context.getClient(); | ||
| if (client == null) | ||
| throw new ClientPolicyException(OAuthErrorException.INVALID_CLIENT, "No issuing client"); | ||
|
|
||
| // Get the list of requested credential scopes that are associated with this client | ||
| // | ||
| AuthorizationEndpointRequest request = context.getAuthorizationEndpointRequest(); | ||
| List<CredentialScopeModel> credScopes = CredentialScopeUtils.getCredentialScopesForAuthorization(client, request); | ||
|
|
||
| // Proceed when there are requested credential scopes | ||
| // | ||
| if (!credScopes.isEmpty()) { | ||
|
|
||
| PredicateCredentialClientPolicy offerRequiredPolicy = VC_POLICY_CREDENTIAL_OFFER_REQUIRED; | ||
|
|
||
| // Get the potential offer state derived from issuer_state | ||
| // | ||
| String issuerStateParam = request.getAdditionalReqParams().get(ISSUER_STATE); | ||
| CredentialOfferStorage offerStorage = session.getProvider(CredentialOfferStorage.class); | ||
| CredentialOfferState offerState = Optional.ofNullable(issuerStateParam) | ||
| .map(IssuerState::fromEncodedString) | ||
| .map(IssuerState::getCredentialsOfferId) | ||
| .map(offerStorage::getOfferStateById) | ||
| .orElse(null); | ||
|
|
||
| // Get the offered credential configuration ids | ||
| // | ||
| List<String> offeredConfigurationIds = Optional.ofNullable(offerState) | ||
| .map(CredentialOfferState::getCredentialsOffer) | ||
| .map(CredentialsOffer::getCredentialConfigurationIds) | ||
| .orElse(List.of()); | ||
|
|
||
| // Check whether each requested credential_configuration_id has actually been offered | ||
| // | ||
| for (CredentialScopeModel credScope : credScopes) { | ||
| String credConfigId = credScope.getCredentialConfigurationId(); | ||
| if (!offeredConfigurationIds.contains(credConfigId)) { | ||
| String errorDetail = "Authorization request rejected by policy " + offerRequiredPolicy.getName() + " for client: " + client.getClientId(); | ||
| throw new ClientPolicyException(Errors.NOT_ALLOWED, errorDetail); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
54 changes: 54 additions & 0 deletions
54
...java/org/keycloak/protocol/oid4vc/clientpolicy/CredentialClientPolicyExecutorFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package org.keycloak.protocol.oid4vc.clientpolicy; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| import org.keycloak.Config; | ||
| import org.keycloak.common.Profile; | ||
| import org.keycloak.models.KeycloakSession; | ||
| import org.keycloak.models.KeycloakSessionFactory; | ||
| import org.keycloak.provider.ProviderConfigProperty; | ||
| import org.keycloak.services.clientpolicy.executor.ClientPolicyExecutorProviderFactory; | ||
|
|
||
| public class CredentialClientPolicyExecutorFactory implements ClientPolicyExecutorProviderFactory { | ||
|
|
||
| public static final String PROVIDER_ID = "oid4vci-policy-executor"; | ||
|
|
||
| @Override | ||
| public CredentialClientPolicyExecutor create(KeycloakSession session) { | ||
| return new CredentialClientPolicyExecutor(session); | ||
| } | ||
|
|
||
| @Override | ||
| public void init(Config.Scope config) { | ||
| } | ||
|
|
||
| @Override | ||
| public void postInit(KeycloakSessionFactory factory) { | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| } | ||
|
|
||
| @Override | ||
| public String getId() { | ||
| return PROVIDER_ID; | ||
| } | ||
|
|
||
| @Override | ||
| public String getHelpText() { | ||
| return "This executor checks client policies related to the credential offer process"; | ||
| } | ||
|
|
||
| @Override | ||
| public List<ProviderConfigProperty> getConfigProperties() { | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isSupported(Config.Scope config) { | ||
| return Profile.isFeatureEnabled(Profile.Feature.CLIENT_POLICIES) | ||
| && Profile.isFeatureEnabled(Profile.Feature.OID4VC_VCI); | ||
| } | ||
| } | ||
26 changes: 26 additions & 0 deletions
26
.../main/java/org/keycloak/protocol/oid4vc/clientpolicy/PredicateCredentialClientPolicy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package org.keycloak.protocol.oid4vc.clientpolicy; | ||
|
|
||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
|
|
||
| im 80F7 port org.keycloak.protocol.oid4vc.model.CredentialScopeRepresentation; | ||
|
|
||
| public class PredicateCredentialClientPolicy extends CredentialClientPolicy<Boolean> { | ||
|
|
||
| public PredicateCredentialClientPolicy(String name, String key, Boolean exp, Boolean def) { | ||
| super(name, key, Boolean.class, exp, def); | ||
| } | ||
|
|
||
| public Boolean getCurrentValue(CredentialScopeRepresentation credScope) { | ||
| Boolean scopeValue = Optional.ofNullable(credScope.getAttribute(getAttrName())) | ||
| .map(Boolean::parseBoolean) | ||
| .orElse(getDefaultValue()); | ||
| return scopeValue; | ||
| } | ||
|
|
||
| public Boolean validate(CredentialScopeRepresentation credScope) { | ||
| Boolean scopeValue = credScope.getCredentialPolicyValue(this); | ||
| return Objects.equals(scopeValue, getExpectedValue()); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.