jwt.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // src/middleware/jwt/jwt.ts
  2. import { getCookie, getSignedCookie } from "../../helper/cookie/index.js";
  3. import { HTTPException } from "../../http-exception.js";
  4. import { Jwt } from "../../utils/jwt/index.js";
  5. import "../../context.js";
  6. var jwt = (options) => {
  7. const verifyOpts = options.verification || {};
  8. if (!options || !options.secret) {
  9. throw new Error('JWT auth middleware requires options for "secret"');
  10. }
  11. if (!options.alg) {
  12. throw new Error('JWT auth middleware requires options for "alg"');
  13. }
  14. if (!crypto.subtle || !crypto.subtle.importKey) {
  15. throw new Error("`crypto.subtle.importKey` is undefined. JWT auth middleware requires it.");
  16. }
  17. return async function jwt2(ctx, next) {
  18. const headerName = options.headerName || "Authorization";
  19. const credentials = ctx.req.raw.headers.get(headerName);
  20. let token;
  21. if (credentials) {
  22. const parts = credentials.split(/\s+/);
  23. if (parts.length !== 2) {
  24. const errDescription = "invalid credentials structure";
  25. throw new HTTPException(401, {
  26. message: errDescription,
  27. res: unauthorizedResponse({
  28. ctx,
  29. error: "invalid_request",
  30. errDescription
  31. })
  32. });
  33. } else {
  34. token = parts[1];
  35. }
  36. } else if (options.cookie) {
  37. if (typeof options.cookie == "string") {
  38. token = getCookie(ctx, options.cookie);
  39. } else if (options.cookie.secret) {
  40. if (options.cookie.prefixOptions) {
  41. token = await getSignedCookie(
  42. ctx,
  43. options.cookie.secret,
  44. options.cookie.key,
  45. options.cookie.prefixOptions
  46. );
  47. } else {
  48. token = await getSignedCookie(ctx, options.cookie.secret, options.cookie.key);
  49. }
  50. } else {
  51. if (options.cookie.prefixOptions) {
  52. token = getCookie(ctx, options.cookie.key, options.cookie.prefixOptions);
  53. } else {
  54. token = getCookie(ctx, options.cookie.key);
  55. }
  56. }
  57. }
  58. if (!token) {
  59. const errDescription = "no authorization included in request";
  60. throw new HTTPException(401, {
  61. message: errDescription,
  62. res: unauthorizedResponse({
  63. ctx,
  64. error: "invalid_request",
  65. errDescription
  66. })
  67. });
  68. }
  69. let payload;
  70. let cause;
  71. try {
  72. payload = await Jwt.verify(token, options.secret, {
  73. alg: options.alg,
  74. ...verifyOpts
  75. });
  76. } catch (e) {
  77. cause = e;
  78. }
  79. if (!payload) {
  80. throw new HTTPException(401, {
  81. message: "Unauthorized",
  82. res: unauthorizedResponse({
  83. ctx,
  84. error: "invalid_token",
  85. statusText: "Unauthorized",
  86. errDescription: "token verification failure"
  87. }),
  88. cause
  89. });
  90. }
  91. ctx.set("jwtPayload", payload);
  92. await next();
  93. };
  94. };
  95. function unauthorizedResponse(opts) {
  96. return new Response("Unauthorized", {
  97. status: 401,
  98. statusText: opts.statusText,
  99. headers: {
  100. "WWW-Authenticate": `Bearer realm="${opts.ctx.req.url}",error="${opts.error}",error_description="${opts.errDescription}"`
  101. }
  102. });
  103. }
  104. var verifyWithJwks = Jwt.verifyWithJwks;
  105. var verify = Jwt.verify;
  106. var decode = Jwt.decode;
  107. var sign = Jwt.sign;
  108. export {
  109. decode,
  110. jwt,
  111. sign,
  112. verify,
  113. verifyWithJwks
  114. };