8000 [OID4VCI] Add CredentialOfferProvider as single entry for offer creation - Part5 by tdiesler · Pull Request #46951 · 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
@@ -0,0 +1,36 @@
/*
* Copyright 2026 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.keycloak.protocol.oid4vc.issuance;

public class CredentialOfferException extends RuntimeException {

private final String errorType;

public CredentialOfferException(String errorType, String message) {
this(errorType, message, null);
}

public CredentialOfferException(String errorType, String message, Throwable cause) {
super(message, cause);
this.errorType = errorType;

}

public String getErrorType() {
return errorType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
import static org.keycloak.OID4VCConstants.OPENID_CREDENTIAL;
import static org.keycloak.protocol.oid4vc.issuance.OID4VCIssuerEndpoint.CREDENTIALS_OFFER_ID_ATTR;
import static org.keycloak.protocol.oid4vc.utils.CredentialScopeModelUtils.findCredentialScopeModelByConfigurationId;
import static org.keycloak.protocol.oid4vc.utils.OID4VCAuthorizationDetailUtils.buildOID4VCAuthorizationDetail;
import static org.keycloak.protocol.oid4vc.utils.CredentialScopeModelUtils.findCredentialScopeModelByName;
import static org.keycloak.protocol.oidc.endpoints.AuthorizationEndpoint.LOGIN_SESSION_NOTE_ADDITIONAL_REQ_PARAMS_PREFIX;

public class OID4VCAuthorizationDetailsProcessor implements AuthorizationDetailsProcessor<OID4VCAuthorizationDetail> {
Expand Down Expand Up @@ -214,8 +214,8 @@ private OID4VCAuthorizationDetail buildAuthorizationDetail(ClientSessionContext
//
CredentialOfferState offerState = getCredentialOfferState(clientSessionCtx);
if (offerState != null) {
OID4VCAuthorizationDetail offeredAuthDetail = offerState.getAuthorizationDetails();
if (!offeredAuthDetail.getCredentialConfigurationId().equals(requestedCredentialConfigurationId)) {
OID4VCAuthorizationDetail offeredAuthDetail = offerState.getAuthorizationDetails(requestedCredentialConfigurationId);
if (offeredAuthDetail == null) {
throw getInvalidRequestException("Unauthorized credential_configuration_id: " + requestedCredentialConfigurationId);
}
OID4VCAuthorizationDetail responseAuthDetail = offeredAuthDetail.clone();
Expand All @@ -230,7 +230,7 @@ private OID4VCAuthorizationDetail buildAuthorizationDetail(ClientSessionContext
if (credScope == null)
throw getInvalidRequestException("Cannot find or access client scope for credential_configuration_id: " + requestedCredentialConfigurationId);

OID4VCAuthorizationDetail responseAuthDetail = buildOID4VCAuthorizationDetail(credScope, offerState);
OID4VCAuthorizationDetail responseAuthDetail = CredentialScopeModelUtils.buildOID4VCAuthorizationDetail(credScope, null);
responseAuthDetail.setClaims(requestAuthDetail.getClaims());

return responseAuthDetail;
Expand All @@ -244,8 +244,7 @@ public List<OID4VCAuthorizationDetail> handleMissingAuthorizationDetails(UserSes
// Works for pre-auth and auth-code grants
CredentialOfferState offerState = getCredentialOfferState(clientSessionCtx);
if (offerState != null) {
OID4VCAuthorizationDetail authDetail = offerState.getAuthorizationDetails();
return List.of(authDetail);
return offerState.getAuthorizationDetails();
}

// AccessToken request with no credential offer and no auth details
Expand All @@ -256,12 +255,18 @@ public List<OID4VCAuthorizationDetail> handleMissingAuthorizationDetails(UserSes
}

List<OID4VCAuthorizationDetail> authorizationDetails = new ArrayList<>();

for (String scope : scopeParam.split(" ")) {
Optional.ofNullable(CredentialScopeModelUtils.findCredentialScopeModelByName(realmModel, clientSessionCtx::getClientScopesStream, scope))
.map(csm -> buildOID4VCAuthorizationDetail(csm, offerState))
.ifPresent(authorizationDetails::add);
CredentialScopeModel credScope = findCredentialScopeModelByName(realmModel, clientSessionCtx::getClientScopesStream, scope);
if (credScope != null) {

// Generate `authorization_details` for the AccessToken Response
// This is the same logic as we use when a credential offer is created
//
OID4VCAuthorizationDetail authDetail = CredentialScopeModelUtils.buildOID4VCAuthorizationDetail(credScope, null);
authorizationDetails.add(authDetail);
}
}

if (authorizationDetails.isEmpty()) {
logger.debug("No generated authorization_details");
}
Expand Down Expand Up @@ -312,7 +317,7 @@ private CredentialOfferState getCredentialOfferState(ClientSessionContext client
if (credOfferId != null) {
String auxCredOfferId = credOfferId;
CredentialOfferStorage offerStorage = session.getProvider(CredentialOfferStorage.class);
offerState = Optional.ofNullable(offerStorage.getOfferStateById(session, credOfferId))
offerState = Optional.ofNullable(offerStorage.getOfferStateById(credOfferId))
.orElseThrow(() -> new IllegalStateException("No credential offer state for: " + auxCredOfferId));
}

Expand Down
Loading
Loading
0