1
- import {
2
- BadRequestException ,
3
- ForbiddenException ,
4
- InternalServerErrorException ,
5
- NotFoundException ,
6
- UseGuards ,
7
- } from '@nestjs/common' ;
1
+ import { UseGuards } from '@nestjs/common' ;
8
2
import { Args , Mutation , Query , Resolver } from '@nestjs/graphql' ;
9
3
import { InjectRepository } from '@nestjs/typeorm' ;
10
4
@@ -25,15 +19,13 @@ import { UpdatePasswordViaResetTokenInput } from 'src/engine/core-modules/auth/d
25
19
import { ValidatePasswordResetToken } from 'src/engine/core-modules/auth/dto/validate-password-reset-token.entity' ;
26
20
import { ValidatePasswordResetTokenInput } from 'src/engine/core-modules/auth/dto/validate-password-reset-token.input' ;
27
21
import { authGraphqlApiExceptionHandler } from 'src/engine/core-modules/auth/utils/auth-graphql-api-exception-handler.util' ;
28
- import { NotFoundError } from 'src/engine/core-modules/graphql/utils/graphql-errors.util' ;
29
22
import { UserService } from 'src/engine/core-modules/user/services/user.service' ;
30
23
import { User } from 'src/engine/core-modules/user/user.entity' ;
31
24
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity' ;
32
25
import { AuthUser } from 'src/engine/decorators/auth/auth-user.decorator' ;
33
26
import { AuthWorkspace } from 'src/engine/decorators/auth/auth-workspace.decorator' ;
34
27
import { JwtAuthGuard } from 'src/engine/guards/jwt.auth.guard' ;
35
28
import { CaptchaGuard } from 'src/engine/integrations/captcha/captcha.guard' ;
36
- import { assert } from 'src/utils/assert' ;
37
29
38
30
import { ChallengeInput } from './dto/challenge.input' ;
39
31
import { ImpersonateInput } from './dto/impersonate.input' ;
@@ -91,81 +83,99 @@ export class AuthResolver {
91
83
@Query ( ( ) => Workspace )
92
84
async findWorkspaceFromInviteHash (
93
85
@Args ( ) workspaceInviteHashValidInput : WorkspaceInviteHashValidInput ,
94
- ) : Promise < Workspace > {
95
- const workspace = await this . workspaceRepository . findOneBy ( {
96
- inviteHash : workspaceInviteHashValidInput . inviteHash ,
97
- } ) ;
98
-
99
- if ( ! workspace ) {
100
- throw new NotFoundError ( 'Workspace does not exist' ) ;
86
+ ) : Promise < Workspace | void > {
87
+ try {
88
+ return await this . authService . findWorkspaceFromInviteHash (
89
+ workspaceInviteHashValidInput . inviteHash ,
90
+ ) ;
91
+ } catch ( error ) {
92
+ authGraphqlApiExceptionHandler ( error ) ;
101
93
}
102
-
103
- return workspace ;
104
94
}
105
95
106
96
@UseGuards ( CaptchaGuard )
107
97
@Mutation ( ( ) => LoginToken )
108
- async challenge ( @Args ( ) challengeInput : ChallengeInput ) : Promise < LoginToken > {
109
- const user = await this . authService . challenge ( challengeInput ) ;
110
- const loginToken = await this . tokenService . generateLoginToken ( user . email ) ;
98
+ async challenge (
99
+ @Args ( ) challengeInput : ChallengeInput ,
100
+ ) : Promise < LoginToken | void > {
101
+ try {
102
+ const user = await this . authService . challenge ( challengeInput ) ;
103
+ const loginToken = await this . tokenService . generateLoginToken ( user . email ) ;
111
104
112
- return { loginToken } ;
105
+ return { loginToken } ;
106
+ } catch ( error ) {
107
+ authGraphqlApiExceptionHandler ( error ) ;
108
+ }
113
109
}
114
110
115
111
@UseGuards ( CaptchaGuard )
116
112
@Mutation ( ( ) => LoginToken )
117
- async signUp ( @Args ( ) signUpInput : SignUpInput ) : Promise < LoginToken > {
118
- const user = await this . authService . signInUp ( {
119
- ...signUpInput ,
120
- fromSSO : false ,
121
- } ) ;
113
+ async signUp ( @Args ( ) signUpInput : SignUpInput ) : Promise < LoginToken | void > {
114
+ try {
115
+ const user = await this . authService . signInUp ( {
116
+ ...signUpInput ,
117
+ fromSSO : false ,
118
+ } ) ;
122
119
123
- const loginToken = await this . tokenService . generateLoginToken ( user . email ) ;
120
+ const loginToken = await this . tokenService . generateLoginToken ( user . email ) ;
124
121
125
- return { loginToken } ;
122
+ return { loginToken } ;
123
+ } catch ( error ) {
124
+ authGraphqlApiExceptionHandler ( error ) ;
125
+ }
126
126
}
127
127
128
128
@Mutation ( ( ) => ExchangeAuthCode )
129
129
async exchangeAuthorizationCode (
130
130
@Args ( ) exchangeAuthCodeInput : ExchangeAuthCodeInput ,
131
131
) {
132
- const tokens = await this . tokenService . verifyAuthorizationCode (
133
- exchangeAuthCodeInput ,
134
- ) ;
132
+ try {
133
+ const tokens = await this . tokenService . verifyAuthorizationCode (
134
+ exchangeAuthCodeInput ,
135
+ ) ;
135
136
136
- return tokens ;
137
+ return tokens ;
138
+ } catch ( error ) {
139
+ authGraphqlApiExceptionHandler ( error ) ;
140
+ }
137
141
}
138
142
139
143
@Mutation ( ( ) => TransientToken )
140
144
@UseGuards ( JwtAuthGuard )
141
145
async generateTransientToken (
142
146
@AuthUser ( ) user : User ,
143
147
) : Promise < TransientToken | void > {
144
- const workspaceMember = await this . userService . loadWorkspaceMember ( user ) ;
148
+ try {
149
+ const workspaceMember = await this . userService . loadWorkspaceMember ( user ) ;
150
+
151
+ if ( ! workspaceMember ) {
152
+ return ;
153
+ }
154
+ const transientToken = await this . tokenService . generateTransientToken (
155
+ workspaceMember . id ,
156
+ user . id ,
157
+ user . defaultWorkspace . id ,
158
+ ) ;
145
159
146
- if ( ! workspaceMember ) {
147
- return ;
160
+ return { transientToken } ;
161
+ } catch ( error ) {
162
+ authGraphqlApiExceptionHandler ( error ) ;
148
163
}
149
- const transientToken = await this . tokenService . generateTransientToken (
150
- workspaceMember . id ,
151
- user . id ,
152
- user . defaultWorkspace . id ,
153
- ) ;
154
-
155
- return { transientToken } ;
156
164
}
157
165
158
166
@Mutation ( ( ) => Verify )
159
- async verify ( @Args ( ) verifyInput : VerifyInput ) : Promise < Verify > {
160
- const email = await this . tokenService . verifyLoginToken (
161
- verifyInput . loginToken ,
162
- ) ;
163
-
164
- assert ( email , 'Invalid token' , ForbiddenException ) ;
167
+ async verify ( @Args ( ) verifyInput : VerifyInput ) : Promise < Verify | void > {
168
+ try {
169
+ const email = await this . tokenService . verifyLoginToken (
170
+ verifyInput . loginToken ,
171
+ ) ;
165
172
166
- const result = await this . authService . verify ( email ) ;
173
+ const result = await this . authService . verify ( email ) ;
167
174
168
- return result ;
175
+ return result ;
176
+ } catch ( error ) {
177
+ authGraphqlApiExceptionHandler ( error ) ;
178
+ }
169
179
}
170
180
171
181
@Mutation ( ( ) => AuthorizeApp )
@@ -191,35 +201,40 @@ export class AuthResolver {
191
201
async generateJWT (
192
202
@AuthUser ( ) user : User ,
193
203
@Args ( ) args : GenerateJwtInput ,
194
- ) : Promise < AuthTokens > {
195
- const token = await this . tokenService . generateSwitchWorkspaceToken (
196
- user ,
197
- args . workspaceId ,
198
- ) ;
204
+ ) : Promise < AuthTokens | void > {
205
+ try {
206
+ const token = await this . tokenService . generateSwitchWorkspaceToken (
207
+ user ,
208
+ args . workspaceId ,
209
+ ) ;
199
210
200
- return token ;
211
+ return token ;
212
+ } catch ( error ) {
213
+ authGraphqlApiExceptionHandler ( error ) ;
214
+ }
201
215
}
202
216
203
217
@Mutation ( ( ) => AuthTokens )
204
- async renewToken ( @Args ( ) args : AppTokenInput ) : Promise < AuthTokens > {
205
- if ( ! args . appToken ) {
206
- throw new BadRequestException ( 'Refresh token is mendatory' ) ;
207
- }
208
-
209
- const tokens = await this . tokenService . generateTokensFromRefreshToken (
210
- args . appToken ,
211
- ) ;
218
+ async renewToken ( @Args ( ) args : AppTokenInput ) : Promise < AuthTokens | void > {
219
+ try {
220
+ const tokens = await this . tokenService . generateTokensFromRefreshToken (
221
+ args . appToken ,
222
+ ) ;
212
223
213
- return { tokens : tokens } ;
224
+ return { tokens : tokens } ;
225
+ } catch ( error ) {
226
+ authGraphqlApiExceptionHandler ( error ) ;
227
+ }
214
228
}
215
229
216
230
@UseGuards ( JwtAuthGuard )
217
231
@Mutation ( ( ) => Verify )
218
232
async impersonate (
219
233
@Args ( ) impersonateInput : ImpersonateInput ,
234
+ @AuthUser ( ) user : User ,
220
235
) : Promise < Verify | void > {
221
236
try {
222
- return await this . authService . impersonate ( impersonateInput . userId ) ;
237
+ return await this . authService . impersonate ( impersonateInput . userId , user ) ;
223
238
} catch ( error ) {
224
239
authGraphqlApiExceptionHandler ( error ) ;
225
240
}
@@ -231,53 +246,62 @@ export class AuthResolver {
231
246
@Args ( ) args : ApiKeyTokenInput ,
232
247
@AuthWorkspace ( ) { id : workspaceId } : Workspace ,
233
248
) : Promise < ApiKeyToken | undefined > {
234
- return await this . tokenService . generateApiKeyToken (
235
- workspaceId ,
236
- args . apiKeyId ,
237
- args . expiresAt ,
238
- ) ;
249
+ try {
250
+ return await this . tokenService . generateApiKeyToken (
251
+ workspaceId ,
252
+ args . apiKeyId ,
253
+ args . expiresAt ,
254
+ ) ;
255
+ } catch ( error ) {
256
+ authGraphqlApiExceptionHandler ( error ) ;
257
+ }
239
258
}
240
259
241
260
@Mutation ( ( ) => EmailPasswordResetLink )
242
261
async emailPasswordResetLink (
243
262
@Args ( ) emailPasswordResetInput : EmailPasswordResetLinkInput ,
244
- ) : Promise < EmailPasswordResetLink > {
245
- const resetToken = await this . tokenService . generatePasswordResetToken (
246
- emailPasswordResetInput . email ,
247
- ) ;
248
-
249
- return await this . tokenService . sendEmailPasswordResetLink (
250
- resetToken ,
251
- emailPasswordResetInput . email ,
252
- ) ;
263
+ ) : Promise < EmailPasswordResetLink | void > {
264
+ try {
265
+ const resetToken = await this . tokenService . generatePasswordResetToken (
266
+ emailPasswordResetInput . email ,
267
+ ) ;
268
+
269
+ return await this . tokenService . sendEmailPasswordResetLink (
270
+ resetToken ,
271
+ emailPasswordResetInput . email ,
272
+ ) ;
273
+ } catch ( error ) {
274
+ authGraphqlApiExceptionHandler ( error ) ;
275
+ }
253
276
}
254
277
255
278
@Mutation ( ( ) => InvalidatePassword )
256
279
async updatePasswordViaResetToken (
257
280
@Args ( ) args : UpdatePasswordViaResetTokenInput ,
258
- ) : Promise < InvalidatePassword > {
259
- const { id } = await this . tokenService . validatePasswordResetToken (
260
- args . passwordResetToken ,
261
- ) ;
262
-
263
- assert ( id , 'User not found' , NotFoundException ) ;
264
-
265
- const { success } = await this . authService . updatePassword (
266
- id ,
267
- args . newPassword ,
268
- ) ;
281
+ ) : Promise < InvalidatePassword | void > {
282
+ try {
283
+ const { id } = await this . tokenService . validatePasswordResetToken (
284
+ args . passwordResetToken ,
285
+ ) ;
269
286
270
- assert ( success , 'Password update failed' , InternalServerErrorException ) ;
287
+ await this . authService . updatePassword ( id , args . newPassword ) ;
271
288
272
- return await this . tokenService . invalidatePasswordResetToken ( id ) ;
289
+ return await this . tokenService . invalidatePasswordResetToken ( id ) ;
290
+ } catch ( error ) {
291
+ authGraphqlApiExceptionHandler ( error ) ;
292
+ }
273
293
}
274
294
275
295
@Query ( ( ) => ValidatePasswordResetToken )
276
296
async validatePasswordResetToken (
277
297
@Args ( ) args : ValidatePasswordResetTokenInput ,
278
- ) : Promise < ValidatePasswordResetToken > {
279
- return this . tokenService . validatePasswordResetToken (
280
- args . passwordResetToken ,
281
- ) ;
298
+ ) : Promise < ValidatePasswordResetToken | void > {
299
+ try {
300
+ return this . tokenService . validatePasswordResetToken (
301
+ args . passwordResetToken ,
302
+ ) ;
303
+ } catch ( error ) {
304
+ authGraphqlApiExceptionHandler ( error ) ;
305
+ }
282
306
}
283
307
}
0 commit comments