formacao
This commit is contained in:
+18
-9
@@ -6,7 +6,7 @@ var PREFIX = "Bearer";
|
||||
var HEADER = "Authorization";
|
||||
var 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 = "";
|
||||
@@ -15,9 +15,9 @@ var 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 = {
|
||||
@@ -43,8 +43,17 @@ var 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,
|
||||
@@ -54,12 +63,12 @@ var 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 timingSafeEqual(options.token, match[1], options.hashFunction);
|
||||
equal = await timingSafeEqual(options.token, tokenValue, options.hashFunction);
|
||||
} else if (Array.isArray(options.token) && options.token.length > 0) {
|
||||
for (const token of options.token) {
|
||||
if (await timingSafeEqual(token, match[1], options.hashFunction)) {
|
||||
if (await timingSafeEqual(token, tokenValue, options.hashFunction)) {
|
||||
equal = true;
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user