forked from ringcentral/pubnub-jtools
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPubnubCoreShared.java
More file actions
500 lines (410 loc) · 15.9 KB
/
PubnubCoreShared.java
File metadata and controls
500 lines (410 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
package com.pubnub.api;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.UUID;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import static com.pubnub.api.PubnubError.*;
/**
* Pubnub object facilitates querying channels for messages and listening on
* channels for presence/message events
*
* @author Pubnub
*
*/
abstract class PubnubCoreShared extends PubnubCore {
/**
* Pubnub Constructor
*
* @param publish_key
* Publish Key
* @param subscribe_key
* Subscribe Key
* @param secret_key
* Secret Key
* @param cipher_key
* Cipher Key
* @param ssl_on
* SSL on ?
*/
public PubnubCoreShared(String publish_key, String subscribe_key, String secret_key,
String cipher_key, boolean ssl_on) {
super(publish_key, subscribe_key, secret_key, cipher_key, ssl_on);
}
/**
* Pubnub Constructor
*
* @param publish_key
* Publish key
* @param subscribe_key
* Subscribe Key
* @param secret_key
* Secret Key
* @param ssl_on
* SSL on ?
*/
public PubnubCoreShared(String publish_key, String subscribe_key, String secret_key,
boolean ssl_on) {
super(publish_key, subscribe_key, secret_key, "", ssl_on);
}
/**
* Pubnub Constructor
*
* @param publish_key
* Publish Key
* @param subscribe_key
* Subscribe Key
*/
public PubnubCoreShared(String publish_key, String subscribe_key) {
super(publish_key, subscribe_key, "", "", false);
}
/**
* @param publish_key
* Publish Key
* @param subscribe_key
* Subscribe Key
* @param ssl
*/
public PubnubCoreShared(String publish_key, String subscribe_key, boolean ssl) {
super(publish_key, subscribe_key, "", "", ssl);
}
/**
* @param publish_key
* @param subscribe_key
* @param secret_key
*/
public PubnubCoreShared(String publish_key, String subscribe_key, String secret_key) {
super(publish_key, subscribe_key, secret_key, "", false);
}
/**
*
* Constructor for Pubnub Class
*
* @param publish_key
* Publish Key
* @param subscribe_key
* Subscribe Key
* @param secret_key
* Secret Key
* @param cipher_key
* Cipher Key
* @param ssl_on
* SSL enabled ?
* @param initialization_vector
* Initialization vector
*/
public PubnubCoreShared(String publish_key, String subscribe_key,
String secret_key, String cipher_key, boolean ssl_on, String initialization_vector) {
super(publish_key, subscribe_key, secret_key, cipher_key, ssl_on, initialization_vector);
}
/**
* Sets value for UUID
*
* @param uuid
* UUID value for Pubnub client
*/
public void setUUID(UUID uuid) {
this.UUID = uuid.toString();
}
public String uuid() {
return java.util.UUID.randomUUID().toString();
}
/**
* This method sets timeout value for subscribe/presence. Default value is
* 310000 milliseconds i.e. 310 seconds
*
* @param timeout
* Timeout value in milliseconds for subscribe/presence
*/
public void setSubscribeTimeout(int timeout) {
super.setSubscribeTimeout(timeout);
}
/**
* This method returns timeout value for subscribe/presence.
*
* @return Timeout value in milliseconds for subscribe/presence
*/
public int getSubscribeTimeout() {
return super.getSubscribeTimeout();
}
/**
* This method set timeout value for non subscribe operations like publish,
* history, hereNow. Default value is 15000 milliseconds i.e. 15 seconds.
*
* @param timeout
* Timeout value in milliseconds for Non subscribe operations
* like publish, history, hereNow
*/
public void setNonSubscribeTimeout(int timeout) {
super.setNonSubscribeTimeout(timeout);
}
/**
* This method returns timeout value for non subscribe operations like publish, history, hereNow
*
* @return Timeout value in milliseconds for for Non subscribe operations like publish, history, hereNow
*/
public int getNonSubscribeTimeout() {
return super.getNonSubscribeTimeout();
}
private String pamSign(String key, String data) throws PubnubException {
Mac sha256_HMAC;
try {
sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(),
"HmacSHA256");
sha256_HMAC.init(secret_key);
byte[] hmacData = sha256_HMAC.doFinal(data.getBytes("UTF-8"));
return new String(Base64Encoder.encode(hmacData)).replace('+', '-')
.replace('/', '_');
} catch (InvalidKeyException e1) {
throw new PubnubException(getErrorObject(PubnubError.PNERROBJ_ULSSIGN_ERROR, 1, "Invalid Key : " + e1.toString()));
} catch (NoSuchAlgorithmException e1) {
throw new PubnubException(getErrorObject(PubnubError.PNERROBJ_ULSSIGN_ERROR, 2, "Invalid Algorithm : " + e1.toString()));
} catch (IllegalStateException e1) {
throw new PubnubException(getErrorObject(PubnubError.PNERROBJ_ULSSIGN_ERROR, 3, "Invalid State : " + e1.toString()));
} catch (UnsupportedEncodingException e1) {
throw new PubnubException(getErrorObject(PubnubError.PNERROBJ_ULSSIGN_ERROR, 4, "Unsupported encoding : " + e1.toString()));
}
}
/** Grant r/w access based on channel and auth key
* @param channel
* @param auth_key
* @param read
* @param write
* @param callback
*/
public void pamGrant(final String channel, String auth_key, boolean read,
boolean write, final Callback callback) {
pamGrant(channel, auth_key, read, write, -1, callback);
}
/** Grant r/w access based on channel
* @param channel
* @param read
* @param write
* @param callback
*/
public void pamGrant(final String channel, boolean read,
boolean write, final Callback callback) {
pamGrant(channel, null, read, write, -1, callback);
}
/** Grant r/w access based on channel
* @param channel
* @param read
* @param write
* @param ttl
* @param callback
*/
public void pamGrant(final String channel, boolean read,
boolean write, int ttl, final Callback callback) {
pamGrant(channel, null, read, write, ttl, callback);
}
/** Grant r/w access based on channel and auth key
* @param channel
* @param auth_key
* @param read
* @param write
* @param ttl
* @param callback
*/
public void pamGrant(final String channel, String auth_key, boolean read,
boolean write, int ttl, Callback callback) {
final Callback cb = getWrappedCallback(callback);
Hashtable parameters = PubnubUtil.hashtableClone(params);
String r = (read) ? "1" : "0";
String w = (write) ? "1" : "0";
String signature = "0";
int timestamp = (int) ((new Date().getTime()) / 1000);
if (this.SECRET_KEY.length() == 0) {
callback.errorCallback(channel,
getErrorObject(PNERROBJ_SECRET_KEY_MISSING, 1));
return;
}
String sign_input = this.SUBSCRIBE_KEY + "\n" + this.PUBLISH_KEY + "\n" + "grant" + "\n" ;
if (auth_key != null && auth_key.length() > 0)
sign_input += "auth=" + auth_key + "&" ;
sign_input += "channel=" + PubnubUtil.urlEncode(channel) + "&" + "pnsdk=" + PubnubUtil.urlEncode(getUserAgent()) + "&" + "r=" + r + "&" + "timestamp=" + timestamp
+ ((ttl >= -1)?"&" + "ttl=" + ttl:"")
+ "&" + "w=" + w;
try {
signature = pamSign(this.SECRET_KEY, sign_input);
} catch (PubnubException e1) {
callback.errorCallback(channel,
e1.getPubnubError());
return;
}
parameters.put("w", w);
parameters.put("timestamp", String.valueOf(timestamp));
parameters.put("signature", signature);
parameters.put("r", r);
parameters.put("channel", channel);
if (auth_key != null && auth_key.length() > 0 ) parameters.put("auth", auth_key);
if (ttl >= -1) parameters.put("ttl", String.valueOf(ttl));
System.out.println(parameters);
String[] urlComponents = { getPubnubUrl(), "v1", "auth", "grant", "sub-key",
this.SUBSCRIBE_KEY
};
HttpRequest hreq = new HttpRequest(urlComponents, parameters,
new ResponseHandler() {
public void handleResponse(HttpRequest hreq, String response) {
invokeCallback(channel, response, "payload", cb, 4);
}
public void handleError(HttpRequest hreq, PubnubError error) {
cb.errorCallback(channel, error);
return;
}
});
_request(hreq, nonSubscribeManager);
}
/** ULS Audit
* @param callback
*/
public void pamAudit(Callback callback) {
final Callback cb = getWrappedCallback(callback);
Hashtable parameters = PubnubUtil.hashtableClone(params);
parameters.remove("auth");
String signature = "0";
int timestamp = (int) ((new Date().getTime()) / 1000);
if (this.SECRET_KEY.length() == 0) {
callback.errorCallback("",
getErrorObject(PNERROBJ_SECRET_KEY_MISSING, 2));
return;
}
String sign_input = this.SUBSCRIBE_KEY + "\n" + this.PUBLISH_KEY + "\n"
+ "audit" + "\n" + "pnsdk=" + PubnubUtil.urlEncode(getUserAgent()) + "&"
+ "timestamp=" + timestamp;
try {
signature = pamSign(this.SECRET_KEY, sign_input);
} catch (PubnubException e1) {
callback.errorCallback("",
e1.getPubnubError());
return;
}
parameters.put("timestamp", String.valueOf(timestamp));
parameters.put("signature", signature);
String[] urlComponents = { getPubnubUrl(), "v1", "auth", "audit", "sub-key",
this.SUBSCRIBE_KEY
};
HttpRequest hreq = new HttpRequest(urlComponents, parameters,
new ResponseHandler() {
public void handleResponse(HttpRequest hreq, String response) {
invokeCallback("", response, "payload", cb, 5 );
}
public void handleError(HttpRequest hreq, PubnubError error) {
cb.errorCallback("", error);
return;
}
});
_request(hreq, nonSubscribeManager);
}
/** ULS audit by channel
* @param channel
* @param callback
*/
public void pamAudit(final String channel,
Callback callback) {
final Callback cb = getWrappedCallback(callback);
Hashtable parameters = PubnubUtil.hashtableClone(params);
parameters.remove("auth");
String signature = "0";
int timestamp = (int) ((new Date().getTime()) / 1000);
if (this.SECRET_KEY.length() == 0) {
callback.errorCallback(channel,
getErrorObject(PNERROBJ_SECRET_KEY_MISSING , 3));
return;
}
String sign_input = this.SUBSCRIBE_KEY + "\n" + this.PUBLISH_KEY + "\n"
+ "audit" + "\n" + "channel="
+ PubnubUtil.urlEncode(channel) + "&" + "pnsdk=" + PubnubUtil.urlEncode(getUserAgent()) + "&" + "timestamp=" + timestamp;
try {
signature = pamSign(this.SECRET_KEY, sign_input);
} catch (PubnubException e1) {
callback.errorCallback(channel,
e1.getPubnubError());
return;
}
parameters.put("timestamp", String.valueOf(timestamp));
parameters.put("signature", signature);
parameters.put("channel", channel);
String[] urlComponents = { getPubnubUrl(), "v1", "auth", "audit", "sub-key",
this.SUBSCRIBE_KEY
};
HttpRequest hreq = new HttpRequest(urlComponents, parameters,
new ResponseHandler() {
public void handleResponse(HttpRequest hreq, String response) {
invokeCallback(channel, response, "payload", cb, 6);
}
public void handleError(HttpRequest hreq, PubnubError error) {
cb.errorCallback(channel, error);
return;
}
});
_request(hreq, nonSubscribeManager);
}
/** ULS audit by channel and auth key
* @param channel
* @param auth_key
* @param callback
*/
public void pamAudit(final String channel, String auth_key,
Callback callback) {
final Callback cb = getWrappedCallback(callback);
Hashtable parameters = PubnubUtil.hashtableClone(params);
String signature = "0";
int timestamp = (int) ((new Date().getTime()) / 1000);
if (this.SECRET_KEY.length() == 0) {
callback.errorCallback(channel,
getErrorObject(PNERROBJ_SECRET_KEY_MISSING, 4));
return;
}
String sign_input = this.SUBSCRIBE_KEY + "\n" + this.PUBLISH_KEY + "\n"
+ "audit" + "\n" + "auth=" + PubnubUtil.urlEncode(auth_key) + "&" + "channel="
+ PubnubUtil.urlEncode(channel) + "&" + "pnsdk=" + PubnubUtil.urlEncode(getUserAgent()) + "&" + "timestamp=" + timestamp;
try {
signature = pamSign(this.SECRET_KEY, sign_input);
} catch (PubnubException e1) {
callback.errorCallback(channel,
e1.getPubnubError());
return;
}
parameters.put("timestamp", String.valueOf(timestamp));
parameters.put("signature", signature);
parameters.put("channel", channel);
parameters.put("auth", auth_key);
String[] urlComponents = { getPubnubUrl(), "v1", "auth", "audit", "sub-key",
this.SUBSCRIBE_KEY
};
HttpRequest hreq = new HttpRequest(urlComponents, parameters,
new ResponseHandler() {
public void handleResponse(HttpRequest hreq, String response) {
invokeCallback(channel, response, "payload", cb, 2);
}
public void handleError(HttpRequest hreq, PubnubError error) {
cb.errorCallback(channel, error);
return;
}
});
_request(hreq, nonSubscribeManager);
}
/** ULS revoke by channel and auth key
* @param channel
* @param auth_key
* @param callback
*/
public void pamRevoke(String channel, String auth_key, Callback callback) {
pamGrant(channel, auth_key, false, false, callback);
}
/** ULS revoke by channel
* @param channel
* @param callback
*/
public void pamRevoke(String channel, Callback callback) {
pamGrant(channel, null, false, false, callback);
}
}