22 lines
593 B
JavaScript
22 lines
593 B
JavaScript
/* Simple token auth for final mode. */
|
|
const crypto = require('crypto');
|
|
const tokens = new Map(); // token -> expiry
|
|
const TTL = 12 * 60 * 60 * 1000;
|
|
|
|
module.exports = {
|
|
login(password) {
|
|
const expected = process.env.ADMIN_PASSWORD;
|
|
if (!expected || password !== expected) return null;
|
|
const t = crypto.randomBytes(24).toString('hex');
|
|
tokens.set(t, Date.now() + TTL);
|
|
return t;
|
|
},
|
|
check(t) {
|
|
if (!t) return false;
|
|
const exp = tokens.get(t);
|
|
if (!exp) return false;
|
|
if (Date.now() > exp) { tokens.delete(t); return false; }
|
|
return true;
|
|
},
|
|
};
|