fetch_jwks.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. import { JOSEError, JWKSTimeout } from '../util/errors.js';
  2. const fetchJwks = async (url, timeout, options) => {
  3. let controller;
  4. let id;
  5. let timedOut = false;
  6. if (typeof AbortController === 'function') {
  7. controller = new AbortController();
  8. id = setTimeout(() => {
  9. timedOut = true;
  10. controller.abort();
  11. }, timeout);
  12. }
  13. const response = await fetch(url.href, {
  14. signal: controller ? controller.signal : undefined,
  15. redirect: 'manual',
  16. headers: options.headers,
  17. }).catch((err) => {
  18. if (timedOut)
  19. throw new JWKSTimeout();
  20. throw err;
  21. });
  22. if (id !== undefined)
  23. clearTimeout(id);
  24. if (response.status !== 200) {
  25. throw new JOSEError('Expected 200 OK from the JSON Web Key Set HTTP response');
  26. }
  27. try {
  28. return await response.json();
  29. }
  30. catch (_a) {
  31. throw new JOSEError('Failed to parse the JSON Web Key Set HTTP response as JSON');
  32. }
  33. };
  34. export default fetchJwks;