From 3a426b544f5cfd38f7ec41c4619e30ab9eac51e7 Mon Sep 17 00:00:00 2001 From: "A.G.J. Cate" Date: Mon, 13 Jan 2025 09:15:08 +0100 Subject: [PATCH] chore: use authorization_code for acquiring the access token --- packages/client/lib/AuthorizationCodeClient.ts | 7 +++---- packages/client/lib/OpenID4VCIClient.ts | 10 +++++++--- packages/client/lib/OpenID4VCIClientV1_0_11.ts | 4 ++-- packages/client/lib/OpenID4VCIClientV1_0_13.ts | 4 ++-- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/packages/client/lib/AuthorizationCodeClient.ts b/packages/client/lib/AuthorizationCodeClient.ts index 0404ea18..5336d165 100644 --- a/packages/client/lib/AuthorizationCodeClient.ts +++ b/packages/client/lib/AuthorizationCodeClient.ts @@ -1,6 +1,5 @@ import { AuthorizationChallengeCodeResponse, - AuthorizationChallengeErrorResponse, AuthorizationChallengeRequestOpts, AuthorizationDetails, AuthorizationRequestOpts, @@ -278,13 +277,13 @@ const handleLocations = (endpointMetadata: EndpointMetadataResultV1_0_13, author return authorizationDetails; }; -export const acquireAuthorizationChallengeAuthCode = async (opts: AuthorizationChallengeRequestOpts): Promise> => { +export const acquireAuthorizationChallengeAuthCode = async (opts: AuthorizationChallengeRequestOpts): Promise> => { //AuthorizationChallengeErrorResponse return await acquireAuthorizationChallengeAuthCodeUsingRequest({ authorizationChallengeRequest: await createAuthorizationChallengeRequest(opts) }); } -export const acquireAuthorizationChallengeAuthCodeUsingRequest = async (opts: { authorizationChallengeRequest: CommonAuthorizationChallengeRequest }): Promise> => { +export const acquireAuthorizationChallengeAuthCodeUsingRequest = async (opts: { authorizationChallengeRequest: CommonAuthorizationChallengeRequest }): Promise> => { //AuthorizationChallengeErrorResponse const { authorizationChallengeRequest } = opts // TODO validate request const authorizationChallengeCodeUrl = '' // TODO @@ -326,7 +325,7 @@ export const sendAuthorizationChallengeRequest = async ( authorizationChallengeCodeUrl: string, authorizationChallengeRequest: CommonAuthorizationChallengeRequest, opts?: { headers?: Record } -): Promise> => { +): Promise> => { //AuthorizationChallengeErrorResponse return await formPost(authorizationChallengeCodeUrl, convertJsonToURI(authorizationChallengeRequest, { mode: JsonURIMode.X_FORM_WWW_URLENCODED }), { // TODO check encoding customHeaders: opts?.headers ? opts.headers : undefined, }); diff --git a/packages/client/lib/OpenID4VCIClient.ts b/packages/client/lib/OpenID4VCIClient.ts index b2bba446..dde5b34e 100644 --- a/packages/client/lib/OpenID4VCIClient.ts +++ b/packages/client/lib/OpenID4VCIClient.ts @@ -4,7 +4,6 @@ import { AccessTokenResponse, Alg, AuthorizationChallengeCodeResponse, - AuthorizationChallengeErrorResponse, AuthorizationChallengeRequestOpts, AuthorizationRequestOpts, AuthorizationResponse, @@ -277,11 +276,16 @@ export class OpenID4VCIClient { this._state.pkce = generateMissingPKCEOpts({ ...this._state.pkce, ...pkce }); } - public async acquireAuthorizationChallengeCode(opts?: AuthorizationChallengeRequestOpts): Promise> { + public async acquireAuthorizationChallengeCode(opts?: AuthorizationChallengeRequestOpts): Promise> { //AuthorizationChallengeErrorResponse const response = await acquireAuthorizationChallengeAuthCode({ clientId: this._state.clientId ?? this._state.authorizationRequestOpts?.clientId, ...opts }) + + if (!this._state.authorizationCodeResponse) { + this._state.authorizationCodeResponse = response.successBody; + } + return response } @@ -299,7 +303,7 @@ export class OpenID4VCIClient { } else if (opts?.code) { this._state.authorizationCodeResponse = { code: opts.code }; } - const code = this._state.authorizationCodeResponse?.code; + const code = (this._state.authorizationCodeResponse as AuthorizationResponse)?.code ?? (this._state.authorizationCodeResponse as AuthorizationChallengeCodeResponse)?.authorization_code; if (opts?.codeVerifier) { this._state.pkce.codeVerifier = opts.codeVerifier; diff --git a/packages/client/lib/OpenID4VCIClientV1_0_11.ts b/packages/client/lib/OpenID4VCIClientV1_0_11.ts index ca17f901..60a2f536 100644 --- a/packages/client/lib/OpenID4VCIClientV1_0_11.ts +++ b/packages/client/lib/OpenID4VCIClientV1_0_11.ts @@ -58,7 +58,7 @@ export interface OpenID4VCIClientStateV1_0_11 { accessTokenResponse?: AccessTokenResponse; dpopResponseParams?: DPoPResponseParams; authorizationRequestOpts?: AuthorizationRequestOpts; - authorizationCodeResponse?: AuthorizationResponse; + authorizationCodeResponse?: AuthorizationResponse | AuthorizationChallengeCodeResponse; pkce: PKCEOpts; accessToken?: string; authorizationURL?: string; @@ -283,7 +283,7 @@ export class OpenID4VCIClientV1_0_11 { } else if (opts?.code) { this._state.authorizationCodeResponse = { code: opts.code }; } - const code = this._state.authorizationCodeResponse?.code; + const code = (this._state.authorizationCodeResponse as AuthorizationResponse)?.code ?? (this._state.authorizationCodeResponse as AuthorizationChallengeCodeResponse)?.authorization_code; if (opts?.codeVerifier) { this._state.pkce.codeVerifier = opts.codeVerifier; diff --git a/packages/client/lib/OpenID4VCIClientV1_0_13.ts b/packages/client/lib/OpenID4VCIClientV1_0_13.ts index 926455f9..a230ed5c 100644 --- a/packages/client/lib/OpenID4VCIClientV1_0_13.ts +++ b/packages/client/lib/OpenID4VCIClientV1_0_13.ts @@ -61,7 +61,7 @@ export interface OpenID4VCIClientStateV1_0_13 { accessTokenResponse?: AccessTokenResponse; dpopResponseParams?: DPoPResponseParams; authorizationRequestOpts?: AuthorizationRequestOpts; - authorizationCodeResponse?: AuthorizationResponse; + authorizationCodeResponse?: AuthorizationResponse | AuthorizationChallengeCodeResponse; pkce: PKCEOpts; accessToken?: string; authorizationURL?: string; @@ -290,7 +290,7 @@ export class OpenID4VCIClientV1_0_13 { } else if (opts?.code) { this._state.authorizationCodeResponse = { code: opts.code }; } - const code = this._state.authorizationCodeResponse?.code; + const code = (this._state.authorizationCodeResponse as AuthorizationResponse)?.code ?? (this._state.authorizationCodeResponse as AuthorizationChallengeCodeResponse)?.authorization_code; if (opts?.codeVerifier) { this._state.pkce.codeVerifier = opts.codeVerifier;