214 lines
4.4 KiB
JavaScript
214 lines
4.4 KiB
JavaScript
/*
|
|
* Copyright (c) 2026 Markil 3. All rights reserved.
|
|
*/
|
|
import fs from "fs";
|
|
import bcrypt from "bcrypt";
|
|
import {config, AUTH_KEY, AUTH_KEY_HASH} from './config.js';
|
|
|
|
/**
|
|
* Checks to see if a request is allowed, based on whether it has the appropriate authentication headers.
|
|
* @param request - The request to verify.
|
|
* @return A promise that resolves to True if the headers are valid, false otherwise.
|
|
*/
|
|
async function isAuthenticated(request)
|
|
{
|
|
if (config[AUTH_KEY_HASH] || config[AUTH_KEY])
|
|
{
|
|
let authHeader = request.get('Authorization'), encodedCredentials, credentials;
|
|
if (!authHeader)
|
|
{
|
|
return false;
|
|
}
|
|
if (authHeader.startsWith("Basic "))
|
|
{
|
|
encodedCredentials = authHeader.substring('Basic '.length);
|
|
if (config[AUTH_KEY_HASH])
|
|
{
|
|
return await bcrypt.compare(encodedCredentials, config[AUTH_KEY_HASH]);
|
|
}
|
|
else
|
|
{
|
|
return encodedCredentials == config[AUTH_KEY];
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
/*
|
|
* We do not have authentication keys configured. We can let anything through.
|
|
*/
|
|
return true;
|
|
}
|
|
}
|
|
|
|
function getMime(filePath)
|
|
{
|
|
let pathExt;
|
|
if (filePath.lastIndexOf('.') > -1)
|
|
{
|
|
pathExt = filePath.substring(filePath.lastIndexOf('.')).toLowerCase();
|
|
}
|
|
else
|
|
{
|
|
pathExt = '';
|
|
return "text/plain";
|
|
}
|
|
if (pathExt == ".html" || pathExt == ".htm")
|
|
{
|
|
return "text/html";
|
|
}
|
|
if (pathExt == ".json")
|
|
{
|
|
return "application/json";
|
|
}
|
|
else if (pathExt == ".css")
|
|
{
|
|
return "text/css";
|
|
}
|
|
else if (pathExt == ".js")
|
|
{
|
|
return "text/javascript";
|
|
}
|
|
else if (pathExt == ".eot")
|
|
{
|
|
return "application/vnd.ms-fontobject";
|
|
}
|
|
else if (pathExt == ".otf")
|
|
{
|
|
return "font/otf";
|
|
}
|
|
else if (pathExt == ".ttf")
|
|
{
|
|
return "font/ttf";
|
|
}
|
|
else if (pathExt == ".woff")
|
|
{
|
|
return "font/woff";
|
|
}
|
|
else if (pathExt == ".woff2")
|
|
{
|
|
return "font/woff2";
|
|
}
|
|
else if (pathExt == ".png")
|
|
{
|
|
return "image/png";
|
|
}
|
|
else if (pathExt == ".apng")
|
|
{
|
|
return "image/apng";
|
|
}
|
|
else if (pathExt == ".jpg" || pathExt == ".jpeg")
|
|
{
|
|
return "image/jpeg";
|
|
}
|
|
else if (pathExt == ".gif")
|
|
{
|
|
return "image/gif";
|
|
}
|
|
else if (pathExt == ".bmp")
|
|
{
|
|
return "image/bmp";
|
|
}
|
|
else if (pathExt == ".webp")
|
|
{
|
|
return "image/webp";
|
|
}
|
|
else if (pathExt == ".avif")
|
|
{
|
|
return "image/avif";
|
|
}
|
|
else if (pathExt == ".ico")
|
|
{
|
|
return "image/vnd.microsoft.icon";
|
|
}
|
|
else if (pathExt == ".svg")
|
|
{
|
|
return "image/svg+xml";
|
|
}
|
|
else if (pathExt == ".aac")
|
|
{
|
|
return "audio/aac";
|
|
}
|
|
else if (pathExt == ".mp3")
|
|
{
|
|
return "audio/mpeg";
|
|
}
|
|
else if (pathExt == ".ogg" || pathExt == ".oga" || pathExt == ".opus")
|
|
{
|
|
return "audio/ogg";
|
|
}
|
|
else if (pathExt == ".wav")
|
|
{
|
|
return "audio/wav";
|
|
}
|
|
else if (pathExt == ".weba")
|
|
{
|
|
return "audio/webm";
|
|
}
|
|
else if (pathExt == ".avi")
|
|
{
|
|
return "video/x-msvideo";
|
|
}
|
|
else if (pathExt == ".mp4")
|
|
{
|
|
return "video/mp4";
|
|
}
|
|
else if (pathExt == ".mpeg")
|
|
{
|
|
return "video/mpeg";
|
|
}
|
|
else if (pathExt == ".ogv")
|
|
{
|
|
return "video/ogg";
|
|
}
|
|
else if (pathExt == ".webm")
|
|
{
|
|
return "video/webm";
|
|
}
|
|
else if (pathExt == ".md")
|
|
{
|
|
return "text/markdown";
|
|
}
|
|
else if (pathExt == ".azw")
|
|
{
|
|
return "application/vnd.amazon.ebook";
|
|
}
|
|
else if (pathExt == ".epub")
|
|
{
|
|
return "application/epub+zip";
|
|
}
|
|
else if (pathExt == ".doc")
|
|
{
|
|
return "application/msword";
|
|
}
|
|
else if (pathExt == ".docx")
|
|
{
|
|
return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
|
}
|
|
else if (pathExt == ".odt")
|
|
{
|
|
return "application/vnd.oasis.opendocument.text";
|
|
}
|
|
else if (pathExt == ".pdf")
|
|
{
|
|
return "application/pdf";
|
|
}
|
|
else if (pathExt == ".rtf")
|
|
{
|
|
return "application/rtf";
|
|
}
|
|
else
|
|
{
|
|
return "text/plain";
|
|
}
|
|
}
|
|
|
|
export {
|
|
isAuthenticated,
|
|
getMime
|
|
};
|