Skip to content
This repository was archived by the owner on Jul 28, 2022. It is now read-only.

Commit 8378cda

Browse files
committed
Don't compute auth key on the client (closes #1)
1 parent 7330714 commit 8378cda

File tree

3 files changed

+4
-37
lines changed

3 files changed

+4
-37
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ defaultConfig {
3939
...
4040
resValue "string", "GCM_SENDER_ID", "%YOUR_GCM_SENDER_ID%"
4141
resValue "string", "BATCH_API_KEY", "%YOUR_BATCH_API_KEY%"
42-
resValue "string", "BATCH_INBOX_SECRET", "%YOUR_BATCH_INBOX_SECRET%" // only if you want to use Batch's optional Inbox feature
4342
}
4443
```
4544

@@ -75,7 +74,7 @@ Only on Android at the moment. See the [Batch's docs](https://batch.com/doc/ios/
7574
```js
7675
import BatchPush from 'react-native-batch-push';
7776

78-
BatchPush.fetchNewNotifications('theUserId')
77+
Batch.fetchNewNotifications('theUserId', 'authKey')
7978
.then(notifications => {
8079
// notifications is Array<{ title: string, body: string, timestamp: number, payload: Object }>
8180
})

android/src/main/java/tech/bam/RNBatchPush/RNBatchPushModule.java

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,13 @@
1919
import com.facebook.react.bridge.WritableArray;
2020
import com.facebook.react.bridge.WritableMap;
2121

22-
import java.security.InvalidKeyException;
23-
import java.security.NoSuchAlgorithmException;
24-
import java.util.Formatter;
2522
import java.util.List;
2623
import java.util.Map;
2724

28-
import javax.crypto.Mac;
29-
import javax.crypto.spec.SecretKeySpec;
30-
3125
public class RNBatchPushModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
3226

3327
private final ReactApplicationContext reactContext;
3428
private String batchAPIKey;
35-
private String batchInboxSecret;
3629

3730
public RNBatchPushModule(ReactApplicationContext reactContext) {
3831
super(reactContext);
@@ -44,12 +37,6 @@ public RNBatchPushModule(ReactApplicationContext reactContext) {
4437
String packageName = reactContext.getApplicationContext().getPackageName();
4538
this.batchAPIKey = resources.getString(resources.getIdentifier("BATCH_API_KEY", "string", packageName));
4639

47-
// Inbox is optional
48-
int batchInboxSecretIdentifier = resources.getIdentifier("BATCH_INBOX_SECRET", "string", packageName);
49-
if (batchInboxSecretIdentifier > 0) {
50-
this.batchInboxSecret = resources.getString(batchInboxSecretIdentifier);
51-
}
52-
5340
Batch.Push.setGCMSenderId(resources.getString(resources.getIdentifier("GCM_SENDER_ID", "string", packageName)));
5441
Batch.setConfig(new Config(this.batchAPIKey));
5542

@@ -88,14 +75,9 @@ public void logoutUser() {
8875
}
8976

9077
@ReactMethod
91-
public void fetchNewNotifications(String userID, final Promise promise) {
78+
public void fetchNewNotifications(String userID, String authKey, final Promise promise) {
9279
try {
93-
SecretKeySpec signinKey = new SecretKeySpec(this.batchInboxSecret.getBytes(), "HmacSHA256");
94-
Mac mac = Mac.getInstance("HmacSHA256");
95-
mac.init(signinKey);
96-
String hash = toHexString(mac.doFinal((this.batchAPIKey + userID).getBytes()));
97-
98-
BatchInboxFetcher inboxFetcher = Batch.Inbox.getFetcher(userID, hash);
80+
BatchInboxFetcher inboxFetcher = Batch.Inbox.getFetcher(userID, authKey);
9981
inboxFetcher.fetchNewNotifications(new BatchInboxFetcher.OnNewNotificationsFetchedListener() {
10082
public void onFetchSuccess(@NonNull List<BatchInboxNotificationContent> notifications, boolean foundNewNotifications, boolean endReached) {
10183
WritableArray jsNotifications = Arguments.createArray();
@@ -120,25 +102,11 @@ public void onFetchFailure(@NonNull String error) {
120102
promise.reject("BATCH_ERROR", "Error fetching new notifications: " + error);
121103
}
122104
});
123-
} catch (NoSuchAlgorithmException exception) {
124-
Log.e("RNBatchPush", "HmacSHA256 is not available");
125-
} catch (InvalidKeyException exception) {
126-
Log.e("RNBatchPush", "Key is invalid");
127105
} catch (Exception exception) {
128106
Log.e("RNBatchPush", "Unknown exception: " + exception.getMessage());
129107
}
130108
}
131109

132-
private static String toHexString(byte[] bytes) {
133-
Formatter formatter = new Formatter();
134-
135-
for (byte b : bytes) {
136-
formatter.format("%02x", b);
137-
}
138-
139-
return formatter.toString();
140-
}
141-
142110
@Override
143111
public void onHostResume()
144112
{

index.js.flow

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ declare module.exports: {
22
registerForRemoteNotifications: Function,
33
loginUser: (userId: string) => void,
44
logoutUser: Function,
5-
fetchNewNotifications: (userId: string) => ({ title: string, body: string, timestamp: number, payload: Object }),
5+
fetchNewNotifications: (userId: string, authKey: string) => Array<{ title: string, body: string, timestamp: number, payload: Object }>,
66
};

0 commit comments

Comments
 (0)