Skip to content

Commit

Permalink
chore: Prefer AUTHORIZATION_CODE over PRE_AUTHORIZED_CODE_FLOW
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderPostma committed Sep 6, 2024
1 parent 2f1fcee commit 0a6c8e3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
25 changes: 13 additions & 12 deletions packages/client/lib/AccessTokenClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,7 @@ export class AccessTokenClient {
const credentialIssuer = opts.credentialIssuer ?? credentialOfferRequest?.credential_offer?.credential_issuer ?? opts.metadata?.issuer;
await createJwtBearerClientAssertion(request, { ...opts, credentialIssuer });

if (credentialOfferRequest?.supportedFlows.includes(AuthzFlowType.PRE_AUTHORIZED_CODE_FLOW)) {
this.assertAlphanumericPin(opts.pinMetadata, pin);
request.user_pin = pin;
request.tx_code = pin;

request.grant_type = GrantTypes.PRE_AUTHORIZED_CODE;
// we actually know it is there because of the isPreAuthCode call
request[PRE_AUTH_CODE_LITERAL] = credentialOfferRequest?.credential_offer.grants?.[PRE_AUTH_GRANT_LITERAL]?.[PRE_AUTH_CODE_LITERAL];

return request as AccessTokenRequest;
}

// Prefer AUTHORIZATION_CODE over PRE_AUTHORIZED_CODE_FLOW
if (!credentialOfferRequest || credentialOfferRequest.supportedFlows.includes(AuthzFlowType.AUTHORIZATION_CODE_FLOW)) {
request.grant_type = GrantTypes.AUTHORIZATION_CODE;
request.code = code;
Expand All @@ -156,6 +145,18 @@ export class AccessTokenClient {
return request as AccessTokenRequest;
}

if (credentialOfferRequest?.supportedFlows.includes(AuthzFlowType.PRE_AUTHORIZED_CODE_FLOW)) {
this.assertAlphanumericPin(opts.pinMetadata, pin);
request.user_pin = pin;
request.tx_code = pin;

request.grant_type = GrantTypes.PRE_AUTHORIZED_CODE;
// we actually know it is there because of the isPreAuthCode call
request[PRE_AUTH_CODE_LITERAL] = credentialOfferRequest?.credential_offer.grants?.[PRE_AUTH_GRANT_LITERAL]?.[PRE_AUTH_CODE_LITERAL];

return request as AccessTokenRequest;
}

throw new Error('Credential offer request follows neither pre-authorized code nor authorization code flow requirements.');
}

Expand Down
22 changes: 11 additions & 11 deletions packages/client/lib/AccessTokenClientV1_0_11.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,7 @@ export class AccessTokenClientV1_0_11 {
}
await createJwtBearerClientAssertion(request, { ...opts, version: OpenId4VCIVersion.VER_1_0_11, credentialIssuer });

if (credentialOfferRequest?.supportedFlows.includes(AuthzFlowType.PRE_AUTHORIZED_CODE_FLOW)) {
this.assertNumericPin(this.isPinRequiredValue(credentialOfferRequest.credential_offer), pin);
request.user_pin = pin;

request.grant_type = GrantTypes.PRE_AUTHORIZED_CODE;
// we actually know it is there because of the isPreAuthCode call
request[PRE_AUTH_CODE_LITERAL] = credentialOfferRequest?.credential_offer.grants?.[PRE_AUTH_GRANT_LITERAL]?.[PRE_AUTH_CODE_LITERAL];

return request as AccessTokenRequest;
}

// Prefer AUTHORIZATION_CODE over PRE_AUTHORIZED_CODE_FLOW
if (!credentialOfferRequest || credentialOfferRequest.supportedFlows.includes(AuthzFlowType.AUTHORIZATION_CODE_FLOW)) {
request.grant_type = GrantTypes.AUTHORIZATION_CODE;
request.code = code;
Expand All @@ -160,6 +150,16 @@ export class AccessTokenClientV1_0_11 {
return request as AccessTokenRequest;
}

if (credentialOfferRequest?.supportedFlows.includes(AuthzFlowType.PRE_AUTHORIZED_CODE_FLOW)) {
this.assertNumericPin(this.isPinRequiredValue(credentialOfferRequest.credential_offer), pin);
request.user_pin = pin;

request.grant_type = GrantTypes.PRE_AUTHORIZED_CODE;
// we actually know it is there because of the isPreAuthCode call
request[PRE_AUTH_CODE_LITERAL] = credentialOfferRequest?.credential_offer.grants?.[PRE_AUTH_GRANT_LITERAL]?.[PRE_AUTH_CODE_LITERAL];

return request as AccessTokenRequest;
}
throw new Error('Credential offer request does not follow neither pre-authorized code nor authorization code flow requirements.');
}

Expand Down
1 change: 1 addition & 0 deletions packages/oid4vci-common/lib/types/Authorization.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ export interface AuthorizationRequestOpts {
redirectUri?: string;
scope?: string;
requestObjectOpts?: RequestObjectOpts;
holderPreferredAuthzFlowTypeOrder?: AuthzFlowType[]
}

export interface AuthorizationResponse {
Expand Down

0 comments on commit 0a6c8e3

Please sign in to comment.