Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,21 @@ import {
AuthenticationResponse,
ErrorCode,
isNil,
Provider,
User,
UserId,
UserStatus,
} from '@openops/shared';
import { userService } from '../../user/user-service';
import { passwordHasher } from '../basic/password-hasher';
import { getProjectAndToken } from '../context/create-project-auth-context';
import { createUser } from '../new-user/create-user';
import { authenticationServiceHooks as hooks } from './hooks';
import { Provider } from './hooks/authentication-service-hooks';
import { getProjectAndToken } from './hooks/community-authentication-hooks';
import { assignDefaultOrganization } from '../new-user/organization-assignment';

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

await hooks.get().postSignUp({
user,
});
await assignDefaultOrganization(user);

return this.authResponse(user, tablesRefreshToken);
},
Expand Down Expand Up @@ -137,16 +134,3 @@ type AssertPasswordsMatchParams = {
requestPassword: string;
userPassword: string;
};

type SignUpResponseParams = {
user: User;
tablesAccessToken: string;
tablesRefreshToken: string;
referringUserId?: UserId;
};

type SignInResponseParams = {
user: User;
tablesAccessToken: string;
tablesRefreshToken: string;
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ALL_PRINCIPAL_TYPES,
OpsEdition,
PrincipalType,
Provider,
SignInRequest,
SignUpRequest,
} from '@openops/shared';
Expand All @@ -16,7 +17,6 @@ import { resolveOrganizationIdForAuthnRequest } from '../organization/organizati
import { userService } from '../user/user-service';
import { analyticsAuthenticationService } from './analytics-authentication-service';
import { authenticationService } from './authentication-service';
import { Provider } from './authentication-service/hooks/authentication-service-hooks';
import {
removeAuthCookiesAndReply,
setAuthCookiesAndReply,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {
ApplicationError,
ErrorCode,
isNil,
PrincipalType,
Project,
ProjectMemberRole,
User,
} from '@openops/shared';
import { organizationService } from '../../organization/organization.service';
import { projectService } from '../../project/project-service';
import { userService } from '../../user/user-service';
import { accessTokenManager } from './access-token-manager';

export async function getProjectAndToken(
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved from community-authentication-hooks.ts

user: User,
tablesRefreshToken: string,
): Promise<{
user: User;
project: Project;
token: string;
tablesRefreshToken: string;
projectRole: ProjectMemberRole;
}> {
const updatedUser = await userService.getOneOrFail({ id: user.id });

const project = await projectService.getOneForUser(updatedUser);
if (isNil(project)) {
throw new ApplicationError({
code: ErrorCode.INVITATION_ONLY_SIGN_UP,
params: {
message: 'No project found for user',
},
});
}

const organization = await organizationService.getOneOrThrow(
project.organizationId,
);

const token = await accessTokenManager.generateToken({
id: user.id,
type: PrincipalType.USER,
projectId: project.id,
organization: {
id: organization.id,
},
});

return {
user: updatedUser,
token,
project,
tablesRefreshToken,
projectRole: ProjectMemberRole.ADMIN,
};
}
23 changes: 16 additions & 7 deletions packages/server/api/src/app/authentication/new-user/create-user.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { cryptoUtils } from '@openops/server-shared';
import {
ApplicationError,
assertValidEmail,
assertValidPassword,
ErrorCode,
isEmpty,
OrganizationRole,
Provider,
User,
UserStatus,
} from '@openops/shared';
import { QueryFailedError } from 'typeorm';
import { openopsTables } from '../../openops-tables';
import { userService } from '../../user/user-service';
import { authenticationServiceHooks as hooks } from '../authentication-service/hooks';
import { Provider } from '../authentication-service/hooks/authentication-service-hooks';

type NewUserParams = {
email: string;
Expand All @@ -37,11 +39,18 @@ const assertValidSignUpParams = async ({
email: string;
password: string;
}): Promise<void> => {
await hooks.get().preSignUp({
name,
email,
password,
});
assertValidEmail(email);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved from community-authentication-hooks.ts

assertValidPassword(password);

if (isEmpty(name)) {
throw new ApplicationError({
code: ErrorCode.INVALID_NAME_FOR_USER,
params: {
name,
message: 'First name and last name were not provided correctly.',
},
});
}
};

const createEditorUser = async (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { authenticateDefaultUserInOpenOpsTables } from '@openops/common';
import { AppSystemProp, system } from '@openops/server-shared';
import { ApplicationError, ErrorCode, isNil, User } from '@openops/shared';
import { openopsTables } from '../../openops-tables';
import { organizationService } from '../../organization/organization.service';
import { userService } from '../../user/user-service';

export async function assignDefaultOrganization(user: User): Promise<void> {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved from community-authentication-hooks.ts

let organization = await organizationService.getOldestOrganization();

const adminUser = await userService.getUserByEmailOrFail({
email: system.getOrThrow(AppSystemProp.OPENOPS_ADMIN_EMAIL),
});

organization = !isNil(adminUser.organizationId)
? await organizationService.getOne(adminUser.organizationId)
: organization;

if (!organization) {
throw new ApplicationError({
code: ErrorCode.ENTITY_NOT_FOUND,
params: {
message: 'Admin organization not found',
},
});
}

await userService.addUserToOrganization({
id: user.id,
organizationId: organization.id,
});

await addUserToDefaultWorkspace({
email: user.email,
workspaceId: organization.tablesWorkspaceId,
});
}

async function addUserToDefaultWorkspace(values: {
email: string;
workspaceId: number;
}): Promise<void> {
const { token: defaultToken } =
await authenticateDefaultUserInOpenOpsTables();

await openopsTables.addUserToWorkspace(defaultToken, {
...values,
});
}
3 changes: 1 addition & 2 deletions packages/server/api/src/app/database/seeds/dev-seeds.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { logger, SharedSystemProp, system } from '@openops/server-shared';
import { EnvironmentType } from '@openops/shared';
import { EnvironmentType, Provider } from '@openops/shared';
import { authenticationService } from '../../authentication/authentication-service';
import { Provider } from '../../authentication/authentication-service/hooks/authentication-service-hooks';
import { FlagEntity } from '../../flags/flag.entity';
import { databaseConnection } from '../database-connection';

Expand Down
Loading
Loading