Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| efb237885f | |||
|
ec61e29fb0
|
|||
|
d97a7d7f95
|
|||
| 4bbe520366 | |||
|
e657d09ae5
|
|||
|
08293e8c16
|
|||
|
fdfd8b567a
|
|||
|
55c2274a4f
|
|||
|
7d40fdfe74
|
|||
|
8e5092b943
|
|||
|
35b89062e4
|
|||
|
65e30ba1c7
|
|||
|
40645f58e3
|
|||
|
7619899edc
|
@@ -1,6 +1,7 @@
|
|||||||
.build.yarnrc.yml
|
.build.yarnrc.yml
|
||||||
.drone.yml
|
.drone.yml
|
||||||
.eslintrc*
|
.eslintrc*
|
||||||
|
.npmrc
|
||||||
.prettierrc*
|
.prettierrc*
|
||||||
.yarnrc.yml
|
.yarnrc.yml
|
||||||
babel.config.*
|
babel.config.*
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@mifi/auth-db",
|
"name": "@mifi/auth-db",
|
||||||
"version": "1.0.5",
|
"version": "1.0.6",
|
||||||
"author": "mifi (Mike Fitzpatrick)",
|
"author": "mifi (Mike Fitzpatrick)",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
62
src/dao/auth/create.ts
Normal file
62
src/dao/auth/create.ts
Normal 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);
|
||||||
20
src/dao/auth/deleteById.ts
Normal file
20
src/dao/auth/deleteById.ts
Normal 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
9
src/dao/auth/readAll.ts
Normal 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 } });
|
||||||
5
src/dao/auth/readOneById.ts
Normal file
5
src/dao/auth/readOneById.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import { Types } from 'mongoose';
|
||||||
|
|
||||||
|
import { Auth } from '../../model/auth';
|
||||||
|
|
||||||
|
export const readOneById = async (id: Types.ObjectId) => Auth.findById(id);
|
||||||
5
src/dao/auth/readOneByRecord.ts
Normal file
5
src/dao/auth/readOneByRecord.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import { Types } from 'mongoose';
|
||||||
|
|
||||||
|
import { Auth } from '../../model/auth';
|
||||||
|
|
||||||
|
export const readOneByRecord = async (record: Types.ObjectId) => Auth.findOne({ record });
|
||||||
3
src/dao/auth/readOneByUsername.ts
Normal file
3
src/dao/auth/readOneByUsername.ts
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
import { Auth } from '../../model/auth';
|
||||||
|
|
||||||
|
export const readOneByUsername = async (username: string) => Auth.findOne({ username });
|
||||||
@@ -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 '..';
|
export { create };
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|||||||
@@ -1,20 +1,3 @@
|
|||||||
import { StringSchemaDefinition } from 'mongoose';
|
import { deleteById } from './auth/deleteById';
|
||||||
|
|
||||||
import { Auth, Log, Strategy, Token } from '..';
|
export { deleteById };
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|||||||
@@ -1,9 +1,3 @@
|
|||||||
import { FilterQuery } from 'mongoose';
|
import { readAll } from './auth/readAll';
|
||||||
|
|
||||||
import { Auth } from '../model/auth';
|
export { readAll };
|
||||||
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 } });
|
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
import { Types } from 'mongoose';
|
import { readOneById } from './auth/readOneById';
|
||||||
|
|
||||||
import { Auth } from '../model/auth';
|
export { readOneById };
|
||||||
|
|
||||||
export const readOneById = async (id: Types.ObjectId) => Auth.findById(id);
|
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
import { Types } from 'mongoose';
|
import { readOneByRecord } from './auth/readOneByRecord';
|
||||||
|
|
||||||
import { Auth } from '../model/auth';
|
export { readOneByRecord };
|
||||||
|
|
||||||
export const readOneByRecord = async (record: Types.ObjectId) => Auth.findOne({ record });
|
|
||||||
|
|||||||
@@ -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 };
|
||||||
|
|||||||
24
src/dao/strategy/create.ts
Normal file
24
src/dao/strategy/create.ts
Normal 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);
|
||||||
8
src/dao/strategy/deleteById.ts
Normal file
8
src/dao/strategy/deleteById.ts
Normal 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;
|
||||||
|
};
|
||||||
6
src/dao/strategy/readAll.ts
Normal file
6
src/dao/strategy/readAll.ts
Normal 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);
|
||||||
3
src/dao/strategy/readOneByExternalId.ts
Normal file
3
src/dao/strategy/readOneByExternalId.ts
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
import { Strategy } from '../../model/strategy';
|
||||||
|
|
||||||
|
export const readOneByExternalId = async (externalId: string) => Strategy.findOne({ externalId });
|
||||||
5
src/dao/strategy/readOneById.ts
Normal file
5
src/dao/strategy/readOneById.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import { Types } from 'mongoose';
|
||||||
|
|
||||||
|
import { Strategy } from '../../model/strategy';
|
||||||
|
|
||||||
|
export const readOneById = async (id: Types.ObjectId) => Strategy.findById(id);
|
||||||
7
src/dao/strategy/readOneByParentAndMethod.ts
Normal file
7
src/dao/strategy/readOneByParentAndMethod.ts
Normal 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 });
|
||||||
Reference in New Issue
Block a user