0b3ab76405
Immich connections stay server-side with an encrypted API key, albums can be imported, and photos are only sent back when an event's push button is pressed: originals upload once, edited photos re-render, and edits hashes plus checksums skip anything already in Immich. This also lands the accompanying editor, cloud and administration work that was still uncommitted. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
78 lines
2.4 KiB
JavaScript
78 lines
2.4 KiB
JavaScript
import crypto from 'node:crypto';
|
|
import { promisify } from 'node:util';
|
|
import { SESSION_DAYS } from './config.mjs';
|
|
import { sessions, users } from './db.mjs';
|
|
|
|
const KEYLEN = 64;
|
|
const scrypt = promisify(crypto.scrypt);
|
|
|
|
export async function hashPassword(password) {
|
|
const salt = crypto.randomBytes(16);
|
|
const hash = await scrypt(password, salt, KEYLEN);
|
|
return `scrypt$${salt.toString('hex')}$${hash.toString('hex')}`;
|
|
}
|
|
|
|
export async 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 = await scrypt(password, Buffer.from(saltHex, 'hex'), expected.length);
|
|
return expected.length === actual.length && crypto.timingSafeEqual(expected, actual);
|
|
}
|
|
|
|
/**
|
|
* A fixed hash for addresses that do not exist, so a failed login costs the
|
|
* same scrypt work as a real one and does not reveal registration by timing.
|
|
* Built synchronously once at boot; this is not on a request path.
|
|
*/
|
|
const DUMMY_HASH = (() => {
|
|
const salt = crypto.randomBytes(16);
|
|
const hash = crypto.scryptSync('luminary-timing-equalizer', salt, KEYLEN);
|
|
return `scrypt$${salt.toString('hex')}$${hash.toString('hex')}`;
|
|
})();
|
|
|
|
export async function spendPasswordCheck(password) {
|
|
await verifyPassword(password, DUMMY_HASH);
|
|
}
|
|
|
|
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);
|
|
}
|