Finally have prettier and linting maybe working
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -1,17 +1,14 @@
|
||||
import Auth from '../model/auth';
|
||||
import auth from '../model/auth';
|
||||
import { AuthModel, AuthPrivate } from '../schema/auth';
|
||||
import { sign } from './jwt';
|
||||
|
||||
export const getAuthenticationBundle = async (
|
||||
username: string,
|
||||
password: string,
|
||||
) => {
|
||||
const auth = await Auth.findByUsername(username).catch();
|
||||
const isAuthenticated = !!auth && (auth as AuthModel).authenticate(password);
|
||||
const record = isAuthenticated ? (auth as AuthPrivate).record : null;
|
||||
const token = isAuthenticated ? (auth as AuthModel).getToken() : sign();
|
||||
return {
|
||||
record,
|
||||
token,
|
||||
};
|
||||
export const getAuthenticationBundle = async (username: string, password: string) => {
|
||||
const auth = await Auth.findByUsername(username).catch();
|
||||
const isAuthenticated = !!auth && (auth as AuthModel).authenticate(password);
|
||||
const record = isAuthenticated ? (auth as AuthPrivate).record : null;
|
||||
const token = isAuthenticated ? (auth as AuthModel).getToken() : sign();
|
||||
return {
|
||||
record,
|
||||
token,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,40 +1,35 @@
|
||||
import jwt, { JwtPayload } from 'jsonwebtoken';
|
||||
import jsonwebtoken, { JwtPayload } from 'jsonwebtoken';
|
||||
|
||||
export interface TokenProps {
|
||||
aud?: string;
|
||||
exp?: number | Date;
|
||||
iss?: string;
|
||||
sub: string | null;
|
||||
[key: string]: any;
|
||||
aud?: string;
|
||||
exp?: number | Date;
|
||||
iss?: string;
|
||||
sub: string | null;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
export type SignProps = string | TokenProps | void;
|
||||
|
||||
export const sign = (props: SignProps) => {
|
||||
const today = new Date();
|
||||
const { sub = null, ...rest }: TokenProps =
|
||||
typeof props === 'string' || typeof props === 'undefined'
|
||||
? { sub: props || null }
|
||||
: props;
|
||||
let exp = rest.exp;
|
||||
if (!exp) {
|
||||
exp = new Date(today);
|
||||
exp.setDate(
|
||||
today.getDate() + parseInt(process.env.JWT_DAYS_VALID as string),
|
||||
const today = new Date();
|
||||
const { sub = null, ...rest }: TokenProps =
|
||||
typeof props === 'string' || typeof props === 'undefined' ? { sub: props || null } : props;
|
||||
let { exp } = rest;
|
||||
if (!exp) {
|
||||
exp = new Date(today);
|
||||
exp.setDate(today.getDate() + parseInt(process.env.JWT_DAYS_VALID as string));
|
||||
exp = exp.getTime() / 1000;
|
||||
}
|
||||
return jwt.sign(
|
||||
{
|
||||
exp,
|
||||
sub,
|
||||
aud: rest.aud || process.env.JWT_AUDIENCE,
|
||||
iat: today.getTime(),
|
||||
iss: rest.iss || process.env.JWT_ISSUER,
|
||||
},
|
||||
process.env.JWT_SECRET || 'secret',
|
||||
);
|
||||
exp = exp.getTime() / 1000;
|
||||
}
|
||||
return jwt.sign(
|
||||
{
|
||||
exp,
|
||||
sub,
|
||||
aud: rest.aud || process.env.JWT_AUDIENCE,
|
||||
iat: today.getTime(),
|
||||
iss: rest.iss || process.env.JWT_ISSUER,
|
||||
},
|
||||
process.env.JWT_SECRET || 'secret',
|
||||
);
|
||||
};
|
||||
|
||||
export const verify = (token: string) =>
|
||||
jwt.verify(token, process.env.JWT_SECRET || 'secret');
|
||||
export const verify = (token: string) => jwt.verify(token, process.env.JWT_SECRET || 'secret');
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import { pbkdf2Sync, randomBytes } from 'crypto';
|
||||
|
||||
export const encrypt = (password: string) => {
|
||||
const salt = randomBytes(16).toString('hex');
|
||||
const hash = pbkdf2Sync(password, salt, 10000, 512, 'sha512').toString('hex');
|
||||
return `${salt}:${hash}`;
|
||||
const salt = randomBytes(16).toString('hex');
|
||||
const hash = pbkdf2Sync(password, salt, 10000, 512, 'sha512').toString('hex');
|
||||
return `${salt}:${hash}`;
|
||||
};
|
||||
|
||||
export const verify = (test: string, secret: string) => {
|
||||
const [salt, hash] = secret.split(':');
|
||||
return pbkdf2Sync(test, salt, 10000, 512, 'sha512').toString('hex') === hash;
|
||||
const [salt, hash] = secret.split(':');
|
||||
return pbkdf2Sync(test, salt, 10000, 512, 'sha512').toString('hex') === hash;
|
||||
};
|
||||
|
||||
@@ -3,11 +3,11 @@ import crypto from 'crypto';
|
||||
import { sign } from './jwt';
|
||||
|
||||
export const generateResetToken = (sub: string) => {
|
||||
const key = crypto.randomBytes(16).toString('hex');
|
||||
const token = sign({
|
||||
sub,
|
||||
key,
|
||||
exp: Date.now() + 24 * 60 * 60 * 1000,
|
||||
});
|
||||
return { key, token };
|
||||
const key = crypto.randomBytes(16).toString('hex');
|
||||
const token = sign({
|
||||
sub,
|
||||
key,
|
||||
exp: Date.now() + 24 * 60 * 60 * 1000,
|
||||
});
|
||||
return { key, token };
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user