Accounts and login.

This commit is contained in:
Mike Cao 2020-07-23 19:56:55 -07:00
parent f3f0ad15f2
commit 49a55b40b4
22 changed files with 347 additions and 172 deletions

View file

@ -6,23 +6,19 @@ 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 sha256(...args) {
return crypto.createHash('sha256').update(args.join('')).digest('hex');
export function hash(...args) {
return crypto.createHash('sha512').update(args.join('')).digest('hex');
}
export function secret() {
return sha256(process.env.HASH_SALT);
return hash(process.env.HASH_SALT);
}
export function uuid(...args) {
return v5(args.join(''), v5(process.env.HASH_SALT, v5.DNS));
}
export function random(n = 64) {
return crypto.randomBytes(n).toString('hex');
}
export function isValidHash(s) {
export function isValidId(s) {
return UUID_REGEX.test(s);
}

View file

@ -1,12 +1,12 @@
import { getWebsite, getSession, createSession } from 'lib/db';
import { getCountry, getDevice, getIpAddress } from 'lib/utils';
import { uuid, isValidHash, verifyToken } from 'lib/crypto';
import { uuid, isValidId, verifyToken } from 'lib/crypto';
export default async req => {
const { payload } = req.body;
const { website: website_uuid, hostname, screen, language, session } = payload;
if (!isValidHash(website_uuid)) {
if (!isValidId(website_uuid)) {
throw new Error(`Invalid website: ${website_uuid}`);
}

10
lib/web.js Normal file
View file

@ -0,0 +1,10 @@
export const post = (url, params) =>
fetch(url, {
method: 'post',
cache: 'no-cache',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(params),
}).then(res => (res.status === 200 ? res.json() : null));