Reorganizing
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2023-05-03 11:12:59 -04:00
parent 27a78dd471
commit dc72cefece
23 changed files with 163 additions and 87 deletions

View File

@@ -1,11 +1,11 @@
import Auth from '../model/auth';
import { AuthModel, AuthPrivate } from '../schema/auth';
import Auth from '../db/model/auth';
import { AuthModel, AuthPrivate } from '../db/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 as string) : null;
const isAuthenticated = !!auth && (<AuthModel>auth).authenticate(password);
const record = isAuthenticated ? <string>(<AuthPrivate>auth).record : null;
const token = sign(record || undefined);
return {
record,

View File

@@ -1,7 +1,5 @@
import jwt from 'jsonwebtoken';
import { JWT_SECRET } from '../constants/defaults';
export const getJwtSecret = () => process.env.JWT_SECRET || JWT_SECRET;
import { JWT_AUDIENCE, JWT_ISSUER, JWT_SECRET } from '../constants/constants';
export interface TokenProps {
aud?: string;
exp?: number | Date;
@@ -26,12 +24,12 @@ export const sign = (props: SignProps) => {
{
exp,
sub,
aud: rest.aud || process.env.JWT_AUDIENCE,
aud: rest.aud || JWT_AUDIENCE,
iat: today.getTime(),
iss: rest.iss || process.env.JWT_ISSUER,
iss: rest.iss || JWT_ISSUER,
},
getJwtSecret(),
JWT_SECRET,
);
};
export const verify = (token: string) => jwt.verify(token, getJwtSecret());
export const verify = (token: string) => jwt.verify(token, JWT_SECRET);

View File

@@ -1,7 +1,3 @@
import { API_PATH, PORT, RESET_ROUTE } from '../constants/defaults';
import { RESET_ROUTE, ROUTE_PREFIX } from '../constants/constants';
export const getPasswordResetLink = (token: string) => {
const hostname = process.env.HOST_NAME || `localhost:${process.env.PORT || PORT}`;
const path = `${process.env.API_PATH || API_PATH}${process.env.RESET_ROUTE || RESET_ROUTE}`;
return `https://${hostname}${path}?t=${token}`;
};
export const getPasswordResetPath = (token: string) => `${ROUTE_PREFIX}${RESET_ROUTE}?t=${token}`;

View File

@@ -1,13 +1,34 @@
import crypto from 'crypto';
import { sign } from './jwt';
import { LOGIN_VALID_TIME, RESET_VALID_MINUTES } from '../constants/constants';
import { Status } from '../constants/auth';
const parseLoginValid = () => {
const [number, unit] = process.env.LOGIN_VALID_TIME || LOGIN_VALID_TIME;
return [
unit === 'd' ? parseInt(number) : 1,
unit === 'h' ? parseInt(number) : (unit === 'm' && 1) || 24,
unit === 'm' ? parseInt(number) : 60,
];
};
export const generateLoginToken = (sub: string, status: Status) => {
const [days, hours, mins] = parseLoginValid();
return sign({
sub,
status,
exp: Date.now() + days * hours * mins * 60 * 1000,
});
};
export const generateResetToken = (sub: string) => {
const hoursValid = <number>(process.env.RESET_VALID_HOURS || RESET_VALID_MINUTES);
const key = crypto.randomBytes(16).toString('hex');
const token = sign({
sub,
key,
exp: Date.now() + 24 * 60 * 60 * 1000,
exp: Date.now() + hoursValid * 60 * 60 * 1000,
});
return { key, token };
};