Skip to content

Commit

Permalink
chore: use authorization_code for acquiring the access token
Browse files Browse the repository at this point in the history
  • Loading branch information
Brummos committed Jan 13, 2025
1 parent b97ff27 commit 3a426b5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
7 changes: 3 additions & 4 deletions packages/client/lib/AuthorizationCodeClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
AuthorizationChallengeCodeResponse,
AuthorizationChallengeErrorResponse,
AuthorizationChallengeRequestOpts,
AuthorizationDetails,
AuthorizationRequestOpts,
Expand Down Expand Up @@ -278,13 +277,13 @@ const handleLocations = (endpointMetadata: EndpointMetadataResultV1_0_13, author
return authorizationDetails;
};

export const acquireAuthorizationChallengeAuthCode = async (opts: AuthorizationChallengeRequestOpts): Promise<OpenIDResponse<AuthorizationChallengeCodeResponse | AuthorizationChallengeErrorResponse>> => {
export const acquireAuthorizationChallengeAuthCode = async (opts: AuthorizationChallengeRequestOpts): Promise<OpenIDResponse<AuthorizationChallengeCodeResponse>> => { //AuthorizationChallengeErrorResponse
return await acquireAuthorizationChallengeAuthCodeUsingRequest({

Check warning on line 281 in packages/client/lib/AuthorizationCodeClient.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/lib/AuthorizationCodeClient.ts#L281

Added line #L281 was not covered by tests
authorizationChallengeRequest: await createAuthorizationChallengeRequest(opts)
});
}

export const acquireAuthorizationChallengeAuthCodeUsingRequest = async (opts: { authorizationChallengeRequest: CommonAuthorizationChallengeRequest }): Promise<OpenIDResponse<AuthorizationChallengeCodeResponse | AuthorizationChallengeErrorResponse>> => {
export const acquireAuthorizationChallengeAuthCodeUsingRequest = async (opts: { authorizationChallengeRequest: CommonAuthorizationChallengeRequest }): Promise<OpenIDResponse<AuthorizationChallengeCodeResponse>> => { //AuthorizationChallengeErrorResponse
const { authorizationChallengeRequest } = opts

Check warning on line 287 in packages/client/lib/AuthorizationCodeClient.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/lib/AuthorizationCodeClient.ts#L287

Added line #L287 was not covered by tests
// TODO validate request
const authorizationChallengeCodeUrl = '' // TODO
Expand Down Expand Up @@ -326,7 +325,7 @@ export const sendAuthorizationChallengeRequest = async (
authorizationChallengeCodeUrl: string,
authorizationChallengeRequest: CommonAuthorizationChallengeRequest,
opts?: { headers?: Record<string, string> }
): Promise<OpenIDResponse<AuthorizationChallengeCodeResponse | AuthorizationChallengeErrorResponse>> => {
): Promise<OpenIDResponse<AuthorizationChallengeCodeResponse>> => { //AuthorizationChallengeErrorResponse
return await formPost(authorizationChallengeCodeUrl, convertJsonToURI(authorizationChallengeRequest, { mode: JsonURIMode.X_FORM_WWW_URLENCODED }), { // TODO check encoding

Check warning on line 329 in packages/client/lib/AuthorizationCodeClient.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/lib/AuthorizationCodeClient.ts#L328-L329

Added lines #L328 - L329 were not covered by tests
customHeaders: opts?.headers ? opts.headers : undefined,
});
Expand Down
10 changes: 7 additions & 3 deletions packages/client/lib/OpenID4VCIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
AccessTokenResponse,
Alg,
AuthorizationChallengeCodeResponse,
AuthorizationChallengeErrorResponse,
AuthorizationChallengeRequestOpts,
AuthorizationRequestOpts,
AuthorizationResponse,
Expand Down Expand Up @@ -277,11 +276,16 @@ export class OpenID4VCIClient {
this._state.pkce = generateMissingPKCEOpts({ ...this._state.pkce, ...pkce });
}

public async acquireAuthorizationChallengeCode(opts?: AuthorizationChallengeRequestOpts): Promise<OpenIDResponse<AuthorizationChallengeCodeResponse | AuthorizationChallengeErrorResponse>> {
public async acquireAuthorizationChallengeCode(opts?: AuthorizationChallengeRequestOpts): Promise<OpenIDResponse<AuthorizationChallengeCodeResponse>> { //AuthorizationChallengeErrorResponse
const response = await acquireAuthorizationChallengeAuthCode({

Check warning on line 280 in packages/client/lib/OpenID4VCIClient.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/lib/OpenID4VCIClient.ts#L279-L280

Added lines #L279 - L280 were not covered by tests
clientId: this._state.clientId ?? this._state.authorizationRequestOpts?.clientId,
...opts
})

if (!this._state.authorizationCodeResponse) {
this._state.authorizationCodeResponse = response.successBody;

Check warning on line 286 in packages/client/lib/OpenID4VCIClient.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/lib/OpenID4VCIClient.ts#L286

Added line #L286 was not covered by tests
}

return response

Check warning on line 289 in packages/client/lib/OpenID4VCIClient.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/lib/OpenID4VCIClient.ts#L289

Added line #L289 was not covered by tests
}

Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions packages/client/lib/OpenID4VCIClientV1_0_11.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions packages/client/lib/OpenID4VCIClientV1_0_13.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 3a426b5

Please sign in to comment.