Skip to content

Commit c98d931

Browse files
authored
Split authentication hooks into appropriate files (#1607)
Part of OPS-3003.
1 parent 0b1857a commit c98d931

14 files changed

Lines changed: 136 additions & 233 deletions

File tree

packages/server/api/src/app/authentication/authentication-service/hooks/authentication-service-hooks.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

packages/server/api/src/app/authentication/authentication-service/hooks/community-authentication-hooks.ts

Lines changed: 0 additions & 130 deletions
This file was deleted.

packages/server/api/src/app/authentication/authentication-service/hooks/index.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

packages/server/api/src/app/authentication/authentication-service/index.ts

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,21 @@ import {
44
AuthenticationResponse,
55
ErrorCode,
66
isNil,
7+
Provider,
78
User,
8-
UserId,
99
UserStatus,
1010
} from '@openops/shared';
1111
import { userService } from '../../user/user-service';
1212
import { passwordHasher } from '../basic/password-hasher';
13+
import { getProjectAndToken } from '../context/create-project-auth-context';
1314
import { createUser } from '../new-user/create-user';
14-
import { authenticationServiceHooks as hooks } from './hooks';
15-
import { Provider } from './hooks/authentication-service-hooks';
16-
import { getProjectAndToken } from './hooks/community-authentication-hooks';
15+
import { assignDefaultOrganization } from '../new-user/organization-assignment';
1716

1817
export const authenticationService = {
1918
async signUp(params: SignUpParams): Promise<AuthenticationResponse> {
2019
const { user, tablesRefreshToken } = await createUser(params);
2120

22-
await hooks.get().postSignUp({
23-
user,
24-
});
21+
await assignDefaultOrganization(user);
2522

2623
return this.authResponse(user, tablesRefreshToken);
2724
},
@@ -137,16 +134,3 @@ type AssertPasswordsMatchParams = {
137134
requestPassword: string;
138135
userPassword: string;
139136
};
140-
141-
type SignUpResponseParams = {
142-
user: User;
143-
tablesAccessToken: string;
144-
tablesRefreshToken: string;
145-
referringUserId?: UserId;
146-
};
147-
148-
type SignInResponseParams = {
149-
user: User;
150-
tablesAccessToken: string;
151-
tablesRefreshToken: string;
152-
};

packages/server/api/src/app/authentication/authentication.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
ALL_PRINCIPAL_TYPES,
99
OpsEdition,
1010
PrincipalType,
11+
Provider,
1112
SignInRequest,
1213
SignUpRequest,
1314
} from '@openops/shared';
@@ -16,7 +17,6 @@ import { resolveOrganizationIdForAuthnRequest } from '../organization/organizati
1617
import { userService } from '../user/user-service';
1718
import { analyticsAuthenticationService } from './analytics-authentication-service';
1819
import { authenticationService } from './authentication-service';
19-
import { Provider } from './authentication-service/hooks/authentication-service-hooks';
2020
import {
2121
removeAuthCookiesAndReply,
2222
setAuthCookiesAndReply,
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import {
2+
ApplicationError,
3+
ErrorCode,
4+
isNil,
5+
PrincipalType,
6+
Project,
7+
ProjectMemberRole,
8+
User,
9+
} from '@openops/shared';
10+
import { organizationService } from '../../organization/organization.service';
11+
import { projectService } from '../../project/project-service';
12+
import { userService } from '../../user/user-service';
13+
import { accessTokenManager } from './access-token-manager';
14+
15+
export async function getProjectAndToken(
16+
user: User,
17+
tablesRefreshToken: string,
18+
): Promise<{
19+
user: User;
20+
project: Project;
21+
token: string;
22+
tablesRefreshToken: string;
23+
projectRole: ProjectMemberRole;
24+
}> {
25+
const updatedUser = await userService.getOneOrFail({ id: user.id });
26+
27+
const project = await projectService.getOneForUser(updatedUser);
28+
if (isNil(project)) {
29+
throw new ApplicationError({
30+
code: ErrorCode.INVITATION_ONLY_SIGN_UP,
31+
params: {
32+
message: 'No project found for user',
33+
},
34+
});
35+
}
36+
37+
const organization = await organizationService.getOneOrThrow(
38+
project.organizationId,
39+
);
40+
41+
const token = await accessTokenManager.generateToken({
42+
id: user.id,
43+
type: PrincipalType.USER,
44+
projectId: project.id,
45+
organization: {
46+
id: organization.id,
47+
},
48+
});
49+
50+
return {
51+
user: updatedUser,
52+
token,
53+
project,
54+
tablesRefreshToken,
55+
projectRole: ProjectMemberRole.ADMIN,
56+
};
57+
}

packages/server/api/src/app/authentication/new-user/create-user.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import { cryptoUtils } from '@openops/server-shared';
22
import {
33
ApplicationError,
4+
assertValidEmail,
5+
assertValidPassword,
46
ErrorCode,
7+
isEmpty,
58
OrganizationRole,
9+
Provider,
610
User,
711
UserStatus,
812
} from '@openops/shared';
913
import { QueryFailedError } from 'typeorm';
1014
import { openopsTables } from '../../openops-tables';
1115
import { userService } from '../../user/user-service';
12-
import { authenticationServiceHooks as hooks } from '../authentication-service/hooks';
13-
import { Provider } from '../authentication-service/hooks/authentication-service-hooks';
1416

1517
type NewUserParams = {
1618
email: string;
@@ -37,11 +39,18 @@ const assertValidSignUpParams = async ({
3739
email: string;
3840
password: string;
3941
}): Promise<void> => {
40-
await hooks.get().preSignUp({
41-
name,
42-
email,
43-
password,
44-
});
42+
assertValidEmail(email);
43+
assertValidPassword(password);
44+
45+
if (isEmpty(name)) {
46+
throw new ApplicationError({
47+
code: ErrorCode.INVALID_NAME_FOR_USER,
48+
params: {
49+
name,
50+
message: 'First name and last name were not provided correctly.',
51+
},
52+
});
53+
}
4554
};
4655

4756
const createEditorUser = async (
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { authenticateDefaultUserInOpenOpsTables } from '@openops/common';
2+
import { AppSystemProp, system } from '@openops/server-shared';
3+
import { ApplicationError, ErrorCode, isNil, User } from '@openops/shared';
4+
import { openopsTables } from '../../openops-tables';
5+
import { organizationService } from '../../organization/organization.service';
6+
import { userService } from '../../user/user-service';
7+
8+
export async function assignDefaultOrganization(user: User): Promise<void> {
9+
let organization = await organizationService.getOldestOrganization();
10+
11+
const adminUser = await userService.getUserByEmailOrFail({
12+
email: system.getOrThrow(AppSystemProp.OPENOPS_ADMIN_EMAIL),
13+
});
14+
15+
organization = !isNil(adminUser.organizationId)
16+
? await organizationService.getOne(adminUser.organizationId)
17+
: organization;
18+
19+
if (!organization) {
20+
throw new ApplicationError({
21+
code: ErrorCode.ENTITY_NOT_FOUND,
22+
params: {
23+
message: 'Admin organization not found',
24+
},
25+
});
26+
}
27+
28+
await userService.addUserToOrganization({
29+
id: user.id,
30+
organizationId: organization.id,
31+
});
32+
33+
await addUserToDefaultWorkspace({
34+
email: user.email,
35+
workspaceId: organization.tablesWorkspaceId,
36+
});
37+
}
38+
39+
async function addUserToDefaultWorkspace(values: {
40+
email: string;
41+
workspaceId: number;
42+
}): Promise<void> {
43+
const { token: defaultToken } =
44+
await authenticateDefaultUserInOpenOpsTables();
45+
46+
await openopsTables.addUserToWorkspace(defaultToken, {
47+
...values,
48+
});
49+
}

packages/server/api/src/app/database/seeds/dev-seeds.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { logger, SharedSystemProp, system } from '@openops/server-shared';
2-
import { EnvironmentType } from '@openops/shared';
2+
import { EnvironmentType, Provider } from '@openops/shared';
33
import { authenticationService } from '../../authentication/authentication-service';
4-
import { Provider } from '../../authentication/authentication-service/hooks/authentication-service-hooks';
54
import { FlagEntity } from '../../flags/flag.entity';
65
import { databaseConnection } from '../database-connection';
76

0 commit comments

Comments
 (0)