Skip to content

Commit 0f13fc7

Browse files
committed
Allowing expiration dates on the SHC's JWT
1 parent 45412fa commit 0f13fc7

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

lib/shc.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const URI_SCHEMA = 'shc';
88
/*
99
* I am not sure if I should build this by hand.
1010
*/
11-
export async function makeJWT(payload, issuer, notBeforeDate) {
11+
export async function makeJWT(payload, issuer, notBeforeDate, expirationDate) {
1212
let jwt = {
1313
iss: issuer,
1414
vc: payload
@@ -17,6 +17,9 @@ export async function makeJWT(payload, issuer, notBeforeDate) {
1717
if (notBeforeDate)
1818
jwt['nbf'] = Math.round(notBeforeDate.getTime()/1000);
1919

20+
if (expirationDate)
21+
jwt['exp'] = Math.round(expirationDate.getTime()/1000);
22+
2023
return jwt;
2124
}
2225

test/sign-verify.spec.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,6 @@ describe('JWS Crypto', function() {
106106
it('should sign the package', async function() {
107107
const signed = await sign(await makeJWT(TEST_PAYLOAD, "https://pcf.pw"), PRIVATE_KEY);
108108
expect(signed).to.not.be.null;
109-
expect(signed.proof).to.not.be.null;
110-
expect(signed.issuer).to.not.be.null;
111-
expect(signed.issuanceDate).to.not.be.null;
112109
});
113110

114111
it('should verify the package', async function() {
@@ -123,7 +120,6 @@ describe('JWS Crypto', function() {
123120
});
124121
});
125122

126-
127123
describe('JWS Data Minimization', function() {
128124
it('should pack And unpack', async function() {
129125
const packed = await pack(SIGNED_TEST_PAYLOAD);
@@ -132,6 +128,28 @@ describe('JWS Data Minimization', function() {
132128
});
133129
});
134130

131+
describe('Sign And Pack, UnpackAndverify', function() {
132+
it('should pack And unpack', async function() {
133+
const packed = await signAndPack(await makeJWT(TEST_PAYLOAD, "https://pcf.pw"), GENERATED_PRIVATE_KEY);
134+
const result = await unpackAndVerify(packed, CACHED_KEYS);
135+
expect(result.credential.vc).to.eql(TEST_PAYLOAD);
136+
expect(result.credential.iss).to.eql("https://pcf.pw");
137+
expect(result.credential.nbf).to.be.undefined;
138+
expect(result.credential.exp).to.be.undefined;
139+
});
140+
141+
it('should pack And unpack with dates', async function() {
142+
let nbf = new Date(2021, 11, 02);
143+
let exp = new Date(2022, 11, 02);
144+
const packed = await signAndPack(await makeJWT(TEST_PAYLOAD, "https://pcf.pw", nbf, exp), GENERATED_PRIVATE_KEY);
145+
const result = await unpackAndVerify(packed, CACHED_KEYS);
146+
expect(result.credential.vc).to.eql(TEST_PAYLOAD);
147+
expect(result.credential.iss).to.eql("https://pcf.pw");
148+
expect(new Date(result.credential.nbf*1000)).to.eql(nbf);
149+
expect(new Date(result.credential.exp*1000)).to.eql(exp);
150+
});
151+
});
152+
135153
const EXAMPLE1_PACKED = "shc:/56762909524320603460292437404460312229595326546034602925407728043360287028647167452228092861333145643765314159064022030645045908564355034142454136403706366541713724123638030437562204673740753232392543344332605736010645292953127074242843503861221276716852752941725536670334373625647345380024213944077025250726312423573657001132105220316267750968640761356508111008270666243020277044446712214341455936637024282703544034660963252707282555072932056232255262395660612010735336331255715610420057716412306973057066214536651135113958591233120032575026733958333075072812533734264534700060266054734545664338772667663471584128617435526828390065275357404052057121004150076600323056277610287226003175060305765803534256207472564464060539095425076777272921345209305565332021506258456045760350722804223710051277402927664527742911662372066523664321240336446744622769760467573259652733383263657311072452563376417025746807407539006144613252696869456340066810522645386256555532111000531265754227302628303438085756243800662563286838775672222439672172403542396107375860647335106645704512536703506321757004413636764365347431287321355256580631556063583463563610567737660541737377552828605563116564297412076854033003344323337052606873573426066033102439280977115921594064314334576408722871427337224310757744412937522268303871367257627564750472597763507745283571571263580550066921715611703323062474012471272931363924743604256803437445104259400424433362673769543855403976310365501153573745056364696326060377575776050561075823064353055604551028500311453022452525062305534574";
136154
const EXAMPLE1_SIGNED = "eyJ6aXAiOiJERUYiLCJhbGciOiJFUzI1NiIsImtpZCI6IjNLZmRnLVh3UC03Z1h5eXd0VWZVQUR3QnVtRE9QS01ReC1pRUxMMTFXOXMifQ.3ZJJb9swEIX_SjC9ytqaxJVudQp0ORQFmvZS-EBTY4sFF4GLEDfQf-8M7aALkpx6qm4jPn5875H3oEKAHsYYp9BXVZhQlsEIH0cUOo6lFH4IFd4JM2kMFakTeijA7vbQN9dte7W-ftm9Krv2soBZQn8P8Tgh9N9-Mf_GvTgNKx4I9bROGZOs-iGicvZZoXSzGpoOtgVIjwPaqIT-nHbfUUa2tB-V_4o-MKeHy7IuG-Lx302yg0bWeAwueYm32T6cF4pzHJBOa6KdnNAB_kgZiZy0_uI1CR729zUJHoZHwJ8oDu3nDoXBE0QYpYkHry1pfMhnHNSMlnv84EaeNyVsFwq4UxT-jYjMarqrZlU3q7aGZSkeddM87-b9nxWHKGIKOS5feES-oFlIqSzeuCETpBuUPWTj4RgimvP7oZsZ9bp0_lBxs1VQQyXnOwLIvBPaeg3LdilgOleQ7ezRo2VvvzdIIidl8nmJw94qc0K0OXDNsaiqvfOG3iN7ETI6z8hBhUmLXOfm5uItWvRCX7xzYVJRaCqKStQufkxmx1uhzl_zZIPtf9lg2_3rBte8sND3Ew.EtHJLQTEwQ1Fq0XwZ7WhU1EXNkpRrcSdUTyL0n_8bfRZ2lmrlG30zffy22j4gD3Xb2e1d7I_08ZKCZFF3D2bZw"
137155

0 commit comments

Comments
 (0)