forked from DhanushNehru/ToolJet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.controller.ts
More file actions
209 lines (185 loc) · 7.53 KB
/
Copy pathapp.controller.ts
File metadata and controls
209 lines (185 loc) · 7.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import {
Controller,
Get,
Request,
Post,
UseGuards,
Body,
Param,
BadRequestException,
Query,
Res,
NotFoundException,
} from '@nestjs/common';
import { User } from 'src/decorators/user.decorator';
import { JwtAuthGuard } from '../../src/modules/auth/jwt-auth.guard';
import {
AppAuthenticationDto,
AppForgotPasswordDto,
AppPasswordResetDto,
AppSignupDto,
} from '@dto/app-authentication.dto';
import { AuthService } from '../services/auth.service';
import { SignupDisableGuard } from 'src/modules/auth/signup-disable.guard';
import { CreateAdminDto, CreateUserDto } from '@dto/user.dto';
import { AcceptInviteDto } from '@dto/accept-organization-invite.dto';
import { FirstUserSignupDisableGuard } from 'src/modules/auth/first-user-signup-disable.guard';
import { FirstUserSignupGuard } from 'src/modules/auth/first-user-signup.guard';
import { OrganizationAuthGuard } from 'src/modules/auth/organization-auth.guard';
import { AuthorizeWorkspaceGuard } from 'src/modules/auth/authorize-workspace-guard';
import { Response } from 'express';
import { SessionAuthGuard } from 'src/modules/auth/session-auth-guard';
import { UsersService } from '@services/users.service';
import { SessionService } from '@services/session.service';
import { OrganizationsService } from '@services/organizations.service';
import { Organization } from 'src/entities/organization.entity';
import { InvitedUserSessionAuthGuard } from 'src/modules/auth/invited-user-session.guard';
import { InvitedUser } from 'src/decorators/invited-user.decorator';
import { InvitedUserSessionDto } from '@dto/invited-user-session.dto';
import { ActivateAccountWithTokenDto } from '@dto/activate-account-with-token.dto';
import { OrganizationInviteAuthGuard } from 'src/modules/auth/organization-invite-auth.guard';
import { ResendInviteDto } from '@dto/resend-invite.dto';
@Controller()
export class AppController {
constructor(
private authService: AuthService,
private userService: UsersService,
private sessionService: SessionService,
private organizationService: OrganizationsService
) {}
@Post('authenticate')
async login(@Body() appAuthDto: AppAuthenticationDto, @Res({ passthrough: true }) response: Response) {
return this.authService.login(response, appAuthDto);
}
@UseGuards(OrganizationAuthGuard)
@Post('authenticate/:organizationId')
async organizationLogin(
@User() user,
@Body() appAuthDto: AppAuthenticationDto,
@Param('organizationId') organizationId,
@Res({ passthrough: true }) response: Response
) {
return this.authService.login(response, appAuthDto, organizationId, user);
}
@UseGuards(InvitedUserSessionAuthGuard)
@Post('invited-user-session')
async getInvitedUserSessionDetails(@User() user, @InvitedUser() invitedUser, @Body() tokens: InvitedUserSessionDto) {
return await this.authService.validateInvitedUserSession(user, invitedUser, tokens);
}
@UseGuards(SignupDisableGuard)
@UseGuards(FirstUserSignupDisableGuard)
@Post('activate-account-with-token')
async activateAccountWithToken(
@Body() activateAccountWithPasswordDto: ActivateAccountWithTokenDto,
@Res({ passthrough: true }) response: Response
) {
return this.authService.activateAccountWithToken(activateAccountWithPasswordDto, response);
}
@UseGuards(SessionAuthGuard)
@Get('session')
async getSessionDetails(@User() user, @Query('appId') appId: string, @Query('workspaceSlug') workspaceSlug: string) {
let currentOrganization: Organization;
let app: { organizationId: string; isPublic: boolean };
if (appId) {
app = await this.userService.returnOrgIdOfAnApp(appId);
}
/* if the user has a session and the app is public, we don't need to authorize the app organization id */
if ((app && !app?.isPublic) || workspaceSlug) {
const organization = await this.organizationService.fetchOrganization(workspaceSlug || app.organizationId);
if (!organization) {
throw new NotFoundException("Coudn't found workspace. workspace id or slug is incorrect!.");
}
currentOrganization = organization;
}
return await this.authService.generateSessionPayload(user, currentOrganization);
}
@UseGuards(SessionAuthGuard)
@Get('logout')
async terminateUserSession(@User() user, @Res({ passthrough: true }) response: Response) {
await this.sessionService.terminateSession(user.id, user.sessionId, response);
return;
}
@UseGuards(JwtAuthGuard)
@Get('profile')
async getUserDetails(@User() user) {
return this.sessionService.getSessionUserDetails(user);
}
@UseGuards(AuthorizeWorkspaceGuard)
@Get('authorize')
async authorize(@User() user) {
return await this.authService.authorizeOrganization(user);
}
@UseGuards(JwtAuthGuard)
@Get('switch/:organizationId')
async switch(@Param('organizationId') organizationId, @User() user, @Res({ passthrough: true }) response: Response) {
if (!organizationId) {
throw new BadRequestException();
}
return await this.authService.switchOrganization(response, organizationId, user);
}
@UseGuards(FirstUserSignupGuard)
@Post('setup-admin')
async setupAdmin(@Body() userCreateDto: CreateAdminDto, @Res({ passthrough: true }) response: Response) {
return await this.authService.setupAdmin(response, userCreateDto);
}
@UseGuards(FirstUserSignupDisableGuard)
@Post('setup-account-from-token')
async create(@Body() userCreateDto: CreateUserDto, @Res({ passthrough: true }) response: Response) {
return await this.authService.setupAccountFromInvitationToken(response, userCreateDto);
}
@UseGuards(FirstUserSignupDisableGuard)
@UseGuards(OrganizationInviteAuthGuard)
@Post('accept-invite')
async acceptInvite(
@User() user,
@Body() acceptInviteDto: AcceptInviteDto,
@Res({ passthrough: true }) response: Response
) {
return await this.authService.acceptOrganizationInvite(response, user, acceptInviteDto);
}
@UseGuards(SignupDisableGuard)
@UseGuards(FirstUserSignupDisableGuard)
@Post('signup')
async signup(@Body() appSignUpDto: AppSignupDto, @Res({ passthrough: true }) response: Response) {
return this.authService.signup(appSignUpDto, response);
}
@UseGuards(SignupDisableGuard)
@UseGuards(FirstUserSignupDisableGuard)
@Post('resend-invite')
async resendInvite(@Body() body: ResendInviteDto) {
return this.authService.resendEmail(body);
}
@UseGuards(FirstUserSignupDisableGuard)
@Get('verify-invite-token')
async verifyInviteToken(@Query('token') token, @Query('organizationToken') organizationToken) {
return await this.authService.verifyInviteToken(token, organizationToken);
}
@Get('invitee-details')
async getInviteeDetails(@Query('token') token) {
return await this.authService.getInviteeDetails(token);
}
@UseGuards(FirstUserSignupDisableGuard)
@Get('verify-organization-token')
async verifyOrganizationToken(@Query('token') token) {
return await this.authService.verifyOrganizationToken(token);
}
@Post('/forgot-password')
async forgotPassword(@Body() appAuthDto: AppForgotPasswordDto) {
await this.authService.forgotPassword(appAuthDto.email);
return {};
}
@Post('/reset-password')
async resetPassword(@Body() appAuthDto: AppPasswordResetDto) {
const { token, password } = appAuthDto;
await this.authService.resetPassword(token, password);
return {};
}
@Get(['/health', '/api/health'])
async healthCheck(@Request() req) {
return { works: 'yeah' };
}
@Get('/')
async rootPage(@Request() req) {
return { message: 'Instance seems healthy but this is probably not the right URL to access.' };
}
}