Skip to content

Commit

Permalink
feature/attestation: Add secondsToExpiry
Browse files Browse the repository at this point in the history
  • Loading branch information
tbergmueller committed May 29, 2024
1 parent b851d16 commit 5a1ccbd
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"dist"
],
"scripts": {
"test": "jest",
"test": "jest --verbose",
"build": "tsup"
},
"repository": {
Expand Down
25 changes: 24 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@ type AttestationPayload = {
*/
extrefs: Array<object>;

// TODO location, gtin, exp, iat, aud, _v etc
/**
* Expiry date, see PASETO-docs for details.
*/
exp: string;

/**
* Issuing date, see PASETO-docs for details
*/
iat: string;

// TODO location, gtin, aud, _v etc
}

/**
Expand Down Expand Up @@ -104,7 +114,20 @@ class Attestation {
*/
public getPayload(): AttestationPayload {
return this.payload;
}

/**
* Provides time to expiry relative to a `from` time. Returns negative numbers if expired.
* @param from The reference time, Date.now() if not provided
* @returns Seconds left until expiry in seconds.
*/
public secondsToExpiry(from: string | Date = new Date()): number {
const expiration = new Date(this.payload.exp).getTime();
const fromTime = from ? new Date(from!).getTime() : Date.now();

const timeLeft: number = expiration - fromTime;

return timeLeft / 1000.0;
}
}

Expand Down
46 changes: 46 additions & 0 deletions tests/attestation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { AttestationError, attestationDecoder} from '../src';
import { describe, test, expect } from "@jest/globals"

const KEY_ID = "k4.pid.2uab3h18sgaYX1PKFW3OIMvGIfAMnuwWBJ6TuCbuwQii";
const KEY_PUBKEY = "k4.public.f2AxH__c3AQy_abwIYAZvwzLYrLPAUNH5o6cFzPj1_0";


describe('Attestation utility tests', () => {
// This token will expire 2030-01-01T00:00:00Z. Expect the test to fail then!
const testToken: string = "v4.public.eyJhdWQiOiJleGFtcGxlLmNvbSIsImV4cCI6IjIwMzAtMDEtMDFUMDA6MDA6MDBaIiwiaWF0IjoiMjAyMy0wNC0yMFQxNjo1NDowMVoiLCJqdGkiOiJmOGIxZDdmNzNiNzEzYWY0M2FkNTllMzNiN2MwMmRmNSIsInJlc3VsdCI6IkFVVEhFTlRJQyIsInNsaWQiOiJaNDVKQkpSNlM5IiwibG9jYXRpb24iOnsibGF0Ijo0Ny43OTQ2LCJsb24iOjEyLjk4NjR9LCJleHRyZWZzIjpbImZvbyIseyJiYXIiOiJiYXoifSwxMjNdffeoKRK7wfueWl9ti4h9JTYM2ZOXOPgHMOq-6eRxFEKFUYz1LLcNxUp9JtHHY-FD5pHxP9OQ9nOg_izxMwK3GgU.eyJraWQiOiAiazQucGlkLjJ1YWIzaDE4c2dhWVgxUEtGVzNPSU12R0lmQU1udXdXQko2VHVDYnV3UWlpIn0";

beforeEach(() => {
// From https://docs.authenticvision.com/sdk/pdf/sip-v4-paseto.pdf
attestationDecoder.clearCaches();
attestationDecoder.addKey(KEY_ID, KEY_PUBKEY)
});

it('SecondsToExpiry relative to defined time', async () => {
const attestation = await attestationDecoder.decode(testToken);
const SecondsBeforeExpiry = "2029-12-31T23:59:30Z"; // 30 sec left
// TODO maybe add tests that define the tolerance, i.e. to how many MS is this accurate etc
expect(attestation?.secondsToExpiry(SecondsBeforeExpiry)).toEqual(30.0);
});

it('SecondsToExpiry relative to defined time', async () => {
const attestation = await attestationDecoder.decode(testToken);
const timeNow = new Date().getTime();
const expTime = new Date(attestation!.getPayload().exp).getTime()

// time left
// Note this test will start failing starting 2030-01-01, because the time left becomes negative!
const timeInSecondsLeft = (expTime - timeNow) / 1000.0;
const eps = 0.5; // 0.5 sec grace for test runtime
expect(attestation?.secondsToExpiry()).toBeGreaterThanOrEqual(timeInSecondsLeft);
expect(attestation?.secondsToExpiry()).toBeLessThanOrEqual(timeInSecondsLeft + eps);
});

it('SecondsToExpiry negative if expired', async () => {
const attestation = await attestationDecoder.decode(testToken);
const SecondsBeforeExpiry = "2030-01-01T00:01:00Z"; // expired for 60sec
// TODO maybe add tests that define the tolerance, i.e. to how many MS is this accurate etc
expect(attestation?.secondsToExpiry(SecondsBeforeExpiry)).toEqual(-60.0);
});


});

0 comments on commit 5a1ccbd

Please sign in to comment.