-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccessToken.js
More file actions
39 lines (35 loc) · 984 Bytes
/
Copy pathAccessToken.js
File metadata and controls
39 lines (35 loc) · 984 Bytes
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
const jwt = require('jsonwebtoken');
const { v4: uuidv4 } = require('uuid');
function AccessToken(apiKey, secret, options) {
if (!apiKey) {
throw new Error('apiKey is required');
}
if (!secret) {
throw new Error('secret is required');
}
options = options || {};
this.apiKey = apiKey;
this.secret = secret;
this.audience = options.audience || 'apiRTC';
this.ttl = options.ttl || 3600;
this.apiRTC_UserAgent_Id = options.apiRTC_UserAgent_Id;
}
AccessToken.ALGORITHM = 'HS256';
AccessToken.prototype.toJwt = function() {
let payload = {
grants: {
apiRTC_UserAgent_Id: this.apiRTC_UserAgent_Id
}
};
return jwt.sign(payload, this.secret, {
header: {
typ: 'JWT'
},
algorithm: AccessToken.ALGORITHM,
subject: this.apiKey,
audience: this.audience,
expiresIn: this.ttl,
jwtid: uuidv4()
})
};
module.exports = AccessToken;