-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapi.js
192 lines (174 loc) · 6.71 KB
/
api.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
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
const jwt = require('jsonwebtoken');
const axios = require("axios");
const {URL} = require('url');
// todo: url from token, error handling when REST api call fails
module.exports = function (_from, _connId, _context) {
const endpoint = process.env["AzureSignalREndpoint"];
if (!endpoint) throw "AzureSignalREndpoint is not set";
const key = process.env["AzureSignalRAccessKey"];
if (!key) throw "AzureSignalRAccessKey is not set";
var url = new URL(endpoint);
const host = url.toString();
// no port for audience
url.port = '';
const audienceHost = url.toString();
var getToken = function (path) {
return "Bearer " + jwt.sign({}, key, {
issuer: 'chat',
subject: _from,
audience: audienceHost + path,
expiresIn: "12h",
algorithm: "HS256"
});
}
return {
addToGroup: async (user, group) => {
var log = "Adding " + user + " to group " + group;
var path = "ws/api/v1/hubs/chat/groups/" + group + "/users/" + user;
var url = host + path;
var token = getToken(path);
_context.log(log + ", url:" + url + ", token:" + token);
const response = await axios.put(url,
null, {
headers: { "Authorization": token }
});
return {
type: 'log',
text: log
};
},
addConnectionToGroup: async (conn, group) => {
// PUT /ws/api/v1/hubs/chat/groups/1/connections/123-456
var log = "Adding " + conn + " to group " + group;
var path = "ws/api/v1/hubs/chat/groups/" + group + "/connections/" + conn;
var url = host + path;
var token = getToken(path);
_context.log(log + ", url:" + url + ", token:" + token);
const response = await axios.put(url,
null, {
headers: { "Authorization": token }
});
return {
type: 'log',
text: log
};
},
removeFromGroup: async (user, group) => {
// DELETE /ws/api/v1/hubs/chat/groups/1/users/a
var log = "Removing " + user + " from group " + group;
var path = "ws/api/v1/hubs/chat/groups/" + group + "/users/" + user;
var url = host + path;
var token = getToken(path);
_context.log(log + ", url:" + url + ", token:" + token);
const response = await axios.delete(url, {
headers: { "Authorization": token }
});
return {
type: 'log',
text: log
};
},
removeFromAllGroup: async (user) => {
// DELETE /ws/api/v1/hubs/chat/users/a/groups
var log = "Removing " + user + " from all groups";
var path = "ws/api/v1/hubs/chat/users/" + user + "/groups";
var url = host + path;
var token = getToken(path);
_context.log(log + ", url:" + url + ", token:" + token);
const response = await axios.delete(url, {
headers: { "Authorization": token }
});
return {
type: 'log',
text: log
};
},
removeConnectionFromGroup: async (conn, group) => {
// DELETE /ws/api/v1/hubs/chat/groups/1/connections/a
var log = "Removing " + conn + " from group " + group;
var path = "ws/api/v1/hubs/chat/groups/" + group + "/connections/" + conn;
var url = host + path;
var token = getToken(path);
_context.log(log + ", url:" + url + ", token:" + token);
const response = await axios.delete(url, {
headers: { "Authorization": token }
});
return {
type: 'log',
text: log
};
},
sendToGroup: async (group, content) => {
// POST /ws/api/v1/hubs/chat/groups/{group}
const log = "Sending to group " + group + ": " + JSON.stringify(content);
const path = "ws/api/v1/hubs/chat/groups/" + group;
var url = host + path;
var token = getToken(path);
_context.log(log + ", url:" + url + ", token:" + token);
const response = await axios.post(url, content, {
headers: {
"Content-Type": "application/json",
"Authorization": token
}
});
return {
type: 'log',
text: log
};
},
sendToUser: async (user, content) => {
// POST /ws/api/v1/hubs/chat/users/{user}
var log = "Send to user " + user + ": " + JSON.stringify(content);
var path = "ws/api/v1/hubs/chat/users/" + user;
var url = host + path;
var token = getToken(path);
_context.log(log + ", url:" + url + ", token:" + token);
const response = await axios.post(url, content, {
headers: {
"Content-Type": "application/json",
"Authorization": token
}
});
return {
type: 'log',
text: log
};
},
sendToConnection: async (connection, content) => {
// POST /ws/api/v1/hubs/chat/connections/{connection}
var log = "Send to connection " + connection + ": " + JSON.stringify(content);
var path = "ws/api/v1/hubs/chat/connections/" + connection;
var url = host + path;
var token = getToken(path);
_context.log(log + ", url:" + url + ", token:" + token);
const response = await axios.post(url, content, {
headers: {
"Content-Type": "application/json",
"Authorization": token
}
});
return {
type: 'log',
text: log
};
},
broadcast: async (content) => {
// POST /ws/api/v1/hubs/chat
var log = "Broadcast: " + JSON.stringify(content);
var path = "ws/api/v1/hubs/chat";
var url = host + path;
var token = getToken(path);
_context.log(log + ", url:" + url + ", token:" + token);
const response = await axios.post(url, content, {
headers: {
"Content-Type": "application/json",
"Authorization": token
}
});
return {
type: 'log',
text: log
};
},
};
};