index.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 basic_auth_exports = {};
  19. __export(basic_auth_exports, {
  20. basicAuth: () => basicAuth
  21. });
  22. module.exports = __toCommonJS(basic_auth_exports);
  23. var import_http_exception = require("../../http-exception");
  24. var import_basic_auth = require("../../utils/basic-auth");
  25. var import_buffer = require("../../utils/buffer");
  26. const basicAuth = (options, ...users) => {
  27. const usernamePasswordInOptions = "username" in options && "password" in options;
  28. const verifyUserInOptions = "verifyUser" in options;
  29. if (!(usernamePasswordInOptions || verifyUserInOptions)) {
  30. throw new Error(
  31. 'basic auth middleware requires options for "username and password" or "verifyUser"'
  32. );
  33. }
  34. if (!options.realm) {
  35. options.realm = "Secure Area";
  36. }
  37. if (!options.invalidUserMessage) {
  38. options.invalidUserMessage = "Unauthorized";
  39. }
  40. if (usernamePasswordInOptions) {
  41. users.unshift({ username: options.username, password: options.password });
  42. }
  43. return async function basicAuth2(ctx, next) {
  44. const requestUser = (0, import_basic_auth.auth)(ctx.req.raw);
  45. if (requestUser) {
  46. if (verifyUserInOptions) {
  47. if (await options.verifyUser(requestUser.username, requestUser.password, ctx)) {
  48. if (options.onAuthSuccess) {
  49. await options.onAuthSuccess(ctx, requestUser.username);
  50. }
  51. await next();
  52. return;
  53. }
  54. } else {
  55. for (const user of users) {
  56. const [usernameEqual, passwordEqual] = await Promise.all([
  57. (0, import_buffer.timingSafeEqual)(user.username, requestUser.username, options.hashFunction),
  58. (0, import_buffer.timingSafeEqual)(user.password, requestUser.password, options.hashFunction)
  59. ]);
  60. if (usernameEqual && passwordEqual) {
  61. if (options.onAuthSuccess) {
  62. await options.onAuthSuccess(ctx, requestUser.username);
  63. }
  64. await next();
  65. return;
  66. }
  67. }
  68. }
  69. }
  70. const status = 401;
  71. const headers = {
  72. "WWW-Authenticate": 'Basic realm="' + options.realm?.replace(/"/g, '\\"') + '"'
  73. };
  74. const responseMessage = typeof options.invalidUserMessage === "function" ? await options.invalidUserMessage(ctx) : options.invalidUserMessage;
  75. const res = typeof responseMessage === "string" ? new Response(responseMessage, { status, headers }) : new Response(JSON.stringify(responseMessage), {
  76. status,
  77. headers: {
  78. ...headers,
  79. "content-type": "application/json"
  80. }
  81. });
  82. throw new import_http_exception.HTTPException(status, { res });
  83. };
  84. };
  85. // Annotate the CommonJS export names for ESM import in node:
  86. 0 && (module.exports = {
  87. basicAuth
  88. });