Cookie authentication.
This commit is contained in:
31
lib/crypto.js
Normal file
31
lib/crypto.js
Normal file
@@ -0,0 +1,31 @@
|
||||
import crypto from 'crypto';
|
||||
import { v5 as uuid, v4 } from 'uuid';
|
||||
import Cryptr from 'cryptr';
|
||||
|
||||
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 cryptr = new Cryptr(hash(process.env.HASH_SALT, process.env.DATABASE_URL));
|
||||
|
||||
export function md5(s) {
|
||||
return crypto.createHash('md5').update(s).digest('hex');
|
||||
}
|
||||
|
||||
export function hash(...args) {
|
||||
return uuid(args.join(''), md5(process.env.HASH_SALT));
|
||||
}
|
||||
|
||||
export function validHash(s) {
|
||||
return UUID_REGEX.test(s);
|
||||
}
|
||||
|
||||
export function encrypt(s) {
|
||||
return cryptr.encrypt(s);
|
||||
}
|
||||
|
||||
export function decrypt(s) {
|
||||
return cryptr.decrypt(s);
|
||||
}
|
||||
|
||||
export function random() {
|
||||
return v4();
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { getWebsite, getSession, createSession } from 'lib/db';
|
||||
import { getCountry, getDevice, getIpAddress, hash, isValidSession } from 'lib/utils';
|
||||
import { getCountry, getDevice, getIpAddress, isValidSession } from 'lib/utils';
|
||||
import { hash } from 'lib/crypto';
|
||||
|
||||
export default async req => {
|
||||
const { payload } = req.body;
|
||||
|
||||
17
lib/utils.js
17
lib/utils.js
@@ -1,24 +1,9 @@
|
||||
import crypto from 'crypto';
|
||||
import { v5 as uuid } from 'uuid';
|
||||
import requestIp from 'request-ip';
|
||||
import { browserName, detectOS } from 'detect-browser';
|
||||
import maxmind from 'maxmind';
|
||||
import geolite2 from 'geolite2-redist';
|
||||
import isLocalhost from 'is-localhost-ip';
|
||||
|
||||
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}$/;
|
||||
|
||||
export function md5(s) {
|
||||
return crypto.createHash('md5').update(s).digest('hex');
|
||||
}
|
||||
|
||||
export function hash(...args) {
|
||||
return uuid(args.join(''), md5(process.env.HASH_SALT));
|
||||
}
|
||||
|
||||
export function validHash(s) {
|
||||
return UUID_REGEX.test(s);
|
||||
}
|
||||
import { hash } from './crypto';
|
||||
|
||||
export function getIpAddress(req) {
|
||||
// Cloudflare
|
||||
|
||||
Reference in New Issue
Block a user