Skip to content

Commit b2f077b

Browse files
authored
Merge pull request #20 from pusher/custom-authorizer
add custom authorizer example
2 parents a0a70c3 + 669b4dc commit b2f077b

File tree

7 files changed

+55
-14
lines changed

7 files changed

+55
-14
lines changed

CHANGELOG.md

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

3+
## 1.0.2
4+
5+
* [CHANGED] Use latest pusher websocket java sdk.
6+
* [ADDED] Example to use a custom authorizer.
7+
38
## 1.0.1
49

510
* [ADDED] Add onAuthorizer support to iOS

android/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ buildscript {
55
repositories {
66
google()
77
mavenCentral()
8-
jcenter()
98
}
109

1110
dependencies {
12-
classpath 'com.android.tools.build:gradle:7.1.3'
11+
classpath 'com.android.tools.build:gradle:7.2.0'
1312
// noinspection DifferentKotlinGradleVersion
1413
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1514
}
@@ -124,5 +123,6 @@ dependencies {
124123
// noinspection GradleDynamicVersion
125124
api 'com.facebook.react:react-native:+'
126125
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
127-
api 'com.pusher:pusher-java-client:2.2.8'
126+
api 'com.pusher:pusher-java-client:2.+'
127+
api 'com.google.code.gson:gson:2.+'
128128
}

android/src/main/java/com/pusherwebsocketreactnative/PusherWebsocketReactNativeModule.kt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,23 @@ import android.util.Log
44
import com.facebook.react.bridge.*
55
import com.facebook.react.modules.core.DeviceEventManagerModule
66
import com.google.gson.Gson
7-
import com.pusher.client.Authorizer
7+
import com.pusher.client.ChannelAuthorizer
88
import com.pusher.client.Pusher
99
import com.pusher.client.PusherOptions
1010
import com.pusher.client.channel.*
1111
import com.pusher.client.connection.ConnectionEventListener
1212
import com.pusher.client.connection.ConnectionState
1313
import com.pusher.client.connection.ConnectionStateChange
14-
import com.pusher.client.util.HttpAuthorizer
14+
import com.pusher.client.util.HttpChannelAuthorizer
1515
import java.net.InetSocketAddress
1616
import java.net.Proxy
1717
import java.util.concurrent.Semaphore
1818

19-
2019
class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
2120
ReactContextBaseJavaModule(reactContext),
2221
ConnectionEventListener, ChannelEventListener, SubscriptionEventListener,
2322
PrivateChannelEventListener, PrivateEncryptedChannelEventListener, PresenceChannelEventListener,
24-
Authorizer {
23+
ChannelAuthorizer {
2524

2625
private var pusher: Pusher? = null
2726
private val TAG = "PusherReactNative"
@@ -73,9 +72,10 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
7372
arguments.getInt("maxReconnectionAttempts")
7473
if (arguments.hasKey("maxReconnectGapInSeconds")) options.maxReconnectGapInSeconds =
7574
arguments.getInt("maxReconnectGapInSeconds")
76-
if (arguments.hasKey("authEndpoint")) options.authorizer =
77-
HttpAuthorizer(arguments.getString("authEndpoint"))
78-
if (arguments.hasKey("authorizer") && arguments.getBoolean("authorizer")) options.authorizer = this
75+
if (arguments.hasKey("authEndpoint")) options.channelAuthorizer =
76+
HttpChannelAuthorizer(arguments.getString("authEndpoint"))
77+
if (arguments.hasKey("authorizer") && arguments.getBoolean("authorizer")) options.channelAuthorizer =
78+
this
7979
if (arguments.hasKey("proxy")) {
8080
val (host, port) = arguments.getString("proxy")!!.split(':')
8181
options.proxy = Proxy(Proxy.Type.HTTP, InetSocketAddress(host, port.toInt()))
@@ -130,7 +130,8 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
130130
try {
131131
when {
132132
channelName.startsWith("private-encrypted-") -> throw Exception("It's not currently possible to send a message using private encrypted channels.")
133-
channelName.startsWith("private-") -> pusher!!.getPrivateChannel(channelName).trigger(eventName, data)
133+
channelName.startsWith("private-") -> pusher!!.getPrivateChannel(channelName)
134+
.trigger(eventName, data)
134135
channelName.startsWith("presence-") -> pusher!!.getPresenceChannel(channelName)
135136
.trigger(eventName, data)
136137
else -> throw Exception("Messages can only be sent to private and presence channels.")

example/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
},
1111
"dependencies": {
1212
"@react-native-async-storage/async-storage": "^1.17.5",
13+
"crypto-es": "^1.2.7",
1314
"react": "17.0.2",
1415
"react-native": "0.68.2"
1516
},

example/src/App.tsx

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
FlatList,
1212
} from 'react-native';
1313
import AsyncStorage from '@react-native-async-storage/async-storage';
14+
import CryptoES from 'crypto-es';
1415
import { Pusher, PusherMember, PusherChannel, PusherEvent } from '../../src'; // This links the example app to the current SDK implementation
1516

1617
export default function App() {
@@ -22,7 +23,7 @@ export default function App() {
2223
const [channelName, onChangeChannelName] = React.useState('');
2324
const [eventName, onChangeEventName] = React.useState('');
2425
const [eventData, onChangeEventData] = React.useState('');
25-
const [members, onChangeMembers] = React.useState(new Array<PusherMember>());
26+
const [members, onChangeMembers] = React.useState<PusherMember[]>([]);
2627
const [logText, setLog] = React.useState('');
2728

2829
const log = async (line: string) => {
@@ -54,7 +55,21 @@ export default function App() {
5455
await pusher.init({
5556
apiKey,
5657
cluster,
57-
// authEndpoint: '<Add your Auth Endpoint here>',
58+
// authEndpoint
59+
// ============
60+
// You can let the pusher library call an endpoint URL,
61+
// Please look here to implement a server side authorizer:
62+
// https://pusher.com/docs/channels/server_api/authenticating-users/
63+
//
64+
// authEndpoint: '<Add your Auth Endpoint URL here>',
65+
//
66+
// onAuthorizer
67+
// ============
68+
// Or you can implement your own authorizer callback.
69+
// See https://pusher.com/docs/channels/library_auth_reference/auth-signatures/
70+
// for the format of this object, you need your key and secret from your Pusher
71+
// Account.
72+
onAuthorizer,
5873
onConnectionStateChange,
5974
onError,
6075
onEvent,
@@ -123,6 +138,20 @@ export default function App() {
123138
onChangeMembers([...channel.members.values()]);
124139
};
125140

141+
// See https://pusher.com/docs/channels/library_auth_reference/auth-signatures/ for the format of this object.
142+
const onAuthorizer = (channelName: string, socketId: string) => {
143+
const user = JSON.stringify({ user_id: 12345 });
144+
const stringToSign = socketId + ':' + channelName + ':' + user;
145+
const pusherKey = '<YOUR PUSHER KEY>';
146+
const pusherSecret = '<YOUR PUSHER SECRET>';
147+
const signature = CryptoES.HmacSHA256(stringToSign, pusherSecret);
148+
return {
149+
auth: pusherKey + ':' + signature,
150+
channel_data: user,
151+
shared_secret: 'foobar',
152+
};
153+
};
154+
126155
const trigger = async () => {
127156
try {
128157
await AsyncStorage.multiSet([

example/yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,6 +1693,11 @@ cross-spawn@^6.0.0:
16931693
shebang-command "^1.2.0"
16941694
which "^1.2.9"
16951695

1696+
crypto-es@^1.2.7:
1697+
version "1.2.7"
1698+
resolved "https://registry.yarnpkg.com/crypto-es/-/crypto-es-1.2.7.tgz#754a6d52319a94fb4eb1f119297f17196b360f88"
1699+
integrity sha512-UUqiVJ2gUuZFmbFsKmud3uuLcNP2+Opt+5ysmljycFCyhA0+T16XJmo1ev/t5kMChMqWh7IEvURNCqsg+SjZGQ==
1700+
16961701
dayjs@^1.8.15:
16971702
version "1.11.3"
16981703
resolved "https://registry.yarnpkg.com/dayjs/-/dayjs-1.11.3.tgz#4754eb694a624057b9ad2224b67b15d552589258"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pusher/pusher-websocket-react-native",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "Pusher Channels Client for React Native",
55
"main": "lib/commonjs/index",
66
"module": "lib/module/index",

0 commit comments

Comments
 (0)