make auth dynamic
parent
399cfa222f
commit
ea50afa4ec
18
index.js
18
index.js
|
@ -1,6 +1,10 @@
|
||||||
const { getSignedUrl } = require('@aws-sdk/s3-request-presigner');
|
const { getSignedUrl } = require('@aws-sdk/s3-request-presigner');
|
||||||
const { S3Client, GetObjectCommand } = require('@aws-sdk/client-s3');
|
const { S3Client, GetObjectCommand } = require('@aws-sdk/client-s3');
|
||||||
|
|
||||||
|
const KV = {
|
||||||
|
'skydusky': SKYDUSKY
|
||||||
|
};
|
||||||
|
|
||||||
/************************************************************/
|
/************************************************************/
|
||||||
/*
|
/*
|
||||||
* Most of this code is based on
|
* Most of this code is based on
|
||||||
|
@ -64,8 +68,13 @@ function BadRequestException(reason) {
|
||||||
* @param {string} pass
|
* @param {string} pass
|
||||||
* @throws {UnauthorizedException}
|
* @throws {UnauthorizedException}
|
||||||
*/
|
*/
|
||||||
async function verifyCredentials(user, pass) {
|
async function verifyCredentials(store, user, pass) {
|
||||||
const KVpass = await SKYDUSKY.get(user);
|
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) {
|
if (KVpass === 'null' || KVpass !== pass) {
|
||||||
throw new UnauthorizedException('Invalid password.');
|
throw new UnauthorizedException('Invalid password.');
|
||||||
}
|
}
|
||||||
|
@ -78,19 +87,20 @@ async function verifyCredentials(user, pass) {
|
||||||
async function handleRequest(request) {
|
async function handleRequest(request) {
|
||||||
const requestURL = new URL(request.url);
|
const requestURL = new URL(request.url);
|
||||||
const path = requestURL.pathname.substring(1);
|
const path = requestURL.pathname.substring(1);
|
||||||
|
const area = path.split('/')[0];
|
||||||
|
|
||||||
// Prompt login
|
// Prompt login
|
||||||
if (!request.headers.has('Authorization')) {
|
if (!request.headers.has('Authorization')) {
|
||||||
return new Response('Please login.', {
|
return new Response('Please login.', {
|
||||||
status: 401,
|
status: 401,
|
||||||
headers: { 'WWW-Authenticate': 'Basic realm="SKYDUSKY", charset="UTF-8"' }
|
headers: { 'WWW-Authenticate': `Basic realm="${area.toUpperCase()}", charset="UTF-8"` }
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify login
|
// Verify login
|
||||||
const { user, pass } = basicAuthentication(request);
|
const { user, pass } = basicAuthentication(request);
|
||||||
try {
|
try {
|
||||||
await verifyCredentials(user, pass);
|
await verifyCredentials(area, user, pass);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue