From eddd400235e84e3d84c1a8471b01915a12d3d866 Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Wed, 18 Oct 2023 11:48:31 +0200 Subject: [PATCH] refactor(types): rename type parameters for the KeyLike returns --- src/jwe/compact/decrypt.ts | 4 ++-- src/jwe/flattened/decrypt.ts | 4 ++-- src/jwe/general/decrypt.ts | 4 ++-- src/jwk/embedded.ts | 6 +++--- src/jwks/local.ts | 29 ++++++++++++++++------------- src/jwks/remote.ts | 13 ++++++++----- src/jws/compact/verify.ts | 4 ++-- src/jws/flattened/verify.ts | 4 ++-- src/jws/general/verify.ts | 4 ++-- src/jwt/decrypt.ts | 4 ++-- src/jwt/verify.ts | 4 ++-- src/key/generate_key_pair.ts | 10 +++++----- src/key/generate_secret.ts | 4 ++-- src/key/import.ts | 16 ++++++++-------- src/types.d.ts | 4 ++-- 15 files changed, 60 insertions(+), 54 deletions(-) diff --git a/src/jwe/compact/decrypt.ts b/src/jwe/compact/decrypt.ts index 161bcd8b19..e9f689c5d1 100644 --- a/src/jwe/compact/decrypt.ts +++ b/src/jwe/compact/decrypt.ts @@ -49,11 +49,11 @@ export async function compactDecrypt( * {@link https://github.com/panva/jose/issues/210#jwe-alg Algorithm Key Requirements}. * @param options JWE Decryption options. */ -export async function compactDecrypt( +export async function compactDecrypt( jwe: string | Uint8Array, getKey: CompactDecryptGetKey, options?: DecryptOptions, -): Promise> +): Promise> export async function compactDecrypt( jwe: string | Uint8Array, key: KeyLike | Uint8Array | CompactDecryptGetKey, diff --git a/src/jwe/flattened/decrypt.ts b/src/jwe/flattened/decrypt.ts index 3456e2ee1a..69d60f1801 100644 --- a/src/jwe/flattened/decrypt.ts +++ b/src/jwe/flattened/decrypt.ts @@ -67,11 +67,11 @@ export function flattenedDecrypt( * {@link https://github.com/panva/jose/issues/210#jwe-alg Algorithm Key Requirements}. * @param options JWE Decryption options. */ -export function flattenedDecrypt( +export function flattenedDecrypt( jwe: FlattenedJWE, getKey: FlattenedDecryptGetKey, options?: DecryptOptions, -): Promise> +): Promise> export async function flattenedDecrypt( jwe: FlattenedJWE, key: KeyLike | Uint8Array | FlattenedDecryptGetKey, diff --git a/src/jwe/general/decrypt.ts b/src/jwe/general/decrypt.ts index 009a87828b..c194917f5c 100644 --- a/src/jwe/general/decrypt.ts +++ b/src/jwe/general/decrypt.ts @@ -63,11 +63,11 @@ export function generalDecrypt( * {@link https://github.com/panva/jose/issues/210#jwe-alg Algorithm Key Requirements}. * @param options JWE Decryption options. */ -export function generalDecrypt( +export function generalDecrypt( jwe: GeneralJWE, getKey: GeneralDecryptGetKey, options?: DecryptOptions, -): Promise> +): Promise> export async function generalDecrypt( jwe: GeneralJWE, key: KeyLike | Uint8Array | GeneralDecryptGetKey, diff --git a/src/jwk/embedded.ts b/src/jwk/embedded.ts index c2c6a7894f..e43b48e748 100644 --- a/src/jwk/embedded.ts +++ b/src/jwk/embedded.ts @@ -24,10 +24,10 @@ import { JWSInvalid } from '../util/errors.js' * console.log(payload) * ``` */ -export async function EmbeddedJWK( +export async function EmbeddedJWK( protectedHeader?: JWSHeaderParameters, token?: FlattenedJWSInput, -): Promise { +): Promise { const joseHeader = { ...protectedHeader, ...token?.header, @@ -36,7 +36,7 @@ export async function EmbeddedJWK( throw new JWSInvalid('"jwk" (JSON Web Key) Header Parameter must be a JSON object') } - const key = await importJWK({ ...joseHeader.jwk, ext: true }, joseHeader.alg!) + const key = await importJWK({ ...joseHeader.jwk, ext: true }, joseHeader.alg!) if (key instanceof Uint8Array || key.type !== 'public') { throw new JWSInvalid('"jwk" (JSON Web Key) Header Parameter must be a public key') diff --git a/src/jwks/local.ts b/src/jwks/local.ts index f5d583ab80..ece0bd02b8 100644 --- a/src/jwks/local.ts +++ b/src/jwks/local.ts @@ -28,8 +28,8 @@ function getKtyFromAlg(alg: unknown) { } } -interface Cache { - [alg: string]: T +interface Cache { + [alg: string]: KeyLikeType } /** @private */ @@ -59,10 +59,10 @@ function clone(obj: T): T { } /** @private */ -export class LocalJWKSet { +export class LocalJWKSet { protected _jwks?: JSONWebKeySet - private _cached: WeakMap> = new WeakMap() + private _cached: WeakMap> = new WeakMap() constructor(jwks: unknown) { if (!isJWKSLike(jwks)) { @@ -72,7 +72,10 @@ export class LocalJWKSet { this._jwks = clone(jwks) } - async getKey(protectedHeader?: JWSHeaderParameters, token?: FlattenedJWSInput): Promise { + async getKey( + protectedHeader?: JWSHeaderParameters, + token?: FlattenedJWSInput, + ): Promise { const { alg, kid } = { ...protectedHeader, ...token?.header } const kty = getKtyFromAlg(alg) @@ -137,7 +140,7 @@ export class LocalJWKSet { error[Symbol.asyncIterator] = async function* () { for (const jwk of candidates) { try { - yield await importWithAlgCache(_cached, jwk, alg!) + yield await importWithAlgCache(_cached, jwk, alg!) } catch { continue } @@ -147,18 +150,18 @@ export class LocalJWKSet { throw error } - return importWithAlgCache(this._cached, jwk, alg!) + return importWithAlgCache(this._cached, jwk, alg!) } } -async function importWithAlgCache( - cache: WeakMap>, +async function importWithAlgCache( + cache: WeakMap>, jwk: JWK, alg: string, ) { const cached = cache.get(jwk) || cache.set(jwk, {}).get(jwk)! if (cached[alg] === undefined) { - const key = await importJWK({ ...jwk, ext: true }, alg) + const key = await importJWK({ ...jwk, ext: true }, alg) if (key instanceof Uint8Array || key.type !== 'public') { throw new JWKSInvalid('JSON Web Key Set members must be public keys') @@ -244,12 +247,12 @@ async function importWithAlgCache( * * @param jwks JSON Web Key Set formatted object. */ -export function createLocalJWKSet(jwks: JSONWebKeySet) { - const set = new LocalJWKSet(jwks) +export function createLocalJWKSet(jwks: JSONWebKeySet) { + const set = new LocalJWKSet(jwks) return async function ( protectedHeader?: JWSHeaderParameters, token?: FlattenedJWSInput, - ): Promise { + ): Promise { return set.getKey(protectedHeader, token) } } diff --git a/src/jwks/remote.ts b/src/jwks/remote.ts index 9964064555..09c291fc48 100644 --- a/src/jwks/remote.ts +++ b/src/jwks/remote.ts @@ -50,7 +50,7 @@ export interface RemoteJWKSetOptions { headers?: Record } -class RemoteJWKSet extends LocalJWKSet { +class RemoteJWKSet extends LocalJWKSet { private _url: URL private _timeoutDuration: number @@ -94,7 +94,10 @@ class RemoteJWKSet extends LocalJWKSet { : false } - async getKey(protectedHeader?: JWSHeaderParameters, token?: FlattenedJWSInput): Promise { + async getKey( + protectedHeader?: JWSHeaderParameters, + token?: FlattenedJWSInput, + ): Promise { if (!this._jwks || !this.fresh()) { await this.reload() } @@ -199,15 +202,15 @@ class RemoteJWKSet extends LocalJWKSet { * @param url URL to fetch the JSON Web Key Set from. * @param options Options for the remote JSON Web Key Set. */ -export function createRemoteJWKSet( +export function createRemoteJWKSet( url: URL, options?: RemoteJWKSetOptions, ) { - const set = new RemoteJWKSet(url, options) + const set = new RemoteJWKSet(url, options) return async function ( protectedHeader?: JWSHeaderParameters, token?: FlattenedJWSInput, - ): Promise { + ): Promise { return set.getKey(protectedHeader, token) } } diff --git a/src/jws/compact/verify.ts b/src/jws/compact/verify.ts index 0bb4084746..cf60caeb75 100644 --- a/src/jws/compact/verify.ts +++ b/src/jws/compact/verify.ts @@ -51,11 +51,11 @@ export function compactVerify( * {@link https://github.com/panva/jose/issues/210#jws-alg Algorithm Key Requirements}. * @param options JWS Verify options. */ -export function compactVerify( +export function compactVerify( jws: string | Uint8Array, getKey: CompactVerifyGetKey, options?: VerifyOptions, -): Promise> +): Promise> export async function compactVerify( jws: string | Uint8Array, key: KeyLike | Uint8Array | CompactVerifyGetKey, diff --git a/src/jws/flattened/verify.ts b/src/jws/flattened/verify.ts index 38d5176640..3121b327a3 100644 --- a/src/jws/flattened/verify.ts +++ b/src/jws/flattened/verify.ts @@ -64,11 +64,11 @@ export function flattenedVerify( * {@link https://github.com/panva/jose/issues/210#jws-alg Algorithm Key Requirements}. * @param options JWS Verify options. */ -export function flattenedVerify( +export function flattenedVerify( jws: FlattenedJWSInput, getKey: FlattenedVerifyGetKey, options?: VerifyOptions, -): Promise> +): Promise> export async function flattenedVerify( jws: FlattenedJWSInput, key: KeyLike | Uint8Array | FlattenedVerifyGetKey, diff --git a/src/jws/general/verify.ts b/src/jws/general/verify.ts index 96168aaf88..f2cf013c01 100644 --- a/src/jws/general/verify.ts +++ b/src/jws/general/verify.ts @@ -60,11 +60,11 @@ export function generalVerify( * {@link https://github.com/panva/jose/issues/210#jws-alg Algorithm Key Requirements}. * @param options JWS Verify options. */ -export function generalVerify( +export function generalVerify( jws: GeneralJWSInput, getKey: GeneralVerifyGetKey, options?: VerifyOptions, -): Promise> +): Promise> export async function generalVerify( jws: GeneralJWSInput, key: KeyLike | Uint8Array | GeneralVerifyGetKey, diff --git a/src/jwt/decrypt.ts b/src/jwt/decrypt.ts index 3be8372bfb..76339f7329 100644 --- a/src/jwt/decrypt.ts +++ b/src/jwt/decrypt.ts @@ -58,11 +58,11 @@ export async function jwtDecrypt( * {@link https://github.com/panva/jose/issues/210#jwe-alg Algorithm Key Requirements}. * @param options JWT Decryption and JWT Claims Set validation options. */ -export async function jwtDecrypt( +export async function jwtDecrypt( jwt: string | Uint8Array, getKey: JWTDecryptGetKey, options?: JWTDecryptOptions, -): Promise> +): Promise> export async function jwtDecrypt( jwt: string | Uint8Array, key: KeyLike | Uint8Array | JWTDecryptGetKey, diff --git a/src/jwt/verify.ts b/src/jwt/verify.ts index 07d3621cc9..7c0f6da57c 100644 --- a/src/jwt/verify.ts +++ b/src/jwt/verify.ts @@ -123,11 +123,11 @@ export async function jwtVerify( * {@link https://github.com/panva/jose/issues/210#jws-alg Algorithm Key Requirements}. * @param options JWT Decryption and JWT Claims Set validation options. */ -export async function jwtVerify( +export async function jwtVerify( jwt: string | Uint8Array, getKey: JWTVerifyGetKey, options?: JWTVerifyOptions, -): Promise> +): Promise> export async function jwtVerify( jwt: string | Uint8Array, diff --git a/src/key/generate_key_pair.ts b/src/key/generate_key_pair.ts index 78203e063a..c12c1aaef5 100644 --- a/src/key/generate_key_pair.ts +++ b/src/key/generate_key_pair.ts @@ -2,12 +2,12 @@ import { generateKeyPair as generate } from '../runtime/generate.js' import type { KeyLike } from '../types.d' -export interface GenerateKeyPairResult { +export interface GenerateKeyPairResult { /** The generated Private Key. */ - privateKey: T + privateKey: KeyLikeType /** Public Key corresponding to the generated Private Key. */ - publicKey: T + publicKey: KeyLikeType } export interface GenerateKeyPairOptions { @@ -60,10 +60,10 @@ export interface GenerateKeyPairOptions { * @param alg JWA Algorithm Identifier to be used with the generated key pair. * @param options Additional options passed down to the key pair generation. */ -export async function generateKeyPair( +export async function generateKeyPair( alg: string, options?: GenerateKeyPairOptions, -): Promise> { +): Promise> { // @ts-ignore return generate(alg, options) } diff --git a/src/key/generate_secret.ts b/src/key/generate_secret.ts index 952cbb39e6..5d854eefc1 100644 --- a/src/key/generate_secret.ts +++ b/src/key/generate_secret.ts @@ -27,10 +27,10 @@ export interface GenerateSecretOptions { * @param alg JWA Algorithm Identifier to be used with the generated secret. * @param options Additional options passed down to the secret generation. */ -export async function generateSecret( +export async function generateSecret( alg: string, options?: GenerateSecretOptions, -): Promise { +): Promise { // @ts-ignore return generate(alg, options) } diff --git a/src/key/import.ts b/src/key/import.ts index 22b3cfaa8a..42edf59cde 100644 --- a/src/key/import.ts +++ b/src/key/import.ts @@ -35,11 +35,11 @@ export interface PEMImportOptions { * with the imported key, its presence is only enforced in Web Crypto API runtimes. See * {@link https://github.com/panva/jose/issues/210 Algorithm Key Requirements}. */ -export async function importSPKI( +export async function importSPKI( spki: string, alg: string, options?: PEMImportOptions, -): Promise { +): Promise { if (typeof spki !== 'string' || spki.indexOf('-----BEGIN PUBLIC KEY-----') !== 0) { throw new TypeError('"spki" must be SPKI formatted string') } @@ -73,11 +73,11 @@ export async function importSPKI( * with the imported key, its presence is only enforced in Web Crypto API runtimes. See * {@link https://github.com/panva/jose/issues/210 Algorithm Key Requirements}. */ -export async function importX509( +export async function importX509( x509: string, alg: string, options?: PEMImportOptions, -): Promise { +): Promise { if (typeof x509 !== 'string' || x509.indexOf('-----BEGIN CERTIFICATE-----') !== 0) { throw new TypeError('"x509" must be X.509 formatted string') } @@ -106,11 +106,11 @@ export async function importX509( * with the imported key, its presence is only enforced in Web Crypto API runtimes. See * {@link https://github.com/panva/jose/issues/210 Algorithm Key Requirements}. */ -export async function importPKCS8( +export async function importPKCS8( pkcs8: string, alg: string, options?: PEMImportOptions, -): Promise { +): Promise { if (typeof pkcs8 !== 'string' || pkcs8.indexOf('-----BEGIN PRIVATE KEY-----') !== 0) { throw new TypeError('"pkcs8" must be PKCS#8 formatted string') } @@ -153,10 +153,10 @@ export async function importPKCS8( * in Web Crypto API runtimes. See * {@link https://github.com/panva/jose/issues/210 Algorithm Key Requirements}. */ -export async function importJWK( +export async function importJWK( jwk: JWK, alg?: string, -): Promise { +): Promise { if (!isObject(jwk)) { throw new TypeError('JWK must be an object') } diff --git a/src/types.d.ts b/src/types.d.ts index 449df91c40..28ae8197d2 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -606,9 +606,9 @@ export interface JWTDecryptResult { protectedHeader: CompactJWEHeaderParameters } -export interface ResolvedKey { +export interface ResolvedKey { /** Key resolved from the key resolver function. */ - key: T | Uint8Array + key: KeyLikeType | Uint8Array } /** Recognized Compact JWS Header Parameters, any other Header Members may also be present. */