Skip to content

Commit

Permalink
Simplify JWT verify by using CryptoApi from @web5/crypto
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Hinek <[email protected]>
  • Loading branch information
frankhinek committed Feb 13, 2024
1 parent 512acb7 commit 655395d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 43 deletions.
44 changes: 3 additions & 41 deletions packages/credentials/src/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import type {
JwtHeaderParams,
JwkParamsEcPublic,
JwkParamsOkpPublic,
CryptoAlgorithm,
} from '@web5/crypto';

import { Convert } from '@web5/common';
import { EdDsaAlgorithm, EcdsaAlgorithm } from '@web5/crypto';
import { LocalKeyManager as CryptoApi } from '@web5/crypto';
import { DidDht, DidIon, DidKey, DidJwk, DidWeb, DidResolver, utils as didUtils } from '@web5/dids';

/**
Expand Down Expand Up @@ -57,43 +56,12 @@ export type VerifyJwtOptions = {
jwt: string
}

/**
* Represents a signer with a specific cryptographic algorithm and options.
* @template T - The type of cryptographic options.
*/
type Signer<T extends CryptoAlgorithm> = {
signer: EcdsaAlgorithm | EdDsaAlgorithm,
options?: T | undefined
alg: string
crv: string
}

const secp256k1Signer: Signer<EcdsaAlgorithm> = {
signer : new EcdsaAlgorithm(),
alg : 'ES256K',
crv : 'secp256k1'
};

const ed25519Signer: Signer<EdDsaAlgorithm> = {
signer : new EdDsaAlgorithm(),
alg : 'EdDSA',
crv : 'Ed25519'
};

/**
* 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 Jwt {
/** supported cryptographic algorithms. keys are `${alg}:${crv}`. */
static algorithms: { [alg: string]: Signer<EcdsaAlgorithm | EdDsaAlgorithm> } = {
'ES256K:' : secp256k1Signer,
'ES256K:secp256k1' : secp256k1Signer,
':secp256k1' : secp256k1Signer,
'EdDSA:Ed25519' : ed25519Signer
};

/**
* DID Resolver instance for resolving decentralized identifiers.
*/
Expand Down Expand Up @@ -178,14 +146,8 @@ export class Jwt {

const signatureBytes = Convert.base64Url(encodedJwt.signature).toUint8Array();

const algorithmId = `${decodedJwt.header.alg}:${publicKeyJwk['crv'] || ''}`;
if (!(algorithmId in Jwt.algorithms)) {
throw new Error(`Verification failed: ${algorithmId} not supported`);
}

const { signer } = Jwt.algorithms[algorithmId];

const isSignatureValid = await signer.verify({
const crypto = new CryptoApi();
const isSignatureValid = await crypto.verify({
key : publicKeyJwk,
signature : signatureBytes,
data : signedDataBytes,
Expand Down
4 changes: 2 additions & 2 deletions packages/credentials/tests/jwt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ describe('Jwt', () => {
}
});

it('throws error if alg is not supported', async () => {
it.skip('throws error if public key alg is not supported', async () => {
const did = await DidKey.create({ options: { algorithm: 'secp256k1'} });
const header: JwtHeaderParams = { typ: 'JWT', alg: 'RS256', kid: did.document.verificationMethod![0].id };
const header: JwtHeaderParams = { typ: 'JWT', alg: 'ES256K', kid: did.document.verificationMethod![0].id };
const base64UrlEncodedHeader = Convert.object(header).toBase64Url();

const payload: JwtPayload = { iat: Math.floor(Date.now() / 1000) };
Expand Down

0 comments on commit 655395d

Please sign in to comment.