Skip to content

Commit 53de835

Browse files
authored
Introduce UserAuthenticator and rename Authorizer to ChannelAuthorizer (#325)
1 parent 774a27c commit 53de835

31 files changed

+635
-304
lines changed

README.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,15 @@ PusherOptions options = new PusherOptions().setCluster(YOUR_APP_CLUSTER);
148148
Pusher pusher = new Pusher(YOUR_APP_KEY, options);
149149
```
150150

151-
If you are going to use [private](https://pusher.com/docs/channels/using_channels/private-channels) or [presence](https://pusher.com/docs/channels/using_channels/presence-channels) channels then you will need to provide an `Authorizer` to be used when authenticating subscriptions. In order to do this you need to pass in a `PusherOptions` object which has had an `Authorizer` set.
151+
If you are going to use [private](https://pusher.com/docs/channels/using_channels/private-channels) or [presence](https://pusher.com/docs/channels/using_channels/presence-channels) channels then you will need to provide an `ChannelAuthorizer` to be used when authenticating subscriptions. In order to do this you need to pass in a `PusherOptions` object which has had an `ChannelAuthorizer` set.
152152

153153
```java
154-
HttpAuthorizer authorizer = new HttpAuthorizer("http://example.com/some_auth_endpoint");
155-
PusherOptions options = new PusherOptions().setCluster(YOUR_APP_CLUSTER).setAuthorizer(authorizer);
154+
HttpChannelAuthorizer channelAuthorizer = new HttpChannelAuthorizer("http://example.com/some_auth_endpoint");
155+
PusherOptions options = new PusherOptions().setCluster(YOUR_APP_CLUSTER).setChannelAuthorizer(channelAuthorizer);
156156
Pusher pusher = new Pusher(YOUR_APP_KEY, options);
157157
```
158158

159-
See the documentation on [Authenticating Users](https://pusher.com/docs/channels/server_api/authenticating-users) for more information.
159+
See the documentation on [Authorizing Users](https://pusher.com/docs/channels/server_api/authorizing-users) for more information.
160160

161161
If you need finer control over the endpoint then the setHost, setWsPort and setWssPort methods can be employed.
162162
## Connecting
@@ -178,7 +178,7 @@ methods you can call.
178178
| Method | Parameter | Description |
179179
|-----------------------------|-------------------|-----------------------------------------------------------------------------------------------------------------------------------------------|
180180
| setEncrypted | Boolean | Sets whether the connection should be made with TLS or not. |
181-
| setAuthorizer | Pusher Authorizer | Sets the authorizer to be used when authenticating private and presence channels. |
181+
| setChannelAuthorizer | ChannelAuthorizer | Sets the channel authorizer to be used when authorizing private and presence channels. |
182182
| setHost | String | The host to which connections will be made. |
183183
| setWsPort | int | The port to which unencrypted connections will be made. Automatically set correctly. |
184184
| setWssPort | int | The port to which encrypted connections will be made. Automatically set correctly. |
@@ -260,17 +260,17 @@ Channel channel = pusher.subscribe("my-channel", new ChannelEventListener() {
260260

261261
### Private channels
262262

263-
It's possible to subscribe to [private channels](https://pusher.com/docs/channels/using_channels/private-channels) that provide a mechanism for [authenticating channel subscriptions](https://pusher.com/docs/channels/server_api/authenticating-users). In order to do this you need to provide an `Authorizer` when creating the `Pusher` instance (see **The Pusher constructor** above).
263+
It's possible to subscribe to [private channels](https://pusher.com/docs/channels/using_channels/private-channels) that provide a mechanism for [authorizing channel subscriptions](https://pusher.com/docs/channels/server_api/authorizing-users). In order to do this you need to provide a `ChannelAuthorizer` when creating the `Pusher` instance (see **The Pusher constructor** above).
264264

265-
The library provides a `HttpAuthorizer` implementation of `Authorizer` which makes an HTTP `POST` request to an authenticating endpoint. However, you can implement your own authentication mechanism if required.
265+
The library provides a `HttpChannelAuthorizer` implementation of `ChannelAuthorizer` which makes an HTTP `POST` request to an authorization endpoint. However, you can implement your own authorization mechanism if required.
266266

267267
Private channels are subscribed to as follows:
268268

269269
```java
270270
PrivateChannel privateChannel = pusher.subscribePrivate( "private-channel" );
271271
```
272272

273-
In addition to the events that are possible on public channels a private channel exposes an `onAuthenticationFailure`. This is called if the `Authorizer` does not successfully authenticate the subscription:
273+
In addition to the events that are possible on public channels a private channel exposes an `onAuthenticationFailure`. This is called if the `ChannelAuthorizer` does not successfully authorize the subscription:
274274

275275
```java
276276
PrivateChannel channel = pusher.subscribePrivate("private-channel",
@@ -292,7 +292,7 @@ Similar to Private channels, you can also subscribe to a
292292
[private encrypted channel](https://pusher.com/docs/channels/using_channels/encrypted-channels).
293293
This library now fully supports end-to-end encryption. This means that only you and your connected clients will be able to read your messages. Pusher cannot decrypt them.
294294

295-
Like the private channel, you must provide your own authentication endpoint,
295+
Like the private channel, you must provide your own authorization endpoint,
296296
with your own encryption master key. There is a
297297
[demonstration endpoint to look at using nodejs](https://github.com/pusher/pusher-channels-auth-example#using-e2e-encryption).
298298

@@ -307,10 +307,10 @@ PrivateEncryptedChannel privateEncryptedChannel =
307307
In addition to the events that are possible on public channels the
308308
`PrivateEncryptedChannelEventListener` also has the following methods:
309309
* `onAuthenticationFailure(String message, Exception e)` - This is called if
310-
the `Authorizer` does not successfully authenticate the subscription:
310+
the `ChannelAuthorizer` does not successfully authorize the subscription:
311311
* `onDecryptionFailure(String event, String reason);` - This is called if the message cannot be
312312
decrypted. The decryption will attempt to refresh the shared secret key once
313-
from the `Authorizer`.
313+
from the `ChannelAuthorizer`.
314314

315315
There is a
316316
[working example in the repo](https://github.com/pusher/pusher-websocket-java/blob/master/src/main/java/com/pusher/client/example/PrivateEncryptedChannelExampleApp.java)
@@ -319,7 +319,7 @@ which you can use with the
319319

320320
### Presence channels
321321

322-
[Presence channels](https://pusher.com/docs/channels/using_channels/presence-channels) are private channels which provide additional events exposing who is currently subscribed to the channel. Since they extend private channels they also need to be authenticated (see [authenticating channel subscriptions](https://pusher.com/docs/channels/server_api/authenticating-users)).
322+
[Presence channels](https://pusher.com/docs/channels/using_channels/presence-channels) are private channels which provide additional events exposing who is currently subscribed to the channel. Since they extend private channels they also need to be authorized (see [authorizing channel subscriptions](https://pusher.com/docs/channels/server_api/authorizing-users)).
323323

324324
Presence channels can be subscribed to as follows:
325325

@@ -385,11 +385,11 @@ Gson gson = new Gson();
385385
UserInfo info = gson.fromJson(jsonInfo, UserInfo.class);
386386
```
387387

388-
For more information on defining the user id and user info on the server see [Implementing the auth endpoint for a presence channel](https://pusher.com/docs/channels/server_api/authenticating-users#implementing-the-auth-endpoint-for-a-presence-channel) documentation.
388+
For more information on defining the user id and user info on the server see [Implementing the authorization endpoint for a presence channel](https://pusher.com/docs/channels/server_api/authorizing-users#implementing-the-authorization-endpoint-for-a-presence-channel) documentation.
389389

390390
#### Client event authenticity
391391

392-
Channels now provides a 'user-id' with client events sent from the server. With presence channels, your authentication endpoint provides your user with a user-id. Previously, it was up to you to include this user-id in every client-event triggered by your clients. Now, when a client of yours triggers a client event, Channels will append their user-id to their triggered message, so that the other clients in the channel receive it. This allows you to trust that a given user really did trigger a given payload.
392+
Channels now provides a 'user-id' with client events sent from the server. With presence channels, your authorization endpoint provides your user with a user-id. Previously, it was up to you to include this user-id in every client-event triggered by your clients. Now, when a client of yours triggers a client event, Channels will append their user-id to their triggered message, so that the other clients in the channel receive it. This allows you to trust that a given user really did trigger a given payload.
393393

394394
If you’d like to make use of this feature, you’ll need to extract the user-id from the message delivered by Channels. To do this, call getUserId() on the event payload your event handler gets called with, like so:
395395

@@ -503,7 +503,7 @@ public class Example implements ChannelEventListener {
503503

504504
## Triggering events
505505

506-
Once a [private](https://pusher.com/docs/channels/using_channels/private-channels) or [presence](https://pusher.com/docs/channels/using_channels/presence-channels) subscription has been authorized (see [authenticating users](https://pusher.com/docs/channels/server_api/authenticating-users)) and the subscription has succeeded, it is possible to trigger events on those channels.
506+
Once a [private](https://pusher.com/docs/channels/using_channels/private-channels) or [presence](https://pusher.com/docs/channels/using_channels/presence-channels) subscription has been authorized (see [authorizing users](https://pusher.com/docs/channels/server_api/authorizing-users)) and the subscription has succeeded, it is possible to trigger events on those channels.
507507

508508
```java
509509
channel.trigger("client-myEvent", "{\"myName\":\"Bob\"}");
@@ -539,7 +539,7 @@ You can access the value **once the connection has been established** as follows
539539
String socketId = pusher.getConnection().getSocketId();
540540
```
541541

542-
For more information on how and why there is a `socket_id` see the documentation on [authenticating users](ttps://pusher.com/docs/channels/server_api/authenticating-users) and [excluding recipients](https://pusher.com/docs/channels/server_api/excluding-event-recipients).
542+
For more information on how and why there is a `socket_id` see the documentation on [authorizing users](ttps://pusher.com/docs/channels/server_api/authorizing-users) and [excluding recipients](https://pusher.com/docs/channels/server_api/excluding-event-recipients).
543543

544544
## Helper Methods
545545

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.pusher.client;
2+
3+
/**
4+
* Used to indicate an authentication failure.
5+
*
6+
* @see com.pusher.client.UserAuthenticator
7+
*/
8+
public class AuthenticationFailureException extends RuntimeException {
9+
10+
private static final long serialVersionUID = -7208133561904200801L;
11+
12+
public AuthenticationFailureException() {
13+
super();
14+
}
15+
16+
public AuthenticationFailureException(final String msg) {
17+
super(msg);
18+
}
19+
20+
public AuthenticationFailureException(final Exception cause) {
21+
super(cause);
22+
}
23+
24+
public AuthenticationFailureException(final String msg, final Exception cause) {
25+
super(msg, cause);
26+
}
27+
}

src/main/java/com/pusher/client/AuthorizationFailureException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* Used to indicate an authorization failure.
55
*
6-
* @see com.pusher.client.Authorizer
6+
* @see com.pusher.client.ChannelAuthorizer
77
*/
88
public class AuthorizationFailureException extends RuntimeException {
99

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,9 @@
11
package com.pusher.client;
22

3+
34
/**
4-
* Subscriptions to {@link com.pusher.client.channel.PrivateChannel Private} and
5-
* {@link com.pusher.client.channel.PresenceChannel presence} channels need to
6-
* be authorized. This interface provides an {@link #authorize} as a mechanism
7-
* for doing this.
8-
*
9-
* <p>
10-
* See the {@link com.pusher.client.util.HttpAuthorizer HttpAuthorizer} as an
11-
* example.
12-
* </p>
5+
* @deprecated
6+
* Please use {@link com.pusher.client.ChannelAuthorizer}
137
*/
14-
public interface Authorizer {
15-
16-
/**
17-
* Called when a channel is to be authenticated.
18-
*
19-
* @param channelName
20-
* The name of the channel to be authenticated.
21-
* @param socketId
22-
* A unique socket connection ID to be used with the
23-
* authentication. This uniquely identifies the connection that
24-
* the subscription is being authenticated for.
25-
* @return An authentication token.
26-
* @throws AuthorizationFailureException
27-
* if the authentication fails.
28-
*/
29-
String authorize(String channelName, String socketId) throws AuthorizationFailureException;
30-
}
8+
@Deprecated
9+
public interface Authorizer extends ChannelAuthorizer {}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.pusher.client;
2+
3+
/**
4+
* Subscriptions to {@link com.pusher.client.channel.PrivateChannel Private} and
5+
* {@link com.pusher.client.channel.PresenceChannel presence} channels need to
6+
* be authorized. This interface provides an {@link #authorize} method as a mechanism
7+
* for doing this.
8+
*
9+
* <p>
10+
* See the {@link com.pusher.client.util.HttpChannelAuthorizer HttpChannelAuthorizer} as an
11+
* example.
12+
* </p>
13+
*/
14+
public interface ChannelAuthorizer {
15+
16+
/**
17+
* Called when a channel subscription is to be authorized.
18+
*
19+
* @param channelName
20+
* The name of the channel to be authorized.
21+
* @param socketId
22+
* A unique socket connection ID to be used with the
23+
* authorization. This uniquely identifies the connection that
24+
* the subscription is being authorized for.
25+
* @return A channel authorization token.
26+
* @throws AuthorizationFailureException
27+
* if the authorization fails.
28+
*/
29+
String authorize(String channelName, String socketId) throws AuthorizationFailureException;
30+
}

0 commit comments

Comments
 (0)