make auth dynamic

master
Paul Walko 2022-02-22 21:46:11 -05:00
parent 399cfa222f
commit ea50afa4ec
1 changed files with 14 additions and 4 deletions

View File

@ -1,6 +1,10 @@
const { getSignedUrl } = require('@aws-sdk/s3-request-presigner');
const { S3Client, GetObjectCommand } = require('@aws-sdk/client-s3');
const KV = {
'skydusky': SKYDUSKY
};
/************************************************************/
/*
* Most of this code is based on
@ -64,8 +68,13 @@ function BadRequestException(reason) {
* @param {string} pass
* @throws {UnauthorizedException}
*/
async function verifyCredentials(user, pass) {
const KVpass = await SKYDUSKY.get(user);
async function verifyCredentials(store, user, pass) {
console.log(`STORE: ${store}`)
if (!KV.hasOwnProperty(store)) {
throw new UnauthorizedException('Invalid password.');
}
const KVpass = await KV[store].get(user);
console.log(`PASS: ${KVpass}`)
if (KVpass === 'null' || KVpass !== pass) {
throw new UnauthorizedException('Invalid password.');
}
@ -78,19 +87,20 @@ async function verifyCredentials(user, pass) {
async function handleRequest(request) {
const requestURL = new URL(request.url);
const path = requestURL.pathname.substring(1);
const area = path.split('/')[0];
// Prompt login
if (!request.headers.has('Authorization')) {
return new Response('Please login.', {
status: 401,
headers: { 'WWW-Authenticate': 'Basic realm="SKYDUSKY", charset="UTF-8"' }
headers: { 'WWW-Authenticate': `Basic realm="${area.toUpperCase()}", charset="UTF-8"` }
});
}
// Verify login
const { user, pass } = basicAuthentication(request);
try {
await verifyCredentials(user, pass);
await verifyCredentials(area, user, pass);
} catch (e) {
return e;
}