generated from spknetwork/js-typescript-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.controller.ts
379 lines (350 loc) · 10.8 KB
/
auth.controller.ts
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import { cryptoUtils } from "@hiveio/dhive";
import { BadRequestException, Body, Controller, Get, Headers, HttpException, HttpStatus, Post, Request, Response, UseGuards } from "@nestjs/common";
import { AuthGuard } from "@nestjs/passport";
import { ApiOkResponse, ApiBadRequestResponse, ApiInternalServerErrorResponse, ApiHeader, ApiUnauthorizedResponse, ApiOperation, ApiBody, ApiParam, ApiMovedPermanentlyResponse } from "@nestjs/swagger";
import moment from "moment";
import { authenticator } from "otplib";
import { HiveClient } from "../../utils/hiveClient";
import { LoginDto } from "../api/dto/Login.dto";
import { LoginErrorResponseDto } from "../api/dto/LoginErrorResponse.dto";
import { LoginResponseDto } from "../api/dto/LoginResponse.dto";
import { LoginSingletonDto } from "../api/dto/LoginSingleton.dto";
import { AuthService } from "./auth.service";
import { HiveAccountRepository } from "../../repositories/hive-account/hive-account.repository";
import { UserRepository } from "../../repositories/user/user.repository";
import { HiveRepository } from "../../repositories/hive/hive.repository";
import { EmailService } from "../email/email.service";
import bcrypt from 'bcryptjs'
import { Magic } from '@magic-sdk/admin';
@Controller('/api/v1/auth')
export class AuthController {
readonly #magic = new Magic(process.env.MAGIC_SECRET_KEY);
constructor(
private readonly authService: AuthService,
private readonly hiveAccountRepository: HiveAccountRepository,
private readonly userRepository: UserRepository,
private readonly hiveRepository: HiveRepository,
//private readonly delegatedAuthorityRepository: DelegatedAuthorityRepository,
private readonly emailService: EmailService
) {}
@UseGuards(AuthGuard('local'))
@Post('/login')
@ApiOkResponse({
description: "Login success",
type: LoginResponseDto
})
async login(@Request() req, @Body() body: LoginDto) {
return this.authService.login(req.user)
}
//@UseGuards(AuthGuard('local'))
@ApiOkResponse({
description: "Successfully logged in",
type: LoginResponseDto
})
@ApiBadRequestResponse({
description: "Invalid options",
type: LoginErrorResponseDto
})
@ApiInternalServerErrorResponse({
description: "Internal Server Error - unrelated to request body"
})
@Post('/login_singleton')
async loginSingletonReturn(@Body() body: LoginSingletonDto, @Headers('authorization') didToken?: string) {
if (body.network === 'hive') {
const proof_payload = JSON.parse(body.proof_payload)
const accountDetails = await this.hiveRepository.getAccount(proof_payload.account)
if (
this.hiveRepository.verifyHiveMessage(cryptoUtils.sha256(JSON.stringify(proof_payload)), body.proof, accountDetails) &&
new Date(proof_payload.ts) > moment().subtract('1', 'minute').toDate() //Extra safety to prevent request reuse
) {
if (this.hiveRepository.verifyPostingAuth(accountDetails)) {
return await this.authService.authenticateUser(proof_payload.account)
} else {
throw new HttpException(
{
reason: `Hive Account @${proof_payload.account} has not granted posting authority to @threespeak`,
errorType: "MISSING_POSTING_AUTHORITY"
},
HttpStatus.BAD_REQUEST,
)
}
} else {
throw new HttpException(
{
reason: 'Invalid Signature',
errorType: "INVALID_SIGNATURE"
},
HttpStatus.BAD_REQUEST,
)
}
} else if (body.network === 'did') {
try {
await this.#magic.token.validate(didToken)
return {
valid: true
}
} catch {
throw new HttpException(
{
reason: 'Invalid Decentralized ID',
errorType: "INVALID_DID"
},
HttpStatus.UNAUTHORIZED,
)
}
} else {
throw new HttpException(
{
reason: 'Unsupported network type',
errorType: "UNSUPPORTED_NETWORK"
},
HttpStatus.BAD_REQUEST,
)
}
// return this.authService.login(req.user)
}
@ApiHeader({
name: "Authorization",
description: "JWT Authorization",
example: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
required: true
})
@ApiOkResponse({
schema: {
properties: {
ok: {
type: 'boolean',
default: true
}
}
}
})
@ApiUnauthorizedResponse({
description: "Invalid or expired authentication token"
})
@UseGuards(AuthGuard('jwt'))
@Post('/check')
async checkAuth(@Request() req) {
console.log('user details check', req.user)
return {
ok: true
}
}
@ApiOperation({
summary: "Registers an account using light OTP/TOTP login"
})
@ApiBody({
schema: {
properties: {
username: {
type: 'string',
default: "test-account"
},
otp_code: {
type: 'string',
default: '029735'
},
secret: {
type: 'string',
default: "GYZWKZBSGUYDEMLGHFTDQYJWHEYTKMLCMZQTGZA"
}
}
}
})
@ApiOkResponse({
description: "Successfully logged in",
type: LoginResponseDto
})
@ApiBadRequestResponse({
description: "Invalid options",
schema: {
type: "object",
properties: {
reason: {
type: "string",
enum: ['Invalid OTP code', 'Hive account with the requested name already exists'],
default: 'Invalid OTP code'
},
errorType: {
type: "string",
enum: ['INVALID_OTP', 'HIVE_ACCOUNT_EXISTS'],
default: 'INVALID_OTP'
}
}
}
})
@ApiInternalServerErrorResponse({
description: "Internal Server Error - unrelated to request body"
})
@Post('/lite/register-initial')
async registerLite(@Body() body) {
const { username, otp_code } = body
const output = await HiveClient.database.getAccounts([username])
if (output.length === 0) {
// const secret = authenticator.generateSecret(32)
if(authenticator.verify({
token: otp_code,
secret: body.secret
})) {
// const accountCreation = await createAccountWithAuthority(
// username,
// process.env.ACCOUNT_CREATOR
// )
await this.hiveAccountRepository.createLite(username, body.secret)
const jwt = await this.authService.jwtService.signAsync({
username,
})
return {
// id: accountCreation.id,
access_token: jwt
}
} else {
throw new HttpException({
reason: "Invalid OTP code",
errorType: "INVALID_OTP"
}, HttpStatus.BAD_REQUEST)
}
} else {
throw new HttpException({
reason: 'Hive account with the requested name already exists',
errorType: "HIVE_ACCOUNT_EXISTS"
},
HttpStatus.BAD_REQUEST,
)
}
}
// @Post('/lite/register-initial')
// async registerLiteFinish(@Body() body) {
// await appContainer.self.usersDb.insertOne({
// status: 'requested',
// username: body.username
// })
// }
@ApiOperation({
summary: "Registers an account using email/password login"
})
@ApiBody({
schema: {
properties: {
password: {
type: 'string',
default: "!SUPER-SECRET_PASSWORD!"
},
email: {
type: 'string',
default: 'test@invalid.example.org'
}
}
}
})
@ApiOkResponse({
schema: {
properties: {
ok: {
type: 'boolean',
default: true
}
}
}
})
// @UseGuards(AuthGuard('local'))
@Post('/register')
async register(@Request() req, @Body() body) {
const password = req.body.password
const {email} = req.body;
const hashedPassword = bcrypt.hashSync(password, bcrypt.genSaltSync(10))
const existingRecord = await this.userRepository.findOneByEmail(email)
if(existingRecord) {
throw new HttpException(
{ reason: 'Email Password account already created!' },
HttpStatus.BAD_REQUEST,
)
} else {
const { email_code } = await this.authService.createUser(email, hashedPassword)
await this.emailService.sendRegistration(email, email_code);
return {
ok: true
}
}
// return this.authService.login(req.user);
}
@ApiParam({
name: "code",
type: 'string'
})
@ApiMovedPermanentlyResponse({
description: "Redirect user to 3Speak.tv",
})
@Get('/verifyemail')
async verifyEmail(@Request() req, @Response() res) {
const verifyCode = req.query.code
if (!verifyCode) {
throw new BadRequestException('Verification code is required');
}
await this.userRepository.verifyEmail(verifyCode);
return res.redirect('https://3speak.tv')
}
@ApiHeader({
name: "Authorization",
description: "JWT Authorization",
example: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
required: true
})
@ApiBody({
schema: {
properties: {
username: {
type: 'string',
default: "test-account",
description: "Username of requested HIVE account"
}
}
}
})
@ApiOkResponse({
description: "Account created",
schema: {
properties: {
id: {
type: 'string',
default: "f555e5e690aefa99f5d6c1fe47c08db6ad79af1f"
}
}
}
})
@UseGuards(AuthGuard('jwt'))
@Post('/request_hive_account')
async requestHiveAccount(@Request() req) {
const existingAcocunt = await this.hiveAccountRepository.findOneByOwner(req.user.user_id)
if (existingAcocunt) {
throw new HttpException(
{ reason: 'You have already created the maximum of 1 free Hive account' },
HttpStatus.BAD_REQUEST,
)
}
// console.log(existingAcocunt)
const output = await HiveClient.database.getAccounts([req.body.username])
// console.log(output)
if (output.length === 0) {
try {
const accountCreation = await this.hiveRepository.createAccountWithAuthority(
req.body.username,
process.env.ACCOUNT_CREATOR,
)
//Here will be thrown if failed at this point
await this.hiveAccountRepository.insertCreated(req.body.username, req.user.user_id)
return accountCreation
} catch(ex) {
throw new HttpException(
{ reason: `On chain error - ${ex.message}` },
HttpStatus.INTERNAL_SERVER_ERROR,
)
}
} else {
throw new HttpException(
{ reason: 'Hive account with the requested name already exists' },
HttpStatus.BAD_REQUEST,
)
}
}
}