pusher 5.0.1 → 5.1.0-beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/README.md +33 -6
- package/lib/auth.js +10 -0
- package/lib/pusher.js +69 -2
- package/package.json +1 -1
- package/tests/integration/pusher/authenticate.js +160 -33
- package/tests/integration/pusher/terminate.js +55 -0
- package/tests/integration/pusher/trigger.js +42 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
## 5.1.0-beta (2022-04-22)
|
|
2
|
+
|
|
3
|
+
[ADDED] Support for terminating user connections based on user id
|
|
4
|
+
[ADDED] Support for sending messages to users based on user id
|
|
5
|
+
[ADDED] Support for implementing user authentication endpoints
|
|
6
|
+
[DEPRECATED] authenticate function is deprecated. The same functionality (and interface) is now provided by authorizeChannel
|
|
7
|
+
|
|
1
8
|
## 5.0.1 (2022-01-24)
|
|
2
9
|
|
|
3
10
|
[FIXED] Incorrect `require` on `version.js` was causing a compilation error in Webpack
|
package/README.md
CHANGED
|
@@ -241,7 +241,7 @@ pusher
|
|
|
241
241
|
|
|
242
242
|
This library supports end-to-end encryption of your private channels. This means that only you and your connected clients will be able to read your messages. Pusher cannot decrypt them. You can enable this feature by following these steps:
|
|
243
243
|
|
|
244
|
-
1. You should first set up Private channels. This involves [creating an
|
|
244
|
+
1. You should first set up Private channels. This involves [creating an authorization endpoint on your server](https://pusher.com/docs/authenticating_users).
|
|
245
245
|
|
|
246
246
|
2. Next, generate your 32 byte master encryption key, encode it as base64 and pass it to the Pusher constructor.
|
|
247
247
|
|
|
@@ -278,17 +278,44 @@ pusher.trigger(["channel-1", "private-encrypted-channel-2"], "test_event", {
|
|
|
278
278
|
|
|
279
279
|
Rationale: the methods in this library map directly to individual Channels HTTP API requests. If we allowed triggering a single event on multiple channels (some encrypted, some unencrypted), then it would require two API requests: one where the event is encrypted to the encrypted channels, and one where the event is unencrypted for unencrypted channels.
|
|
280
280
|
|
|
281
|
-
### Authenticating
|
|
281
|
+
### Authenticating users
|
|
282
282
|
|
|
283
|
-
To
|
|
283
|
+
To authenticate users during sign in, you can use the `authenticateUser` function:
|
|
284
284
|
|
|
285
285
|
```javascript
|
|
286
|
-
const
|
|
286
|
+
const userData = {
|
|
287
|
+
id: "unique_user_id",
|
|
288
|
+
name: "John Doe",
|
|
289
|
+
image: "https://...",
|
|
290
|
+
}
|
|
291
|
+
const auth = pusher.authenticateUser(socketId, userData)
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
The `userData` parameter must contain an `id` property with a non empty string. For more information see: <http://pusher.com/docs/authenticating_users>
|
|
295
|
+
|
|
296
|
+
### Terminating user connections
|
|
297
|
+
|
|
298
|
+
In order to terminate a user's connections, the user must have been authenticated. Check the [Server user authentication docs](http://pusher.com/docs/authenticating_users) for the information on how to create a user authentication endpoint.
|
|
299
|
+
|
|
300
|
+
To terminate all connections established by a given user, you can use the `terminateUserConnections` function:
|
|
301
|
+
|
|
302
|
+
```javascript
|
|
303
|
+
pusher.terminateUserConnections(userId)
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
Please note, that it only terminates the user's active connections. This means, if nothing else is done, the user will be able to reconnect. For more information see: [Terminating user connections docs](https://pusher.com/docs/channels/server_api/terminating-user-connections/).
|
|
307
|
+
|
|
308
|
+
### Private channel authorisation
|
|
309
|
+
|
|
310
|
+
To authorise your users to access private channels on Pusher Channels, you can use the `authorizeChannel` function:
|
|
311
|
+
|
|
312
|
+
```javascript
|
|
313
|
+
const auth = pusher.authorizeChannel(socketId, channel)
|
|
287
314
|
```
|
|
288
315
|
|
|
289
316
|
For more information see: <http://pusher.com/docs/authenticating_users>
|
|
290
317
|
|
|
291
|
-
###
|
|
318
|
+
### Presence channel authorisation
|
|
292
319
|
|
|
293
320
|
Using presence channels is similar to private channels, but you can specify extra data to identify that particular user:
|
|
294
321
|
|
|
@@ -300,7 +327,7 @@ const channelData = {
|
|
|
300
327
|
twitter_id: '@leggetter'
|
|
301
328
|
}
|
|
302
329
|
};
|
|
303
|
-
const auth = pusher.
|
|
330
|
+
const auth = pusher.authorizeChannel(socketId, channel, channelData);
|
|
304
331
|
```
|
|
305
332
|
|
|
306
333
|
The `auth` is then returned to the caller as JSON.
|
package/lib/auth.js
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
const util = require("./util")
|
|
2
2
|
|
|
3
|
+
function getSocketSignatureForUser(token, socketId, userData) {
|
|
4
|
+
const serializedUserData = JSON.stringify(userData)
|
|
5
|
+
const signature = token.sign(`${socketId}::user::${serializedUserData}`)
|
|
6
|
+
return {
|
|
7
|
+
auth: `${token.key}:${signature}`,
|
|
8
|
+
user_data: serializedUserData,
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
3
12
|
function getSocketSignature(pusher, token, channel, socketID, data) {
|
|
4
13
|
const result = {}
|
|
5
14
|
|
|
@@ -26,4 +35,5 @@ function getSocketSignature(pusher, token, channel, socketID, data) {
|
|
|
26
35
|
return result
|
|
27
36
|
}
|
|
28
37
|
|
|
38
|
+
exports.getSocketSignatureForUser = getSocketSignatureForUser
|
|
29
39
|
exports.getSocketSignature = getSocketSignature
|
package/lib/pusher.js
CHANGED
|
@@ -34,6 +34,19 @@ const validateSocketId = function (socketId) {
|
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
const validateUserId = function (userId) {
|
|
38
|
+
if (typeof userId !== "string" || userId === "") {
|
|
39
|
+
throw new Error("Invalid user id: '" + userId + "'")
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const validateUserData = function (userData) {
|
|
44
|
+
if (userData == null || typeof userData !== "object") {
|
|
45
|
+
throw new Error("Invalid user data: '" + userData + "'")
|
|
46
|
+
}
|
|
47
|
+
validateUserId(userData.id)
|
|
48
|
+
}
|
|
49
|
+
|
|
37
50
|
/** Provides access to Pusher's REST API, WebHooks and authentication.
|
|
38
51
|
*
|
|
39
52
|
* @constructor
|
|
@@ -103,9 +116,9 @@ Pusher.forCluster = function (cluster, options) {
|
|
|
103
116
|
* @param {String} socketId socket id
|
|
104
117
|
* @param {String} channel channel name
|
|
105
118
|
* @param {Object} [data] additional socket data
|
|
106
|
-
* @returns {String}
|
|
119
|
+
* @returns {String} authorization signature
|
|
107
120
|
*/
|
|
108
|
-
Pusher.prototype.
|
|
121
|
+
Pusher.prototype.authorizeChannel = function (socketId, channel, data) {
|
|
109
122
|
validateSocketId(socketId)
|
|
110
123
|
validateChannel(channel)
|
|
111
124
|
|
|
@@ -118,6 +131,60 @@ Pusher.prototype.authenticate = function (socketId, channel, data) {
|
|
|
118
131
|
)
|
|
119
132
|
}
|
|
120
133
|
|
|
134
|
+
/** Returns a signature for given socket id, channel and socket data.
|
|
135
|
+
*
|
|
136
|
+
* DEPRECATED. Use authorizeChannel.
|
|
137
|
+
*
|
|
138
|
+
* @param {String} socketId socket id
|
|
139
|
+
* @param {String} channel channel name
|
|
140
|
+
* @param {Object} [data] additional socket data
|
|
141
|
+
* @returns {String} authorization signature
|
|
142
|
+
*/
|
|
143
|
+
Pusher.prototype.authenticate = Pusher.prototype.authorizeChannel
|
|
144
|
+
|
|
145
|
+
/** Returns a signature for given socket id and user data.
|
|
146
|
+
*
|
|
147
|
+
* @param {String} socketId socket id
|
|
148
|
+
* @param {Object} userData user data
|
|
149
|
+
* @returns {String} authentication signature
|
|
150
|
+
*/
|
|
151
|
+
Pusher.prototype.authenticateUser = function (socketId, userData) {
|
|
152
|
+
validateSocketId(socketId)
|
|
153
|
+
validateUserData(userData)
|
|
154
|
+
|
|
155
|
+
return auth.getSocketSignatureForUser(this.config.token, socketId, userData)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Sends an event to a user.
|
|
159
|
+
*
|
|
160
|
+
* Event name can be at most 200 characters long.
|
|
161
|
+
*
|
|
162
|
+
* @param {String} userId user id
|
|
163
|
+
* @param {String} event event name
|
|
164
|
+
* @param data event data, objects are JSON-encoded
|
|
165
|
+
* @returns {Promise} a promise resolving to a response, or rejecting to a RequestError.
|
|
166
|
+
* @see RequestError
|
|
167
|
+
*/
|
|
168
|
+
Pusher.prototype.sendToUser = function (userId, event, data) {
|
|
169
|
+
if (event.length > 200) {
|
|
170
|
+
throw new Error("Too long event name: '" + event + "'")
|
|
171
|
+
}
|
|
172
|
+
validateUserId(userId)
|
|
173
|
+
return events.trigger(this, [`#server-to-user-${userId}`], event, data)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Terminate users's connections.
|
|
177
|
+
*
|
|
178
|
+
*
|
|
179
|
+
* @param {String} userId user id
|
|
180
|
+
* @returns {Promise} a promise resolving to a response, or rejecting to a RequestError.
|
|
181
|
+
* @see RequestError
|
|
182
|
+
*/
|
|
183
|
+
Pusher.prototype.terminateUserConnections = function (userId) {
|
|
184
|
+
validateUserId(userId)
|
|
185
|
+
return this.post({ path: `/users/${userId}/terminate_connections`, body: {} })
|
|
186
|
+
}
|
|
187
|
+
|
|
121
188
|
/** Triggers an event.
|
|
122
189
|
*
|
|
123
190
|
* Channel names can contain only characters which are alphanumeric, '_' or '-'
|
package/package.json
CHANGED
|
@@ -9,38 +9,163 @@ describe("Pusher", function () {
|
|
|
9
9
|
pusher = new Pusher({ appId: 10000, key: "aaaa", secret: "tofu" })
|
|
10
10
|
})
|
|
11
11
|
|
|
12
|
-
describe("#
|
|
12
|
+
describe("#authenticateUser", function () {
|
|
13
13
|
it("should prefix the signature with the app key", function () {
|
|
14
14
|
let pusher = new Pusher({ appId: 10000, key: "1234", secret: "tofu" })
|
|
15
|
-
expect(pusher.
|
|
15
|
+
expect(pusher.authenticateUser("123.456", { id: "45678" })).to.eql({
|
|
16
|
+
auth:
|
|
17
|
+
"1234:f4b1fdeea7c93e32648c7230e32b172057c5623cace6cfce791c6e7035e0babd",
|
|
18
|
+
user_data: '{"id":"45678"}',
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
pusher = new Pusher({ appId: 10000, key: "abcdef", secret: "tofu" })
|
|
22
|
+
expect(pusher.authenticateUser("123.456", { id: "45678" })).to.eql({
|
|
23
|
+
auth:
|
|
24
|
+
"abcdef:f4b1fdeea7c93e32648c7230e32b172057c5623cace6cfce791c6e7035e0babd",
|
|
25
|
+
user_data: '{"id":"45678"}',
|
|
26
|
+
})
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it("should return correct authentication signatures for different user data", function () {
|
|
30
|
+
expect(pusher.authenticateUser("123.456", { id: "45678" })).to.eql({
|
|
31
|
+
auth:
|
|
32
|
+
"aaaa:f4b1fdeea7c93e32648c7230e32b172057c5623cace6cfce791c6e7035e0babd",
|
|
33
|
+
user_data: '{"id":"45678"}',
|
|
34
|
+
})
|
|
35
|
+
expect(
|
|
36
|
+
pusher.authenticateUser("123.456", { id: "55555", user_name: "test" })
|
|
37
|
+
).to.eql({
|
|
38
|
+
auth:
|
|
39
|
+
"aaaa:b8a9f173455903792ae2b788add0c4c78ad7372b3ae7fb5769479276a1993743",
|
|
40
|
+
user_data: JSON.stringify({ id: "55555", user_name: "test" }),
|
|
41
|
+
})
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it("should return correct authentication signatures for different secrets", function () {
|
|
45
|
+
let pusher = new Pusher({ appId: 10000, key: "11111", secret: "1" })
|
|
46
|
+
expect(pusher.authenticateUser("123.456", { id: "45678" })).to.eql({
|
|
47
|
+
auth:
|
|
48
|
+
"11111:79bddf29fe8e2153dd5d8d569b3f45e5aeb26ae2eb4758879d844791b466cfa2",
|
|
49
|
+
user_data: '{"id":"45678"}',
|
|
50
|
+
})
|
|
51
|
+
pusher = new Pusher({ appId: 10000, key: "11111", secret: "2" })
|
|
52
|
+
expect(pusher.authenticateUser("123.456", { id: "45678" })).to.eql({
|
|
53
|
+
auth:
|
|
54
|
+
"11111:a542498ffa6faf6de7c17a8106b923c042319bd73acfd1d274df32e269b55d1f",
|
|
55
|
+
user_data: '{"id":"45678"}',
|
|
56
|
+
})
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it("should return correct authentication signature with utf-8 in user data", function () {
|
|
60
|
+
expect(pusher.authenticateUser("1.1", { id: "ą§¶™€łü€ß£" })).to.eql({
|
|
61
|
+
auth:
|
|
62
|
+
"aaaa:620494cee53d6c568b49598313194088afda37218f0d059af03c0c898ed61ff4",
|
|
63
|
+
user_data: '{"id":"ą§¶™€łü€ß£"}',
|
|
64
|
+
})
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
it("should raise an exception if socket id is not a string", function () {
|
|
68
|
+
expect(function () {
|
|
69
|
+
pusher.authenticateUser(undefined, { id: "123" })
|
|
70
|
+
}).to.throwException(/^Invalid socket id: 'undefined'$/)
|
|
71
|
+
expect(function () {
|
|
72
|
+
pusher.authenticateUser(null, { id: "123" })
|
|
73
|
+
}).to.throwException(/^Invalid socket id: 'null'$/)
|
|
74
|
+
expect(function () {
|
|
75
|
+
pusher.authenticateUser(111, { id: "123" })
|
|
76
|
+
}).to.throwException(/^Invalid socket id: '111'$/)
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
it("should raise an exception if socket id is an empty string", function () {
|
|
80
|
+
expect(function () {
|
|
81
|
+
pusher.authenticateUser("", { id: "123" })
|
|
82
|
+
}).to.throwException(/^Invalid socket id: ''$/)
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
it("should raise an exception if socket id is invalid", function () {
|
|
86
|
+
expect(function () {
|
|
87
|
+
pusher.authenticateUser("1.1:", { id: "123" })
|
|
88
|
+
}).to.throwException(/^Invalid socket id/)
|
|
89
|
+
expect(function () {
|
|
90
|
+
pusher.authenticateUser(":1.1", { id: "123" })
|
|
91
|
+
}).to.throwException(/^Invalid socket id/)
|
|
92
|
+
expect(function () {
|
|
93
|
+
pusher.authenticateUser(":\n1.1", { id: "123" })
|
|
94
|
+
}).to.throwException(/^Invalid socket id/)
|
|
95
|
+
expect(function () {
|
|
96
|
+
pusher.authenticateUser("1.1\n:", { id: "123" })
|
|
97
|
+
}).to.throwException(/^Invalid socket id/)
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
it("should raise an exception if user data is not a non-null object", function () {
|
|
101
|
+
expect(function () {
|
|
102
|
+
pusher.authenticateUser("111.222", undefined)
|
|
103
|
+
}).to.throwException(/^Invalid user data: 'undefined'$/)
|
|
104
|
+
expect(function () {
|
|
105
|
+
pusher.authenticateUser("111.222", null)
|
|
106
|
+
}).to.throwException(/^Invalid user data: 'null'$/)
|
|
107
|
+
expect(function () {
|
|
108
|
+
pusher.authenticateUser("111.222", 111)
|
|
109
|
+
}).to.throwException(/^Invalid user data: '111'$/)
|
|
110
|
+
expect(function () {
|
|
111
|
+
pusher.authenticateUser("111.222", "")
|
|
112
|
+
}).to.throwException(/^Invalid user data: ''$/)
|
|
113
|
+
expect(function () {
|
|
114
|
+
pusher.authenticateUser("111.222", "abc")
|
|
115
|
+
}).to.throwException(/^Invalid user data: 'abc'$/)
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
it("should raise an exception if user data doesn't have a valid id field", function () {
|
|
119
|
+
expect(function () {
|
|
120
|
+
pusher.authenticateUser("111.222", {})
|
|
121
|
+
}).to.throwException(/^Invalid user id: 'undefined'$/)
|
|
122
|
+
expect(function () {
|
|
123
|
+
pusher.authenticateUser("111.222", { id: "" })
|
|
124
|
+
}).to.throwException(/^Invalid user id: ''$/)
|
|
125
|
+
expect(function () {
|
|
126
|
+
pusher.authenticateUser("111.222", { id: 123 })
|
|
127
|
+
}).to.throwException(/^Invalid user id: '123'$/)
|
|
128
|
+
})
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
describe("#authenticate", function () {
|
|
132
|
+
it("should be the exactly the same as authorizeChannel", function () {
|
|
133
|
+
expect(pusher.authenticate).to.eql(pusher.authorizeChannel)
|
|
134
|
+
})
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
describe("#authorizeChannel", function () {
|
|
138
|
+
it("should prefix the signature with the app key", function () {
|
|
139
|
+
let pusher = new Pusher({ appId: 10000, key: "1234", secret: "tofu" })
|
|
140
|
+
expect(pusher.authorizeChannel("123.456", "test")).to.eql({
|
|
16
141
|
auth:
|
|
17
142
|
"1234:efa6cf7644a0b35cba36aa0f776f3cbf7bb60e95ea2696bde1dbe8403b61bd7c",
|
|
18
143
|
})
|
|
19
144
|
|
|
20
145
|
pusher = new Pusher({ appId: 10000, key: "abcdef", secret: "tofu" })
|
|
21
|
-
expect(pusher.
|
|
146
|
+
expect(pusher.authorizeChannel("123.456", "test")).to.eql({
|
|
22
147
|
auth:
|
|
23
148
|
"abcdef:efa6cf7644a0b35cba36aa0f776f3cbf7bb60e95ea2696bde1dbe8403b61bd7c",
|
|
24
149
|
})
|
|
25
150
|
})
|
|
26
151
|
|
|
27
152
|
it("should return correct authentication signatures for different socket ids", function () {
|
|
28
|
-
expect(pusher.
|
|
153
|
+
expect(pusher.authorizeChannel("123.456", "test")).to.eql({
|
|
29
154
|
auth:
|
|
30
155
|
"aaaa:efa6cf7644a0b35cba36aa0f776f3cbf7bb60e95ea2696bde1dbe8403b61bd7c",
|
|
31
156
|
})
|
|
32
|
-
expect(pusher.
|
|
157
|
+
expect(pusher.authorizeChannel("321.654", "test")).to.eql({
|
|
33
158
|
auth:
|
|
34
159
|
"aaaa:f6ecb0a17d3e4f68aca28f1673197a7608587c09deb0208faa4b5519aee0a777",
|
|
35
160
|
})
|
|
36
161
|
})
|
|
37
162
|
|
|
38
163
|
it("should return correct authentication signatures for different channels", function () {
|
|
39
|
-
expect(pusher.
|
|
164
|
+
expect(pusher.authorizeChannel("123.456", "test1")).to.eql({
|
|
40
165
|
auth:
|
|
41
166
|
"aaaa:d5ab857f805433cb50562da96afa41688d7742a3c3a021ed15a4d991a4d8cf94",
|
|
42
167
|
})
|
|
43
|
-
expect(pusher.
|
|
168
|
+
expect(pusher.authorizeChannel("123.456", "test2")).to.eql({
|
|
44
169
|
auth:
|
|
45
170
|
"aaaa:43affa6a09af1fb9ce1cadf176171346beaf7366673ec1e5920f68b3e97a466d",
|
|
46
171
|
})
|
|
@@ -48,12 +173,12 @@ describe("Pusher", function () {
|
|
|
48
173
|
|
|
49
174
|
it("should return correct authentication signatures for different secrets", function () {
|
|
50
175
|
let pusher = new Pusher({ appId: 10000, key: "11111", secret: "1" })
|
|
51
|
-
expect(pusher.
|
|
176
|
+
expect(pusher.authorizeChannel("123.456", "test")).to.eql({
|
|
52
177
|
auth:
|
|
53
178
|
"11111:584828bd6e80b2d177d2b28fde07b8e170abf87ccb5a791a50c933711fb8eb28",
|
|
54
179
|
})
|
|
55
180
|
pusher = new Pusher({ appId: 10000, key: "11111", secret: "2" })
|
|
56
|
-
expect(pusher.
|
|
181
|
+
expect(pusher.authorizeChannel("123.456", "test")).to.eql({
|
|
57
182
|
auth:
|
|
58
183
|
"11111:269bbf3f7625db4e0d0525b617efa5915c3ae667fd222dc8e4cb94bc531f26f2",
|
|
59
184
|
})
|
|
@@ -61,24 +186,26 @@ describe("Pusher", function () {
|
|
|
61
186
|
|
|
62
187
|
it("should return the channel data", function () {
|
|
63
188
|
expect(
|
|
64
|
-
pusher.
|
|
189
|
+
pusher.authorizeChannel("123.456", "test", { foo: "bar" }).channel_data
|
|
65
190
|
).to.eql('{"foo":"bar"}')
|
|
66
191
|
})
|
|
67
192
|
|
|
68
193
|
it("should return correct authentication signatures with and without the channel data", function () {
|
|
69
|
-
expect(pusher.
|
|
194
|
+
expect(pusher.authorizeChannel("123.456", "test")).to.eql({
|
|
70
195
|
auth:
|
|
71
196
|
"aaaa:efa6cf7644a0b35cba36aa0f776f3cbf7bb60e95ea2696bde1dbe8403b61bd7c",
|
|
72
197
|
})
|
|
73
|
-
expect(pusher.
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
198
|
+
expect(pusher.authorizeChannel("123.456", "test", { foo: "bar" })).to.eql(
|
|
199
|
+
{
|
|
200
|
+
auth:
|
|
201
|
+
"aaaa:f41faf9ead2ea76772cc6b1168363057459f02499ae4d92e88229dc7f4efa2d4",
|
|
202
|
+
channel_data: '{"foo":"bar"}',
|
|
203
|
+
}
|
|
204
|
+
)
|
|
78
205
|
})
|
|
79
206
|
|
|
80
207
|
it("should return correct authentication signature with utf-8 in channel data", function () {
|
|
81
|
-
expect(pusher.
|
|
208
|
+
expect(pusher.authorizeChannel("1.1", "test", "ą§¶™€łü€ß£")).to.eql({
|
|
82
209
|
auth:
|
|
83
210
|
"aaaa:2a229263e89d9c50524fd80c2e88be2843379f6931e28995e2cc214282c9db0c",
|
|
84
211
|
channel_data: '"ą§¶™€łü€ß£"',
|
|
@@ -87,58 +214,58 @@ describe("Pusher", function () {
|
|
|
87
214
|
|
|
88
215
|
it("should raise an exception if socket id is not a string", function () {
|
|
89
216
|
expect(function () {
|
|
90
|
-
pusher.
|
|
217
|
+
pusher.authorizeChannel(undefined, "test")
|
|
91
218
|
}).to.throwException(/^Invalid socket id: 'undefined'$/)
|
|
92
219
|
expect(function () {
|
|
93
|
-
pusher.
|
|
220
|
+
pusher.authorizeChannel(null, "test")
|
|
94
221
|
}).to.throwException(/^Invalid socket id: 'null'$/)
|
|
95
222
|
expect(function () {
|
|
96
|
-
pusher.
|
|
223
|
+
pusher.authorizeChannel(111, "test")
|
|
97
224
|
}).to.throwException(/^Invalid socket id: '111'$/)
|
|
98
225
|
})
|
|
99
226
|
|
|
100
227
|
it("should raise an exception if socket id is an empty string", function () {
|
|
101
228
|
expect(function () {
|
|
102
|
-
pusher.
|
|
229
|
+
pusher.authorizeChannel("", "test")
|
|
103
230
|
}).to.throwException(/^Invalid socket id: ''$/)
|
|
104
231
|
})
|
|
105
232
|
|
|
106
233
|
it("should raise an exception if socket id is invalid", function () {
|
|
107
234
|
expect(function () {
|
|
108
|
-
pusher.
|
|
235
|
+
pusher.authorizeChannel("1.1:", "test")
|
|
109
236
|
}).to.throwException(/^Invalid socket id/)
|
|
110
237
|
expect(function () {
|
|
111
|
-
pusher.
|
|
238
|
+
pusher.authorizeChannel(":1.1", "test")
|
|
112
239
|
}).to.throwException(/^Invalid socket id/)
|
|
113
240
|
expect(function () {
|
|
114
|
-
pusher.
|
|
241
|
+
pusher.authorizeChannel(":\n1.1", "test")
|
|
115
242
|
}).to.throwException(/^Invalid socket id/)
|
|
116
243
|
expect(function () {
|
|
117
|
-
pusher.
|
|
244
|
+
pusher.authorizeChannel("1.1\n:", "test")
|
|
118
245
|
}).to.throwException(/^Invalid socket id/)
|
|
119
246
|
})
|
|
120
247
|
|
|
121
248
|
it("should raise an exception if channel name is not a string", function () {
|
|
122
249
|
expect(function () {
|
|
123
|
-
pusher.
|
|
250
|
+
pusher.authorizeChannel("111.222", undefined)
|
|
124
251
|
}).to.throwException(/^Invalid channel name: 'undefined'$/)
|
|
125
252
|
expect(function () {
|
|
126
|
-
pusher.
|
|
253
|
+
pusher.authorizeChannel("111.222", null)
|
|
127
254
|
}).to.throwException(/^Invalid channel name: 'null'$/)
|
|
128
255
|
expect(function () {
|
|
129
|
-
pusher.
|
|
256
|
+
pusher.authorizeChannel("111.222", 111)
|
|
130
257
|
}).to.throwException(/^Invalid channel name: '111'$/)
|
|
131
258
|
})
|
|
132
259
|
|
|
133
260
|
it("should raise an exception if channel name is an empty string", function () {
|
|
134
261
|
expect(function () {
|
|
135
|
-
pusher.
|
|
262
|
+
pusher.authorizeChannel("111.222", "")
|
|
136
263
|
}).to.throwException(/^Invalid channel name: ''$/)
|
|
137
264
|
})
|
|
138
265
|
|
|
139
266
|
it("should throw an error for private-encrypted- channels", function () {
|
|
140
267
|
expect(function () {
|
|
141
|
-
pusher.
|
|
268
|
+
pusher.authorizeChannel("123.456", "private-encrypted-bla", "foo")
|
|
142
269
|
}).to.throwException(
|
|
143
270
|
"Cannot generate shared_secret because encryptionMasterKey is not set"
|
|
144
271
|
)
|
|
@@ -160,10 +287,10 @@ describe("Pusher with encryptionMasterKey", function () {
|
|
|
160
287
|
})
|
|
161
288
|
})
|
|
162
289
|
|
|
163
|
-
describe("#
|
|
290
|
+
describe("#authorizeChannel", function () {
|
|
164
291
|
it("should return a shared_secret for private-encrypted- channels", function () {
|
|
165
292
|
expect(
|
|
166
|
-
pusher.
|
|
293
|
+
pusher.authorizeChannel("123.456", "private-encrypted-bla", "foo")
|
|
167
294
|
).to.eql({
|
|
168
295
|
auth:
|
|
169
296
|
"f00d:962c48b78bf93d98ff4c92ee7dff04865821455b7b401e9d60a9e0a90af2c105",
|
|
@@ -172,7 +299,7 @@ describe("Pusher with encryptionMasterKey", function () {
|
|
|
172
299
|
})
|
|
173
300
|
})
|
|
174
301
|
it("should not return a shared_secret for non-encrypted channels", function () {
|
|
175
|
-
expect(pusher.
|
|
302
|
+
expect(pusher.authorizeChannel("123.456", "bla", "foo")).to.eql({
|
|
176
303
|
auth:
|
|
177
304
|
"f00d:013ad3da0d88e0df6ae0a8184bef50b9c3933f2344499e6e3d1ad67fad799e20",
|
|
178
305
|
channel_data: '"foo"',
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
const expect = require("expect.js")
|
|
2
|
+
const nock = require("nock")
|
|
3
|
+
|
|
4
|
+
const Pusher = require("../../../lib/pusher")
|
|
5
|
+
const sinon = require("sinon")
|
|
6
|
+
|
|
7
|
+
describe("Pusher", function () {
|
|
8
|
+
let pusher
|
|
9
|
+
|
|
10
|
+
beforeEach(function () {
|
|
11
|
+
pusher = new Pusher({ appId: 1234, key: "f00d", secret: "tofu" })
|
|
12
|
+
nock.disableNetConnect()
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
afterEach(function () {
|
|
16
|
+
nock.cleanAll()
|
|
17
|
+
nock.enableNetConnect()
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
describe("#terminateUserConnections", function () {
|
|
21
|
+
it("should throw an error if user id is empty", function () {
|
|
22
|
+
expect(function () {
|
|
23
|
+
pusher.terminateUserConnections("")
|
|
24
|
+
}).to.throwError(function (e) {
|
|
25
|
+
expect(e).to.be.an(Error)
|
|
26
|
+
expect(e.message).to.equal("Invalid user id: ''")
|
|
27
|
+
})
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it("should throw an error if user id is not a string", function () {
|
|
31
|
+
expect(function () {
|
|
32
|
+
pusher.terminateUserConnections(123)
|
|
33
|
+
}).to.throwError(function (e) {
|
|
34
|
+
expect(e).to.be.an(Error)
|
|
35
|
+
expect(e.message).to.equal("Invalid user id: '123'")
|
|
36
|
+
})
|
|
37
|
+
})
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it("should call /terminate_connections endpoint", function (done) {
|
|
41
|
+
sinon.stub(pusher, "post")
|
|
42
|
+
pusher.appId = 1234
|
|
43
|
+
const userId = "testUserId"
|
|
44
|
+
|
|
45
|
+
pusher.terminateUserConnections(userId)
|
|
46
|
+
|
|
47
|
+
expect(pusher.post.called).to.be(true)
|
|
48
|
+
expect(pusher.post.getCall(0).args[0]).eql({
|
|
49
|
+
path: `/users/${userId}/terminate_connections`,
|
|
50
|
+
body: {},
|
|
51
|
+
})
|
|
52
|
+
pusher.post.restore()
|
|
53
|
+
done()
|
|
54
|
+
})
|
|
55
|
+
})
|
|
@@ -2,8 +2,10 @@ const expect = require("expect.js")
|
|
|
2
2
|
const nock = require("nock")
|
|
3
3
|
const nacl = require("tweetnacl")
|
|
4
4
|
const naclUtil = require("tweetnacl-util")
|
|
5
|
+
const sinon = require("sinon")
|
|
5
6
|
|
|
6
7
|
const Pusher = require("../../../lib/pusher")
|
|
8
|
+
const events = require("../../../lib/events")
|
|
7
9
|
|
|
8
10
|
describe("Pusher", function () {
|
|
9
11
|
let pusher
|
|
@@ -441,6 +443,46 @@ describe("Pusher", function () {
|
|
|
441
443
|
.catch(done)
|
|
442
444
|
})
|
|
443
445
|
})
|
|
446
|
+
|
|
447
|
+
describe("#sendToUser", function () {
|
|
448
|
+
it("should trigger an event on #server-to-user-{userId}", function () {
|
|
449
|
+
sinon.stub(events, "trigger")
|
|
450
|
+
pusher.sendToUser("abc123", "halo", { foo: "bar" })
|
|
451
|
+
expect(events.trigger.called).to.be(true)
|
|
452
|
+
expect(events.trigger.getCall(0).args[1]).eql(["#server-to-user-abc123"])
|
|
453
|
+
expect(events.trigger.getCall(0).args[2]).equal("halo")
|
|
454
|
+
expect(events.trigger.getCall(0).args[3]).eql({ foo: "bar" })
|
|
455
|
+
events.trigger.restore()
|
|
456
|
+
})
|
|
457
|
+
|
|
458
|
+
it("should throw an error if user id is empty", function () {
|
|
459
|
+
expect(function () {
|
|
460
|
+
pusher.sendToUser("", "halo", { foo: "bar" })
|
|
461
|
+
}).to.throwError(function (e) {
|
|
462
|
+
expect(e).to.be.an(Error)
|
|
463
|
+
expect(e.message).to.equal("Invalid user id: ''")
|
|
464
|
+
})
|
|
465
|
+
})
|
|
466
|
+
|
|
467
|
+
it("should throw an error if user id is not a string", function () {
|
|
468
|
+
expect(function () {
|
|
469
|
+
pusher.sendToUser(123, "halo", { foo: "bar" })
|
|
470
|
+
}).to.throwError(function (e) {
|
|
471
|
+
expect(e).to.be.an(Error)
|
|
472
|
+
expect(e.message).to.equal("Invalid user id: '123'")
|
|
473
|
+
})
|
|
474
|
+
})
|
|
475
|
+
|
|
476
|
+
it("should throw an error if event name is longer than 200 characters", function () {
|
|
477
|
+
const event = new Array(202).join("x") // 201 characters
|
|
478
|
+
expect(function () {
|
|
479
|
+
pusher.sendToUser("abc123", event, { foo: "bar" })
|
|
480
|
+
}).to.throwError(function (e) {
|
|
481
|
+
expect(e).to.be.an(Error)
|
|
482
|
+
expect(e.message).to.equal("Too long event name: '" + event + "'")
|
|
483
|
+
})
|
|
484
|
+
})
|
|
485
|
+
})
|
|
444
486
|
})
|
|
445
487
|
|
|
446
488
|
describe("Pusher with encryptionMasterKey", function () {
|