basic-auth.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. auth: () => auth
  21. });
  22. module.exports = __toCommonJS(basic_auth_exports);
  23. var import_encode = require("./encode");
  24. const CREDENTIALS_REGEXP = /^ *(?:[Bb][Aa][Ss][Ii][Cc]) +([A-Za-z0-9._~+/-]+=*) *$/;
  25. const USER_PASS_REGEXP = /^([^:]*):(.*)$/;
  26. const utf8Decoder = new TextDecoder();
  27. const auth = (req) => {
  28. const match = CREDENTIALS_REGEXP.exec(req.headers.get("Authorization") || "");
  29. if (!match) {
  30. return void 0;
  31. }
  32. let userPass = void 0;
  33. try {
  34. userPass = USER_PASS_REGEXP.exec(utf8Decoder.decode((0, import_encode.decodeBase64)(match[1])));
  35. } catch {
  36. }
  37. if (!userPass) {
  38. return void 0;
  39. }
  40. return { username: userPass[1], password: userPass[2] };
  41. };
  42. // Annotate the CommonJS export names for ESM import in node:
  43. 0 && (module.exports = {
  44. auth
  45. });