jwt.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name in all)
  7. __defProp(target, name, { get: all[name], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var jwt_exports = {};
  19. __export(jwt_exports, {
  20. decode: () => decode,
  21. jwt: () => jwt,
  22. sign: () => sign,
  23. verify: () => verify,
  24. verifyWithJwks: () => verifyWithJwks
  25. });
  26. module.exports = __toCommonJS(jwt_exports);
  27. var import_cookie = require("../../helper/cookie");
  28. var import_http_exception = require("../../http-exception");
  29. var import_jwt = require("../../utils/jwt");
  30. var import_context = require("../../context");
  31. const jwt = (options) => {
  32. const verifyOpts = options.verification || {};
  33. if (!options || !options.secret) {
  34. throw new Error('JWT auth middleware requires options for "secret"');
  35. }
  36. if (!options.alg) {
  37. throw new Error('JWT auth middleware requires options for "alg"');
  38. }
  39. if (!crypto.subtle || !crypto.subtle.importKey) {
  40. throw new Error("`crypto.subtle.importKey` is undefined. JWT auth middleware requires it.");
  41. }
  42. return async function jwt2(ctx, next) {
  43. const headerName = options.headerName || "Authorization";
  44. const credentials = ctx.req.raw.headers.get(headerName);
  45. let token;
  46. if (credentials) {
  47. const parts = credentials.split(/\s+/);
  48. if (parts.length !== 2) {
  49. const errDescription = "invalid credentials structure";
  50. throw new import_http_exception.HTTPException(401, {
  51. message: errDescription,
  52. res: unauthorizedResponse({
  53. ctx,
  54. error: "invalid_request",
  55. errDescription
  56. })
  57. });
  58. } else {
  59. token = parts[1];
  60. }
  61. } else if (options.cookie) {
  62. if (typeof options.cookie == "string") {
  63. token = (0, import_cookie.getCookie)(ctx, options.cookie);
  64. } else if (options.cookie.secret) {
  65. if (options.cookie.prefixOptions) {
  66. token = await (0, import_cookie.getSignedCookie)(
  67. ctx,
  68. options.cookie.secret,
  69. options.cookie.key,
  70. options.cookie.prefixOptions
  71. );
  72. } else {
  73. token = await (0, import_cookie.getSignedCookie)(ctx, options.cookie.secret, options.cookie.key);
  74. }
  75. } else {
  76. if (options.cookie.prefixOptions) {
  77. token = (0, import_cookie.getCookie)(ctx, options.cookie.key, options.cookie.prefixOptions);
  78. } else {
  79. token = (0, import_cookie.getCookie)(ctx, options.cookie.key);
  80. }
  81. }
  82. }
  83. if (!token) {
  84. const errDescription = "no authorization included in request";
  85. throw new import_http_exception.HTTPException(401, {
  86. message: errDescription,
  87. res: unauthorizedResponse({
  88. ctx,
  89. error: "invalid_request",
  90. errDescription
  91. })
  92. });
  93. }
  94. let payload;
  95. let cause;
  96. try {
  97. payload = await import_jwt.Jwt.verify(token, options.secret, {
  98. alg: options.alg,
  99. ...verifyOpts
  100. });
  101. } catch (e) {
  102. cause = e;
  103. }
  104. if (!payload) {
  105. throw new import_http_exception.HTTPException(401, {
  106. message: "Unauthorized",
  107. res: unauthorizedResponse({
  108. ctx,
  109. error: "invalid_token",
  110. statusText: "Unauthorized",
  111. errDescription: "token verification failure"
  112. }),
  113. cause
  114. });
  115. }
  116. ctx.set("jwtPayload", payload);
  117. await next();
  118. };
  119. };
  120. function unauthorizedResponse(opts) {
  121. return new Response("Unauthorized", {
  122. status: 401,
  123. statusText: opts.statusText,
  124. headers: {
  125. "WWW-Authenticate": `Bearer realm="${opts.ctx.req.url}",error="${opts.error}",error_description="${opts.errDescription}"`
  126. }
  127. });
  128. }
  129. const verifyWithJwks = import_jwt.Jwt.verifyWithJwks;
  130. const verify = import_jwt.Jwt.verify;
  131. const decode = import_jwt.Jwt.decode;
  132. const sign = import_jwt.Jwt.sign;
  133. // Annotate the CommonJS export names for ESM import in node:
  134. 0 && (module.exports = {
  135. decode,
  136. jwt,
  137. sign,
  138. verify,
  139. verifyWithJwks
  140. });