2023-11-08 07:29:39 -08:00
|
|
|
import Container from 'typedi';
|
|
|
|
import { hash } from 'bcryptjs';
|
|
|
|
import { AuthIdentity } from '@db/entities/AuthIdentity';
|
2024-01-24 04:38:57 -08:00
|
|
|
import type { GlobalRole, User } from '@db/entities/User';
|
2023-11-10 06:04:26 -08:00
|
|
|
import { AuthIdentityRepository } from '@db/repositories/authIdentity.repository';
|
|
|
|
import { UserRepository } from '@db/repositories/user.repository';
|
2023-11-08 07:29:39 -08:00
|
|
|
import { TOTPService } from '@/Mfa/totp.service';
|
|
|
|
import { MfaService } from '@/Mfa/mfa.service';
|
|
|
|
|
|
|
|
import { randomApiKey, randomEmail, randomName, randomValidPassword } from '../random';
|
|
|
|
|
2024-02-07 08:56:02 -08:00
|
|
|
// pre-computed bcrypt hash for the string 'password', using `await hash('password', 10)`
|
|
|
|
const passwordHash = '$2a$10$njedH7S6V5898mj6p0Jr..IGY9Ms.qNwR7RbSzzX9yubJocKfvGGK';
|
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
/**
|
|
|
|
* Store a user in the DB, defaulting to a `member`.
|
|
|
|
*/
|
|
|
|
export async function createUser(attributes: Partial<User> = {}): Promise<User> {
|
2024-01-24 04:38:57 -08:00
|
|
|
const { email, password, firstName, lastName, role, ...rest } = attributes;
|
2023-11-29 06:48:36 -08:00
|
|
|
const user = Container.get(UserRepository).create({
|
2023-11-08 07:29:39 -08:00
|
|
|
email: email ?? randomEmail(),
|
2024-02-07 08:56:02 -08:00
|
|
|
password: password ? await hash(password, 1) : passwordHash,
|
2023-11-08 07:29:39 -08:00
|
|
|
firstName: firstName ?? randomName(),
|
|
|
|
lastName: lastName ?? randomName(),
|
2024-01-24 04:38:57 -08:00
|
|
|
role: role ?? 'global:member',
|
2023-11-08 07:29:39 -08:00
|
|
|
...rest,
|
2023-11-29 06:48:36 -08:00
|
|
|
});
|
|
|
|
user.computeIsOwner();
|
2023-11-08 07:29:39 -08:00
|
|
|
|
2024-01-17 07:08:50 -08:00
|
|
|
return await Container.get(UserRepository).save(user);
|
2023-11-08 07:29:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function createLdapUser(attributes: Partial<User>, ldapId: string): Promise<User> {
|
|
|
|
const user = await createUser(attributes);
|
|
|
|
await Container.get(AuthIdentityRepository).save(AuthIdentity.create(user, ldapId, 'ldap'));
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function createUserWithMfaEnabled(
|
|
|
|
data: { numberOfRecoveryCodes: number } = { numberOfRecoveryCodes: 10 },
|
|
|
|
) {
|
|
|
|
const email = randomEmail();
|
|
|
|
const password = randomValidPassword();
|
|
|
|
|
|
|
|
const toptService = new TOTPService();
|
|
|
|
|
|
|
|
const secret = toptService.generateSecret();
|
|
|
|
|
|
|
|
const mfaService = Container.get(MfaService);
|
|
|
|
|
|
|
|
const recoveryCodes = mfaService.generateRecoveryCodes(data.numberOfRecoveryCodes);
|
|
|
|
|
|
|
|
const { encryptedSecret, encryptedRecoveryCodes } = mfaService.encryptSecretAndRecoveryCodes(
|
|
|
|
secret,
|
|
|
|
recoveryCodes,
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
user: await createUser({
|
|
|
|
mfaEnabled: true,
|
|
|
|
password,
|
|
|
|
email,
|
|
|
|
mfaSecret: encryptedSecret,
|
|
|
|
mfaRecoveryCodes: encryptedRecoveryCodes,
|
|
|
|
}),
|
|
|
|
rawPassword: password,
|
|
|
|
rawSecret: secret,
|
|
|
|
rawRecoveryCodes: recoveryCodes,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function createOwner() {
|
2024-01-24 04:38:57 -08:00
|
|
|
return await createUser({ role: 'global:owner' });
|
2023-11-08 07:29:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function createMember() {
|
2024-01-24 04:38:57 -08:00
|
|
|
return await createUser({ role: 'global:member' });
|
2023-11-08 07:29:39 -08:00
|
|
|
}
|
|
|
|
|
2023-11-24 02:40:08 -08:00
|
|
|
export async function createAdmin() {
|
2024-01-24 04:38:57 -08:00
|
|
|
return await createUser({ role: 'global:admin' });
|
2023-11-24 02:40:08 -08:00
|
|
|
}
|
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
export async function createUserShell(role: GlobalRole): Promise<User> {
|
|
|
|
const shell: Partial<User> = { role };
|
2023-11-08 07:29:39 -08:00
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
if (role !== 'global:owner') {
|
2023-11-08 07:29:39 -08:00
|
|
|
shell.email = randomEmail();
|
|
|
|
}
|
|
|
|
|
2024-01-17 07:08:50 -08:00
|
|
|
return await Container.get(UserRepository).save(shell);
|
2023-11-08 07:29:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create many users in the DB, defaulting to a `member`.
|
|
|
|
*/
|
|
|
|
export async function createManyUsers(
|
|
|
|
amount: number,
|
|
|
|
attributes: Partial<User> = {},
|
|
|
|
): Promise<User[]> {
|
2024-01-24 04:38:57 -08:00
|
|
|
let { email, password, firstName, lastName, role, ...rest } = attributes;
|
2023-11-08 07:29:39 -08:00
|
|
|
|
|
|
|
const users = await Promise.all(
|
|
|
|
[...Array(amount)].map(async () =>
|
|
|
|
Container.get(UserRepository).create({
|
|
|
|
email: email ?? randomEmail(),
|
2024-02-07 08:56:02 -08:00
|
|
|
password: password ? await hash(password, 1) : passwordHash,
|
2023-11-08 07:29:39 -08:00
|
|
|
firstName: firstName ?? randomName(),
|
|
|
|
lastName: lastName ?? randomName(),
|
2024-01-24 04:38:57 -08:00
|
|
|
role: role ?? 'global:member',
|
2023-11-08 07:29:39 -08:00
|
|
|
...rest,
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2024-01-17 07:08:50 -08:00
|
|
|
return await Container.get(UserRepository).save(users);
|
2023-11-08 07:29:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function addApiKey(user: User): Promise<User> {
|
|
|
|
user.apiKey = randomApiKey();
|
2024-01-17 07:08:50 -08:00
|
|
|
return await Container.get(UserRepository).save(user);
|
2023-11-08 07:29:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getAllUsers = async () =>
|
2024-01-17 07:08:50 -08:00
|
|
|
await Container.get(UserRepository).find({
|
2024-01-24 04:38:57 -08:00
|
|
|
relations: ['authIdentities'],
|
2023-11-08 07:29:39 -08:00
|
|
|
});
|
|
|
|
|
2023-11-24 02:40:08 -08:00
|
|
|
export const getUserById = async (id: string) =>
|
2024-01-17 07:08:50 -08:00
|
|
|
await Container.get(UserRepository).findOneOrFail({
|
2023-11-24 02:40:08 -08:00
|
|
|
where: { id },
|
2024-01-24 04:38:57 -08:00
|
|
|
relations: ['authIdentities'],
|
2023-11-24 02:40:08 -08:00
|
|
|
});
|
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
export const getLdapIdentities = async () =>
|
2024-01-17 07:08:50 -08:00
|
|
|
await Container.get(AuthIdentityRepository).find({
|
2023-11-08 07:29:39 -08:00
|
|
|
where: { providerType: 'ldap' },
|
|
|
|
relations: ['user'],
|
|
|
|
});
|