Skip to content
This repository was archived by the owner on Oct 23, 2024. It is now read-only.

Commit 6a84d9f

Browse files
committed
Add token for WebTransport in the response of join.
No update for tests for portal because it looks like no check in or night testing is running the test code, and some cases are failed in my development environment.
1 parent 6d6c6df commit 6a84d9f

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

source/agent/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,10 @@ var joinCluster = function (on_ok) {
9393
purpose: myPurpose,
9494
clusterName: config.cluster.name,
9595
joinRetry: config.cluster.worker.join_retry,
96+
// Cannot find a defination about |info|. It looks like it will be used by cluster manager, but agents and portal may have different properties of |info|.
9697
info: {
9798
ip: config.cluster.worker.ip,
99+
hostname: config[myPurpose] ? config[myPurpose].hostname : undefined,
98100
port: config[myPurpose] ? config[myPurpose].port : undefined,
99101
purpose: myPurpose,
100102
state: 2,

source/portal/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,13 @@ var rpcPublic = {
232232
var notifyFail = (err) => {};
233233
socketio_server && socketio_server.notify(participantId, event, data).catch(notifyFail);
234234
callback('callback', 'ok');
235+
},
236+
validateWebTransportToken: (token, callback) => {
237+
if(portal.validateWebTransportToken(token)) {
238+
callback('callback','ok');
239+
} else {
240+
callback('callback', 'error');
241+
}
235242
}
236243
};
237244

source/portal/portal.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ var url = require('url');
99
var crypto = require('crypto');
1010
var log = require('./logger').logger.getLogger('Portal');
1111
var dataAccess = require('./data_access');
12+
const { v4 : uuid } = require('uuid');
13+
const vsprintf = require("sprintf-js").vsprintf;
1214

1315
var Portal = function(spec, rpcReq) {
1416
var that = {},
@@ -24,6 +26,38 @@ var Portal = function(spec, rpcReq) {
2426
*/
2527
var participants = {};
2628

29+
// Key is token, value is participant ID. An ID is only valid when the participant is online.
30+
const webTransportIds = new Map();
31+
const calculateSignatureForWebTransportToken = (token) => {
32+
const toSign = vsprintf('%s,%s,%s,%s', [
33+
token.tokenId,
34+
token.transportId,
35+
token.participantId,
36+
token.issueTime
37+
]);
38+
const signed = crypto.createHmac('sha256', token_key).update(toSign).digest('hex');
39+
return (Buffer.from(signed)).toString('base64');
40+
};
41+
const generateWebTransportToken = (participantId) => {
42+
const now = Date.now();
43+
const token = {
44+
tokenId : uuid().replace(/-/g, ''),
45+
transportId: uuid().replace(/-/g, ''),
46+
participantId : participantId,
47+
issueTime : now,
48+
};
49+
token.signature = calculateSignatureForWebTransportToken(token);
50+
return token;
51+
};
52+
53+
that.validateWebTransportToken = (token) => {
54+
// |participants| is better to be a map.
55+
if (!participants.hasOwnProperty(token.participantId)) {
56+
return false;
57+
}
58+
return calculateSignatureForWebTransportToken(token) == token.signature;
59+
};
60+
2761
that.updateTokenKey = function(tokenKey) {
2862
token_key = tokenKey;
2963
};
@@ -78,13 +112,19 @@ var Portal = function(spec, rpcReq) {
78112
controller: room_controller
79113
};
80114

115+
let webTransportToken = undefined;
116+
if (token.webTransportUrl) {
117+
webTransportToken = (Buffer.from(JSON.stringify(generateWebTransportToken(participantId)))).toString('base64');
118+
}
119+
81120
return {
82121
tokenCode: tokenCode,
83122
data: {
84123
user: userInfo,
85124
role: role,
86125
permission: joinResult.permission,
87-
room: joinResult.room
126+
room: joinResult.room,
127+
webTransportToken: webTransportToken
88128
}
89129
};
90130
});

0 commit comments

Comments
 (0)