basic-auth.js 628 B

1234567891011121314151617181920212223
  1. // src/utils/basic-auth.ts
  2. import { decodeBase64 } from "./encode.js";
  3. var CREDENTIALS_REGEXP = /^ *(?:[Bb][Aa][Ss][Ii][Cc]) +([A-Za-z0-9._~+/-]+=*) *$/;
  4. var USER_PASS_REGEXP = /^([^:]*):(.*)$/;
  5. var utf8Decoder = new TextDecoder();
  6. var auth = (req) => {
  7. const match = CREDENTIALS_REGEXP.exec(req.headers.get("Authorization") || "");
  8. if (!match) {
  9. return void 0;
  10. }
  11. let userPass = void 0;
  12. try {
  13. userPass = USER_PASS_REGEXP.exec(utf8Decoder.decode(decodeBase64(match[1])));
  14. } catch {
  15. }
  16. if (!userPass) {
  17. return void 0;
  18. }
  19. return { username: userPass[1], password: userPass[2] };
  20. };
  21. export {
  22. auth
  23. };