Skip to content

Commit

Permalink
chore: refactor authorization challenge code error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Brummos committed Jan 13, 2025
1 parent 1825b96 commit e8dca63
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
16 changes: 10 additions & 6 deletions packages/client/lib/OpenID4VCIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
AccessTokenRequestOpts,
AccessTokenResponse,
Alg,
AuthorizationChallengeCodeResponse,
AuthorizationChallengeCodeResponse, AuthorizationChallengeErrorResponse,
AuthorizationChallengeRequestOpts,
AuthorizationRequestOpts,
AuthorizationResponse,
Expand Down Expand Up @@ -33,7 +33,6 @@ import {
NotificationResponseResult,
OID4VCICredentialFormat,
OpenId4VCIVersion,
OpenIDResponse,
PKCEOpts,
ProofOfPossessionCallbacks,
toAuthorizationResponsePayload
Expand Down Expand Up @@ -276,17 +275,22 @@ 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<AuthorizationChallengeCodeResponse> {
const response = await acquireAuthorizationChallengeAuthCode({

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

View check run for this annotation

Codecov / codecov/patch

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

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

if (!this._state.authorizationCodeResponse) {
this._state.authorizationCodeResponse = response.successBody;
if (response.errorBody) {
debug(`Authorization code error:\r\n${JSON.stringify(response.errorBody)}`);
const error = response.errorBody as AuthorizationChallengeErrorResponse
return Promise.reject(error)

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

View check run for this annotation

Codecov / codecov/patch

packages/client/lib/OpenID4VCIClient.ts#L285-L287

Added lines #L285 - L287 were not covered by tests
} else if (!response.successBody) {
debug(`Authorization code error. No success body`);

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
return Promise.reject(Error(`Retrieving an authorization code token from ${this._state.endpointMetadata?.authorization_challenge_endpoint} for issuer ${this.getIssuer()} failed as there was no success response body`))
}

return response
return { ...response.successBody }

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

View check run for this annotation

Codecov / codecov/patch

packages/client/lib/OpenID4VCIClient.ts#L293

Added line #L293 was not covered by tests
}

public async acquireAccessToken(
Expand Down
17 changes: 13 additions & 4 deletions packages/client/lib/OpenID4VCIClientV1_0_11.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
KID_JWK_X5C_ERROR,
OID4VCICredentialFormat,
OpenId4VCIVersion,
OpenIDResponse,
PKCEOpts,
ProofOfPossessionCallbacks,
toAuthorizationResponsePayload
Expand All @@ -37,13 +36,13 @@ import { CredentialFormat } from '@sphereon/ssi-types';
import Debug from 'debug';

import { AccessTokenClientV1_0_11 } from './AccessTokenClientV1_0_11';
import { acquireAuthorizationChallengeAuthCode } from './AuthorizationCodeClient'
import { createAuthorizationRequestUrlV1_0_11 } from './AuthorizationCodeClientV1_0_11';
import { CredentialOfferClientV1_0_11 } from './CredentialOfferClientV1_0_11';
import { CredentialRequestClientBuilderV1_0_11 } from './CredentialRequestClientBuilderV1_0_11';
import { MetadataClientV1_0_11 } from './MetadataClientV1_0_11';
import { ProofOfPossessionBuilder } from './ProofOfPossessionBuilder';
import { generateMissingPKCEOpts } from './functions';
import { acquireAuthorizationChallengeAuthCode } from './AuthorizationCodeClient'

const debug = Debug('sphereon:oid4vci');

Expand Down Expand Up @@ -261,12 +260,22 @@ export class OpenID4VCIClientV1_0_11 {
this._state.pkce = generateMissingPKCEOpts({ ...this._state.pkce, ...pkce });
}

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

Check warning on line 264 in packages/client/lib/OpenID4VCIClientV1_0_11.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/lib/OpenID4VCIClientV1_0_11.ts#L263-L264

Added lines #L263 - L264 were not covered by tests
clientId: this._state.clientId ?? this._state.authorizationRequestOpts?.clientId,
...opts
})
return response

if (response.errorBody) {
debug(`Authorization code error:\r\n${JSON.stringify(response.errorBody)}`);
const error = response.errorBody as AuthorizationChallengeErrorResponse
return Promise.reject(error)

Check warning on line 272 in packages/client/lib/OpenID4VCIClientV1_0_11.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/lib/OpenID4VCIClientV1_0_11.ts#L270-L272

Added lines #L270 - L272 were not covered by tests
} else if (!response.successBody) {
debug(`Authorization code error. No success body`);

Check warning on line 274 in packages/client/lib/OpenID4VCIClientV1_0_11.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/lib/OpenID4VCIClientV1_0_11.ts#L274

Added line #L274 was not covered by tests
return Promise.reject(Error(`Retrieving an authorization code token from ${this._state.endpointMetadata?.authorization_challenge_endpoint} for issuer ${this.getIssuer()} failed as there was no success response body`))
}

return { ...response.successBody }

Check warning on line 278 in packages/client/lib/OpenID4VCIClientV1_0_11.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/lib/OpenID4VCIClientV1_0_11.ts#L278

Added line #L278 was not covered by tests
}

public async acquireAccessToken(
Expand Down
15 changes: 12 additions & 3 deletions packages/client/lib/OpenID4VCIClientV1_0_13.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
NotificationResponseResult,
OID4VCICredentialFormat,
OpenId4VCIVersion,
OpenIDResponse,
PKCEOpts,
ProofOfPossessionCallbacks,
toAuthorizationResponsePayload
Expand Down Expand Up @@ -268,12 +267,22 @@ export class OpenID4VCIClientV1_0_13 {
this._state.pkce = generateMissingPKCEOpts({ ...this._state.pkce, ...pkce });
}

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

Check warning on line 271 in packages/client/lib/OpenID4VCIClientV1_0_13.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/lib/OpenID4VCIClientV1_0_13.ts#L270-L271

Added lines #L270 - L271 were not covered by tests
clientId: this._state.clientId ?? this._state.authorizationRequestOpts?.clientId,
...opts
})
return response

if (response.errorBody) {
debug(`Authorization code error:\r\n${JSON.stringify(response.errorBody)}`);
const error = response.errorBody as AuthorizationChallengeErrorResponse
return Promise.reject(error)

Check warning on line 279 in packages/client/lib/OpenID4VCIClientV1_0_13.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/lib/OpenID4VCIClientV1_0_13.ts#L277-L279

Added lines #L277 - L279 were not covered by tests
} else if (!response.successBody) {
debug(`Authorization code error. No success body`);

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

View check run for this annotation

Codecov / codecov/patch

packages/client/lib/OpenID4VCIClientV1_0_13.ts#L281

Added line #L281 was not covered by tests
return Promise.reject(Error(`Retrieving an authorization code token from ${this._state.endpointMetadata?.authorization_challenge_endpoint} for issuer ${this.getIssuer()} failed as there was no success response body`))
}

return { ...response.successBody }

Check warning on line 285 in packages/client/lib/OpenID4VCIClientV1_0_13.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/lib/OpenID4VCIClientV1_0_13.ts#L285

Added line #L285 was not covered by tests
}

public async acquireAccessToken(
Expand Down

0 comments on commit e8dca63

Please sign in to comment.