Skip to content

Commit

Permalink
Use filters
Browse files Browse the repository at this point in the history
  • Loading branch information
thomtrp committed Aug 6, 2024
1 parent ea83099 commit a7369af
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 185 deletions.
264 changes: 98 additions & 166 deletions packages/twenty-server/src/engine/core-modules/auth/auth.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { UseGuards } from '@nestjs/common';
import { UseFilters, UseGuards } from '@nestjs/common';
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
import { InjectRepository } from '@nestjs/typeorm';

import { Repository } from 'typeorm';

import { ApiKeyTokenInput } from 'src/engine/core-modules/auth/dto/api-key-token.input';
import { AppTokenInput } from 'src/engine/core-modules/auth/dto/app-token.input';
import { AuthorizeApp } from 'src/engine/core-modules/auth/dto/authorize-app.entity';
Expand All @@ -18,7 +16,7 @@ import { TransientToken } from 'src/engine/core-modules/auth/dto/transient-token
import { UpdatePasswordViaResetTokenInput } from 'src/engine/core-modules/auth/dto/update-password-via-reset-token.input';
import { ValidatePasswordResetToken } from 'src/engine/core-modules/auth/dto/validate-password-reset-token.entity';
import { ValidatePasswordResetTokenInput } from 'src/engine/core-modules/auth/dto/validate-password-reset-token.input';
import { authGraphqlApiExceptionHandler } from 'src/engine/core-modules/auth/utils/auth-graphql-api-exception-handler.util';
import { AuthGraphqlApiExceptionFilter } from 'src/engine/core-modules/auth/filters/auth-graphql-api-exception.filter';
import { UserService } from 'src/engine/core-modules/user/services/user.service';
import { User } from 'src/engine/core-modules/user/user.entity';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
Expand All @@ -42,10 +40,10 @@ import { AuthService } from './services/auth.service';
import { TokenService } from './services/token.service';

@Resolver()
@UseFilters(AuthGraphqlApiExceptionFilter)
export class AuthResolver {
constructor(
@InjectRepository(Workspace, 'core')
private readonly workspaceRepository: Repository<Workspace>,
private authService: AuthService,
private tokenService: TokenService,
private userService: UserService,
Expand All @@ -55,189 +53,139 @@ export class AuthResolver {
@Query(() => UserExists)
async checkUserExists(
@Args() checkUserExistsInput: CheckUserExistsInput,
): Promise<UserExists | void> {
try {
const { exists } = await this.authService.checkUserExists(
checkUserExistsInput.email,
);

return { exists };
} catch (error) {
authGraphqlApiExceptionHandler(error);
}
): Promise<UserExists> {
const { exists } = await this.authService.checkUserExists(
checkUserExistsInput.email,
);

return { exists };
}

@Query(() => WorkspaceInviteHashValid)
async checkWorkspaceInviteHashIsValid(
@Args() workspaceInviteHashValidInput: WorkspaceInviteHashValidInput,
): Promise<WorkspaceInviteHashValid | void> {
try {
return await this.authService.checkWorkspaceInviteHashIsValid(
workspaceInviteHashValidInput.inviteHash,
);
} catch (error) {
authGraphqlApiExceptionHandler(error);
}
): Promise<WorkspaceInviteHashValid> {
return await this.authService.checkWorkspaceInviteHashIsValid(
workspaceInviteHashValidInput.inviteHash,
);
}

@Query(() => Workspace)
async findWorkspaceFromInviteHash(
@Args() workspaceInviteHashValidInput: WorkspaceInviteHashValidInput,
): Promise<Workspace | void> {
try {
return await this.authService.findWorkspaceFromInviteHash(
workspaceInviteHashValidInput.inviteHash,
);
} catch (error) {
authGraphqlApiExceptionHandler(error);
}
): Promise<Workspace> {
return await this.authService.findWorkspaceFromInviteHash(
workspaceInviteHashValidInput.inviteHash,
);
}

@UseGuards(CaptchaGuard)
@Mutation(() => LoginToken)
async challenge(
@Args() challengeInput: ChallengeInput,
): Promise<LoginToken | void> {
try {
const user = await this.authService.challenge(challengeInput);
const loginToken = await this.tokenService.generateLoginToken(user.email);

return { loginToken };
} catch (error) {
authGraphqlApiExceptionHandler(error);
}
async challenge(@Args() challengeInput: ChallengeInput): Promise<LoginToken> {
const user = await this.authService.challenge(challengeInput);
const loginToken = await this.tokenService.generateLoginToken(user.email);

return { loginToken };
}

@UseGuards(CaptchaGuard)
@Mutation(() => LoginToken)
async signUp(@Args() signUpInput: SignUpInput): Promise<LoginToken | void> {
try {
const user = await this.authService.signInUp({
...signUpInput,
fromSSO: false,
});

const loginToken = await this.tokenService.generateLoginToken(user.email);

return { loginToken };
} catch (error) {
authGraphqlApiExceptionHandler(error);
}
async signUp(@Args() signUpInput: SignUpInput): Promise<LoginToken> {
const user = await this.authService.signInUp({
...signUpInput,
fromSSO: false,
});

const loginToken = await this.tokenService.generateLoginToken(user.email);

return { loginToken };
}

@Mutation(() => ExchangeAuthCode)
async exchangeAuthorizationCode(
@Args() exchangeAuthCodeInput: ExchangeAuthCodeInput,
) {
try {
const tokens = await this.tokenService.verifyAuthorizationCode(
exchangeAuthCodeInput,
);

return tokens;
} catch (error) {
authGraphqlApiExceptionHandler(error);
}
const tokens = await this.tokenService.verifyAuthorizationCode(
exchangeAuthCodeInput,
);

return tokens;
}

@Mutation(() => TransientToken)
@UseGuards(JwtAuthGuard)
async generateTransientToken(
@AuthUser() user: User,
): Promise<TransientToken | void> {
try {
const workspaceMember = await this.userService.loadWorkspaceMember(user);

if (!workspaceMember) {
return;
}
const transientToken = await this.tokenService.generateTransientToken(
workspaceMember.id,
user.id,
user.defaultWorkspace.id,
);

return { transientToken };
} catch (error) {
authGraphqlApiExceptionHandler(error);
const workspaceMember = await this.userService.loadWorkspaceMember(user);

if (!workspaceMember) {
return;
}
const transientToken = await this.tokenService.generateTransientToken(
workspaceMember.id,
user.id,
user.defaultWorkspace.id,
);

return { transientToken };
}

@Mutation(() => Verify)
async verify(@Args() verifyInput: VerifyInput): Promise<Verify | void> {
try {
const email = await this.tokenService.verifyLoginToken(
verifyInput.loginToken,
);
async verify(@Args() verifyInput: VerifyInput): Promise<Verify> {
const email = await this.tokenService.verifyLoginToken(
verifyInput.loginToken,
);

const result = await this.authService.verify(email);
const result = await this.authService.verify(email);

return result;
} catch (error) {
authGraphqlApiExceptionHandler(error);
}
return result;
}

@Mutation(() => AuthorizeApp)
@UseGuards(JwtAuthGuard)
async authorizeApp(
@Args() authorizeAppInput: AuthorizeAppInput,
@AuthUser() user: User,
): Promise<AuthorizeApp | void> {
try {
const authorizedApp = await this.authService.generateAuthorizationCode(
authorizeAppInput,
user,
);

return authorizedApp;
} catch (error) {
authGraphqlApiExceptionHandler(error);
}
): Promise<AuthorizeApp> {
const authorizedApp = await this.authService.generateAuthorizationCode(
authorizeAppInput,
user,
);

return authorizedApp;
}

@Mutation(() => AuthTokens)
@UseGuards(JwtAuthGuard)
async generateJWT(
@AuthUser() user: User,
@Args() args: GenerateJwtInput,
): Promise<AuthTokens | void> {
try {
const token = await this.tokenService.generateSwitchWorkspaceToken(
user,
args.workspaceId,
);

return token;
} catch (error) {
authGraphqlApiExceptionHandler(error);
}
): Promise<AuthTokens> {
const token = await this.tokenService.generateSwitchWorkspaceToken(
user,
args.workspaceId,
);

return token;
}

@Mutation(() => AuthTokens)
async renewToken(@Args() args: AppTokenInput): Promise<AuthTokens | void> {
try {
const tokens = await this.tokenService.generateTokensFromRefreshToken(
args.appToken,
);

return { tokens: tokens };
} catch (error) {
authGraphqlApiExceptionHandler(error);
}
async renewToken(@Args() args: AppTokenInput): Promise<AuthTokens> {
const tokens = await this.tokenService.generateTokensFromRefreshToken(
args.appToken,
);

return { tokens: tokens };
}

@UseGuards(JwtAuthGuard)
@Mutation(() => Verify)
async impersonate(
@Args() impersonateInput: ImpersonateInput,
@AuthUser() user: User,
): Promise<Verify | void> {
try {
return await this.authService.impersonate(impersonateInput.userId, user);
} catch (error) {
authGraphqlApiExceptionHandler(error);
}
): Promise<Verify> {
return await this.authService.impersonate(impersonateInput.userId, user);
}

@UseGuards(JwtAuthGuard)
Expand All @@ -246,62 +194,46 @@ export class AuthResolver {
@Args() args: ApiKeyTokenInput,
@AuthWorkspace() { id: workspaceId }: Workspace,
): Promise<ApiKeyToken | undefined> {
try {
return await this.tokenService.generateApiKeyToken(
workspaceId,
args.apiKeyId,
args.expiresAt,
);
} catch (error) {
authGraphqlApiExceptionHandler(error);
}
return await this.tokenService.generateApiKeyToken(
workspaceId,
args.apiKeyId,
args.expiresAt,
);
}

@Mutation(() => EmailPasswordResetLink)
async emailPasswordResetLink(
@Args() emailPasswordResetInput: EmailPasswordResetLinkInput,
): Promise<EmailPasswordResetLink | void> {
try {
const resetToken = await this.tokenService.generatePasswordResetToken(
emailPasswordResetInput.email,
);

return await this.tokenService.sendEmailPasswordResetLink(
resetToken,
emailPasswordResetInput.email,
);
} catch (error) {
authGraphqlApiExceptionHandler(error);
}
): Promise<EmailPasswordResetLink> {
const resetToken = await this.tokenService.generatePasswordResetToken(
emailPasswordResetInput.email,
);

return await this.tokenService.sendEmailPasswordResetLink(
resetToken,
emailPasswordResetInput.email,
);
}

@Mutation(() => InvalidatePassword)
async updatePasswordViaResetToken(
@Args()
{ passwordResetToken, newPassword }: UpdatePasswordViaResetTokenInput,
): Promise<InvalidatePassword | void> {
try {
const { id } =
await this.tokenService.validatePasswordResetToken(passwordResetToken);
): Promise<InvalidatePassword> {
const { id } =
await this.tokenService.validatePasswordResetToken(passwordResetToken);

await this.authService.updatePassword(id, newPassword);
await this.authService.updatePassword(id, newPassword);

return await this.tokenService.invalidatePasswordResetToken(id);
} catch (error) {
authGraphqlApiExceptionHandler(error);
}
return await this.tokenService.invalidatePasswordResetToken(id);
}

@Query(() => ValidatePasswordResetToken)
async validatePasswordResetToken(
@Args() args: ValidatePasswordResetTokenInput,
): Promise<ValidatePasswordResetToken | void> {
try {
return this.tokenService.validatePasswordResetToken(
args.passwordResetToken,
);
} catch (error) {
authGraphqlApiExceptionHandler(error);
}
): Promise<ValidatePasswordResetToken> {
return this.tokenService.validatePasswordResetToken(
args.passwordResetToken,
);
}
}
Loading

0 comments on commit a7369af

Please sign in to comment.