Skip to content

Commit

Permalink
chore: small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Brummos committed Jan 15, 2025
1 parent e8dca63 commit 3e2c8d7
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 30 deletions.
8 changes: 4 additions & 4 deletions packages/client/lib/AuthorizationCodeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,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>> => {
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>> => {
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 @@ -325,8 +325,8 @@ export const sendAuthorizationChallengeRequest = async (
authorizationChallengeCodeUrl: string,
authorizationChallengeRequest: CommonAuthorizationChallengeRequest,
opts?: { headers?: Record<string, string> }
): Promise<OpenIDResponse<AuthorizationChallengeCodeResponse>> => { //AuthorizationChallengeErrorResponse
return await formPost(authorizationChallengeCodeUrl, convertJsonToURI(authorizationChallengeRequest, { mode: JsonURIMode.X_FORM_WWW_URLENCODED }), { // TODO check encoding
): Promise<OpenIDResponse<AuthorizationChallengeCodeResponse>> => {
return await formPost(authorizationChallengeCodeUrl, convertJsonToURI(authorizationChallengeRequest, { mode: JsonURIMode.X_FORM_WWW_URLENCODED }), {

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,
});
}
25 changes: 16 additions & 9 deletions packages/client/lib/OpenID4VCIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
AccessTokenRequestOpts,
AccessTokenResponse,
Alg,
AuthorizationChallengeCodeResponse, AuthorizationChallengeErrorResponse,
AuthorizationChallengeCodeResponse,
AuthorizationChallengeErrorResponse,
AuthorizationChallengeRequestOpts,
AuthorizationRequestOpts,
AuthorizationResponse,
Expand Down Expand Up @@ -94,7 +95,7 @@ export class OpenID4VCIClient {
endpointMetadata?: EndpointMetadataResult;
accessTokenResponse?: AccessTokenResponse;
authorizationRequestOpts?: AuthorizationRequestOpts;
authorizationCodeResponse?: AuthorizationResponse;
authorizationCodeResponse?: AuthorizationResponse | AuthorizationChallengeCodeResponse;
authorizationURL?: string;
}) {
const issuer = credentialIssuer ?? (credentialOffer ? getIssuerFromCredentialOfferPayload(credentialOffer.credential_offer) : undefined);
Expand Down Expand Up @@ -296,18 +297,14 @@ export class OpenID4VCIClient {
public async acquireAccessToken(
opts?: Omit<AccessTokenRequestOpts, 'credentialOffer' | 'credentialIssuer' | 'metadata' | 'additionalParams'> & {
clientId?: string;
authorizationResponse?: string | AuthorizationResponse | AuthorizationChallengeCodeResponse; // Pass in an auth response, either as URI/redirect, or object // TODO we need to add support for the authorization code from the auth challenge
authorizationResponse?: string | AuthorizationResponse | AuthorizationChallengeCodeResponse; // Pass in an auth response, either as URI/redirect, or object

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

View check run for this annotation

Codecov / codecov/patch

packages/client/lib/OpenID4VCIClient.ts#L300

Added line #L300 was not covered by tests
additionalRequestParams?: Record<string, any>;
},
): Promise<AccessTokenResponse & { params?: DPoPResponseParams }> {
const { pin, clientId = this._state.clientId ?? this._state.authorizationRequestOpts?.clientId } = opts ?? {};
let { redirectUri } = opts ?? {};
if (opts?.authorizationResponse) {
this._state.authorizationCodeResponse = { ...toAuthorizationResponsePayload(opts.authorizationResponse) };
} else if (opts?.code) {
this._state.authorizationCodeResponse = { code: opts.code };
}
const code = (this._state.authorizationCodeResponse as AuthorizationResponse)?.code ?? (this._state.authorizationCodeResponse as AuthorizationChallengeCodeResponse)?.authorization_code;

const code = this.getAuthorizationCode(opts?.authorizationResponse, opts?.code)

if (opts?.codeVerifier) {
this._state.pkce.codeVerifier = opts.codeVerifier;
Expand Down Expand Up @@ -759,4 +756,14 @@ export class OpenID4VCIClient {
authorizationRequestOpts.clientId = clientId;
return authorizationRequestOpts;
}

private getAuthorizationCode = (authorizationResponse?: string | AuthorizationResponse | AuthorizationChallengeCodeResponse, code?: string): string | undefined => {
if (authorizationResponse) {
this._state.authorizationCodeResponse = { ...toAuthorizationResponsePayload(authorizationResponse) };
} else if (code) {
this._state.authorizationCodeResponse = { code };
}

return (this._state.authorizationCodeResponse as AuthorizationResponse)?.code ?? (this._state.authorizationCodeResponse as AuthorizationChallengeCodeResponse)?.authorization_code;
}

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

View check run for this annotation

Codecov / codecov/patch

packages/client/lib/OpenID4VCIClient.ts#L768

Added line #L768 was not covered by tests
}
20 changes: 13 additions & 7 deletions packages/client/lib/OpenID4VCIClientV1_0_11.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class OpenID4VCIClientV1_0_11 {
endpointMetadata?: EndpointMetadataResultV1_0_11;
accessTokenResponse?: AccessTokenResponse;
authorizationRequestOpts?: AuthorizationRequestOpts;
authorizationCodeResponse?: AuthorizationResponse;
authorizationCodeResponse?: AuthorizationResponse | AuthorizationChallengeCodeResponse;
authorizationURL?: string;
}) {
const issuer = credentialIssuer ?? (credentialOffer ? getIssuerFromCredentialOfferPayload(credentialOffer.credential_offer) : undefined);
Expand Down Expand Up @@ -287,12 +287,8 @@ export class OpenID4VCIClientV1_0_11 {
): Promise<AccessTokenResponse & { params?: DPoPResponseParams }> {
const { pin, clientId = this._state.clientId ?? this._state.authorizationRequestOpts?.clientId } = opts ?? {};
let { redirectUri } = opts ?? {};
if (opts?.authorizationResponse) {
this._state.authorizationCodeResponse = { ...toAuthorizationResponsePayload(opts.authorizationResponse) };
} else if (opts?.code) {
this._state.authorizationCodeResponse = { code: opts.code };
}
const code = (this._state.authorizationCodeResponse as AuthorizationResponse)?.code ?? (this._state.authorizationCodeResponse as AuthorizationChallengeCodeResponse)?.authorization_code;

const code = this.getAuthorizationCode(opts?.authorizationResponse, opts?.code)

if (opts?.codeVerifier) {
this._state.pkce.codeVerifier = opts.codeVerifier;
Expand Down Expand Up @@ -694,4 +690,14 @@ export class OpenID4VCIClientV1_0_11 {
authorizationRequestOpts.clientId = clientId;
return authorizationRequestOpts;
}

private getAuthorizationCode = (authorizationResponse?: string | AuthorizationResponse | AuthorizationChallengeCodeResponse, code?: string): string | undefined => {
if (authorizationResponse) {
this._state.authorizationCodeResponse = { ...toAuthorizationResponsePayload(authorizationResponse) };

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

View check run for this annotation

Codecov / codecov/patch

packages/client/lib/OpenID4VCIClientV1_0_11.ts#L696

Added line #L696 was not covered by tests
} else if (code) {
this._state.authorizationCodeResponse = { code };

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

View check run for this annotation

Codecov / codecov/patch

packages/client/lib/OpenID4VCIClientV1_0_11.ts#L698

Added line #L698 was not covered by tests
}

return (this._state.authorizationCodeResponse as AuthorizationResponse)?.code ?? (this._state.authorizationCodeResponse as AuthorizationChallengeCodeResponse)?.authorization_code;
}
}
20 changes: 13 additions & 7 deletions packages/client/lib/OpenID4VCIClientV1_0_13.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class OpenID4VCIClientV1_0_13 {
endpointMetadata?: EndpointMetadataResultV1_0_13;
accessTokenResponse?: AccessTokenResponse;
authorizationRequestOpts?: AuthorizationRequestOpts;
authorizationCodeResponse?: AuthorizationResponse;
authorizationCodeResponse?: AuthorizationResponse | AuthorizationChallengeCodeResponse;
authorizationURL?: string;
}) {
const issuer = credentialIssuer ?? (credentialOffer ? getIssuerFromCredentialOfferPayload(credentialOffer.credential_offer) : undefined);
Expand Down Expand Up @@ -294,12 +294,8 @@ export class OpenID4VCIClientV1_0_13 {
): Promise<AccessTokenResponse & { params?: DPoPResponseParams }> {
const { pin, clientId = this._state.clientId ?? this._state.authorizationRequestOpts?.clientId } = opts ?? {};
let { redirectUri } = opts ?? {};
if (opts?.authorizationResponse) {
this._state.authorizationCodeResponse = { ...toAuthorizationResponsePayload(opts.authorizationResponse) };
} else if (opts?.code) {
this._state.authorizationCodeResponse = { code: opts.code };
}
const code = (this._state.authorizationCodeResponse as AuthorizationResponse)?.code ?? (this._state.authorizationCodeResponse as AuthorizationChallengeCodeResponse)?.authorization_code;

const code = this.getAuthorizationCode(opts?.authorizationResponse, opts?.code)

if (opts?.codeVerifier) {
this._state.pkce.codeVerifier = opts.codeVerifier;
Expand Down Expand Up @@ -797,4 +793,14 @@ export class OpenID4VCIClientV1_0_13 {
authorizationRequestOpts.clientId = clientId;
return authorizationRequestOpts;
}

private getAuthorizationCode = (authorizationResponse?: string | AuthorizationResponse | AuthorizationChallengeCodeResponse, code?: string): string | undefined => {
if (authorizationResponse) {
this._state.authorizationCodeResponse = { ...toAuthorizationResponsePayload(authorizationResponse) };

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

View check run for this annotation

Codecov / codecov/patch

packages/client/lib/OpenID4VCIClientV1_0_13.ts#L799

Added line #L799 was not covered by tests
} else if (code) {
this._state.authorizationCodeResponse = { code };
}

return (this._state.authorizationCodeResponse as AuthorizationResponse)?.code ?? (this._state.authorizationCodeResponse as AuthorizationChallengeCodeResponse)?.authorization_code;
}
}
4 changes: 3 additions & 1 deletion packages/issuer-rest/lib/OID4VCIServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ export interface IGetIssueStatusEndpointOpts extends ISingleEndpointOpts {
}

export interface IAuthorizationChallengeEndpointOpts extends ISingleEndpointOpts {
createAuthRequestUriEndpointPath?: string
verifyAuthResponseEndpointPath?: string
/**
* Callback used for creating the authorization request uri used for the RP.
* Added an optional state parameter so that when direct calls are used,
Expand All @@ -96,7 +98,7 @@ export interface IAuthorizationChallengeEndpointOpts extends ISingleEndpointOpts
* Callback used for verifying the status of the authorization response.
* This is checked by the issuer before issuing an authorization code.
*/
verifyAuthResponseCallback: (correlationId: string) => Promise<boolean>
verifyAuthResponseCallback: (presentationDefinitionId: string, correlationId: string) => Promise<boolean>
}

export interface IOID4VCIServerOpts extends HasEndpointOpts {
Expand Down
4 changes: 2 additions & 2 deletions packages/issuer-rest/lib/oid4vci-api-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export function authorizationChallengeEndpoint<DIDDoc extends object>(
return Promise.reject(authorizationChallengeErrorResponse)

Check warning on line 153 in packages/issuer-rest/lib/oid4vci-api-functions.ts

View check run for this annotation

Codecov / codecov/patch

packages/issuer-rest/lib/oid4vci-api-functions.ts#L153

Added line #L153 was not covered by tests
}

if (auth_session && presentation_during_issuance_session) {
if (auth_session && presentation_during_issuance_session && definition_id) {
const session = await issuer.credentialOfferSessions.get(auth_session)

Check warning on line 157 in packages/issuer-rest/lib/oid4vci-api-functions.ts

View check run for this annotation

Codecov / codecov/patch

packages/issuer-rest/lib/oid4vci-api-functions.ts#L157

Added line #L157 was not covered by tests
if (!session) {
const authorizationChallengeErrorResponse: AuthorizationChallengeErrorResponse = {

Check warning on line 159 in packages/issuer-rest/lib/oid4vci-api-functions.ts

View check run for this annotation

Codecov / codecov/patch

packages/issuer-rest/lib/oid4vci-api-functions.ts#L159

Added line #L159 was not covered by tests
Expand All @@ -162,7 +162,7 @@ export function authorizationChallengeEndpoint<DIDDoc extends object>(
return Promise.reject(authorizationChallengeErrorResponse)

Check warning on line 162 in packages/issuer-rest/lib/oid4vci-api-functions.ts

View check run for this annotation

Codecov / codecov/patch

packages/issuer-rest/lib/oid4vci-api-functions.ts#L162

Added line #L162 was not covered by tests
}

const verifiedResponse = await opts.verifyAuthResponseCallback(presentation_during_issuance_session)
const verifiedResponse = await opts.verifyAuthResponseCallback(definition_id, presentation_during_issuance_session)

Check warning on line 165 in packages/issuer-rest/lib/oid4vci-api-functions.ts

View check run for this annotation

Codecov / codecov/patch

packages/issuer-rest/lib/oid4vci-api-functions.ts#L165

Added line #L165 was not covered by tests
if (verifiedResponse) {
const authorizationCode = generateRandomString(16, 'base64url')
session.authorizationCode = authorizationCode
Expand Down

0 comments on commit 3e2c8d7

Please sign in to comment.