Skip to content

Commit

Permalink
fix the iat claim when creating a keybinding JWT in `getKeyBindingJ…
Browse files Browse the repository at this point in the history
…WT` (#24)

Co-authored-by: Vijay Shiyani <[email protected]>
  • Loading branch information
vijayshiyani and Vijay Shiyani authored Feb 14, 2024
1 parent 909f3c7 commit c7d48f5
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 18 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project (loosely) adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 1.0.2 - 2024-02-14

### Fixed

- fix the `iat` claim when creating a keybinding JWT in `getKeyBindingJWT` method

## 1.0.1 - 2024-02-14

### FIX
### Fixed

- Correct the wellKnownPath in URL formation in `getIssuerPublicKeyFromWellKnownURI` utility function

Expand Down
13 changes: 11 additions & 2 deletions demo/holder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { KeyBindingVerifier, Signer, decodeJWT } from '@meeco/sd-jwt';
import { KeyBindingVerifier, Signer, base64encode, decodeJWT } from '@meeco/sd-jwt';
import { createHash } from 'crypto';
import { JWK, JWTHeaderParameters, JWTPayload, KeyLike, SignJWT, importJWK, jwtVerify } from 'jose';
import { Holder, SDJWTVCError, SignerConfig, supportedAlgorithm } from '../dev/src';

Expand All @@ -10,6 +11,14 @@ const signerCallbackFn = function (privateKey: Uint8Array | KeyLike): Signer {
};
};

export const hasherFnResolver = (alg: string) => {
const hasher = (data: string): string => {
const digest = createHash(alg).update(data).digest();
return base64encode(digest);
};
return Promise.resolve(hasher);
};

const keyBindingVerifierCallbackFn = function (): KeyBindingVerifier {
return async (kbjwt: string, holderJWK: JWK) => {
const { header } = decodeJWT(kbjwt);
Expand Down Expand Up @@ -45,7 +54,7 @@ async function main() {
alg: supportedAlgorithm.ES256,
callback: signerCallbackFn(pk),
};
const hol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TRhc5Cpy9qxCWPu5G1tOWicNqysPqi88ATqD4HRD_zRg~WyJTa0MwMXpMZXZnbDEwakpYIiwiZ2l2ZW5fbmFtZSIsIkpvaG4iXQ~WyJRbmZJVE9GUDdrcjhQcUpWIiwiZmFtaWx5X25hbWUiLCJEb2UiXQ~WyJudXpYZzRMZEhDRWQyMlRMIiwiZW1haWwiLCJqb2huZG9lQGV4YW1wbGUuY29tIl0~WyJQb2RwWmF3Q3ZUbW03TGNSIiwicGhvbmVfbnVtYmVyIiwiKzEtMjAyLTU1NS0wMTAxIl0~WyJwZVhLajk0TU5DaEc3TkZLIiwiYWRkcmVzcyIseyJzdHJlZXRfYWRkcmVzcyI6IjEyMyBNYWluIFN0IiwibG9jYWxpdHkiOiJBbnl0b3duIiwicmVnaW9uIjoiQW55c3RhdGUiLCJjb3VudHJ5IjoiVVMifV0~WyJERTdPdThRNE54aXI3ZmRnIiwiYmlydGhkYXRlIiwiMTk0MC0wMS0wMSJd~WyJnQTdUWUJyWnR3VjZMZFlFIiwiaXNfb3Zlcl8xOCIsdHJ1ZV0~WyJ3bFdOSVBmREcxWHNCSW13IiwiaXNfb3Zlcl8yMSIsdHJ1ZV0~WyJWNTY5S2ZzYUFCamFaU0tVIiwiaXNfb3Zlcl82NSIsdHJ1ZV0~';
Expand Down
2 changes: 1 addition & 1 deletion demo/issuer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async function main() {
};

const payload: CreateSDJWTPayload = {
iat: Date.now(),
iat: Math.floor(Date.now() / 1000),
cnf: {
jwk: holderPublicKey,
},
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@meeco/sd-jwt-vc",
"version": "1.0.1",
"version": "1.0.2",
"description": "SD-JWT VC implementation in typescript",
"scripts": {
"build": "tsc",
Expand Down
2 changes: 1 addition & 1 deletion src/holder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class Holder {
aud: audience,
nonce,
sd_hash: sdHash,
iat: Date.now(),
iat: Math.floor(Date.now() / 1000),
};

const signature: string = await this.signer.callback(protectedHeader, presentSDJWTPayload);
Expand Down
20 changes: 10 additions & 10 deletions src/issuer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('Issuer', () => {
};

const payload: CreateSDJWTPayload = {
iat: Date.now(),
iat: Math.floor(Date.now() / 1000),
cnf: {
jwk: holderPublicKey,
},
Expand Down Expand Up @@ -148,7 +148,7 @@ describe('Issuer', () => {
describe('validateSDJWTPayload', () => {
it('should throw an error if iss is missing', () => {
const sdJWTPayload = {
iat: Date.now(),
iat: Math.floor(Date.now() / 1000),
cnf: {
jwk: {},
},
Expand All @@ -161,7 +161,7 @@ describe('Issuer', () => {

it('should throw an error if iss is not a valid URL', () => {
const sdJWTPayload = {
iat: Date.now(),
iat: Math.floor(Date.now() / 1000),
cnf: {
jwk: {},
},
Expand Down Expand Up @@ -202,7 +202,7 @@ describe('Issuer', () => {

it('should throw an error if cnf is missing', () => {
const sdJWTPayload = {
iat: Date.now(),
iat: Math.floor(Date.now() / 1000),
iss: 'https://valid.issuer.url',
};

Expand All @@ -213,7 +213,7 @@ describe('Issuer', () => {

it('should throw an error if cnf.jwk is missing', () => {
const sdJWTPayload = {
iat: Date.now(),
iat: Math.floor(Date.now() / 1000),
cnf: {},
iss: 'https://valid.issuer.url',
};
Expand All @@ -225,7 +225,7 @@ describe('Issuer', () => {

it('should throw an error if cnf.jwk is not an object', () => {
const sdJWTPayload = {
iat: Date.now(),
iat: Math.floor(Date.now() / 1000),
cnf: {
jwk: 'invalid-jwk',
},
Expand All @@ -239,7 +239,7 @@ describe('Issuer', () => {

it('should throw an error if cnf.jwk is missing kty', () => {
const sdJWTPayload = {
iat: Date.now(),
iat: Math.floor(Date.now() / 1000),
cnf: {
jwk: {
crv: 'P-256',
Expand All @@ -255,7 +255,7 @@ describe('Issuer', () => {

it('should throw an error if vct is not a valid String', () => {
const sdJWTPayload = {
iat: Date.now(),
iat: Math.floor(Date.now() / 1000),
cnf: {
jwk: {
kty: 'EC',
Expand All @@ -273,7 +273,7 @@ describe('Issuer', () => {

it('should throw an error if vct is not a valid url', () => {
const sdJWTPayload = {
iat: Date.now(),
iat: Math.floor(Date.now() / 1000),
cnf: {
jwk: {
kty: 'EC',
Expand All @@ -291,7 +291,7 @@ describe('Issuer', () => {

it('should not throw an error if all properties are valid', () => {
const sdJWTPayload = {
iat: Date.now(),
iat: Math.floor(Date.now() / 1000),
cnf: {
jwk: {
kty: 'EC',
Expand Down

0 comments on commit c7d48f5

Please sign in to comment.