Skip to content

Commit 7c4c9d5

Browse files
Send to user and user authentication v5.1.0 (#84)
* Bump to version 5.1.0 Co-authored-by: Pusher CI <[email protected]>
1 parent c3c7d91 commit 7c4c9d5

File tree

6 files changed

+394
-54
lines changed

6 files changed

+394
-54
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Changelog
22

3+
## 5.1.0
4+
5+
* [ADDED] SendToUser method
6+
* [ADDED] AuthenticateUser method
7+
* [ADDED] AuthorizePrivateChannel method
8+
* [ADDED] AuthorizePresenceChannel method
9+
* [CHANGED] AuthenticatePrivateChannel method deprecated
10+
* [CHANGED] AuthenticatePresenceChannel method deprecated
11+
312
## 5.0.0 / 2021-02-19
413

514
* Breaking change: `TriggerBatch` now returns `(*TriggerBatchChannelsList, error)` instead of `error`

README.md

Lines changed: 72 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
The Golang library for interacting with the Pusher Channels HTTP API.
66

7-
This package lets you trigger events to your client and query the state of your Pusher channels. When used with a server, you can validate Pusher Channels webhooks and authenticate `private-` or `presence-` channels.
7+
This package lets you trigger events to your client and query the state of your Pusher channels. When used with a server, you can validate Pusher Channels webhooks and authorize `private-` or `presence-` channels.
88

99
Register for free at <https://pusher.com/channels> and use the application credentials within your app as shown below.
1010

@@ -21,7 +21,8 @@ Register for free at <https://pusher.com/channels> and use the application crede
2121
- [Google App Engine](#google-app-engine)
2222
- [Usage](#usage)
2323
- [Triggering events](#triggering-events)
24-
- [Authenticating Channels](#authenticating-channels)
24+
- [Authenticating Users](#authenticating-users)
25+
- [Authorizing Channels](#authorizing-channels)
2526
- [Application state](#application-state)
2627
- [Webhook validation](#webhook-validation)
2728
- [Feature Support](#feature-support)
@@ -146,7 +147,7 @@ pusherClient.Cluster = "eu" // in this case requests will be made to api-eu.push
146147

147148
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:
148149

149-
1. You should first set up Private channels. This involves [creating an authentication endpoint on your server](https://pusher.com/docs/authenticating_users).
150+
1. You should first set up Private channels. This involves [creating an authorization endpoint on your server](https://pusher.com/docs/authorizing_users).
150151

151152
2. Next, generate a 32 byte master encryption key, base64 encode it and store
152153
it securely.
@@ -371,35 +372,88 @@ for i, attributes := range response.Batch {
371372
// channel: presence-b-channel, name: event, user_count: 4
372373
```
373374

374-
### Authenticating Channels
375+
#### Send to user
375376

376-
Application security is very important so Pusher Channels provides a mechanism for authenticating a user’s access to a channel at the point of subscription.
377+
##### `func (c *Client) SendToUser`
377378

378-
This can be used both to restrict access to private channels, and in the case of presence channels notify subscribers of who else is also subscribed via presence events.
379+
| Argument |Description |
380+
| :-: | :-: |
381+
| userId `string` | The id of the user who should receive the event. |
382+
| event `string` | The name of the event you wish to trigger. |
383+
| data `interface{}` | The payload you wish to send. Must be marshallable into JSON. |
384+
385+
###### Example
386+
387+
```go
388+
data := map[string]string{"hello": "world"}
389+
pusherClient.SendToUser("user123", "say_hello", data)
390+
```
391+
392+
### Authenticating Users
379393

380-
This library provides a mechanism for generating an authentication signature to send back to the client and authorize them.
394+
Pusher Channels provides a mechanism for authenticating users. This can be used to send messages to specific users based on user id and to terminate misbehaving user connections, for example.
381395

382396
For more information see our [docs](http://pusher.com/docs/authenticating_users).
383397

398+
#### `func (c *Client) AuthenticateUser`
399+
400+
| Argument | Description |
401+
| :-: | :-: |
402+
| params `[]byte` | The request body sent by the client |
403+
404+
| Return Value | Description |
405+
| :-: | :-: |
406+
| response `[]byte` | The response to send back to the client, carrying an authentication signature |
407+
| err `error` | Any errors generated |
408+
409+
###### Example
410+
411+
```go
412+
func pusherUserAuth(res http.ResponseWriter, req *http.Request) {
413+
params, _ := ioutil.ReadAll(req.Body)
414+
response, err := pusherClient.AuthenticateUser(params)
415+
if err != nil {
416+
panic(err)
417+
}
418+
419+
fmt.Fprintf(res, string(response))
420+
}
421+
422+
func main() {
423+
http.HandleFunc("/pusher/user-auth", pusherUserAuth)
424+
http.ListenAndServe(":5000", nil)
425+
}
426+
```
427+
428+
### Authorizing Channels
429+
430+
Application security is very important so Pusher Channels provides a mechanism for authorizing a user’s access to a channel at the point of subscription.
431+
432+
This can be used both to restrict access to private channels, and in the case of presence channels notify subscribers of who else is also subscribed via presence events.
433+
434+
This library provides a mechanism for generating an authorization signature to send back to the client and authorize them.
435+
436+
For more information see our [docs](http://pusher.com/docs/authorizing_users).
437+
384438
#### Private channels
385439

386-
##### `func (c *Client) AuthenticatePrivateChannel`
440+
##### `func (c *Client) AuthorizePrivateChannel`
387441

388442
| Argument | Description |
389443
| :-: | :-: |
390444
| params `[]byte` | The request body sent by the client |
391445

392446
| Return Value | Description |
393447
| :-: | :-: |
394-
| response `[]byte` | The response to send back to the client, carrying an authentication signature |
448+
| response `[]byte` | The response to send back to the client, carrying an authorization signature |
395449
| err `error` | Any errors generated |
396450

397451
###### Example
398452

399453
```go
400454
func pusherAuth(res http.ResponseWriter, req *http.Request) {
401455
params, _ := ioutil.ReadAll(req.Body)
402-
response, err := pusherClient.AuthenticatePrivateChannel(params)
456+
response, err := pusherClient.AuthorizePrivateChannel(params)
403457
if err != nil {
404458
panic(err)
405459
}
@@ -431,7 +485,7 @@ func pusherJsonpAuth(res http.ResponseWriter, req *http.Request) {
431485
params = []byte(q.Encode())
432486
}
433487

434-
response, err := pusherClient.AuthenticatePrivateChannel(params)
488+
response, err := pusherClient.AuthorizePrivateChannel(params)
435489
if err != nil {
436490
panic(err)
437491
}
@@ -446,11 +500,11 @@ func main() {
446500
}
447501
```
448502

449-
#### Authenticating presence channels
503+
#### Authorizing presence channels
450504

451505
Using presence channels is similar to private channels, but in order to identify a user, clients are sent a user_id and, optionally, custom data.
452506

453-
##### `func (c *Client) AuthenticatePresenceChannel`
507+
##### `func (c *Client) AuthorizePresenceChannel`
454508

455509
| Argument | Description |
456510
| :-: | :-: |
@@ -480,7 +534,7 @@ presenceData := pusher.MemberData{
480534
},
481535
}
482536

483-
response, err := pusherClient.AuthenticatePresenceChannel(params, presenceData)
537+
response, err := pusherClient.AuthorizePresenceChannel(params, presenceData)
484538

485539
if err != nil {
486540
panic(err)
@@ -692,8 +746,10 @@ Trigger event on multiple channels | *&#10004;*
692746
Trigger events in batches | *&#10004;*
693747
Excluding recipients from events | *&#10004;*
694748
Fetching info on trigger | *&#10004;*
695-
Authenticating private channels | *&#10004;*
696-
Authenticating presence channels | *&#10004;*
749+
Send to user | *&#10004;*
750+
Authenticating users | *&#10004;*
751+
Authorizing private channels | *&#10004;*
752+
Authorizing presence channels | *&#10004;*
697753
Get the list of channels in an application | *&#10004;*
698754
Get the state of a single channel | *&#10004;*
699755
Get a list of users in a presence channel | *&#10004;*

0 commit comments

Comments
 (0)