Skip to content

Commit 8241d7f

Browse files
committed
Add fixed salt for admin password
1 parent e9b82a8 commit 8241d7f

4 files changed

Lines changed: 31 additions & 20 deletions

File tree

packages/server/api/src/app/database/seeds/seed-admin.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import {
2-
authenticateUserInOpenOpsTables,
3-
resetUserPassword,
4-
} from '@openops/common';
51
import { AppSystemProp, logger, system } from '@openops/server-shared';
62
import { OrganizationRole, Provider, User } from '@openops/shared';
73
import { authenticationService } from '../../authentication/basic/authentication-service';
@@ -79,9 +75,6 @@ async function ensureUserExists(
7975
);
8076

8177
user = await createAdminUser(email, password);
82-
const { token } = await authenticateUserInOpenOpsTables(email, password);
83-
await resetUserPassword(email, user.password, token);
84-
8578
return user;
8679
}
8780

@@ -170,13 +163,10 @@ async function upsertAdminPassword(
170163
const email = user.email;
171164
logger.info(`Updating password for admin [${email}]`, email);
172165

173-
const updatedUser = await userService.updatePassword({
166+
await userService.updateAdminPassword({
174167
id: user.id,
175168
newPassword,
176169
});
177-
178-
const { token } = await authenticateUserInOpenOpsTables(email, newPassword);
179-
await resetUserPassword(email, updatedUser.password, token);
180170
}
181171

182172
async function upsertAdminEmail(user: User, email: string): Promise<void> {
@@ -186,7 +176,7 @@ async function upsertAdminEmail(user: User, email: string): Promise<void> {
186176
}
187177

188178
function createAdminUser(email: string, password: string): Promise<User> {
189-
return userService.create({
179+
return userService.createAdminUser({
190180
email,
191181
password,
192182
organizationRole: OrganizationRole.ADMIN,

packages/server/api/src/app/user/user-service.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { cacheWrapper } from '@openops/server-shared';
1+
import { AppSystemProp, cacheWrapper, system } from '@openops/server-shared';
22
import {
33
ApplicationError,
44
assertValidEmail,
@@ -16,6 +16,7 @@ import {
1616
UserMeta,
1717
UserStatus,
1818
} from '@openops/shared';
19+
import bcrypt from 'bcrypt';
1920
import dayjs from 'dayjs';
2021
import { passwordHasher } from '../authentication/basic/password-hasher';
2122
import { repoFactory } from '../core/db/repo-factory';
@@ -28,17 +29,24 @@ export const userService = {
2829
async create(params: CreateParams): Promise<User> {
2930
const hashedPassword = await passwordHasher.hash(params.password);
3031

31-
const user: NewUser = {
32+
return saveUser({
3233
id: openOpsId(),
3334
...params,
3435
organizationRole: params.organizationRole,
3536
status: UserStatus.ACTIVE,
3637
password: hashedPassword,
37-
};
38-
39-
sendUserCreatedEvent(user.id, user.organizationId);
38+
});
39+
},
40+
async createAdminUser(params: CreateParams): Promise<User> {
41+
const hashedPassword = await bcrypt.hash(params.password, getStaticSalt());
4042

41-
return userRepo().save(user);
43+
return saveUser({
44+
id: openOpsId(),
45+
...params,
46+
organizationRole: params.organizationRole,
47+
status: UserStatus.ACTIVE,
48+
password: hashedPassword,
49+
});
4250
},
4351
async update({
4452
id,
@@ -198,13 +206,13 @@ export const userService = {
198206
});
199207
},
200208

201-
async updatePassword({
209+
async updateAdminPassword({
202210
id,
203211
newPassword,
204212
}: UpdatePasswordParams): Promise<User> {
205213
assertValidPassword(newPassword);
206214

207-
const hashedPassword = await passwordHasher.hash(newPassword);
215+
const hashedPassword = await bcrypt.hash(newPassword, getStaticSalt());
208216

209217
await userRepo().update(id, {
210218
updated: dayjs().toISOString(),
@@ -295,6 +303,17 @@ export const userService = {
295303
},
296304
};
297305

306+
function saveUser(user: NewUser): Promise<User> {
307+
sendUserCreatedEvent(user.id, user.organizationId);
308+
309+
return userRepo().save(user);
310+
}
311+
312+
function getStaticSalt(): string {
313+
return system.getOrThrow<string>(AppSystemProp.OPENOPS_ADMIN_PASSWORD_SALT);
314+
}
315+
316+
298317
type DeleteParams = {
299318
id: UserId;
300319
organizationId: OrganizationId | null;

packages/server/shared/src/lib/system/system-prop.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export enum AppSystemProp {
6666

6767
OPENOPS_ADMIN_EMAIL = 'OPENOPS_ADMIN_EMAIL',
6868
OPENOPS_ADMIN_PASSWORD = 'OPENOPS_ADMIN_PASSWORD',
69+
OPENOPS_ADMIN_PASSWORD_SALT = 'OPENOPS_ADMIN_PASSWORD_SALT',
6970

7071
OPENOPS_TABLES_DATABASE_NAME = 'OPENOPS_TABLES_DATABASE_NAME',
7172
OPENOPS_TABLES_PUBLIC_URL = 'OPENOPS_TABLES_PUBLIC_URL',

packages/server/shared/src/lib/system/system.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ const systemPropDefaultValues: Partial<Record<SystemProp, string>> = {
100100
[AppSystemProp.TELEMETRY_MODE]: 'COLLECTOR',
101101
[AppSystemProp.TELEMETRY_COLLECTOR_URL]: 'https://telemetry.openops.com/save',
102102
[SharedSystemProp.ENABLE_HOST_VALIDATION]: 'true',
103+
[AppSystemProp.OPENOPS_ADMIN_PASSWORD_SALT]: '$2b$10$6zuoB5d8Dz9bzV91gpuynO',
103104
};
104105

105106
export const system = {

0 commit comments

Comments
 (0)