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0.2.4/dist/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","// eslint-disable-next-line @typescript-eslint/no-empty-object-type\nexport type Simplify<T> = { [K in keyof T]: T[K] } & {}\n\nexport type RequiredKey<T, K extends keyof T = never> = Simplify<\n  T & {\n    [L in K]-?: unknown extends T[L]\n      ? NonNullable<unknown> | null\n      : Exclude<T[L], undefined>\n  }\n>\n\nexport function either<T extends string | number | boolean>(\n  a?: T,\n  b?: T,\n): T | undefined {\n  if (a != null && b != null && a !== b) {\n    throw new TypeError(`Expected \"${b}\", got \"${a}\"`)\n  }\n  return a ?? b ?? undefined\n}\n","import {\n  type GenerateKeyPairOptions,\n  type GenerateKeyPairResult,\n  type JWK,\n  type JWTVerifyOptions,\n  type KeyLike,\n  SignJWT,\n  errors,\n  exportJWK,\n  generateKeyPair,\n  importJWK,\n  importPKCS8,\n  jwtVerify,\n} from 'jose'\nimport {\n  type Jwk,\n  JwkError,\n  JwtCreateError,\n  type JwtHeader,\n  type JwtPayload,\n  JwtVerifyError,\n  Key,\n  type SignedJwt,\n  type VerifyOptions,\n  type VerifyResult,\n  isPrivateJwk,\n  jwkSchema,\n  jwtHeaderSchema,\n  jwtPayloadSchema,\n} from '@atproto/jwk'\nimport { type RequiredKey, either } from './util.js'\n\nconst { JOSEError } = errors\n\nexport {\n  type GenerateKeyPairOptions,\n  type GenerateKeyPairResult,\n  type Jwk,\n  type JwtHeader,\n  type JwtPayload,\n  type KeyLike,\n  type SignedJwt,\n  type VerifyOptions,\n}\n\nexport class JoseKey<J extends Jwk = Jwk> extends Key<J> {\n  /**\n   * Some runtimes (e.g. Bun) require an `alg` second argument to be set when\n   * invoking `importJWK`. In order to be compatible with these runtimes, we\n   * provide the following method to ensure the `alg` is always set. We also\n   * take the opportunity to ensure that the `alg` is compatible with this key.\n   */\n  protected async getKeyObj(alg: string) {\n    if (!this.algorithms.includes(alg)) {\n      throw new JwkError(`Key cannot be used with algorithm \"${alg}\"`)\n    }\n    try {\n      return await importJWK(this.jwk as JWK, alg)\n    } catch (cause) {\n      throw new JwkError('Failed to import JWK', undefined, { cause })\n    }\n  }\n\n  async createJwt(header: JwtHeader, payload: JwtPayload): Promise<SignedJwt> {\n    try {\n      const { kid } = header\n      if (kid && kid !== this.kid) {\n        throw new JwtCreateError(\n          `Invalid \"kid\" (${kid}) used to sign with key \"${this.kid}\"`,\n        )\n      }\n\n      const { alg } = header\n      if (!alg) {\n        throw new JwtCreateError('Missing \"alg\" in JWT header')\n      }\n\n      const keyObj = await this.getKeyObj(alg)\n      const jwtBuilder = new SignJWT(payload).setProtectedHeader({\n        ...header,\n        alg,\n        kid: this.kid,\n      })\n\n      const signedJwt = await jwtBuilder.sign(keyObj)\n\n      return signedJwt as SignedJwt\n    } catch (cause) {\n      if (cause instanceof JOSEError) {\n        throw new JwtCreateError(cause.message, cause.code, { cause })\n      } else {\n        throw JwtCreateError.from(cause)\n      }\n    }\n  }\n\n  async verifyJwt<C extends string = never>(\n    token: SignedJwt,\n    options?: VerifyOptions<C>,\n  ): Promise<VerifyResult<C>> {\n    try {\n      const result = await jwtVerify(\n        token,\n        async ({ alg }) => this.getKeyObj(alg),\n        { ...options, algorithms: this.algorithms } as JWTVerifyOptions,\n      )\n\n      // @NOTE if all tokens are signed exclusively through createJwt(), then\n      // there should be no need to parse the payload and headers here. But\n      // since the JWT could have been signed with the same key from somewhere\n      // else, let's parse it to ensure the integrity (and type safety) of the\n      // data.\n      const headerParsed = jwtHeaderSchema.safeParse(result.protectedHeader)\n      if (!headerParsed.success) {\n        throw new JwtVerifyError('Invalid JWT header', undefined, {\n          cause: headerParsed.error,\n        })\n      }\n\n      const payloadParsed = jwtPayloadSchema.safeParse(result.payload)\n      if (!payloadParsed.success) {\n        throw new JwtVerifyError('Invalid JWT payload', undefined, {\n          cause: payloadParsed.error,\n        })\n      }\n\n      return {\n        protectedHeader: headerParsed.data,\n        // \"requiredClaims\" enforced by jwtVerify()\n        payload: payloadParsed.data as RequiredKey<JwtPayload, C>,\n      }\n    } catch (cause) {\n      if (cause instanceof JOSEError) {\n        throw new JwtVerifyError(cause.message, cause.code, { cause })\n      } else {\n        throw JwtVerifyError.from(cause)\n      }\n    }\n  }\n\n  static async generateKeyPair(\n    allowedAlgos: readonly string[] = ['ES256'],\n    options?: GenerateKeyPairOptions,\n  ) {\n    if (!allowedAlgos.length) {\n      throw new JwkError('No algorithms provided for key generation')\n    }\n\n    const errors: unknown[] = []\n    for (const alg of allowedAlgos) {\n      try {\n        return await generateKeyPair(alg, options)\n      } catch (err) {\n        errors.push(err)\n      }\n    }\n\n    throw new JwkError('Failed to generate key pair', undefined, {\n      cause: new AggregateError(errors, 'None of the algorithms worked'),\n    })\n  }\n\n  static async generate(\n    allowedAlgos: string[] = ['ES256'],\n    kid?: string,\n    options?: Omit<GenerateKeyPairOptions, 'extractable'>,\n  ): Promise<JoseKey> {\n    const kp = await this.generateKeyPair(allowedAlgos, {\n      ...options,\n      extractable: true,\n    })\n    return this.fromKeyLike(kp.privateKey, kid)\n  }\n\n  static async fromImportable(\n    input: string | KeyLike | Jwk,\n    kid?: string,\n  ): Promise<JoseKey> {\n    if (typeof input === 'string') {\n      // PKCS8\n      if (input.startsWith('-----')) {\n        // The \"alg\" is only needed in WebCrypto (NodeJS will be fine)\n        return this.fromPKCS8(input, '', kid)\n      }\n\n      // Jwk (string)\n      if (input.startsWith('{')) {\n        return this.fromJWK(input, kid)\n      }\n\n      throw new JwkError('Invalid input')\n    }\n\n    if (typeof input === 'object') {\n      // Jwk\n      if ('kty' in input || 'alg' in input) {\n        return this.fromJWK(input, kid)\n      }\n\n      // KeyLike\n      return this.fromKeyLike(input, kid)\n    }\n\n    throw new JwkError('Invalid input')\n  }\n\n  /**\n   * @see {@link exportJWK}\n   */\n  static async fromKeyLike(\n    keyLike: KeyLike | Uint8Array,\n    kid?: string,\n    alg?: string,\n  ): Promise<JoseKey> {\n    const jwk = await exportJWK(keyLike)\n    if (alg) {\n      if (!jwk.alg) jwk.alg = alg\n      else if (jwk.alg !== alg) throw new JwkError('Invalid \"alg\" in JWK')\n    }\n    return this.fromJWK(jwk, kid)\n  }\n\n  /**\n   * @see {@link importPKCS8}\n   */\n  static async fromPKCS8(\n    pem: string,\n    alg: string,\n    kid?: string,\n  ): Promise<JoseKey> {\n    const keyLike = await importPKCS8(pem, alg, { extractable: true })\n    return this.fromKeyLike(keyLike, kid)\n  }\n\n  static async fromJWK(\n    input: string | Record<string, unknown>,\n    inputKid?: string,\n  ): Promise<JoseKey> {\n    const jwk = typeof input === 'string' ? JSON.parse(input) : input\n    if (!jwk || typeof jwk !== 'object') throw new JwkError('Invalid JWK')\n\n    const kid = either(jwk.kid, inputKid)\n\n    // Backwards compatibility with old behavior\n    if (jwk.use != null && isPrivateJwk(jwk)) {\n      console.warn(\n        'Deprecation warning: Private JWK with a \"use\" property will be rejected in the future. Please remove replace \"use\" with (valid) \"key_ops\".',\n      )\n      jwk.key_ops ??= jwk.use === 'sig' ? ['sign'] : ['encrypt']\n      delete jwk.use\n    }\n\n    return new JoseKey<Jwk>(jwkSchema.parse({ ...jwk, kid }))\n  }\n}\n"]}