pusher 5.0.0 → 5.1.1-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/.github/stale.yml CHANGED
@@ -1,7 +1,7 @@
1
1
  # Configuration for probot-stale - https://github.com/probot/stale
2
2
 
3
3
  # Number of days of inactivity before an Issue or Pull Request becomes stale
4
- daysUntilStale: 365
4
+ daysUntilStale: 90
5
5
 
6
6
  # Number of days of inactivity before an Issue or Pull Request with the stale label is closed.
7
7
  # Set to false to disable. If disabled, issues still need to be closed manually, but will remain marked as stale.
@@ -23,4 +23,4 @@ markComment: >
23
23
  This issue has been automatically marked as stale because it has not had
24
24
  recent activity. It will be closed if no further activity occurs. If you'd
25
25
  like this issue to stay open please leave a comment indicating how this issue
26
- is affecting you. Thankyou.
26
+ is affecting you. Thank you.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,23 @@
1
+ ## 5.1.1-beta (2022-06-01)
2
+
3
+ [FIXED] Updated typescript types with new user features.
4
+
5
+ ## 5.1.0-beta (2022-04-22)
6
+
7
+ [ADDED] Support for terminating user connections based on user id
8
+ [ADDED] Support for sending messages to users based on user id
9
+ [ADDED] Support for implementing user authentication endpoints
10
+ [DEPRECATED] authenticate function is deprecated. The same functionality (and interface) is now provided by authorizeChannel
11
+
12
+ ## 5.0.1 (2022-01-24)
13
+
14
+ [FIXED] Incorrect `require` on `version.js` was causing a compilation error in Webpack
15
+ [FIXED] Inconsistent encoding for shared secret between other SDKs
16
+
1
17
  ## 5.0.0 (2021-02-18)
2
18
 
3
19
  [BREAKING CHANGE] `trigger` now accepts a `params` _object_ instead of a `socket_id` as the third parameter.
20
+
4
21
  [ADDED] Support for requesting channel attributes as part of a `trigger` and `triggerBatch` request via an `info` parameter.
5
22
 
6
23
  ## 4.0.2 (2020-11-30)
@@ -51,6 +68,7 @@ const pusher = new Pusher.forURL(process.env.PUSHER_URL, {
51
68
  [UPGRADED] development dependencies
52
69
 
53
70
  [ADDED] encryptionMasterKeyBase64 constructor parameter to make it easier to use the full range of 32 byte binary values in encryption key
71
+
54
72
  [DEPRECATED] encryptionMasterKey constructor parameter - use encryptionMasterKeyBase64
55
73
 
56
74
  ## 3.0.0 (2019-09-26)
package/README.md CHANGED
@@ -7,9 +7,9 @@ In order to use this library, you need to have an account on <https://pusher.com
7
7
 
8
8
  ## Supported platforms
9
9
 
10
- This SDK supports **Node.js** version 8+.
10
+ This SDK supports **Node.js** version 10+.
11
11
 
12
- We test the library against a selection of Node.js versions which we update over time. Please refer to [travis.yml](https://github.com/pusher/pusher-http-node/blob/master/.travis.yml) for the set of versions that are currently tested with CI.
12
+ We test the library against a selection of Node.js versions which we update over time. Please refer to [test.yml](https://github.com/pusher/pusher-http-node/blob/master/.github/workflows/test.yml) for the set of versions that are currently tested with CI.
13
13
 
14
14
  If you find any compatibility issues, please [raise an issue](https://github.com/pusher/pusher-http-node/issues/new) in the repository or contact support at [support@pusher.com](support@pusher.com). We will happily investigate reported problems ❤️.
15
15
 
@@ -237,11 +237,11 @@ pusher
237
237
  })
238
238
  ```
239
239
 
240
- ### End-to-end encryption [BETA]
240
+ ### End-to-end encryption
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 authentication endpoint on your server](https://pusher.com/docs/authenticating_users).
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 private channels
281
+ ### Authenticating users
282
282
 
283
- To authorise your users to access private channels on Pusher Channels, you can use the `authenticate` function:
283
+ To authenticate users during sign in, you can use the `authenticateUser` function:
284
284
 
285
285
  ```javascript
286
- const auth = pusher.authenticate(socketId, channel)
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
- ### Authenticating presence channels
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.authenticate(socketId, channel, channelData);
330
+ const auth = pusher.authorizeChannel(socketId, channel, channelData);
304
331
  ```
305
332
 
306
333
  The `auth` is then returned to the caller as JSON.
@@ -4,8 +4,7 @@ import * as Pusher from "pusher"
4
4
 
5
5
  const pusher = Pusher.forURL(process.env.PUSHER_URL, {
6
6
  encryptionMasterKeyBase64: Buffer.from(
7
- "01234567890123456789012345678901",
8
- "binary"
7
+ "01234567890123456789012345678901"
9
8
  ).toString("base64"),
10
9
  agent: new Agent({ keepAlive: true }),
11
10
  })
package/index.d.ts CHANGED
@@ -24,11 +24,29 @@ declare class Pusher {
24
24
  get(opts: Pusher.GetOptions): Promise<Response>
25
25
  post(opts: Pusher.PostOptions): Promise<Response>
26
26
 
27
+ /**
28
+ * @deprecated Use authorizeChannel
29
+ */
27
30
  authenticate(
28
31
  socketId: string,
29
32
  channel: string,
30
33
  data?: Pusher.PresenceChannelData
31
- ): Pusher.AuthResponse
34
+ ): Pusher.ChannelAuthResponse
35
+
36
+ authorizeChannel(
37
+ socketId: string,
38
+ channel: string,
39
+ data?: Pusher.PresenceChannelData
40
+ ): Pusher.ChannelAuthResponse
41
+
42
+ authenticateUser(
43
+ socketId: string,
44
+ userData: Pusher.UserChannelData
45
+ ): Pusher.UserAuthResponse
46
+
47
+ sendToUser(userId: string, event: string, data: any): Promise<Response>
48
+
49
+ terminateUserConnections(userId: string): Promise<Response>
32
50
 
33
51
  webhook(request: Pusher.WebHookRequest): Pusher.WebHook
34
52
  createSignedQueryString(opts: Pusher.SignedQueryStringOptions): string
@@ -106,12 +124,26 @@ declare namespace Pusher {
106
124
  params?: Params
107
125
  }
108
126
 
127
+ /**
128
+ * @deprecated Use ChannelAuthResponse
129
+ */
109
130
  export interface AuthResponse {
110
131
  auth: string
111
132
  channel_data?: string
112
133
  shared_secret?: string
113
134
  }
114
135
 
136
+ export interface ChannelAuthResponse {
137
+ auth: string
138
+ channel_data?: string
139
+ shared_secret?: string
140
+ }
141
+
142
+ export interface UserAuthResponse {
143
+ auth: string
144
+ user_data: string
145
+ }
146
+
115
147
  export interface PresenceChannelData {
116
148
  user_id: string
117
149
  user_info?: {
@@ -119,6 +151,11 @@ declare namespace Pusher {
119
151
  }
120
152
  }
121
153
 
154
+ export interface UserChannelData {
155
+ id: string
156
+ [key: string]: any
157
+ }
158
+
122
159
  export interface WebHookRequest {
123
160
  headers: object
124
161
  rawBody: string
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/config.js CHANGED
@@ -49,7 +49,7 @@ function Config(options) {
49
49
  )
50
50
  }
51
51
 
52
- this.encryptionMasterKey = options.encryptionMasterKey
52
+ this.encryptionMasterKey = Buffer.from(options.encryptionMasterKey)
53
53
  }
54
54
 
55
55
  // Handle base64 encoded 32 byte key to encourage use of the full range of byte values
@@ -61,10 +61,7 @@ function Config(options) {
61
61
  throw new Error("encryptionMasterKeyBase64 must be valid base64")
62
62
  }
63
63
 
64
- const decodedKey = Buffer.from(
65
- options.encryptionMasterKeyBase64,
66
- "base64"
67
- ).toString("binary")
64
+ const decodedKey = Buffer.from(options.encryptionMasterKeyBase64, "base64")
68
65
  if (decodedKey.length !== 32) {
69
66
  throw new Error(
70
67
  "encryptionMasterKeyBase64 must decode to 32 bytes, but the string " +
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} authentication signature
119
+ * @returns {String} authorization signature
107
120
  */
108
- Pusher.prototype.authenticate = function (socketId, channel, data) {
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 '-'
@@ -234,7 +301,9 @@ Pusher.prototype.createSignedQueryString = function (options) {
234
301
  Pusher.prototype.channelSharedSecret = function (channel) {
235
302
  return crypto
236
303
  .createHash("sha256")
237
- .update(channel + this.config.encryptionMasterKey)
304
+ .update(
305
+ Buffer.concat([Buffer.from(channel), this.config.encryptionMasterKey])
306
+ )
238
307
  .digest()
239
308
  }
240
309
 
package/lib/version.js CHANGED
@@ -1 +1 @@
1
- module.exports = require("../package").version
1
+ module.exports = require("../package.json").version
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "pusher",
3
3
  "description": "Node.js client to interact with the Pusher Channels REST API",
4
- "version": "5.0.0",
4
+ "version": "5.1.1-beta",
5
5
  "author": "Pusher <support@pusher.com>",
6
6
  "contributors": [
7
7
  {
@@ -54,7 +54,7 @@
54
54
  "realtime"
55
55
  ],
56
56
  "license": "MIT",
57
- "repository": "git://github.com/pusher/pusher-rest-node",
57
+ "repository": "git://github.com/pusher/pusher-http-node",
58
58
  "main": "lib/pusher",
59
59
  "typings": "index.d.ts",
60
60
  "engines": {