@block65/auth-token
Advanced tools
| import { CustomError } from '@block65/custom-error'; | ||
| interface AssertionErrorParams { | ||
| message: string; | ||
| actual: unknown; | ||
| expected: unknown; | ||
| } | ||
| export declare class AssertionError extends CustomError { | ||
| expected: unknown; | ||
| actual: unknown; | ||
| constructor(params: AssertionErrorParams, err?: Error); | ||
| constructor(message: string, err?: Error); | ||
| } | ||
| export {}; |
| import { CustomError, Status } from '@block65/custom-error'; | ||
| export class AssertionError extends CustomError { | ||
| expected; | ||
| actual; | ||
| constructor(messageOrParams, err) { | ||
| if (typeof messageOrParams === 'string') { | ||
| super(messageOrParams, err); | ||
| } | ||
| else { | ||
| super(messageOrParams.message, err); | ||
| this.expected = messageOrParams.expected; | ||
| this.actual = messageOrParams.actual; | ||
| } | ||
| this.code = Status.FAILED_PRECONDITION; | ||
| } | ||
| } |
@@ -13,2 +13,3 @@ interface CommonClaims { | ||
| export interface AccessTokenClaims extends CommonClaims { | ||
| client_id: string; | ||
| token_use: 'access'; | ||
@@ -15,0 +16,0 @@ } |
@@ -1,4 +0,2 @@ | ||
| /// <reference types="node" resolution-mode="require"/> | ||
| import { BinaryLike, Encoding, Hash } from 'node:crypto'; | ||
| import type { AnyClaims, IdTokenClaims } from './claims.js'; | ||
| import type { AccessTokenClaims, AnyClaims, IdTokenClaims } from './claims.js'; | ||
| export * from './claims.js'; | ||
@@ -8,3 +6,3 @@ export * from './aws-cognito.js'; | ||
| export * from './auth0.js'; | ||
| export interface AuthToken<TClaims extends AnyClaims = never> { | ||
| export interface AuthToken<TClaims extends AnyClaims = AccessTokenClaims> { | ||
| id: string; | ||
@@ -30,3 +28,2 @@ ips: string[]; | ||
| export declare function withNullProto<T extends Record<string, unknown>>(obj: T): T; | ||
| export declare function sha256(bufferOrString: BinaryLike, encoding?: Encoding): Promise<Hash>; | ||
| export declare function createAuthToken<TClaims extends AnyClaims>({ jwt, claims, ips, }: { | ||
@@ -33,0 +30,0 @@ jwt: string; |
+12
-11
@@ -1,3 +0,3 @@ | ||
| import { AssertionError } from 'node:assert'; | ||
| import { createHash } from 'node:crypto'; | ||
| import '@block65/custom-error'; | ||
| import { AssertionError } from './assertion-error.js'; | ||
| export * from './claims.js'; | ||
@@ -32,8 +32,11 @@ export * from './aws-cognito.js'; | ||
| } | ||
| export async function sha256(bufferOrString, encoding = 'utf-8') { | ||
| const hash = createHash('sha256'); | ||
| if (typeof bufferOrString === 'string') { | ||
| return hash.update(bufferOrString, encoding); | ||
| } | ||
| return hash.update(bufferOrString); | ||
| async function sha256(message) { | ||
| const encoder = new TextEncoder(); | ||
| const data = encoder.encode(message); | ||
| const hashBuffer = await crypto.subtle.digest('SHA-256', data); | ||
| const hashArray = Array.from(new Uint8Array(hashBuffer)); | ||
| const hashHex = hashArray | ||
| .map((b) => b.toString(16).padStart(2, '0')) | ||
| .join(''); | ||
| return hashHex; | ||
| } | ||
@@ -49,5 +52,3 @@ export async function createAuthToken({ jwt, claims, ips, }) { | ||
| assertNumber(exp, 'exp is not a number'); | ||
| const id = jti | ||
| ? String(jti) | ||
| : await sha256(jwt).then((hash) => hash.digest('base64url')); | ||
| const id = jti ? String(jti) : await sha256(jwt); | ||
| return withNullProto(Object.freeze({ | ||
@@ -54,0 +55,0 @@ id, |
+4
-2
| { | ||
| "name": "@block65/auth-token", | ||
| "version": "4.1.0", | ||
| "version": "4.2.0", | ||
| "license": "UNLICENSED", | ||
@@ -24,2 +24,5 @@ "type": "module", | ||
| }, | ||
| "dependencies": { | ||
| "@block65/custom-error": "^11.0.1" | ||
| }, | ||
| "devDependencies": { | ||
@@ -32,3 +35,2 @@ "@babel/core": "^7.20.5", | ||
| "@types/jest": "^29.2.4", | ||
| "@types/node": "^18.11.15", | ||
| "@typescript-eslint/eslint-plugin": ">=5.46.1", | ||
@@ -35,0 +37,0 @@ "@typescript-eslint/parser": ">=5.46.1", |
9225
9.76%14
-6.67%14
16.67%233
13.66%1
Infinity%+ Added
+ Added
+ Added