UNPKG

@authduo/authduo

Version:

Free User-sovereign Authentication for the World

34 lines 1.49 kB
import { Hex } from "@benev/slate"; import { Token } from "./jwt/token.js"; import { CryptoConstants } from "./crypto-constants.js"; import { TokenVerifyError } from "./jwt/types.js"; export class Pubkey { thumbprint; publicKey; constructor(thumbprint, publicKey) { this.thumbprint = thumbprint; this.publicKey = publicKey; } static async fromData(data) { const extractable = true; const publicBuffer = Hex.bytes(data.publicKey).buffer; const thumbprint = Hex.string(new Uint8Array(await crypto.subtle.digest(CryptoConstants.algos.thumbprint, publicBuffer))); if (data.thumbprint !== thumbprint) throw new TokenVerifyError("incorrect thumbprint"); const publicKey = await crypto.subtle.importKey(CryptoConstants.formats.public, publicBuffer, CryptoConstants.algos.generate, extractable, ["verify"]); return new this(thumbprint, publicKey); } async toData() { const publicBuffer = await crypto.subtle .exportKey(CryptoConstants.formats.public, this.publicKey); const thumbprint = Hex.string(new Uint8Array(await crypto.subtle.digest(CryptoConstants.algos.thumbprint, publicBuffer))); return { thumbprint, publicKey: Hex.string(new Uint8Array(publicBuffer)), }; } async verify(token, options = {}) { return await Token.verify(this.publicKey, token, options); } } //# sourceMappingURL=pubkey.js.map