Skip to content

Commit

Permalink
fixing jwt test
Browse files Browse the repository at this point in the history
  • Loading branch information
nitro-neal committed Feb 6, 2024
1 parent 0c1337a commit 933b4aa
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/credentials/src/jwt.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PortableDid, BearerDid } from '@web5/dids';
import type { BearerDid } from '@web5/dids';
import type {
JwtPayload,
Web5Crypto,
Expand Down
56 changes: 38 additions & 18 deletions packages/credentials/tests/jwt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import type { JwtHeaderParams, JwtPayload, PrivateKeyJwk } from '@web5/crypto';
import { expect } from 'chai';
import { Convert } from '@web5/common';
import { Secp256k1 } from '@web5/crypto';

Check failure on line 5 in packages/credentials/tests/jwt.spec.ts

View workflow job for this annotation

GitHub Actions / test-with-node

'Secp256k1' is defined but never used. Allowed unused vars must match /^_/u
import { DidKey } from '@web5/dids';
import { DidKey, PortableDid } from '@web5/dids';

import { Jwt } from '../src/jwt.js';
import { Ed25519 } from '@web5/crypto';

describe('Jwt', () => {
describe('parse()', () => {
Expand Down Expand Up @@ -116,28 +117,47 @@ describe('Jwt', () => {
}
});

// it('returns signer DID if verification succeeds', async () => {
// // TODO: need to convert to portable did:
// const did = await DidKeyMethod.create({ keyAlgorithm: 'secp256k1' });
// const header: JwtHeaderParams = { typ: 'JWT', alg: 'ES256K', kid: did.document.verificationMethod![0].id };
// const base64UrlEncodedHeader = Convert.object(header).toBase64Url();
it('returns signer DID if verification succeeds', async () => {
const portabldDid: PortableDid = {
uri : 'did:key:z6MkkGkByH7rSY3uxDEPTk1CZzPG5hvf564ABFLQzCFwyYNN',
verificationMethods : [{
publicKeyJwk: {
kty : 'OKP',
crv : 'Ed25519',
x : 'VnSOQ-n7kRcYd0XGW2MNCv7DDY5py5XhNcjM7-Y1HVM'
},
privateKeyJwk: {
kty : 'OKP',
crv : 'Ed25519',
x : 'VnSOQ-n7kRcYd0XGW2MNCv7DDY5py5XhNcjM7-Y1HVM',
d : 'iTD5DIOKZNkwgzsND-I8CLIXmgTxfQ1HUzl9fpMktAo'
},
purposes: ['authentication']
}]
};

const did = await DidKey.fromKeys(portabldDid);

// const did = await DidKey.create({ options: { algorithm: 'secp256k1'} });
const header: JwtHeaderParams = { typ: 'JWT', alg: 'EdDSA', kid: did.didDocument.verificationMethod![0].id };
const base64UrlEncodedHeader = Convert.object(header).toBase64Url();

// const payload: JwtPayload = { iat: Math.floor(Date.now() / 1000) };
// const base64UrlEncodedPayload = Convert.object(payload).toBase64Url();
const payload: JwtPayload = { iat: Math.floor(Date.now() / 1000) };
const base64UrlEncodedPayload = Convert.object(payload).toBase64Url();

// const toSign = `${base64UrlEncodedHeader}.${base64UrlEncodedPayload}`;
// const toSignBytes = Convert.string(toSign).toUint8Array();
const toSign = `${base64UrlEncodedHeader}.${base64UrlEncodedPayload}`;
const toSignBytes = Convert.string(toSign).toUint8Array();

// const privateKeyJwk = did.keySet.verificationMethodKeys![0].privateKeyJwk;
const privateKeyJwk = portabldDid.verificationMethods![0].privateKeyJwk;

// const signatureBytes = await Secp256k1.sign({ key: privateKeyJwk as PrivateKeyJwk, data: toSignBytes });
// const base64UrlEncodedSignature = Convert.uint8Array(signatureBytes).toBase64Url();
const signatureBytes = await Ed25519.sign({ key: privateKeyJwk as PrivateKeyJwk, data: toSignBytes });
const base64UrlEncodedSignature = Convert.uint8Array(signatureBytes).toBase64Url();

// const jwt = `${toSign}.${base64UrlEncodedSignature}`;
// const verifyResult = await Jwt.verify({ jwt });
const jwt = `${toSign}.${base64UrlEncodedSignature}`;
const verifyResult = await Jwt.verify({ jwt });

// expect(verifyResult.header).to.deep.equal(header);
// expect(verifyResult.payload).to.deep.equal(payload);
// });
expect(verifyResult.header).to.deep.equal(header);
expect(verifyResult.payload).to.deep.equal(payload);
});
});
});

0 comments on commit 933b4aa

Please sign in to comment.