Updates as I integrate

This commit is contained in:
2023-05-30 17:44:50 -04:00
parent ec61e29fb0
commit 64e1f53f4e
7 changed files with 19 additions and 11 deletions

View File

@@ -6,6 +6,5 @@
.yarnrc.yml .yarnrc.yml
babel.config.* babel.config.*
jest.config.* jest.config.*
src
tsconfig*.json tsconfig*.json
tslint.json tslint.json

View File

@@ -1,6 +1,6 @@
{ {
"name": "@mifi/auth-db", "name": "@mifi/auth-db",
"version": "1.0.6", "version": "1.0.10",
"author": "mifi (Mike Fitzpatrick)", "author": "mifi (Mike Fitzpatrick)",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {

View File

@@ -2,15 +2,15 @@ import { Auth, Log } from '..';
import { Action } from '../constants/action'; import { Action } from '../constants/action';
import { getLoginToken } from '../utils/getLoginToken'; 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(); const doc = await Auth.findByUsername(username).catch();
if (!!doc && (await doc.authenticate(password))) { if (!!doc && (await doc.authenticate(password))) {
Log.add(doc.id, Action.AUTHENTICATE); 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) { if (doc) {
Log.add(doc.id, Action.AUTHENTICATE_FAILURE); Log.add(doc.id, Action.AUTHENTICATE_FAILURE, { ...doc });
} }
return false; return false;

View File

@@ -8,15 +8,18 @@ import { TokenType } from '../../constants/tokens';
import { Status } from '../../constants/auth'; import { Status } from '../../constants/auth';
import { Action } from '../../constants/action'; import { Action } from '../../constants/action';
type CreateProps = Pick<AuthProps, 'record' | 'username'> & { type CreateProps = Pick<AuthProps, 'username'> & {
externalId?: string; externalId?: string;
handle?: AuthProps['handle'];
password?: string; password?: string;
publicKey?: 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 status = REQUIRE_VERIFICATION ? Status.UNVERIFIED : Status.ACTIVE;
const doc = await Auth.create({ const doc = await Auth.create({
handle,
record, record,
status, status,
username, username,
@@ -55,7 +58,7 @@ export const create = async ({ record, username, externalId, password, publicKey
return null; return null;
}; };
export type Fido2UserProps = Pick<AuthProps, 'record' | 'username'> & { externalId: string; publicKey: string }; export type Fido2UserProps = Pick<AuthProps, 'handle' | 'username'> & { externalId: string; publicKey: string };
export const createFido2User = (props: Fido2UserProps) => create(props); export const createFido2User = (props: Fido2UserProps) => create(props);
export type LocalUserProps = Pick<AuthProps, 'record' | 'username'> & { password: string }; export type LocalUserProps = Pick<AuthProps, 'record' | 'username'> & { password: string };

View File

@@ -1,3 +1,7 @@
import { Strategy } from '../../model/strategy'; 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 });

View File

@@ -7,6 +7,7 @@ import { StrategyDocument } from './strategy';
import { verify } from '../utils/password'; import { verify } from '../utils/password';
export interface Auth { export interface Auth {
handle?: string;
is2FA?: boolean; is2FA?: boolean;
record: StringSchemaDefinition; record: StringSchemaDefinition;
username: string; username: string;
@@ -35,6 +36,7 @@ export interface AuthModel extends Model<AuthDocument> {
export const AuthSchema = new Schema<AuthDocument, AuthModel>( export const AuthSchema = new Schema<AuthDocument, AuthModel>(
{ {
handle: { type: String },
is2FA: { type: Boolean, default: false }, is2FA: { type: Boolean, default: false },
record: { type: Types.ObjectId, unique: true }, record: { type: Types.ObjectId, unique: true },
status: { status: {

View File

@@ -10,8 +10,8 @@
"rootDirs": ["./", "src/"], "rootDirs": ["./", "src/"],
"strict": true, "strict": true,
"esModuleInterop": true, "esModuleInterop": true,
"sourceMap": false, "sourceMap": true,
"removeComments": true "removeComments": false
}, },
"include": ["src"] "include": ["src"]
} }