-
Notifications
You must be signed in to change notification settings - Fork 82
/
index.js
143 lines (134 loc) · 4.62 KB
/
index.js
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
const express = require('express');
const cors = require('cors');
const dotenv = require('dotenv');
const {RtcTokenBuilder, RtcRole, RtmTokenBuilder, RtmRole} = require('agora-access-token');
dotenv.config();
const app = express();
const PORT = process.env.PORT || 8080;
const APP_ID = process.env.APP_ID;
const APP_CERTIFICATE = process.env.APP_CERTIFICATE;
const nocache = (_, resp, next) => {
resp.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
resp.header('Expires', '-1');
resp.header('Pragma', 'no-cache');
next();
}
const ping = (req, resp) => {
resp.send({message: 'pong'});
}
const generateRTCToken = (req, resp) => {
// set response header
resp.header('Access-Control-Allow-Origin', '*');
// get channel name
const channelName = req.params.channel;
if (!channelName) {
return resp.status(400).json({ 'error': 'channel is required' });
}
// get uid
let uid = req.params.uid;
if(!uid || uid === '') {
return resp.status(400).json({ 'error': 'uid is required' });
}
// get role
let role;
if (req.params.role === 'publisher') {
role = RtcRole.PUBLISHER;
} else if (req.params.role === 'audience') {
role = RtcRole.SUBSCRIBER
} else {
return resp.status(400).json({ 'error': 'role is incorrect' });
}
// get the expire time
let expireTime = req.query.expiry;
if (!expireTime || expireTime === '') {
expireTime = 3600;
} else {
expireTime = parseInt(expireTime, 10);
}
// calculate privilege expire time
const currentTime = Math.floor(Date.now() / 1000);
const privilegeExpireTime = currentTime + expireTime;
// build the token
let token;
if (req.params.tokentype === 'userAccount') {
token = RtcTokenBuilder.buildTokenWithAccount(APP_ID, APP_CERTIFICATE, channelName, uid, role, privilegeExpireTime);
} else if (req.params.tokentype === 'uid') {
token = RtcTokenBuilder.buildTokenWithUid(APP_ID, APP_CERTIFICATE, channelName, uid, role, privilegeExpireTime);
} else {
return resp.status(400).json({ 'error': 'token type is invalid' });
}
// return the token
return resp.json({ 'rtcToken': token });
}
const generateRTMToken = (req, resp) => {
// set response header
resp.header('Access-Control-Allow-Origin', '*');
// get uid
let uid = req.params.uid;
if(!uid || uid === '') {
return resp.status(400).json({ 'error': 'uid is required' });
}
// get role
let role = RtmRole.Rtm_User;
// get the expire time
let expireTime = req.query.expiry;
if (!expireTime || expireTime === '') {
expireTime = 3600;
} else {
expireTime = parseInt(expireTime, 10);
}
// calculate privilege expire time
const currentTime = Math.floor(Date.now() / 1000);
const privilegeExpireTime = currentTime + expireTime;
// build the token
console.log(APP_ID, APP_CERTIFICATE, uid, role, privilegeExpireTime)
const token = RtmTokenBuilder.buildToken(APP_ID, APP_CERTIFICATE, uid, role, privilegeExpireTime);
// return the token
return resp.json({ 'rtmToken': token });
}
const generateRTEToken = (req, resp) => {
// set response header
resp.header('Access-Control-Allow-Origin', '*');
// get channel name
const channelName = req.params.channel;
if (!channelName) {
return resp.status(400).json({ 'error': 'channel is required' });
}
// get uid
let uid = req.params.uid;
if(!uid || uid === '') {
return resp.status(400).json({ 'error': 'uid is required' });
}
// get role
let role;
if (req.params.role === 'publisher') {
role = RtcRole.PUBLISHER;
} else if (req.params.role === 'audience') {
role = RtcRole.SUBSCRIBER
} else {
return resp.status(400).json({ 'error': 'role is incorrect' });
}
// get the expire time
let expireTime = req.query.expiry;
if (!expireTime || expireTime === '') {
expireTime = 3600;
} else {
expireTime = parseInt(expireTime, 10);
}
// calculate privilege expire time
const currentTime = Math.floor(Date.now() / 1000);
const privilegeExpireTime = currentTime + expireTime;
// build the token
const rtcToken = RtcTokenBuilder.buildTokenWithUid(APP_ID, APP_CERTIFICATE, channelName, uid, role, privilegeExpireTime);
const rtmToken = RtmTokenBuilder.buildToken(APP_ID, APP_CERTIFICATE, uid, role, privilegeExpireTime);
// return the token
return resp.json({ 'rtcToken': rtcToken, 'rtmToken': rtmToken });
}
app.options('*', cors());
app.get('/ping', nocache, ping)
app.get('/rtc/:channel/:role/:tokentype/:uid', nocache , generateRTCToken);
app.get('/rtm/:uid/', nocache , generateRTMToken);
app.get('/rte/:channel/:role/:tokentype/:uid', nocache , generateRTEToken);
app.listen(PORT, () => {
console.log(`Listening on port: ${PORT}`);
});