2023-11-08 07:29:39 -08:00
|
|
|
import Container from 'typedi';
|
|
|
|
import { hash } from 'bcryptjs';
|
|
|
|
import { AuthIdentity } from '@db/entities/AuthIdentity';
|
2024-05-17 01:53:15 -07:00
|
|
|
import { type GlobalRole, type 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-05-31 00:40:19 -07:00
|
|
|
import { AuthUserRepository } from '@/databases/repositories/authUser.repository';
|
2023-11-08 07:29:39 -08:00
|
|
|
|
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';
|
|
|
|
|
2024-04-05 04:47:49 -07:00
|
|
|
/** Store a new user object, defaulting to a `member` */
|
|
|
|
export async function newUser(attributes: Partial<User> = {}): Promise<User> {
|
2024-01-24 04:38:57 -08:00
|
|
|
const { email, password, firstName, lastName, role, ...rest } = attributes;
|
2024-04-05 04:47:49 -07:00
|
|
|
return 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
|
|
|
});
|
2024-04-05 04:47:49 -07:00
|
|
|
}
|
2023-11-08 07:29:39 -08:00
|
|
|
|
2024-04-05 04:47:49 -07:00
|
|
|
/** Store a user object in the DB */
|
|
|
|
export async function createUser(attributes: Partial<User> = {}): Promise<User> {
|
2024-05-17 01:53:15 -07:00
|
|
|
const userInstance = await newUser(attributes);
|
|
|
|
const { user } = await Container.get(UserRepository).createUserWithProject(userInstance);
|
2024-04-05 04:47:49 -07:00
|
|
|
user.computeIsOwner();
|
2024-05-17 01:53:15 -07:00
|
|
|
return 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,
|
|
|
|
);
|
|
|
|
|
2024-05-31 00:40:19 -07:00
|
|
|
const user = await createUser({
|
|
|
|
mfaEnabled: true,
|
|
|
|
password,
|
|
|
|
email,
|
|
|
|
});
|
|
|
|
|
|
|
|
await Container.get(AuthUserRepository).update(user.id, {
|
|
|
|
mfaSecret: encryptedSecret,
|
|
|
|
mfaRecoveryCodes: encryptedRecoveryCodes,
|
|
|
|
});
|
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
return {
|
2024-05-31 00:40:19 -07:00
|
|
|
user,
|
2023-11-08 07:29:39 -08:00
|
|
|
rawPassword: password,
|
|
|
|
rawSecret: secret,
|
|
|
|
rawRecoveryCodes: recoveryCodes,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-07-30 05:58:07 -07:00
|
|
|
export async function createOwner({ withApiKey } = { withApiKey: false }) {
|
|
|
|
if (withApiKey) {
|
|
|
|
return await addApiKey(await createUser({ role: 'global:owner' }));
|
|
|
|
}
|
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
return await createUser({ role: 'global:owner' });
|
2023-11-08 07:29:39 -08:00
|
|
|
}
|
|
|
|
|
2024-08-02 03:02:05 -07:00
|
|
|
export async function createMember({ withApiKey } = { withApiKey: false }) {
|
|
|
|
if (withApiKey) {
|
|
|
|
return await addApiKey(await createUser({ role: 'global:member' }));
|
|
|
|
}
|
|
|
|
|
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-05-17 01:53:15 -07:00
|
|
|
const { user } = await Container.get(UserRepository).createUserWithProject(shell);
|
|
|
|
return user;
|
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-05-17 01:53:15 -07:00
|
|
|
const result = await Promise.all(
|
2024-04-05 04:47:49 -07:00
|
|
|
Array(amount)
|
|
|
|
.fill(0)
|
2024-05-17 01:53:15 -07:00
|
|
|
.map(async () => {
|
|
|
|
const userInstance = await newUser(attributes);
|
|
|
|
return await Container.get(UserRepository).createUserWithProject(userInstance);
|
|
|
|
}),
|
2023-11-08 07:29:39 -08:00
|
|
|
);
|
2024-05-17 01:53:15 -07:00
|
|
|
return result.map((result) => result.user);
|
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' },
|
2024-05-17 01:53:15 -07:00
|
|
|
relations: { user: true },
|
2023-11-08 07:29:39 -08:00
|
|
|
});
|
2024-03-26 09:18:41 -07:00
|
|
|
|
|
|
|
export async function getGlobalOwner() {
|
|
|
|
return await Container.get(UserRepository).findOneByOrFail({ role: 'global:owner' });
|
|
|
|
}
|