index.d.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { Agent as HttpAgent } from 'http';
  2. import { Agent as HttpsAgent } from 'https';
  3. import type {Jwt, Secret} from 'jsonwebtoken'
  4. import Express = require('express')
  5. declare function JwksRsa(options: JwksRsa.Options): JwksRsa.JwksClient;
  6. declare namespace JwksRsa {
  7. class JwksClient {
  8. constructor(options: Options);
  9. getKeys(): Promise<unknown>;
  10. getSigningKeys(): Promise<SigningKey[]>;
  11. getSigningKey(kid?: string | null | undefined): Promise<SigningKey>;
  12. getSigningKey(kid: string | null | undefined, cb: (err: Error | null, key?: SigningKey) => void): void;
  13. }
  14. interface Headers {
  15. [key: string]: string;
  16. }
  17. interface Options {
  18. jwksUri: string;
  19. rateLimit?: boolean;
  20. cache?: boolean;
  21. cacheMaxEntries?: number;
  22. cacheMaxAge?: number;
  23. jwksRequestsPerMinute?: number;
  24. proxy?: string;
  25. requestHeaders?: Headers;
  26. timeout?: number;
  27. requestAgent?: HttpAgent | HttpsAgent;
  28. fetcher?(jwksUri: string): Promise<{ keys: any }>;
  29. getKeysInterceptor?(): Promise<JSONWebKey[]>;
  30. }
  31. interface JSONWebKey {
  32. kid: string,
  33. alg: string,
  34. [key: string]: any
  35. }
  36. interface CertSigningKey {
  37. kid: string;
  38. alg: string;
  39. getPublicKey(): string;
  40. publicKey: string;
  41. }
  42. interface RsaSigningKey {
  43. kid: string;
  44. alg: string;
  45. getPublicKey(): string;
  46. rsaPublicKey: string;
  47. }
  48. type SigningKey = CertSigningKey | RsaSigningKey;
  49. /**
  50. * Types are duplicated from express-jwt@6/7
  51. * due to numerous breaking changes in the lib's types
  52. * whilst this lib supportd both <=6 & >=7 implementations
  53. *
  54. * express-jwt's installed version (or its @types)
  55. * will be the types used at transpilation time
  56. */
  57. /** Types from express-jwt@<=6 */
  58. type secretType = string|Buffer;
  59. type SecretCallbackLong = (req: Express.Request, header: any, payload: any, done: (err: any, secret?: secretType) => void) => void;
  60. type SecretCallback = (req: Express.Request, payload: any, done: (err: any, secret?: secretType) => void) => void;
  61. /** Types from express-jwt@>=7 */
  62. type GetVerificationKey = (req: Express.Request, token: Jwt | undefined) => Secret | undefined | Promise<Secret | undefined>;
  63. function expressJwtSecret(options: ExpressJwtOptions): SecretCallbackLong|GetVerificationKey;
  64. function passportJwtSecret(options: ExpressJwtOptions): SecretCallback;
  65. interface ExpressJwtOptions extends Options {
  66. handleSigningKeyError?: (err: Error | null, cb: (err: Error | null) => void) => void;
  67. }
  68. function hapiJwt2Key(options: HapiJwtOptions): (decodedToken: DecodedToken, cb: HapiCallback) => void;
  69. interface HapiJwtOptions extends Options {
  70. handleSigningKeyError?: (err: Error | null, cb: HapiCallback) => void;
  71. }
  72. type HapiCallback = (err: Error | null, publicKey: string, signingKey: SigningKey) => void;
  73. interface DecodedToken {
  74. header: TokenHeader;
  75. }
  76. interface TokenHeader {
  77. alg: string;
  78. kid: string;
  79. }
  80. function hapiJwt2KeyAsync(options: HapiJwtOptions): (decodedToken: DecodedToken) => Promise<{ key: string }>;
  81. function koaJwtSecret(options: KoaJwtOptions): (header: TokenHeader) => Promise<string>;
  82. interface KoaJwtOptions extends Options {
  83. handleSigningKeyError?(err: Error | null): Promise<void>;
  84. }
  85. class ArgumentError extends Error {
  86. name: 'ArgumentError';
  87. constructor(message: string);
  88. }
  89. class JwksError extends Error {
  90. name: 'JwksError';
  91. constructor(message: string);
  92. }
  93. class JwksRateLimitError extends Error {
  94. name: 'JwksRateLimitError';
  95. constructor(message: string);
  96. }
  97. class SigningKeyNotFoundError extends Error {
  98. name: 'SigningKeyNotFoundError';
  99. constructor(message: string);
  100. }
  101. }
  102. export = JwksRsa;