import { DpopStorage } from './storage'; import * as dpopUtils from './utils'; export class Dpop { protected readonly storage: DpopStorage; public constructor(clientId: string) { this.storage = new DpopStorage(clientId); } public getNonce(id?: string): Promise { return this.storage.findNonce(id); } public setNonce(nonce: string, id?: string): Promise { return this.storage.setNonce(nonce, id); } protected async getOrGenerateKeyPair(): Promise { let keyPair = await this.storage.findKeyPair(); if (!keyPair) { keyPair = await dpopUtils.generateKeyPair(); await this.storage.setKeyPair(keyPair); } return keyPair; } public async generateProof(params: { url: string; method: string; nonce?: string; accessToken?: string; }): Promise { const keyPair = await this.getOrGenerateKeyPair(); return dpopUtils.generateProof({ keyPair, ...params }); } public async calculateThumbprint(): Promise { const keyPair = await this.getOrGenerateKeyPair(); return dpopUtils.calculateThumbprint(keyPair); } public async clear(): Promise { await Promise.all([ this.storage.clearNonces(), this.storage.clearKeyPairs() ]); } }