Finishing touches to publish
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-05-03 11:38:41 -04:00
parent dc72cefece
commit 32bfbd7adc
15 changed files with 36 additions and 38 deletions

5
lib/constants/db.ts Normal file
View File

@@ -0,0 +1,5 @@
export const DB_HOST = process.env.DB_HOST || 'mongodb';
export const DB_PORT = process.env.DB_PORT || 27017;
export const DB_USER = process.env.DB_USER || 'test';
export const DB_PASS = process.env.DB_PASSWORD || 'test';
export const DB_NAME = process.env.DB_NAME || 'auth';

View File

@@ -1,4 +1,3 @@
export const PACKAGE_NAME = '@mifi/latch'; export const PACKAGE_NAME = '@mifi/latch';
export const PORT = process.env.PORT || 9000; export const PORT = process.env.PORT || 9000;

5
lib/db/index.ts Normal file
View File

@@ -0,0 +1,5 @@
import mongoose from 'mongoose';
import { DB_HOST, DB_NAME, DB_PASS, DB_PORT, DB_USER } from '../constants/db';
export const connection = mongoose.connect(`mongodb://${DB_USER}:${DB_PASS}@${DB_HOST}:${DB_PORT}/${DB_NAME}`);

View File

@@ -6,7 +6,7 @@ import { STRATEGIES } from '../../constants/strategies';
import { TokenProps, verify as verifyJwt } from '../../utils/jwt'; import { TokenProps, verify as verifyJwt } from '../../utils/jwt';
import { encrypt, verify as verifyPassword } from '../../utils/password'; import { encrypt, verify as verifyPassword } from '../../utils/password';
import { generateLoginToken, generateResetToken } from '../../utils/tokens'; import { generateLoginToken, generateResetToken } from '../../utils/tokens';
import { getPasswordResetLink } from '../../utils/links'; import { getPasswordResetPath } from '../../utils/links';
import { Status } from '../../constants/auth'; import { Status } from '../../constants/auth';
export type Auth = { export type Auth = {
@@ -42,7 +42,7 @@ export const AuthSchema = new Schema<AuthPrivate, AuthModel, AuthMethods>(
{ {
is2FA: { type: Boolean, default: false }, is2FA: { type: Boolean, default: false },
record: { type: Types.ObjectId }, record: { type: Types.ObjectId },
strategies: { type: Types.ArraySubdocument<Strategy>, required: true }, strategies: { type: Array<Strategy>, required: true },
status: { type: Number, enum: Object.values(Status), default: Status.UNVERIFIED }, status: { type: Number, enum: Object.values(Status), default: Status.UNVERIFIED },
username: { type: String, required: true, unique: true }, username: { type: String, required: true, unique: true },
}, },
@@ -69,7 +69,7 @@ AuthSchema.methods = {
async getResetLink(route) { async getResetLink(route) {
const token = await this.getResetToken(); const token = await this.getResetToken();
if (token) { if (token) {
const resetUrl = getPasswordResetLink(token); const resetUrl = getPasswordResetPath(token);
console.log('[sendPasswordReset] resetUrl:', resetUrl); console.log('[sendPasswordReset] resetUrl:', resetUrl);
return resetUrl; return resetUrl;
} }

View File

@@ -2,7 +2,7 @@ import Koa from 'koa';
import Router from 'koa-router'; import Router from 'koa-router';
import { StatusCodes } from 'http-status-codes'; import { StatusCodes } from 'http-status-codes';
import { ROUTE_PREFIX as prefix, RESET_ROUTE } from '../../constants/constants'; import { ROUTE_PREFIX as prefix, RESET_ROUTE } from '../../constants/env';
import Auth from '../../db/model/auth'; import Auth from '../../db/model/auth';
import { sign } from '../../utils/jwt'; import { sign } from '../../utils/jwt';
import passport from '../passport'; import passport from '../passport';
@@ -28,7 +28,7 @@ router.post('/login', async (ctx, next) => {
}); });
router.post(process.env.RESET_ROUTE || RESET_ROUTE, async (ctx, next) => { router.post(process.env.RESET_ROUTE || RESET_ROUTE, async (ctx, next) => {
const { token = null, password = null } = ctx.request.body as { token?: string, password?: string }; const { token = null, password = null } = ctx.request.body as { token?: string; password?: string };
if (token && password) { if (token && password) {
const loginToken = await Auth.resetPassword(token, password).catch(); const loginToken = await Auth.resetPassword(token, password).catch();
ctx.body({ token: loginToken }); ctx.body({ token: loginToken });
@@ -38,9 +38,7 @@ router.post(process.env.RESET_ROUTE || RESET_ROUTE, async (ctx, next) => {
}); });
router.patch('/:record', (ctx: Koa.Context) => { router.patch('/:record', (ctx: Koa.Context) => {
const data = Auth.findOneAndUpdate( const data = Auth.findOneAndUpdate({ record: ctx.params.record });
{ record: ctx.params.record },
);
if (!data) { if (!data) {
ctx.throw(StatusCodes.NOT_FOUND); ctx.throw(StatusCodes.NOT_FOUND);
} }

View File

@@ -1,9 +0,0 @@
import mongoose from 'mongoose';
const DB_USER = process.env.DB_USER || 'test';
const DB_PASS = process.env.DB_PASSWORD || 'test';
const DB_HOST = process.env.DB_HOST || 'mongodb';
const DB_PORT = process.env.DB_PORT || 27017;
const DB_NAME = process.env.DB_NAME || 'auth';
export const connection = mongoose.connect(`${DB_USER}:${DB_PASS}@${DB_HOST}:${DB_PORT}/${DB_NAME}`);

View File

@@ -1,8 +1,8 @@
import dotenv from 'dotenv'; import dotenv from 'dotenv';
import app from './app'; import app from './app';
import { connection } from './database/database.connection'; import { connection } from '../db';
import { PORT } from '../constants/constants'; import { PORT } from '../constants/env';
dotenv.config(); dotenv.config();

View File

@@ -1,5 +1,5 @@
import { Middleware } from 'koa'; import { Middleware } from 'koa';
import { LOGIN_ROUTE } from '../../constants/constants'; import { LOGIN_ROUTE } from '../../constants/env';
export const authenticated = (): Middleware => { export const authenticated = (): Middleware => {
return (ctx, next) => { return (ctx, next) => {

View File

@@ -1,6 +1,6 @@
import passport from 'koa-passport'; import passport from 'koa-passport';
import Auth from '../../model/auth'; import Auth from '../../db/model/auth';
import { Auth as AuthRecord } from '../../db/schema/auth'; import { Auth as AuthRecord } from '../../db/schema/auth';
import LocalStrategy from './strategies/local'; import LocalStrategy from './strategies/local';
import JwtStrategy from './strategies/jwt'; import JwtStrategy from './strategies/jwt';

View File

@@ -1,12 +1,12 @@
// eslint-disable-next-line import/named // eslint-disable-next-line import/named
import { ExtractJwt, Strategy as JwtStrategy } from 'passport-jwt'; import { ExtractJwt, Strategy as JwtStrategy } from 'passport-jwt';
import Auth from '../../../model/auth'; import Auth from '../../../db/model/auth';
import { getJwtSecret } from '../../../utils/jwt'; import { JWT_SECRET } from '../../../constants/env';
const opts = { const opts = {
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: getJwtSecret(), secretOrKey: JWT_SECRET,
issuer: process.env.JWT_ISSUER, issuer: process.env.JWT_ISSUER,
audience: process.env.JWT_AUDIENCE, audience: process.env.JWT_AUDIENCE,
}; };

View File

@@ -1,7 +1,7 @@
// eslint-disable-next-line import/named // eslint-disable-next-line import/named
import { Strategy as LocalStrategy } from 'passport-local'; import { Strategy as LocalStrategy } from 'passport-local';
import Auth from '../../../model/auth'; import Auth from '../../../db/model/auth';
export default new LocalStrategy(async (username: string, password: string, done: any) => { export default new LocalStrategy(async (username: string, password: string, done: any) => {
const user = await Auth.findOne({ const user = await Auth.findOne({

View File

@@ -1,5 +1,5 @@
import jwt from 'jsonwebtoken'; import jwt from 'jsonwebtoken';
import { JWT_AUDIENCE, JWT_ISSUER, JWT_SECRET } from '../constants/constants'; import { JWT_AUDIENCE, JWT_ISSUER, JWT_SECRET } from '../constants/env';
export interface TokenProps { export interface TokenProps {
aud?: string; aud?: string;
exp?: number | Date; exp?: number | Date;

View File

@@ -1,3 +1,3 @@
import { RESET_ROUTE, ROUTE_PREFIX } from '../constants/constants'; import { RESET_ROUTE, ROUTE_PREFIX } from '../constants/env';
export const getPasswordResetPath = (token: string) => `${ROUTE_PREFIX}${RESET_ROUTE}?t=${token}`; export const getPasswordResetPath = (token: string) => `${ROUTE_PREFIX}${RESET_ROUTE}?t=${token}`;

View File

@@ -1,7 +1,7 @@
import crypto from 'crypto'; import crypto from 'crypto';
import { sign } from './jwt'; import { sign } from './jwt';
import { LOGIN_VALID_TIME, RESET_VALID_MINUTES } from '../constants/constants'; import { LOGIN_VALID_TIME, RESET_VALID_MINUTES } from '../constants/env';
import { Status } from '../constants/auth'; import { Status } from '../constants/auth';
const parseLoginValid = () => { const parseLoginValid = () => {

View File

@@ -10,7 +10,7 @@
"lint:fix": "eslint --fix --ext .ts,.tsx lib/", "lint:fix": "eslint --fix --ext .ts,.tsx lib/",
"prettier": "prettier --check 'lib/**/*.ts'", "prettier": "prettier --check 'lib/**/*.ts'",
"prettier:fix": "prettier --write 'lib/**/*.ts'", "prettier:fix": "prettier --write 'lib/**/*.ts'",
"serve": "ts-node src/server.ts", "serve": "node dist/lib/server/index.js",
"start": "nodemon", "start": "nodemon",
"test": "jest --passWithNoTests" "test": "jest --passWithNoTests"
}, },