Skip to content

Commit

Permalink
dep updates
Browse files Browse the repository at this point in the history
  • Loading branch information
mharj committed May 19, 2024
1 parent 8eed7de commit 098dbcb
Show file tree
Hide file tree
Showing 17 changed files with 2,136 additions and 640 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
node_modules
7 changes: 4 additions & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"sourceType": "module",
"project": "./tsconfig.test.json"
},
"plugins": ["@typescript-eslint", "prettier", "sonarjs", "deprecation"],
"plugins": ["@typescript-eslint", "@stylistic/ts", "prettier", "sonarjs", "deprecation"],
"extends": [
"standard",
"eslint:recommended",
Expand Down Expand Up @@ -46,7 +46,7 @@
],
"deprecation/deprecation": "warn",
"lines-between-class-members": "off",
"@typescript-eslint/lines-between-class-members": [
"@stylistic/ts/lines-between-class-members": [
"warn",
"always",
{
Expand All @@ -60,6 +60,7 @@
"checksVoidReturn": false
}
],
"@typescript-eslint/consistent-type-definitions": "off"
"@typescript-eslint/consistent-type-definitions": "off",
"@typescript-eslint/consistent-type-imports": ["error", {"prefer": "type-imports", "fixStyle": "inline-type-imports"}]
}
}
2,571 changes: 2,011 additions & 560 deletions package-lock.json

Large diffs are not rendered by default.

40 changes: 26 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@
"name": "mharj-jwt-util",
"version": "0.5.2",
"description": "JWT util",
"main": "dist/index.js",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"require": "./dist/index.js",
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"
}
},
"scripts": {
"build": "tsc",
"build": "tsup src/index.ts --minify --sourcemap --format cjs,esm --dts --clean",
"mocha": "mocha",
"test-all": "npm run lint && npm run mocha",
"prepublishOnly": "npm run build",
Expand Down Expand Up @@ -62,41 +71,44 @@
"dist"
],
"devDependencies": {
"@types/chai": "^4.3.14",
"@stylistic/eslint-plugin": "^2.1.0",
"@swc/core": "^1.5.7",
"@types/chai": "^4.3.16",
"@types/chai-as-promised": "^7.1.8",
"@types/jsonwebtoken": "^9.0.6",
"@types/mocha": "^10.0.6",
"@types/node": "^16.18.96",
"@typescript-eslint/eslint-plugin": "^7.6.0",
"@typescript-eslint/parser": "^7.6.0",
"@types/node": "^16.18.97",
"@typescript-eslint/eslint-plugin": "^7.9.0",
"@typescript-eslint/parser": "^7.9.0",
"chai": "^4.4.1",
"chai-as-promised": "^7.1.1",
"chai-as-promised": "^7.1.2",
"dotenv": "^16.4.5",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-config-standard": "^17.1.0",
"eslint-plugin-deprecation": "^2.0.0",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-sonarjs": "^0.25.1",
"google-auth-library": "^9.7.0",
"googleapis": "^134.0.0",
"google-auth-library": "^9.10.0",
"googleapis": "^137.1.0",
"mocha": "^10.4.0",
"mocha-junit-reporter": "^2.2.1",
"nyc": "^15.1.0",
"prettier": "^3.2.5",
"source-map-support": "^0.5.21",
"tachyon-drive-node-fs": "^0.3.2",
"tachyon-expire-cache": "^0.3.0",
"tachyon-drive-node-fs": "^0.6.0",
"tachyon-expire-cache": "^0.6.0",
"ts-node": "^10.9.2",
"tsup": "^8.0.2",
"typed-emitter": "^2.1.0",
"typescript": "^5.4.5"
},
"dependencies": {
"@avanio/auth-header": "^0.0.1",
"@avanio/expire-cache": "^0.3.2",
"@avanio/expire-cache": "^0.3.3",
"@avanio/logger-like": "^0.1.1",
"jsonwebtoken": "^9.0.2",
"tachyon-drive": "^0.3.5",
"zod": "^3.22.4"
"tachyon-drive": "^0.6.0",
"zod": "^3.23.8"
}
}
16 changes: 8 additions & 8 deletions src/cache/CertCache.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {CertRecords} from '../interfaces/CertRecords';
import {type CertRecords} from '../interfaces/CertRecords';

export abstract class CertCache {
protected updateCallback: ((certs: CertRecords) => void) | undefined;
private ts: number;
protected abstract init(): Promise<void>;
protected abstract load(): Promise<CertRecords>;
protected abstract save(certs: CertRecords): Promise<void>;
private ts: number | undefined;
protected abstract init(): void | Promise<void>;
protected abstract load(): CertRecords | Promise<CertRecords>;
protected abstract save(certs: CertRecords): void | Promise<void>;
protected handleUpdate(certs: CertRecords) {
if (this.updateCallback && certs._ts !== this.ts) {
this.updateCallback(certs);
Expand All @@ -16,15 +16,15 @@ export abstract class CertCache {
this.updateCallback = callback;
}

public handleInit(): Promise<void> {
public handleInit(): void | Promise<void> {
return this.init();
}

public handleLoad(): Promise<CertRecords> {
public handleLoad(): CertRecords | Promise<CertRecords> {
return this.load();
}

public handleSave(certs: CertRecords): Promise<void> {
public handleSave(certs: CertRecords): void | Promise<void> {
this.ts = certs._ts;
return this.save(certs);
}
Expand Down
4 changes: 2 additions & 2 deletions src/cache/FileCertCache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from 'fs';
import {CertRecords, isCertRecords} from '../interfaces/CertRecords';
import {ILoggerLike, ISetOptionalLogger} from '@avanio/logger-like';
import {type CertRecords, isCertRecords} from '../interfaces/CertRecords';
import {type ILoggerLike, type ISetOptionalLogger} from '@avanio/logger-like';
import {CertCache} from './CertCache';

interface IProps {
Expand Down
6 changes: 3 additions & 3 deletions src/cache/TachyonCertCache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {CertRecords, certRecordsSchema} from '../interfaces/CertRecords';
import {IPersistSerializer, IStorageDriver} from 'tachyon-drive';
import {type CertRecords, certRecordsSchema} from '../interfaces/CertRecords';
import {type IPersistSerializer, type IStorageDriver} from 'tachyon-drive';
import {CertCache} from './CertCache';

const initialCerts: CertRecords = {
Expand All @@ -26,7 +26,7 @@ export class TachyonCertCache extends CertCache {
return (await this.driver.hydrate()) ?? initialCerts;
}

protected save(certs: CertRecords): Promise<void> {
protected save(certs: CertRecords): void | Promise<void> {
return this.driver.store(certs);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/token.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import * as jwt from 'jsonwebtoken';
import type {Algorithm} from 'jsonwebtoken';
import {JwtHeaderError} from '../lib/JwtHeaderError';

export type RawJwtToken = `${string}.${string}.${string}`;
Expand All @@ -26,7 +26,7 @@ export type TokenIssuerPayload<T = Record<string, any>> = TokenIssuerPayloadComm

export interface TokenHeader extends Record<string, any> {
kid?: string;
alg: jwt.Algorithm | undefined;
alg: Algorithm | undefined;
typ: string | undefined;
}

Expand Down
17 changes: 17 additions & 0 deletions src/lib/errorUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {z} from 'zod';

export function getError(error: unknown) {
if (error instanceof Error) {
return error;
}
if (typeof error === 'string') {
return new Error(error);
}
return new TypeError(`Unknown error: ${JSON.stringify(error)}`);
}

export function assertZodError(error: unknown): asserts error is z.ZodError {
if (!(error instanceof z.ZodError)) {
throw getError(error);
}
}
1 change: 1 addition & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './rsaPublicKeyPem';
export * from './issuerCertLoader';
export * from './JwtHeaderError';
export * from './jwtValidate';
export * from './errorUtil';
19 changes: 11 additions & 8 deletions src/lib/issuerCertLoader.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import {CertIssuerRecord, CertRecords} from '../interfaces/CertRecords';
import {ExpireCache, ExpireCacheLogMapType} from '@avanio/expire-cache';
import {ILoggerLike, ISetOptionalLogger} from '@avanio/logger-like';
import {OpenIdConfig, openIdConfigSchema} from '../interfaces/OpenIdConfig';
import {OpenIdConfigCerts, openIdConfigCertsSchema} from '../interfaces/OpenIdConfigCerts';
import {CertCache} from '../cache/CertCache';
import {type CertIssuerRecord, type CertRecords} from '../interfaces/CertRecords';
import {ExpireCache, type ExpireCacheLogMapType} from '@avanio/expire-cache';
import {type ILoggerLike, type ISetOptionalLogger} from '@avanio/logger-like';
import {type OpenIdConfig, openIdConfigSchema} from '../interfaces/OpenIdConfig';
import {type OpenIdConfigCerts, openIdConfigCertsSchema} from '../interfaces/OpenIdConfigCerts';
import {type CertCache} from '../cache/CertCache';
import {formatZodError} from './zodUtils';
import {JsonWebKey} from '../interfaces/JsonWebKey';
import {type JsonWebKey} from '../interfaces/JsonWebKey';
import {posix as path} from 'path';
import {rsaPublicKeyPem} from './rsaPublicKeyPem';
import {URL} from 'url';
import {assertZodError, getError} from '.';

export type IssuerCertLoaderOptions = {
/**
Expand Down Expand Up @@ -114,7 +115,7 @@ export class IssuerCertLoader implements ISetOptionalLogger {
await this.saveCerts(); // we have store change
return output;
} catch (e) {
throw new Error(`pullIssuerCerts ${issuerUrl} ${e.message}`);
throw new Error(`pullIssuerCerts ${issuerUrl} ${getError(e).message}`);
}
}

Expand Down Expand Up @@ -218,6 +219,7 @@ export class IssuerCertLoader implements ISetOptionalLogger {
try {
openIdConfigSchema.parse(data);
} catch (e) {
assertZodError(e);
throw formatZodError(e);
}
}
Expand All @@ -232,6 +234,7 @@ export class IssuerCertLoader implements ISetOptionalLogger {
try {
openIdConfigCertsSchema.parse(data);
} catch (e) {
assertZodError(e);
throw formatZodError(e);
}
}
Expand Down
14 changes: 9 additions & 5 deletions src/lib/jwtUtil.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import * as jwt from 'jsonwebtoken';
import {TokenPayload} from '../interfaces/token';
import {verify, type VerifyErrors} from 'jsonwebtoken';
import type {TokenPayload} from '../interfaces/token';

type JwtVerifyPromiseFunc<T = Record<string, unknown>> = (...params: Parameters<typeof jwt.verify>) => Promise<TokenPayload<T> | undefined>;
type JwtVerifyPromiseFunc<T = Record<string, unknown>> = (...params: Parameters<typeof verify>) => Promise<TokenPayload<T> | undefined>;
export const jwtVerifyPromise: JwtVerifyPromiseFunc = (token, secretOrPublicKey, options?) => {
return new Promise<TokenPayload | undefined>((resolve, reject) => {
jwt.verify(token, secretOrPublicKey, options, (err: jwt.VerifyErrors | null, decoded: object | undefined) => {
verify(token, secretOrPublicKey, options, (err: VerifyErrors | null, decoded: object | string | undefined) => {
if (err) {
reject(err);
} else {
resolve(decoded);
if (typeof decoded === 'string') {
resolve(undefined);
} else {
resolve(decoded);
}
}
});
});
Expand Down
14 changes: 7 additions & 7 deletions src/lib/jwtValidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import * as jwt from 'jsonwebtoken';
import {
assertIssuerToken,
assertIsTokenFullDecoded,
FullDecodedIssuerTokenStructure,
FullDecodedTokenStructure,
type FullDecodedIssuerTokenStructure,
type FullDecodedTokenStructure,
isRawJwtToken,
RawJwtToken,
TokenPayload,
type RawJwtToken,
type TokenPayload,
} from '../interfaces/token';
import {ExpireCache, ICacheOrAsync} from '@avanio/expire-cache';
import {ExpireCache, type ICacheOrAsync} from '@avanio/expire-cache';
import {AuthHeader} from '@avanio/auth-header';
import {buildCertFrame} from './rsaPublicKeyPem';
import {CertCache} from '../cache/CertCache';
import {type CertCache} from '../cache/CertCache';
import {getTokenOrAuthHeader} from './authUtil';
import {ILoggerLike} from '@avanio/logger-like';
import {type ILoggerLike} from '@avanio/logger-like';
import {IssuerCertLoader} from './issuerCertLoader';
import {JwtHeaderError} from './JwtHeaderError';
import {jwtVerifyPromise} from './jwtUtil';
Expand Down
2 changes: 1 addition & 1 deletion src/lib/zodUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ZodError, ZodIssue} from 'zod';
import type {ZodError, ZodIssue} from 'zod';

function formatZodIssue(issue: ZodIssue): string {
const {path, message} = issue;
Expand Down
Loading

0 comments on commit 098dbcb

Please sign in to comment.