-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.controller.ts
More file actions
85 lines (76 loc) · 2.5 KB
/
app.controller.ts
File metadata and controls
85 lines (76 loc) · 2.5 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
import { Controller, Get, Render, Req } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Request } from 'express';
import { randomStringGenerator } from '@nestjs/common/utils/random-string-generator.util';
import * as crypto from 'crypto';
const BRAND_AUTH_HASH_ALGORITHM = 'sha1';
const BRAND_AUTH_HASH_DIGEST = 'hex';
@Controller()
export class AppController {
constructor(private readonly configService: ConfigService) {}
@Get()
@Render('index')
root(@Req() req: Request) {
if (req.query.userId && req.query.userDisplayName) {
return {
chatBoxLibraryUrl: this.configService.get('chatBoxLibraryUrl'),
chatBoxData: this.getChatBoxesData(req),
};
}
}
generateToken(chatBoxData) {
const headerMissingToken = {
'x-nonce': chatBoxData['xNonce'],
'x-timestamp': chatBoxData['timestamp'],
'x-brand-id': chatBoxData['brandId'],
};
const sortedRequestProperties = Object.keys(headerMissingToken)
.sort()
.reduce((accumulator, key) => {
accumulator[key] = headerMissingToken[key];
return accumulator;
}, {});
const requestString = new URLSearchParams(
sortedRequestProperties,
).toString();
return crypto
.createHmac(
BRAND_AUTH_HASH_ALGORITHM,
this.configService.get('chatBoxSecretKey'),
)
.update(requestString)
.digest(BRAND_AUTH_HASH_DIGEST);
}
getChatBoxesData(req: Request) {
const chatBoxesData = [
{
brandId: this.configService.get('brandId'),
chatName: 'Crypto future trade',
channelId: 'Future_123',
channelName: 'Crypto future trade',
channelRoomId: 'Future-trade',
channelRoomName: 'Crypto future trade',
userId: req.query.userId,
userDisplayName: req.query.userDisplayName,
timestamp: Date.now(),
xNonce: randomStringGenerator(),
},
{
brandId: this.configService.get('brandId'),
chatName: 'Crypto margin trade',
channelId: 'TAMCHUYEN_456',
channelName: 'Crypto margin trade',
channelRoomId: 'TC-TG',
channelRoomName: 'Room tám chuyện',
userId: req.query.userId,
userDisplayName: req.query.userDisplayName,
timestamp: Date.now(),
xNonce: randomStringGenerator(),
},
];
for (const i in chatBoxesData) {
chatBoxesData[i]['token'] = this.generateToken(chatBoxesData[i]);
}
return chatBoxesData;
}
}