Skip to content

Commit

Permalink
chore: fix typ handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nklomp committed Jun 12, 2023
1 parent b048421 commit 8274708
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
36 changes: 23 additions & 13 deletions packages/client/lib/ProofOfPossessionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import { createProofOfPossession } from './functions';
export class ProofOfPossessionBuilder {
private readonly proof?: ProofOfPossession;
private readonly callbacks?: ProofOfPossessionCallbacks;

private version: OpenId4VCIVersion;
private readonly version: OpenId4VCIVersion;

private kid?: string;
private clientId?: string;
Expand All @@ -43,13 +42,15 @@ export class ProofOfPossessionBuilder {
}) {
this.proof = proof;
this.callbacks = callbacks;
this.version = version;
if (jwt) {
this.withJwt(jwt);
} else {
this.withTyp(version < OpenId4VCIVersion.VER_1_0_11 ? 'jwt' : 'openid4vci-proof+jwt');
}
if (accessTokenResponse) {
this.withAccessTokenResponse(accessTokenResponse);
}
this.version = version;
}

static fromJwt({
Expand Down Expand Up @@ -80,54 +81,63 @@ export class ProofOfPossessionBuilder {
return new ProofOfPossessionBuilder({ proof, version });
}

withClientId(clientId: string): ProofOfPossessionBuilder {
withClientId(clientId: string): this {
this.clientId = clientId;
return this;
}

withKid(kid: string): ProofOfPossessionBuilder {
withKid(kid: string): this {
this.kid = kid;
return this;
}

withIssuer(issuer: string): ProofOfPossessionBuilder {
withIssuer(issuer: string): this {
this.issuer = issuer;
return this;
}

withAlg(alg: Alg | string): ProofOfPossessionBuilder {
withAlg(alg: Alg | string): this {
this.alg = alg;
return this;
}

withJti(jti: string): ProofOfPossessionBuilder {
withJti(jti: string): this {
this.jti = jti;
return this;
}

withTyp(typ: Typ): ProofOfPossessionBuilder {
withTyp(typ: Typ): this {
if (this.version >= OpenId4VCIVersion.VER_1_0_11) {
if (!!typ && typ !== 'openid4vci-proof+jwt') {
throw Error('typ must be openid4vci-proof+jwt for version 1.0.11 and up');
}
} else {
if (!!typ && typ !== 'jwt') {
throw Error('typ must be jwt for version 1.0.10 and below');
}
}
this.typ = typ;
return this;
}

withAccessTokenNonce(cNonce: string): ProofOfPossessionBuilder {
withAccessTokenNonce(cNonce: string): this {
this.cNonce = cNonce;
return this;
}

withAccessTokenResponse(accessToken: AccessTokenResponse): ProofOfPossessionBuilder {
withAccessTokenResponse(accessToken: AccessTokenResponse): this {
if (accessToken.c_nonce) {
this.withAccessTokenNonce(accessToken.c_nonce);
}
return this;
}

withEndpointMetadata(endpointMetadata: EndpointMetadata): ProofOfPossessionBuilder {
withEndpointMetadata(endpointMetadata: EndpointMetadata): this {
this.withIssuer(endpointMetadata.issuer);
return this;
}

withJwt(jwt: Jwt): ProofOfPossessionBuilder {
withJwt(jwt: Jwt): this {
if (!jwt) {
throw new Error(NO_JWT_PROVIDED);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/client/lib/__tests__/IT.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const UNIT_TEST_TIMEOUT = 30000;

const ISSUER_URL = 'https://issuer.research.identiproof.io';
const jwt = {
header: { alg: Alg.ES256, kid: 'did:example:ebfeb1f712ebc6f1c276e12ec21/keys/1', typ: 'jwt' },
header: { alg: Alg.ES256, kid: 'did:example:ebfeb1f712ebc6f1c276e12ec21/keys/1', typ: 'openid4vci-proof+jwt' },
payload: { iss: 'test-clientId', nonce: 'tZignsnFbp', jti: 'tZignsnFbp223', aud: ISSUER_URL },
};

Expand Down
9 changes: 9 additions & 0 deletions packages/common/lib/functions/IssuerMetadataUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
CredentialSupportedTypeV1_0_08,
CredentialSupportedV1_0_08,
IssuerMetadataV1_0_08,
MetadataDisplay,
OpenId4VCIVersion,
} from '../types';

Expand Down Expand Up @@ -99,3 +100,11 @@ export function credentialSupportedV8ToV11(key: string, supportedV8: CredentialS
return credentialSupport as CredentialSupported;
});
}

export function getIssuerDisplays(metadata: CredentialIssuerMetadata | IssuerMetadataV1_0_08, opts?: { prefLocales: string[] }): MetadataDisplay[] {
const matchedDisplays =
metadata.display?.filter(
(item) => !opts?.prefLocales || opts.prefLocales.length === 0 || (item.locale && opts.prefLocales.includes(item.locale)) || !item.locale
) ?? [];
return matchedDisplays.sort((item) => (item.locale ? opts?.prefLocales.indexOf(item.locale) ?? 1 : Number.MAX_VALUE));
}

0 comments on commit 8274708

Please sign in to comment.