Skip to content

Commit

Permalink
Merge pull request #24 from auth0-lab/fix_dates
Browse files Browse the repository at this point in the history
feat: use the proper cbor tag code for Dates
  • Loading branch information
jfromaniello authored Oct 18, 2024
2 parents 33c849a + 210757c commit 87a39cb
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 119 deletions.
6 changes: 3 additions & 3 deletions __tests__/__snapshots__/diagnostic.tests.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ exports[`diagnostic info should return the version 1`] = `
"isValid": true,
"matchCertificate": undefined,
"ns": "org.iso.18013.5.1",
"value": 1980-06-15T00:00:00.000Z,
"value": "1980-06-15",
},
{
"id": "issue_date",
"isValid": true,
"matchCertificate": undefined,
"ns": "org.iso.18013.5.1",
"value": 2023-03-01T00:00:00.000Z,
"value": "2023-03-01",
},
{
"id": "expiry_date",
"isValid": true,
"matchCertificate": undefined,
"ns": "org.iso.18013.5.1",
"value": 2028-03-31T00:00:00.000Z,
"value": "2028-03-31",
},
{
"id": "issuing_country",
Expand Down
6 changes: 3 additions & 3 deletions __tests__/example/__snapshots__/example4.tests.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ exports[`example 4: device response with device attributes should get the right
"isValid": true,
"matchCertificate": undefined,
"ns": "org.iso.18013.5.1",
"value": 1980-06-15T00:00:00.000Z,
"value": "1980-06-15",
},
{
"id": "issue_date",
"isValid": true,
"matchCertificate": undefined,
"ns": "org.iso.18013.5.1",
"value": 2023-03-01T00:00:00.000Z,
"value": "2023-03-01",
},
{
"id": "expiry_date",
"isValid": true,
"matchCertificate": undefined,
"ns": "org.iso.18013.5.1",
"value": 2028-03-31T00:00:00.000Z,
"value": "2028-03-31",
},
{
"id": "issuing_country",
Expand Down
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
54 changes: 49 additions & 5 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 @@ -85,8 +111,26 @@ describe('issuing an MDOC', () => {
"age_over_${currentAge}": true,
"age_over_21": ${currentAge >= 21},
"birth_date": "2007-03-25",
"document_number": "01-856-5050",
"driving_privileges": [
Map {
"vehicle_category_code" => "A",
"issue_date" => "2021-09-02",
"expiry_date" => "2026-09-20",
},
Map {
"vehicle_category_code" => "B",
"issue_date" => "2022-09-02",
"expiry_date" => "2027-09-20",
},
],
"expiry_date": "2028-09-30",
"family_name": "Jones",
"given_name": "Ava",
"issue_date": "2023-09-01",
"issuing_authority": "NY DMV",
"issuing_country": "US",
"portrait": "bstr",
}
`);
});
Expand Down
Loading

0 comments on commit 87a39cb

Please sign in to comment.