Initial commit: Luminary photo catalog and RAW editor
Browser-based photo catalog with an optional team server (SQLite + LibRaw). Local database, uploaded photos, build output and dependencies are gitignored. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import crypto from 'node:crypto';
|
||||
import { SESSION_DAYS } from './config.mjs';
|
||||
import { sessions, users } from './db.mjs';
|
||||
|
||||
const KEYLEN = 64;
|
||||
|
||||
export function hashPassword(password) {
|
||||
const salt = crypto.randomBytes(16);
|
||||
const hash = crypto.scryptSync(password, salt, KEYLEN);
|
||||
return `scrypt$${salt.toString('hex')}$${hash.toString('hex')}`;
|
||||
}
|
||||
|
||||
export function verifyPassword(password, stored) {
|
||||
const [scheme, saltHex, hashHex] = String(stored).split('$');
|
||||
if (scheme !== 'scrypt' || !saltHex || !hashHex) return false;
|
||||
const expected = Buffer.from(hashHex, 'hex');
|
||||
const actual = crypto.scryptSync(password, Buffer.from(saltHex, 'hex'), expected.length);
|
||||
return expected.length === actual.length && crypto.timingSafeEqual(expected, actual);
|
||||
}
|
||||
|
||||
export function createSession(userId) {
|
||||
const token = crypto.randomBytes(32).toString('hex');
|
||||
const expiresAt = Date.now() + SESSION_DAYS * 24 * 60 * 60 * 1000;
|
||||
sessions.purge();
|
||||
sessions.create(token, userId, expiresAt);
|
||||
return token;
|
||||
}
|
||||
|
||||
export function publicUser(row) {
|
||||
if (!row) return null;
|
||||
return {
|
||||
id: row.id,
|
||||
email: row.email,
|
||||
name: row.name ?? '',
|
||||
role: row.role ?? 'member',
|
||||
};
|
||||
}
|
||||
|
||||
export function authenticate(req) {
|
||||
const header = String(req.headers.authorization ?? '');
|
||||
const token = header.startsWith('Bearer ') ? header.slice(7).trim() : '';
|
||||
if (!token) return null;
|
||||
const session = sessions.find(token);
|
||||
if (!session) return null;
|
||||
if (Number(session.expires_at) < Date.now()) {
|
||||
sessions.destroy(token);
|
||||
return null;
|
||||
}
|
||||
const user = users.byId(session.user_id);
|
||||
if (!user || user.disabled) return null;
|
||||
return publicUser(user);
|
||||
}
|
||||
|
||||
export function normalizeEmail(value) {
|
||||
return typeof value === 'string' ? value.trim().toLowerCase() : '';
|
||||
}
|
||||
|
||||
export function validEmail(email) {
|
||||
return /^[^@\s]+@[^@\s]+\.[^@\s]{2,}$/.test(email);
|
||||
}
|
||||
Reference in New Issue
Block a user