mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-15 01:35:17 +01:00
45 lines
1.1 KiB
JavaScript
45 lines
1.1 KiB
JavaScript
import crypto from 'crypto';
|
|
import { v5 } from 'uuid';
|
|
import bcrypt from 'bcrypt';
|
|
import { JWT, JWE, JWK } from 'jose';
|
|
|
|
const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/;
|
|
const KEY = JWK.asKey(Buffer.from(secret()));
|
|
|
|
export function hash(...args) {
|
|
return crypto.createHash('sha512').update(args.join('')).digest('hex');
|
|
}
|
|
|
|
export function secret() {
|
|
return hash(process.env.HASH_SALT);
|
|
}
|
|
|
|
export function uuid(...args) {
|
|
return v5(args.join(''), v5(process.env.HASH_SALT, v5.DNS));
|
|
}
|
|
|
|
export function isValidId(s) {
|
|
return UUID_REGEX.test(s);
|
|
}
|
|
|
|
export function checkPassword(password, hash) {
|
|
return bcrypt.compare(password, hash);
|
|
}
|
|
|
|
export async function createToken(payload) {
|
|
return JWT.sign(payload, KEY);
|
|
}
|
|
|
|
export async function verifyToken(token) {
|
|
return JWT.verify(token, KEY);
|
|
}
|
|
|
|
export async function createSecureToken(payload) {
|
|
return JWE.encrypt(await createToken(payload), KEY);
|
|
}
|
|
|
|
export async function verifySecureToken(token) {
|
|
const result = await JWE.decrypt(token, KEY);
|
|
return verifyToken(result.toString());
|
|
}
|