Merge pull request 'Release 1.0.6' (#4) from develop into main
All checks were successful
continuous-integration/drone/tag Build is passing

Reviewed-on: #4
This commit was merged in pull request #4.
This commit is contained in:
2023-05-26 21:49:25 +00:00
19 changed files with 170 additions and 82 deletions

View File

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

62
src/dao/auth/create.ts Normal file
View File

@@ -0,0 +1,62 @@
import { DatabaseError } from '@mifi/services-common/lib/domain/errors/DatabaseError';
import { Auth, Log, Strategy, Token } from '../..';
import { Auth as AuthProps } from '../../schema/auth';
import { STRATEGIES } from '../../constants/strategies';
import { REQUIRE_VERIFICATION } from '../../constants/env';
import { TokenType } from '../../constants/tokens';
import { Status } from '../../constants/auth';
import { Action } from '../../constants/action';
type CreateProps = Pick<AuthProps, 'record' | 'username'> & {
externalId?: string;
password?: string;
publicKey?: string;
};
export const create = async ({ record, username, externalId, password, publicKey }: CreateProps) => {
const status = REQUIRE_VERIFICATION ? Status.UNVERIFIED : Status.ACTIVE;
const doc = await Auth.create({
record,
status,
username,
}).catch((err) => {
throw new DatabaseError('failed to create user', { err });
});
if (doc) {
const method = externalId && publicKey ? STRATEGIES.FIDO2 : STRATEGIES.LOCAL;
const strategy = await Strategy.create({
externalId,
key: password || publicKey,
method,
parent: doc._id,
}).catch((err) => {
throw new DatabaseError(`failed to create strategy ${STRATEGIES[method]}`, { err });
});
if (strategy) {
doc.strategies.push(strategy._id);
await doc.save();
Log.add(doc._id, Action.CREATE);
return {
doc,
token:
method === STRATEGIES.LOCAL &&
REQUIRE_VERIFICATION &&
(await Token.getToken(TokenType.VERIFICATION, doc._id)),
};
}
await doc.deleteOne((err) => {
throw new DatabaseError('failed to remove invalid auth record', {
err,
doc,
});
});
}
return null;
};
export type Fido2UserProps = Pick<AuthProps, 'record' | 'username'> & { externalId: string; publicKey: string };
export const createFido2User = (props: Fido2UserProps) => create(props);
export type LocalUserProps = Pick<AuthProps, 'record' | 'username'> & { password: string };
export const createLocalUser = (props: LocalUserProps) => create(props);

View File

@@ -0,0 +1,20 @@
import { StringSchemaDefinition } from 'mongoose';
import { Auth, Log, Strategy, Token } from '../..';
import { Status } from '../../constants/auth';
import { Action } from '../../constants/action';
export const deleteById = async (id: StringSchemaDefinition) => {
if (
await Auth.findByIdAndUpdate(id, {
status: Status.DELETED,
strategies: [],
}).catch()
) {
await Strategy.deleteMany({ parent: id });
await Token.deleteMany({ auth: id });
Log.add(id, Action.DELETE);
return true;
}
return false;
};

9
src/dao/auth/readAll.ts Normal file
View File

@@ -0,0 +1,9 @@
import { FilterQuery } from 'mongoose';
import { Auth } from '../../model/auth';
import { Status } from '../../constants/auth';
import { AuthDocument } from '../../schema/auth';
export const readAll = async (query: FilterQuery<AuthDocument> = {}) => Auth.find(query);
export const readAllActive = async () => readAll({ status: { $ne: Status.DELETED } });

View File

@@ -0,0 +1,5 @@
import { Types } from 'mongoose';
import { Auth } from '../../model/auth';
export const readOneById = async (id: Types.ObjectId) => Auth.findById(id);

View File

@@ -0,0 +1,5 @@
import { Types } from 'mongoose';
import { Auth } from '../../model/auth';
export const readOneByRecord = async (record: Types.ObjectId) => Auth.findOne({ record });

View File

@@ -0,0 +1,3 @@
import { Auth } from '../../model/auth';
export const readOneByUsername = async (username: string) => Auth.findOne({ username });

View File

@@ -1,45 +1,3 @@
import { DatabaseError } from '@mifi/services-common/lib/domain/errors/DatabaseError';
import { create } from './auth/create';
import { Auth, Log, Strategy, Token } from '..';
import { Auth as AuthProps } from '../schema/auth';
import { STRATEGIES } from '../constants/strategies';
import { REQUIRE_VERIFICATION } from '../constants/env';
import { TokenType } from '../constants/tokens';
import { Status } from '../constants/auth';
import { Action } from '../constants/action';
export const create = async ({ record, username, password }: AuthProps & { password: string }) => {
const status = REQUIRE_VERIFICATION ? Status.UNVERIFIED : Status.ACTIVE;
const doc = await Auth.create({
record,
status,
username,
}).catch((err) => {
throw new DatabaseError('failed to create user', { err });
});
if (doc) {
const strategy = await Strategy.create({
method: STRATEGIES.LOCAL,
key: password,
parent: doc._id,
}).catch((err) => {
throw new DatabaseError('failed to create strategy', { err });
});
if (strategy) {
doc.strategies.push(strategy._id);
await doc.save();
Log.add(doc._id, Action.CREATE);
return {
doc,
token: REQUIRE_VERIFICATION && (await Token.getToken(TokenType.VERIFICATION, doc._id)),
};
}
await doc.deleteOne((err) => {
throw new DatabaseError('failed to remove invalid auth record', {
err,
doc,
});
});
}
return null;
};
export { create };

View File

@@ -1,20 +1,3 @@
import { StringSchemaDefinition } from 'mongoose';
import { deleteById } from './auth/deleteById';
import { Auth, Log, Strategy, Token } from '..';
import { Status } from '../constants/auth';
import { Action } from '../constants/action';
export const deleteById = async (id: StringSchemaDefinition) => {
if (
await Auth.findByIdAndUpdate(id, {
status: Status.DELETED,
strategies: [],
}).catch()
) {
await Strategy.deleteMany({ parent: id });
await Token.deleteMany({ auth: id });
Log.add(id, Action.DELETE);
return true;
}
return false;
};
export { deleteById };

View File

@@ -1,9 +1,3 @@
import { FilterQuery } from 'mongoose';
import { readAll } from './auth/readAll';
import { Auth } from '../model/auth';
import { Status } from '../constants/auth';
import { AuthDocument } from '../schema/auth';
export const readAll = async (query: FilterQuery<AuthDocument> = {}) => Auth.find(query);
export const readAllActive = async () => readAll({ status: { $ne: Status.DELETED } });
export { readAll };

View File

@@ -1,5 +1,3 @@
import { Types } from 'mongoose';
import { readOneById } from './auth/readOneById';
import { Auth } from '../model/auth';
export const readOneById = async (id: Types.ObjectId) => Auth.findById(id);
export { readOneById };

View File

@@ -1,5 +1,3 @@
import { Types } from 'mongoose';
import { readOneByRecord } from './auth/readOneByRecord';
import { Auth } from '../model/auth';
export const readOneByRecord = async (record: Types.ObjectId) => Auth.findOne({ record });
export { readOneByRecord };

View File

@@ -1,3 +1,3 @@
import { Auth } from '../model/auth';
import { readOneByUsername } from './auth/readOneByUsername';
export const readOneByUsername = async (username: string) => Auth.findOne({ username });
export { readOneByUsername };

View File

@@ -0,0 +1,24 @@
import { DatabaseError } from '@mifi/services-common/lib/domain/errors/DatabaseError';
import { Strategy } from '../../model/strategy';
import { Strategy as StrategyProps } from '../../schema/strategy';
const create = async ({ externalId, key, method, parent, profile }: StrategyProps) =>
Strategy.create({
externalId,
key,
method,
parent,
profile,
}).catch((err) => {
throw new DatabaseError(err, {
externalId: externalId || null,
key,
method,
parent,
profile: profile || null,
});
});
export const createFido2Strategy = (props: Omit<StrategyProps, 'profile'>) => create(props);
export const createLocalStrategy = (props: Omit<StrategyProps, 'externalId' | 'profile'>) => create(props);

View File

@@ -0,0 +1,8 @@
import { StringSchemaDefinition } from 'mongoose';
import { Strategy } from '../../model/strategy';
export const deleteById = async (id: StringSchemaDefinition) => {
const result = await Strategy.findByIdAndDelete(id).catch();
return !!result;
};

View File

@@ -0,0 +1,6 @@
import { FilterQuery } from 'mongoose';
import { Strategy } from '../../model/strategy';
import { StrategyDocument } from '../../schema/strategy';
export const readAll = async (query: FilterQuery<StrategyDocument> = {}) => Strategy.find(query);

View File

@@ -0,0 +1,3 @@
import { Strategy } from '../../model/strategy';
export const readOneByExternalId = async (externalId: string) => Strategy.findOne({ externalId });

View File

@@ -0,0 +1,5 @@
import { Types } from 'mongoose';
import { Strategy } from '../../model/strategy';
export const readOneById = async (id: Types.ObjectId) => Strategy.findById(id);

View File

@@ -0,0 +1,7 @@
import { Types } from 'mongoose';
import { STRATEGIES } from '../../constants/strategies';
import { Strategy } from '../../model/strategy';
export const readOneByParentAndMethod = async (parent: Types.ObjectId, method: STRATEGIES) =>
Strategy.findOne({ method, parent });