diff --git a/packages/credentials/src/compact-jwt.ts b/packages/credentials/src/compact-jwt.ts index 3f30dc06d..159c7cde3 100644 --- a/packages/credentials/src/compact-jwt.ts +++ b/packages/credentials/src/compact-jwt.ts @@ -22,9 +22,10 @@ export type VerifyJwtParams = { } /** - * Parameters for decoding a JWT. + * Parameters for parsing a JWT. + * used in {@link CompactJwt.parse} */ -export type DecodeJwtParams = { +export type ParseJwtParams = { compactJwt: string } @@ -65,6 +66,7 @@ const ed25519Signer: Signer = { /** * Class for handling Compact JSON Web Tokens (JWTs). * This class provides methods to create, verify, and decode JWTs using various cryptographic algorithms. + * More information on JWTs can be found [here](https://datatracker.ietf.org/doc/html/rfc7519) */ export class CompactJwt { /** supported cryptographic algorithms. keys are `${alg}:${crv}`. */ @@ -133,7 +135,7 @@ export class CompactJwt { * ``` */ static async verify(params: VerifyJwtParams) { - const { decoded: decodedJwt, encoded: encodedJwt } = CompactJwt.decode({ compactJwt: params.compactJwt }); + const { decoded: decodedJwt, encoded: encodedJwt } = CompactJwt.parse({ compactJwt: params.compactJwt }); // TODO: should really be looking for verificationMethod with authentication verification relationship const verificationMethod = await CompactJwt.didResolver.dereference({ didUrl: decodedJwt.header.kid! }); if (!utils.isVerificationMethod(verificationMethod)) { // ensure that appropriate verification method was found @@ -177,13 +179,13 @@ export class CompactJwt { } /** - * Decodes a JWT without verifying its signature. + * Parses a JWT without verifying its signature. * @param params - Parameters for JWT decoding, including the JWT string. - * @returns Decoded JWT parts, including header and payload. + * @returns both encoded and decoded JWT parts * @example - * const decodedJwt = CompactJwt.decode({ compactJwt: myJwt }); + * const { encoded: encodedJwt, decoded: decodedJwt } = CompactJwt.parse({ compactJwt: myJwt }); */ - static decode(params: DecodeJwtParams) { + static parse(params: ParseJwtParams) { const splitJwt = params.compactJwt.split('.'); if (splitJwt.length !== 3) { throw new Error(`Verification failed: Malformed JWT. expected 3 parts. got ${splitJwt.length}`); diff --git a/packages/credentials/src/verifiable-credential.ts b/packages/credentials/src/verifiable-credential.ts index 2293f810c..07a4a5b2f 100644 --- a/packages/credentials/src/verifiable-credential.ts +++ b/packages/credentials/src/verifiable-credential.ts @@ -202,7 +202,7 @@ export class VerifiableCredential { * ``` */ public static parseJwt(vcJwt: string): VerifiableCredential { - const parsedJwt = CompactJwt.decode({ compactJwt: vcJwt }); + const parsedJwt = CompactJwt.parse({ compactJwt: vcJwt }); const vcDataModel: VcDataModel = parsedJwt.decoded.payload['vc']; if(!vcDataModel) { diff --git a/packages/credentials/tests/compact-jwt.spec.ts b/packages/credentials/tests/compact-jwt.spec.ts index e58acd8f9..fc4371162 100644 --- a/packages/credentials/tests/compact-jwt.spec.ts +++ b/packages/credentials/tests/compact-jwt.spec.ts @@ -6,10 +6,10 @@ import { expect } from 'chai'; import { JwtHeader, JwtPayload } from 'jwt-decode'; describe('CompactJwt', () => { - describe('verify', () => { + describe('parse', () => { it('throws error if JWT doesnt contain 3 parts', async () => { try { - await CompactJwt.verify({ compactJwt: 'abcd123' }); + await CompactJwt.parse({ compactJwt: 'abcd123' }); expect.fail(); } catch(e: any) { expect(e.message).to.include('Malformed JWT. expected 3 parts'); @@ -18,7 +18,7 @@ describe('CompactJwt', () => { it('throws error if JWT header is not properly base64url encoded', async () => { try { - await CompactJwt.verify({ compactJwt: 'abcd123.efgh.hijk' }); + await CompactJwt.parse({ compactJwt: 'abcd123.efgh.hijk' }); expect.fail(); } catch(e: any) { expect(e.message).to.include('Invalid base64url encoding for JWT header'); @@ -30,7 +30,7 @@ describe('CompactJwt', () => { const base64UrlEncodedHeader = Convert.object(header).toBase64Url(); try { - await CompactJwt.verify({ compactJwt: `${base64UrlEncodedHeader}.efgh.hijk` }); + await CompactJwt.parse({ compactJwt: `${base64UrlEncodedHeader}.efgh.hijk` }); expect.fail(); } catch(e: any) { expect(e.message).to.include('typ property set to JWT'); @@ -42,7 +42,7 @@ describe('CompactJwt', () => { const base64UrlEncodedHeader = Convert.object(header).toBase64Url(); try { - await CompactJwt.verify({ compactJwt: `${base64UrlEncodedHeader}.efgh.hijk` }); + await CompactJwt.parse({ compactJwt: `${base64UrlEncodedHeader}.efgh.hijk` }); expect.fail(); } catch(e: any) { expect(e.message).to.include('typ property set to JWT'); @@ -54,7 +54,7 @@ describe('CompactJwt', () => { const base64UrlEncodedHeader = Convert.object(header).toBase64Url(); try { - await CompactJwt.verify({ compactJwt: `${base64UrlEncodedHeader}.efgh.hijk` }); + await CompactJwt.parse({ compactJwt: `${base64UrlEncodedHeader}.efgh.hijk` }); expect.fail(); } catch(e: any) { expect(e.message).to.include('to contain alg and kid'); @@ -66,7 +66,7 @@ describe('CompactJwt', () => { const base64UrlEncodedHeader = Convert.object(header).toBase64Url(); try { - await CompactJwt.verify({ compactJwt: `${base64UrlEncodedHeader}.efgh.hijk` }); + await CompactJwt.parse({ compactJwt: `${base64UrlEncodedHeader}.efgh.hijk` }); expect.fail(); } catch(e: any) { expect(e.message).to.include('to contain alg and kid'); @@ -78,13 +78,14 @@ describe('CompactJwt', () => { const base64UrlEncodedHeader = Convert.object(header).toBase64Url(); try { - await CompactJwt.verify({ compactJwt: `${base64UrlEncodedHeader}.efgh.hijk` }); + await CompactJwt.parse({ compactJwt: `${base64UrlEncodedHeader}.efgh.hijk` }); expect.fail(); } catch(e: any) { expect(e.message).to.include('Invalid base64url encoding for JWT payload'); } }); - + }); + describe('verify', () => { it('throws error if JWT header kid does not dereference a verification method', async () => { const did = await DidKeyMethod.create({ keyAlgorithm: 'secp256k1' }); const header: JwtHeader = { typ: 'JWT', alg: 'ES256K', kid: did.did };