Repository navigation
Expand file tree
/
Copy pathauth.controller.ts
More file actions
285 lines (264 loc) · 8.97 KB
/
Copy pathauth.controller.ts
File metadata and controls
285 lines (264 loc) · 8.97 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import {
Body,
Controller,
Delete,
Get,
HttpCode,
Param,
Patch,
Post,
Req,
Res,
} from '@nestjs/common'
import { Throttle } from '@nestjs/throttler'
import type { Request, Response } from 'express'
import {
AUTH_ACTION,
accountParametersSchema,
registerAuthenticationSchema,
loginAuthenticationSchema,
accountAuthenticationVerifySchema,
accountAuthenticationChangeSchema,
type RegisterAuthentication,
type LoginAuthentication,
type AccountAuthenticationVerify,
type AccountAuthenticationChange,
type User,
} from '@vault/contracts'
import { ValidationPipe } from '@api/checks/validation.pipe'
import {
profileSchema,
loginOptionsSchema,
authenticationVerifySchema,
registrationVerifySchema,
emptySchema,
type ProfileInput,
type AuthenticationInput,
type RegistrationInput,
} from '@api/checks/passkey.checks'
import { Configuration } from '@api/common/config'
import { ROUTE, SECURITY } from '@api/common/constants'
import { respond } from '@api/utilities/response.utility'
import {
cookieOptions,
newSessionToken,
requestCookie as cookie,
} from '@api/utilities/session.utility'
import { AuthService } from './auth.service'
import { PasswordService } from './password.service'
import { CurrentUser, PassiveSession, Public } from './auth.decorators'
@Controller(ROUTE.auth)
class AuthController {
constructor(
private readonly service: AuthService,
private readonly configuration: Configuration,
private readonly passwords: PasswordService,
) {}
@Public()
@HttpCode(200)
@Throttle({ default: { limit: SECURITY.authLimit, ttl: SECURITY.rateWindowMs } })
@Post('password/parameters')
async passwordParameters(
@Body(new ValidationPipe(accountParametersSchema)) input: { email: string },
) {
return respond(await this.passwords.parameters(input.email))
}
@Public()
@Throttle({ default: { limit: SECURITY.authLimit, ttl: SECURITY.rateWindowMs } })
@Post(AUTH_ACTION.register)
async passwordRegister(
@Body(new ValidationPipe(registerAuthenticationSchema)) input: RegisterAuthentication,
@Res({ passthrough: true }) response: Response,
) {
const result = await this.passwords.register(input)
this.session(response, result.token)
return respond(result.user)
}
@Public()
@HttpCode(200)
@Throttle({ default: { limit: SECURITY.authLimit, ttl: SECURITY.rateWindowMs } })
@Post(AUTH_ACTION.login)
async passwordLogin(
@Body(new ValidationPipe(loginAuthenticationSchema)) input: LoginAuthentication,
@Res({ passthrough: true }) response: Response,
) {
const result = await this.passwords.login(input)
this.session(response, result.token)
return respond(result.user)
}
@Get('methods')
async methods(@CurrentUser() user: User) {
return respond(await this.passwords.methods(user.id))
}
@HttpCode(200)
@Throttle({ default: { limit: SECURITY.authLimit, ttl: SECURITY.rateWindowMs } })
@Post('password/verify')
async passwordVerify(
@CurrentUser() user: User,
@Body(new ValidationPipe(accountAuthenticationVerifySchema))
input: AccountAuthenticationVerify,
@Req() request: Request,
) {
await this.passwords.verify(
user.id,
cookie(request, SECURITY.sessionCookie),
input.credential,
)
return respond(null)
}
@Throttle({ default: { limit: SECURITY.authLimit, ttl: SECURITY.rateWindowMs } })
@Patch('password')
async passwordChange(
@CurrentUser() user: User,
@Body(new ValidationPipe(accountAuthenticationChangeSchema))
input: AccountAuthenticationChange,
@Req() request: Request,
) {
return respond(
await this.passwords.change(user, cookie(request, SECURITY.sessionCookie), input),
)
}
@Public()
@Throttle({ default: { limit: SECURITY.authLimit, ttl: SECURITY.rateWindowMs } })
@Post('login/options')
async loginOptions(
@Body(new ValidationPipe(loginOptionsSchema)) input: { email: string },
@Res({ passthrough: true }) response: Response,
) {
return respond(await this.service.loginOptions(input.email, this.flow(response)))
}
@Public()
@HttpCode(200)
@Throttle({ default: { limit: SECURITY.authLimit, ttl: SECURITY.rateWindowMs } })
@Post('login/verify')
async login(
@Body(new ValidationPipe(authenticationVerifySchema)) input: AuthenticationInput,
@Req() request: Request,
@Res({ passthrough: true }) response: Response,
) {
const result = await this.service.login(input, cookie(request, SECURITY.flowCookie))
this.session(response, result.token)
return respond(result.user)
}
@Throttle({ default: { limit: SECURITY.authLimit, ttl: SECURITY.rateWindowMs } })
@Post('passkeys/options')
async addOptions(
@CurrentUser() user: User,
@Req() request: Request,
@Res({ passthrough: true }) response: Response,
) {
await new ValidationPipe(emptySchema).transform(request.body)
return respond(
await this.service.addOptions(
user,
cookie(request, SECURITY.sessionCookie),
this.flow(response),
),
)
}
@Throttle({ default: { limit: SECURITY.authLimit, ttl: SECURITY.rateWindowMs } })
@Post('passkeys/verify')
async add(
@CurrentUser() user: User,
@Body(new ValidationPipe(registrationVerifySchema)) input: RegistrationInput,
@Req() request: Request,
) {
await this.service.add(
user,
cookie(request, SECURITY.sessionCookie),
input,
cookie(request, SECURITY.flowCookie),
)
return respond(null)
}
@Get('passkeys')
async passkeys(@CurrentUser() user: User) {
return respond(await this.service.listPasskeys(user.id))
}
@HttpCode(200)
@Delete('passkeys/:id')
async remove(@CurrentUser() user: User, @Param('id') id: string, @Req() request: Request) {
await this.service.removePasskey(user.id, cookie(request, SECURITY.sessionCookie), id)
return respond(null)
}
@Throttle({ default: { limit: SECURITY.authLimit, ttl: SECURITY.rateWindowMs } })
@Post('reauth/options')
async reauthOptions(
@CurrentUser() user: User,
@Req() request: Request,
@Res({ passthrough: true }) response: Response,
) {
await new ValidationPipe(emptySchema).transform(request.body)
return respond(
await this.service.reauthOptions(
user,
cookie(request, SECURITY.sessionCookie),
this.flow(response),
),
)
}
@HttpCode(200)
@Throttle({ default: { limit: SECURITY.authLimit, ttl: SECURITY.rateWindowMs } })
@Post('reauth/verify')
async reauth(
@CurrentUser() user: User,
@Body(new ValidationPipe(authenticationVerifySchema)) input: AuthenticationInput,
@Req() request: Request,
) {
await this.service.reauth(
user,
cookie(request, SECURITY.sessionCookie),
input,
cookie(request, SECURITY.flowCookie),
)
return respond(null)
}
@Get(AUTH_ACTION.me)
@PassiveSession()
me(@CurrentUser() user: User) {
return respond(user)
}
@Patch(AUTH_ACTION.me)
async updateProfile(
@CurrentUser() user: User,
@Body(new ValidationPipe(profileSchema)) input: ProfileInput,
@Req() request: Request,
) {
return respond(
await this.service.updateProfile(
user.id,
cookie(request, SECURITY.sessionCookie),
input,
),
)
}
@HttpCode(200)
@Post(AUTH_ACTION.logout)
async logout(@Req() request: Request, @Res({ passthrough: true }) response: Response) {
await this.service.logout(cookie(request, SECURITY.sessionCookie))
response.clearCookie(
SECURITY.sessionCookie,
cookieOptions(this.configuration.values.NODE_ENV),
)
response.clearCookie(SECURITY.flowCookie, cookieOptions(this.configuration.values.NODE_ENV))
return respond(null)
}
private flow(response: Response): string {
const binding = newSessionToken()
response.cookie(
SECURITY.flowCookie,
binding,
cookieOptions(this.configuration.values.NODE_ENV, SECURITY.challengeMs),
)
return binding
}
private session(response: Response, token: string): void {
response.cookie(
SECURITY.sessionCookie,
token,
cookieOptions(this.configuration.values.NODE_ENV),
)
response.clearCookie(SECURITY.flowCookie, cookieOptions(this.configuration.values.NODE_ENV))
}
}
export { AuthController }