Skip to content

Commit

Permalink
feat: use a proper cbor tag code for Dates
Browse files Browse the repository at this point in the history
  • Loading branch information
siacomuzzi committed Oct 17, 2024
1 parent 33c849a commit bf0a4ab
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 113 deletions.
32 changes: 18 additions & 14 deletions __tests__/issuing/deviceResponse.tests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createHash, randomFillSync } from 'node:crypto';
import { randomFillSync } from 'node:crypto';
import * as jose from 'jose';
import { COSEKeyFromJWK } from 'cose-kit';
import {
MDoc,
Document,
Expand All @@ -11,7 +10,6 @@ import {
} from '../../src';
import { DEVICE_JWK, ISSUER_CERTIFICATE, ISSUER_PRIVATE_KEY_JWK, PRESENTATION_DEFINITION_1 } from './config';
import { DataItem, cborEncode } from '../../src/cbor';
import COSEKeyToRAW from '../../src/cose/coseKey';

const { d, ...publicKeyJWK } = DEVICE_JWK as jose.JWK;

Expand All @@ -20,6 +18,10 @@ describe('issuing a device response', () => {
let parsedDocument: DeviceSignedDocument;
let mdoc: MDoc;

const signed = new Date('2023-10-24T14:55:18Z');
const validUntil = new Date(signed);
validUntil.setFullYear(signed.getFullYear() + 30);

beforeAll(async () => {
const issuerPrivateKey = ISSUER_PRIVATE_KEY_JWK;

Expand All @@ -31,16 +33,16 @@ describe('issuing a device response', () => {
given_name: 'Ava',
birth_date: '2007-03-25',
issue_date: '2023-09-01',
expiry_date: '2028-09-31',
expiry_date: '2028-09-30',
issuing_country: 'US',
issuing_authority: 'NY DMV',
document_number: '01-856-5050',
portrait: 'bstr',
driving_privileges: [
{
vehicle_category_code: 'C',
issue_date: '2023-09-01',
expiry_date: '2028-09-31',
issue_date: '2022-09-02',
expiry_date: '2027-09-20',
},
],
un_distinguishing_sign: 'tbd-us.ny.dmv',
Expand All @@ -59,8 +61,8 @@ describe('issuing a device response', () => {
})
.useDigestAlgorithm('SHA-512')
.addValidityInfo({
signed: new Date('2023-10-24'),
validUntil: new Date('2050-10-24'),
signed,
validUntil,
})
.addDeviceKeyInfo({ deviceKey: publicKeyJWK })
.sign({
Expand Down Expand Up @@ -133,9 +135,10 @@ describe('issuing a device response', () => {
it('should contain the validity info', () => {
const { validityInfo } = parsedDocument.issuerSigned.issuerAuth.decodedPayload;
expect(validityInfo).toBeDefined();
expect(validityInfo.signed).toEqual(new Date('2023-10-24'));
expect(validityInfo.validFrom).toEqual(new Date('2023-10-24'));
expect(validityInfo.validUntil).toEqual(new Date('2050-10-24'));
expect(validityInfo.signed).toEqual(signed);
expect(validityInfo.validFrom).toEqual(signed);
expect(validityInfo.validUntil).toEqual(validUntil);
expect(validityInfo.expectedUpdate).toBeUndefined();
});

it('should contain the device namespaces', () => {
Expand Down Expand Up @@ -217,9 +220,10 @@ describe('issuing a device response', () => {
it('should contain the validity info', () => {
const { validityInfo } = parsedDocument.issuerSigned.issuerAuth.decodedPayload;
expect(validityInfo).toBeDefined();
expect(validityInfo.signed).toEqual(new Date('2023-10-24'));
expect(validityInfo.validFrom).toEqual(new Date('2023-10-24'));
expect(validityInfo.validUntil).toEqual(new Date('2050-10-24'));
expect(validityInfo.signed).toEqual(signed);
expect(validityInfo.validFrom).toEqual(signed);
expect(validityInfo.validUntil).toEqual(validUntil);
expect(validityInfo.expectedUpdate).toBeUndefined();
});

it('should contain the device namespaces', () => {
Expand Down
28 changes: 17 additions & 11 deletions __tests__/issuing/deviceResponseWithMac.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ describe('issuing a device response with MAC authentication', () => {
let ephemeralPrivateKey: Uint8Array;
let ephemeralPublicKey: Uint8Array;

const signed = new Date('2023-10-24T14:55:18Z');
const validUntil = new Date(signed);
validUntil.setFullYear(signed.getFullYear() + 30);

beforeAll(async () => {
const issuerPrivateKey = ISSUER_PRIVATE_KEY_JWK;

Expand All @@ -33,16 +37,16 @@ describe('issuing a device response with MAC authentication', () => {
given_name: 'Ava',
birth_date: '2007-03-25',
issue_date: '2023-09-01',
expiry_date: '2028-09-31',
expiry_date: '2028-09-30',
issuing_country: 'US',
issuing_authority: 'NY DMV',
document_number: '01-856-5050',
portrait: 'bstr',
driving_privileges: [
{
vehicle_category_code: 'C',
issue_date: '2023-09-01',
expiry_date: '2028-09-31',
issue_date: '2022-09-01',
expiry_date: '2027-09-30',
},
],
un_distinguishing_sign: 'tbd-us.ny.dmv',
Expand All @@ -61,8 +65,8 @@ describe('issuing a device response with MAC authentication', () => {
})
.useDigestAlgorithm('SHA-512')
.addValidityInfo({
signed: new Date('2023-10-24'),
validUntil: new Date('2050-10-24'),
signed,
validUntil,
})
.addDeviceKeyInfo({ deviceKey: publicKeyJWK })
.sign({
Expand Down Expand Up @@ -148,9 +152,10 @@ describe('issuing a device response with MAC authentication', () => {
it('should contain the validity info', () => {
const { validityInfo } = parsedDocument.issuerSigned.issuerAuth.decodedPayload;
expect(validityInfo).toBeDefined();
expect(validityInfo.signed).toEqual(new Date('2023-10-24'));
expect(validityInfo.validFrom).toEqual(new Date('2023-10-24'));
expect(validityInfo.validUntil).toEqual(new Date('2050-10-24'));
expect(validityInfo.signed).toEqual(signed);
expect(validityInfo.validFrom).toEqual(signed);
expect(validityInfo.validUntil).toEqual(validUntil);
expect(validityInfo.expectedUpdate).toBeUndefined();
});

it('should contain the device namespaces', () => {
Expand Down Expand Up @@ -239,9 +244,10 @@ describe('issuing a device response with MAC authentication', () => {
it('should contain the validity info', () => {
const { validityInfo } = parsedDocument.issuerSigned.issuerAuth.decodedPayload;
expect(validityInfo).toBeDefined();
expect(validityInfo.signed).toEqual(new Date('2023-10-24'));
expect(validityInfo.validFrom).toEqual(new Date('2023-10-24'));
expect(validityInfo.validUntil).toEqual(new Date('2050-10-24'));
expect(validityInfo.signed).toEqual(signed);
expect(validityInfo.validFrom).toEqual(signed);
expect(validityInfo.validUntil).toEqual(validUntil);
expect(validityInfo.expectedUpdate).toBeUndefined();
});

it('should contain the device namespaces', () => {
Expand Down
56 changes: 50 additions & 6 deletions __tests__/issuing/issuingMDoc.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ describe('issuing an MDOC', () => {
let encoded: Uint8Array;
let parsedDocument: IssuerSignedDocument;

const signed = new Date('2023-10-24T14:55:18Z');
const validFrom = new Date(signed);
validFrom.setMinutes(signed.getMinutes() + 5);
const validUntil = new Date(signed);
validUntil.setFullYear(signed.getFullYear() + 30);

beforeAll(async () => {
const issuerPrivateKey = ISSUER_PRIVATE_KEY_JWK;

Expand All @@ -23,11 +29,30 @@ describe('issuing an MDOC', () => {
family_name: 'Jones',
given_name: 'Ava',
birth_date: '2007-03-25',
issue_date: '2023-09-01',
expiry_date: '2028-09-30',
issuing_country: 'US',
issuing_authority: 'NY DMV',
document_number: '01-856-5050',
portrait: 'bstr',
driving_privileges: [
{
vehicle_category_code: 'A',
issue_date: '2021-09-02',
expiry_date: '2026-09-20',
},
{
vehicle_category_code: 'B',
issue_date: '2022-09-02',
expiry_date: '2027-09-20',
},
],
})
.useDigestAlgorithm('SHA-512')
.addValidityInfo({
signed: new Date('2023-10-24'),
validUntil: new Date('2050-10-24'),
signed,
validFrom,
validUntil,
})
.addDeviceKeyInfo({ deviceKey: publicKeyJWK })
.sign({
Expand Down Expand Up @@ -58,9 +83,10 @@ describe('issuing an MDOC', () => {
it('should contain the validity info', () => {
const { validityInfo } = parsedDocument.issuerSigned.issuerAuth.decodedPayload;
expect(validityInfo).toBeDefined();
expect(validityInfo.signed).toEqual(new Date('2023-10-24'));
expect(validityInfo.validFrom).toEqual(new Date('2023-10-24'));
expect(validityInfo.validUntil).toEqual(new Date('2050-10-24'));
expect(validityInfo.signed).toEqual(signed);
expect(validityInfo.validFrom).toEqual(validFrom);
expect(validityInfo.validUntil).toEqual(validUntil);
expect(validityInfo.expectedUpdate).toBeUndefined();
});

it('should use the correct digest alg', () => {
Expand All @@ -84,9 +110,27 @@ describe('issuing an MDOC', () => {
{
"age_over_${currentAge}": true,
"age_over_21": ${currentAge >= 21},
"birth_date": "2007-03-25",
"birth_date": 2007-03-25T00:00:00.000Z,
"document_number": "01-856-5050",
"driving_privileges": [
Map {
"vehicle_category_code" => "A",
"issue_date" => 2021-09-02T00:00:00.000Z,
"expiry_date" => 2026-09-20T00:00:00.000Z,
},
Map {
"vehicle_category_code" => "B",
"issue_date" => 2022-09-02T00:00:00.000Z,
"expiry_date" => 2027-09-20T00:00:00.000Z,
},
],
"expiry_date": 2028-09-30T00:00:00.000Z,
"family_name": "Jones",
"given_name": "Ava",
"issue_date": 2023-09-01T00:00:00.000Z,
"issuing_authority": "NY DMV",
"issuing_country": "US",
"portrait": "bstr",
}
`);
});
Expand Down
Loading

0 comments on commit bf0a4ab

Please sign in to comment.