Skip to content

Commit

Permalink
refactor(types): rename type parameters for the KeyLike returns
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Oct 25, 2023
1 parent e2da031 commit eddd400
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 54 deletions.
4 changes: 2 additions & 2 deletions src/jwe/compact/decrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends KeyLike = KeyLike>(
export async function compactDecrypt<KeyLikeType extends KeyLike = KeyLike>(
jwe: string | Uint8Array,
getKey: CompactDecryptGetKey,
options?: DecryptOptions,
): Promise<CompactDecryptResult & ResolvedKey<T>>
): Promise<CompactDecryptResult & ResolvedKey<KeyLikeType>>
export async function compactDecrypt(
jwe: string | Uint8Array,
key: KeyLike | Uint8Array | CompactDecryptGetKey,
Expand Down
4 changes: 2 additions & 2 deletions src/jwe/flattened/decrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends KeyLike = KeyLike>(
export function flattenedDecrypt<KeyLikeType extends KeyLike = KeyLike>(
jwe: FlattenedJWE,
getKey: FlattenedDecryptGetKey,
options?: DecryptOptions,
): Promise<FlattenedDecryptResult & ResolvedKey<T>>
): Promise<FlattenedDecryptResult & ResolvedKey<KeyLikeType>>
export async function flattenedDecrypt(
jwe: FlattenedJWE,
key: KeyLike | Uint8Array | FlattenedDecryptGetKey,
Expand Down
4 changes: 2 additions & 2 deletions src/jwe/general/decrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends KeyLike = KeyLike>(
export function generalDecrypt<KeyLikeType extends KeyLike = KeyLike>(
jwe: GeneralJWE,
getKey: GeneralDecryptGetKey,
options?: DecryptOptions,
): Promise<GeneralDecryptResult & ResolvedKey<T>>
): Promise<GeneralDecryptResult & ResolvedKey<KeyLikeType>>
export async function generalDecrypt(
jwe: GeneralJWE,
key: KeyLike | Uint8Array | GeneralDecryptGetKey,
Expand Down
6 changes: 3 additions & 3 deletions src/jwk/embedded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import { JWSInvalid } from '../util/errors.js'
* console.log(payload)
* ```
*/
export async function EmbeddedJWK<T extends KeyLike = KeyLike>(
export async function EmbeddedJWK<KeyLikeType extends KeyLike = KeyLike>(
protectedHeader?: JWSHeaderParameters,
token?: FlattenedJWSInput,
): Promise<T> {
): Promise<KeyLikeType> {
const joseHeader = {
...protectedHeader,
...token?.header,
Expand All @@ -36,7 +36,7 @@ export async function EmbeddedJWK<T extends KeyLike = KeyLike>(
throw new JWSInvalid('"jwk" (JSON Web Key) Header Parameter must be a JSON object')
}

const key = await importJWK<T>({ ...joseHeader.jwk, ext: true }, joseHeader.alg!)
const key = await importJWK<KeyLikeType>({ ...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')
Expand Down
29 changes: 16 additions & 13 deletions src/jwks/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ function getKtyFromAlg(alg: unknown) {
}
}

interface Cache<T extends KeyLike = KeyLike> {
[alg: string]: T
interface Cache<KeyLikeType extends KeyLike = KeyLike> {
[alg: string]: KeyLikeType
}

/** @private */
Expand Down Expand Up @@ -59,10 +59,10 @@ function clone<T>(obj: T): T {
}

/** @private */
export class LocalJWKSet<T extends KeyLike = KeyLike> {
export class LocalJWKSet<KeyLikeType extends KeyLike = KeyLike> {
protected _jwks?: JSONWebKeySet

private _cached: WeakMap<JWK, Cache<T>> = new WeakMap()
private _cached: WeakMap<JWK, Cache<KeyLikeType>> = new WeakMap()

constructor(jwks: unknown) {
if (!isJWKSLike(jwks)) {
Expand All @@ -72,7 +72,10 @@ export class LocalJWKSet<T extends KeyLike = KeyLike> {
this._jwks = clone<JSONWebKeySet>(jwks)
}

async getKey(protectedHeader?: JWSHeaderParameters, token?: FlattenedJWSInput): Promise<T> {
async getKey(
protectedHeader?: JWSHeaderParameters,
token?: FlattenedJWSInput,
): Promise<KeyLikeType> {
const { alg, kid } = { ...protectedHeader, ...token?.header }
const kty = getKtyFromAlg(alg)

Expand Down Expand Up @@ -137,7 +140,7 @@ export class LocalJWKSet<T extends KeyLike = KeyLike> {
error[Symbol.asyncIterator] = async function* () {
for (const jwk of candidates) {
try {
yield await importWithAlgCache<T>(_cached, jwk, alg!)
yield await importWithAlgCache<KeyLikeType>(_cached, jwk, alg!)
} catch {
continue
}
Expand All @@ -147,18 +150,18 @@ export class LocalJWKSet<T extends KeyLike = KeyLike> {
throw error
}

return importWithAlgCache<T>(this._cached, jwk, alg!)
return importWithAlgCache<KeyLikeType>(this._cached, jwk, alg!)
}
}

async function importWithAlgCache<T extends KeyLike = KeyLike>(
cache: WeakMap<JWK, Cache<T>>,
async function importWithAlgCache<KeyLikeType extends KeyLike = KeyLike>(
cache: WeakMap<JWK, Cache<KeyLikeType>>,
jwk: JWK,
alg: string,
) {
const cached = cache.get(jwk) || cache.set(jwk, {}).get(jwk)!
if (cached[alg] === undefined) {
const key = await importJWK<T>({ ...jwk, ext: true }, alg)
const key = await importJWK<KeyLikeType>({ ...jwk, ext: true }, alg)

if (key instanceof Uint8Array || key.type !== 'public') {
throw new JWKSInvalid('JSON Web Key Set members must be public keys')
Expand Down Expand Up @@ -244,12 +247,12 @@ async function importWithAlgCache<T extends KeyLike = KeyLike>(
*
* @param jwks JSON Web Key Set formatted object.
*/
export function createLocalJWKSet<T extends KeyLike = KeyLike>(jwks: JSONWebKeySet) {
const set = new LocalJWKSet<T>(jwks)
export function createLocalJWKSet<KeyLikeType extends KeyLike = KeyLike>(jwks: JSONWebKeySet) {
const set = new LocalJWKSet<KeyLikeType>(jwks)
return async function (
protectedHeader?: JWSHeaderParameters,
token?: FlattenedJWSInput,
): Promise<T> {
): Promise<KeyLikeType> {
return set.getKey(protectedHeader, token)
}
}
13 changes: 8 additions & 5 deletions src/jwks/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export interface RemoteJWKSetOptions {
headers?: Record<string, string>
}

class RemoteJWKSet<T extends KeyLike = KeyLike> extends LocalJWKSet<T> {
class RemoteJWKSet<KeyLikeType extends KeyLike = KeyLike> extends LocalJWKSet<KeyLikeType> {
private _url: URL

private _timeoutDuration: number
Expand Down Expand Up @@ -94,7 +94,10 @@ class RemoteJWKSet<T extends KeyLike = KeyLike> extends LocalJWKSet<T> {
: false
}

async getKey(protectedHeader?: JWSHeaderParameters, token?: FlattenedJWSInput): Promise<T> {
async getKey(
protectedHeader?: JWSHeaderParameters,
token?: FlattenedJWSInput,
): Promise<KeyLikeType> {
if (!this._jwks || !this.fresh()) {
await this.reload()
}
Expand Down Expand Up @@ -199,15 +202,15 @@ class RemoteJWKSet<T extends KeyLike = KeyLike> extends LocalJWKSet<T> {
* @param url URL to fetch the JSON Web Key Set from.
* @param options Options for the remote JSON Web Key Set.
*/
export function createRemoteJWKSet<T extends KeyLike = KeyLike>(
export function createRemoteJWKSet<KeyLikeType extends KeyLike = KeyLike>(
url: URL,
options?: RemoteJWKSetOptions,
) {
const set = new RemoteJWKSet<T>(url, options)
const set = new RemoteJWKSet<KeyLikeType>(url, options)
return async function (
protectedHeader?: JWSHeaderParameters,
token?: FlattenedJWSInput,
): Promise<T> {
): Promise<KeyLikeType> {
return set.getKey(protectedHeader, token)
}
}
4 changes: 2 additions & 2 deletions src/jws/compact/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends KeyLike = KeyLike>(
export function compactVerify<KeyLikeType extends KeyLike = KeyLike>(
jws: string | Uint8Array,
getKey: CompactVerifyGetKey,
options?: VerifyOptions,
): Promise<CompactVerifyResult & ResolvedKey<T>>
): Promise<CompactVerifyResult & ResolvedKey<KeyLikeType>>
export async function compactVerify(
jws: string | Uint8Array,
key: KeyLike | Uint8Array | CompactVerifyGetKey,
Expand Down
4 changes: 2 additions & 2 deletions src/jws/flattened/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends KeyLike = KeyLike>(
export function flattenedVerify<KeyLikeType extends KeyLike = KeyLike>(
jws: FlattenedJWSInput,
getKey: FlattenedVerifyGetKey,
options?: VerifyOptions,
): Promise<FlattenedVerifyResult & ResolvedKey<T>>
): Promise<FlattenedVerifyResult & ResolvedKey<KeyLikeType>>
export async function flattenedVerify(
jws: FlattenedJWSInput,
key: KeyLike | Uint8Array | FlattenedVerifyGetKey,
Expand Down
4 changes: 2 additions & 2 deletions src/jws/general/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends KeyLike = KeyLike>(
export function generalVerify<KeyLikeType extends KeyLike = KeyLike>(
jws: GeneralJWSInput,
getKey: GeneralVerifyGetKey,
options?: VerifyOptions,
): Promise<GeneralVerifyResult & ResolvedKey<T>>
): Promise<GeneralVerifyResult & ResolvedKey<KeyLikeType>>
export async function generalVerify(
jws: GeneralJWSInput,
key: KeyLike | Uint8Array | GeneralVerifyGetKey,
Expand Down
4 changes: 2 additions & 2 deletions src/jwt/decrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends KeyLike = KeyLike>(
export async function jwtDecrypt<KeyLikeType extends KeyLike = KeyLike>(
jwt: string | Uint8Array,
getKey: JWTDecryptGetKey,
options?: JWTDecryptOptions,
): Promise<JWTDecryptResult & ResolvedKey<T>>
): Promise<JWTDecryptResult & ResolvedKey<KeyLikeType>>
export async function jwtDecrypt(
jwt: string | Uint8Array,
key: KeyLike | Uint8Array | JWTDecryptGetKey,
Expand Down
4 changes: 2 additions & 2 deletions src/jwt/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends KeyLike = KeyLike>(
export async function jwtVerify<KeyLikeType extends KeyLike = KeyLike>(
jwt: string | Uint8Array,
getKey: JWTVerifyGetKey,
options?: JWTVerifyOptions,
): Promise<JWTVerifyResult & ResolvedKey<T>>
): Promise<JWTVerifyResult & ResolvedKey<KeyLikeType>>

export async function jwtVerify(
jwt: string | Uint8Array,
Expand Down
10 changes: 5 additions & 5 deletions src/key/generate_key_pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { generateKeyPair as generate } from '../runtime/generate.js'

import type { KeyLike } from '../types.d'

export interface GenerateKeyPairResult<T extends KeyLike = KeyLike> {
export interface GenerateKeyPairResult<KeyLikeType extends KeyLike = KeyLike> {
/** The generated Private Key. */
privateKey: T
privateKey: KeyLikeType

/** Public Key corresponding to the generated Private Key. */
publicKey: T
publicKey: KeyLikeType
}

export interface GenerateKeyPairOptions {
Expand Down Expand Up @@ -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<T extends KeyLike = KeyLike>(
export async function generateKeyPair<KeyLikeType extends KeyLike = KeyLike>(
alg: string,
options?: GenerateKeyPairOptions,
): Promise<GenerateKeyPairResult<T>> {
): Promise<GenerateKeyPairResult<KeyLikeType>> {
// @ts-ignore
return generate(alg, options)
}
4 changes: 2 additions & 2 deletions src/key/generate_secret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends KeyLike = KeyLike>(
export async function generateSecret<KeyLikeType extends KeyLike = KeyLike>(
alg: string,
options?: GenerateSecretOptions,
): Promise<T | Uint8Array> {
): Promise<KeyLikeType | Uint8Array> {
// @ts-ignore
return generate(alg, options)
}
16 changes: 8 additions & 8 deletions src/key/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends KeyLike = KeyLike>(
export async function importSPKI<KeyLikeType extends KeyLike = KeyLike>(
spki: string,
alg: string,
options?: PEMImportOptions,
): Promise<T> {
): Promise<KeyLikeType> {
if (typeof spki !== 'string' || spki.indexOf('-----BEGIN PUBLIC KEY-----') !== 0) {
throw new TypeError('"spki" must be SPKI formatted string')
}
Expand Down Expand Up @@ -73,11 +73,11 @@ export async function importSPKI<T extends KeyLike = KeyLike>(
* 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<T extends KeyLike = KeyLike>(
export async function importX509<KeyLikeType extends KeyLike = KeyLike>(
x509: string,
alg: string,
options?: PEMImportOptions,
): Promise<T> {
): Promise<KeyLikeType> {
if (typeof x509 !== 'string' || x509.indexOf('-----BEGIN CERTIFICATE-----') !== 0) {
throw new TypeError('"x509" must be X.509 formatted string')
}
Expand Down Expand Up @@ -106,11 +106,11 @@ export async function importX509<T extends KeyLike = KeyLike>(
* 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<T extends KeyLike = KeyLike>(
export async function importPKCS8<KeyLikeType extends KeyLike = KeyLike>(
pkcs8: string,
alg: string,
options?: PEMImportOptions,
): Promise<T> {
): Promise<KeyLikeType> {
if (typeof pkcs8 !== 'string' || pkcs8.indexOf('-----BEGIN PRIVATE KEY-----') !== 0) {
throw new TypeError('"pkcs8" must be PKCS#8 formatted string')
}
Expand Down Expand Up @@ -153,10 +153,10 @@ export async function importPKCS8<T extends KeyLike = KeyLike>(
* in Web Crypto API runtimes. See
* {@link https://github.com/panva/jose/issues/210 Algorithm Key Requirements}.
*/
export async function importJWK<T extends KeyLike = KeyLike>(
export async function importJWK<KeyLikeType extends KeyLike = KeyLike>(
jwk: JWK,
alg?: string,
): Promise<T | Uint8Array> {
): Promise<KeyLikeType | Uint8Array> {
if (!isObject(jwk)) {
throw new TypeError('JWK must be an object')
}
Expand Down
4 changes: 2 additions & 2 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,9 +606,9 @@ export interface JWTDecryptResult {
protectedHeader: CompactJWEHeaderParameters
}

export interface ResolvedKey<T extends KeyLike = KeyLike> {
export interface ResolvedKey<KeyLikeType extends KeyLike = KeyLike> {
/** 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. */
Expand Down

0 comments on commit eddd400

Please sign in to comment.