formacao
This commit is contained in:
+18
-10
@@ -1,4 +1,3 @@
|
||||
"use strict";
|
||||
var __defProp = Object.defineProperty;
|
||||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
||||
var __getOwnPropNames = Object.getOwnPropertyNames;
|
||||
@@ -28,7 +27,7 @@ const PREFIX = "Bearer";
|
||||
const HEADER = "Authorization";
|
||||
const bearerAuth = (options) => {
|
||||
if (!("token" in options || "verifyToken" in options)) {
|
||||
throw new Error('bearer auth middleware requires options for "token"');
|
||||
throw new Error('bearer auth middleware requires options for "token" or "verifyToken"');
|
||||
}
|
||||
if (!options.realm) {
|
||||
options.realm = "";
|
||||
@@ -37,9 +36,9 @@ const bearerAuth = (options) => {
|
||||
options.prefix = PREFIX;
|
||||
}
|
||||
const realm = options.realm?.replace(/"/g, '\\"');
|
||||
const prefixRegexStr = options.prefix === "" ? "" : `${options.prefix} +`;
|
||||
const regexp = new RegExp(`^${prefixRegexStr}(${TOKEN_STRINGS}) *$`, "i");
|
||||
const wwwAuthenticatePrefix = options.prefix === "" ? "" : `${options.prefix} `;
|
||||
const prefix = options.prefix;
|
||||
const tokenRegexp = new RegExp(`^${TOKEN_STRINGS}$`);
|
||||
const wwwAuthenticatePrefix = prefix === "" ? "" : `${prefix} `;
|
||||
const throwHTTPException = async (c, status, wwwAuthenticateHeader, messageOption) => {
|
||||
const wwwAuthenticateHeaderValue = typeof wwwAuthenticateHeader === "function" ? await wwwAuthenticateHeader(c) : wwwAuthenticateHeader;
|
||||
const headers = {
|
||||
@@ -65,8 +64,17 @@ const bearerAuth = (options) => {
|
||||
options.noAuthenticationHeader?.message || options.noAuthenticationHeaderMessage || "Unauthorized"
|
||||
);
|
||||
} else {
|
||||
const match = regexp.exec(headerToken);
|
||||
if (!match) {
|
||||
let tokenValue;
|
||||
if (prefix === "") {
|
||||
tokenValue = headerToken;
|
||||
} else {
|
||||
const headerLower = headerToken.toLowerCase();
|
||||
const prefixLower = prefix.toLowerCase();
|
||||
if (headerLower.startsWith(prefixLower) && headerToken[prefix.length] === " ") {
|
||||
tokenValue = headerToken.slice(prefix.length).trimStart();
|
||||
}
|
||||
}
|
||||
if (!tokenValue || !tokenRegexp.test(tokenValue)) {
|
||||
await throwHTTPException(
|
||||
c,
|
||||
400,
|
||||
@@ -76,12 +84,12 @@ const bearerAuth = (options) => {
|
||||
} else {
|
||||
let equal = false;
|
||||
if ("verifyToken" in options) {
|
||||
equal = await options.verifyToken(match[1], c);
|
||||
equal = await options.verifyToken(tokenValue, c);
|
||||
} else if (typeof options.token === "string") {
|
||||
equal = await (0, import_buffer.timingSafeEqual)(options.token, match[1], options.hashFunction);
|
||||
equal = await (0, import_buffer.timingSafeEqual)(options.token, tokenValue, options.hashFunction);
|
||||
} else if (Array.isArray(options.token) && options.token.length > 0) {
|
||||
for (const token of options.token) {
|
||||
if (await (0, import_buffer.timingSafeEqual)(token, match[1], options.hashFunction)) {
|
||||
if (await (0, import_buffer.timingSafeEqual)(token, tokenValue, options.hashFunction)) {
|
||||
equal = true;
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user