From 64e1f53f4ef6b4a6242fd479403ab5caa187680c Mon Sep 17 00:00:00 2001 From: mifi Date: Tue, 30 May 2023 17:44:50 -0400 Subject: [PATCH] Updates as I integrate --- .npmignore | 1 - package.json | 2 +- src/api/authenticate.ts | 6 +++--- src/dao/auth/create.ts | 9 ++++++--- src/dao/strategy/readOneByExternalId.ts | 6 +++++- src/schema/auth.ts | 2 ++ tsconfig.production.json | 4 ++-- 7 files changed, 19 insertions(+), 11 deletions(-) diff --git a/.npmignore b/.npmignore index bb74c51..dd61efe 100644 --- a/.npmignore +++ b/.npmignore @@ -6,6 +6,5 @@ .yarnrc.yml babel.config.* jest.config.* -src tsconfig*.json tslint.json diff --git a/package.json b/package.json index c54a2b1..951393b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@mifi/auth-db", - "version": "1.0.6", + "version": "1.0.10", "author": "mifi (Mike Fitzpatrick)", "license": "MIT", "scripts": { diff --git a/src/api/authenticate.ts b/src/api/authenticate.ts index bec6a76..99fb59c 100644 --- a/src/api/authenticate.ts +++ b/src/api/authenticate.ts @@ -2,15 +2,15 @@ import { Auth, Log } from '..'; import { Action } from '../constants/action'; import { getLoginToken } from '../utils/getLoginToken'; -export const authenticate = async (username: string, password: string) => { +export const authenticate = async (username: string, password: string, includeToken = false) => { const doc = await Auth.findByUsername(username).catch(); if (!!doc && (await doc.authenticate(password))) { Log.add(doc.id, Action.AUTHENTICATE); - return { ...doc, token: getLoginToken(doc) }; + return { sub: doc._id, record: doc.record, token: includeToken ? getLoginToken(doc) : undefined }; } if (doc) { - Log.add(doc.id, Action.AUTHENTICATE_FAILURE); + Log.add(doc.id, Action.AUTHENTICATE_FAILURE, { ...doc }); } return false; diff --git a/src/dao/auth/create.ts b/src/dao/auth/create.ts index e20b3d5..c1afaf2 100644 --- a/src/dao/auth/create.ts +++ b/src/dao/auth/create.ts @@ -8,15 +8,18 @@ import { TokenType } from '../../constants/tokens'; import { Status } from '../../constants/auth'; import { Action } from '../../constants/action'; -type CreateProps = Pick & { +type CreateProps = Pick & { externalId?: string; + handle?: AuthProps['handle']; password?: string; publicKey?: string; + record?: AuthProps['record']; }; -export const create = async ({ record, username, externalId, password, publicKey }: CreateProps) => { +export const create = async ({ record, username, externalId, handle, password, publicKey }: CreateProps) => { const status = REQUIRE_VERIFICATION ? Status.UNVERIFIED : Status.ACTIVE; const doc = await Auth.create({ + handle, record, status, username, @@ -55,7 +58,7 @@ export const create = async ({ record, username, externalId, password, publicKey return null; }; -export type Fido2UserProps = Pick & { externalId: string; publicKey: string }; +export type Fido2UserProps = Pick & { externalId: string; publicKey: string }; export const createFido2User = (props: Fido2UserProps) => create(props); export type LocalUserProps = Pick & { password: string }; diff --git a/src/dao/strategy/readOneByExternalId.ts b/src/dao/strategy/readOneByExternalId.ts index 0fc751a..8a15e24 100644 --- a/src/dao/strategy/readOneByExternalId.ts +++ b/src/dao/strategy/readOneByExternalId.ts @@ -1,3 +1,7 @@ import { Strategy } from '../../model/strategy'; +import { AuthDocument } from '../../schema/auth'; -export const readOneByExternalId = async (externalId: string) => Strategy.findOne({ externalId }); +export const readOneByExternalId = async (externalId: string, populate = false) => + populate + ? Strategy.findOne({ externalId }).populate<{ parent: AuthDocument }>('parent') + : Strategy.findOne({ externalId }); diff --git a/src/schema/auth.ts b/src/schema/auth.ts index bcab819..025cb91 100644 --- a/src/schema/auth.ts +++ b/src/schema/auth.ts @@ -7,6 +7,7 @@ import { StrategyDocument } from './strategy'; import { verify } from '../utils/password'; export interface Auth { + handle?: string; is2FA?: boolean; record: StringSchemaDefinition; username: string; @@ -35,6 +36,7 @@ export interface AuthModel extends Model { export const AuthSchema = new Schema( { + handle: { type: String }, is2FA: { type: Boolean, default: false }, record: { type: Types.ObjectId, unique: true }, status: { diff --git a/tsconfig.production.json b/tsconfig.production.json index 48f8fba..422e4f8 100644 --- a/tsconfig.production.json +++ b/tsconfig.production.json @@ -10,8 +10,8 @@ "rootDirs": ["./", "src/"], "strict": true, "esModuleInterop": true, - "sourceMap": false, - "removeComments": true + "sourceMap": true, + "removeComments": false }, "include": ["src"] }