local.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { importJWK } from '../key/import.js';
  2. import { JWKSInvalid, JOSENotSupported, JWKSNoMatchingKey, JWKSMultipleMatchingKeys, } from '../util/errors.js';
  3. import { isObject } from '../lib/type_checks.js';
  4. function getKtyFromAlg(alg) {
  5. switch (typeof alg === 'string' && alg.slice(0, 2)) {
  6. case 'RS':
  7. case 'PS':
  8. return 'RSA';
  9. case 'ES':
  10. return 'EC';
  11. case 'Ed':
  12. return 'OKP';
  13. case 'ML':
  14. return 'AKP';
  15. default:
  16. throw new JOSENotSupported('Unsupported "alg" value for a JSON Web Key Set');
  17. }
  18. }
  19. function isJWKSLike(jwks) {
  20. return (jwks &&
  21. typeof jwks === 'object' &&
  22. Array.isArray(jwks.keys) &&
  23. jwks.keys.every(isJWKLike));
  24. }
  25. function isJWKLike(key) {
  26. return isObject(key);
  27. }
  28. class LocalJWKSet {
  29. #jwks;
  30. #cached = new WeakMap();
  31. constructor(jwks) {
  32. if (!isJWKSLike(jwks)) {
  33. throw new JWKSInvalid('JSON Web Key Set malformed');
  34. }
  35. this.#jwks = structuredClone(jwks);
  36. }
  37. jwks() {
  38. return this.#jwks;
  39. }
  40. async getKey(protectedHeader, token) {
  41. const { alg, kid } = { ...protectedHeader, ...token?.header };
  42. const kty = getKtyFromAlg(alg);
  43. const candidates = this.#jwks.keys.filter((jwk) => {
  44. let candidate = kty === jwk.kty;
  45. if (candidate && typeof kid === 'string') {
  46. candidate = kid === jwk.kid;
  47. }
  48. if (candidate && (typeof jwk.alg === 'string' || kty === 'AKP')) {
  49. candidate = alg === jwk.alg;
  50. }
  51. if (candidate && typeof jwk.use === 'string') {
  52. candidate = jwk.use === 'sig';
  53. }
  54. if (candidate && Array.isArray(jwk.key_ops)) {
  55. candidate = jwk.key_ops.includes('verify');
  56. }
  57. if (candidate) {
  58. switch (alg) {
  59. case 'ES256':
  60. candidate = jwk.crv === 'P-256';
  61. break;
  62. case 'ES384':
  63. candidate = jwk.crv === 'P-384';
  64. break;
  65. case 'ES512':
  66. candidate = jwk.crv === 'P-521';
  67. break;
  68. case 'Ed25519':
  69. case 'EdDSA':
  70. candidate = jwk.crv === 'Ed25519';
  71. break;
  72. }
  73. }
  74. return candidate;
  75. });
  76. const { 0: jwk, length } = candidates;
  77. if (length === 0) {
  78. throw new JWKSNoMatchingKey();
  79. }
  80. if (length !== 1) {
  81. const error = new JWKSMultipleMatchingKeys();
  82. const _cached = this.#cached;
  83. error[Symbol.asyncIterator] = async function* () {
  84. for (const jwk of candidates) {
  85. try {
  86. yield await importWithAlgCache(_cached, jwk, alg);
  87. }
  88. catch { }
  89. }
  90. };
  91. throw error;
  92. }
  93. return importWithAlgCache(this.#cached, jwk, alg);
  94. }
  95. }
  96. async function importWithAlgCache(cache, jwk, alg) {
  97. const cached = cache.get(jwk) || cache.set(jwk, {}).get(jwk);
  98. if (cached[alg] === undefined) {
  99. const key = await importJWK({ ...jwk, ext: true }, alg);
  100. if (key instanceof Uint8Array || key.type !== 'public') {
  101. throw new JWKSInvalid('JSON Web Key Set members must be public keys');
  102. }
  103. cached[alg] = key;
  104. }
  105. return cached[alg];
  106. }
  107. export function createLocalJWKSet(jwks) {
  108. const set = new LocalJWKSet(jwks);
  109. const localJWKSet = async (protectedHeader, token) => set.getKey(protectedHeader, token);
  110. Object.defineProperties(localJWKSet, {
  111. jwks: {
  112. value: () => structuredClone(set.jwks()),
  113. enumerable: false,
  114. configurable: false,
  115. writable: false,
  116. },
  117. });
  118. return localJWKSet;
  119. }