Skip to content

Commit 38a29b1

Browse files
author
Devendra
committed
adding support for overriding auth key, error callback for 401 403
1 parent dc91f4d commit 38a29b1

File tree

20 files changed

+747
-353
lines changed

20 files changed

+747
-353
lines changed

core/pubnub-common.js

+43-13
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function build_url( url_components, url_params ) {
4848
if (!url_params) return url;
4949

5050
each( url_params, function( key, value ) {
51-
params.push(key + "=" + encode_param(value));
51+
(typeof value != 'undefined') && params.push(key + "=" + encode_param(value));
5252
} );
5353

5454
url += "?" + params.join(PARAMSBIT);
@@ -280,6 +280,7 @@ function PN_API(setup) {
280280
, count = args['count'] || args['limit'] || 100
281281
, reverse = args['reverse'] || "false"
282282
, err = args['error'] || function(){}
283+
, auth_key = args['auth_key'] || AUTH_KEY
283284
, channel = args['channel']
284285
, start = args['start']
285286
, end = args['end']
@@ -294,7 +295,7 @@ function PN_API(setup) {
294295
params['stringtoken'] = 'true';
295296
params['count'] = count;
296297
params['reverse'] = reverse;
297-
params['auth'] = AUTH_KEY;
298+
params['auth'] = auth_key;
298299

299300
if (jsonp) params['callback'] = jsonp;
300301
if (start) params['start'] = start;
@@ -304,7 +305,13 @@ function PN_API(setup) {
304305
xdr({
305306
callback : jsonp,
306307
data : params,
307-
success : function(response) { callback(response) },
308+
success : function(response) {
309+
if (typeof response == 'object' && response['error']) {
310+
err(response);
311+
return;
312+
}
313+
callback(response)
314+
},
308315
fail : err,
309316
url : [
310317
STD_ORIGIN, 'v2', 'history', 'sub-key',
@@ -321,6 +328,7 @@ function PN_API(setup) {
321328
*/
322329
'replay' : function(args) {
323330
var callback = callback || args['callback'] || function(){}
331+
, auth_key = args['auth_key'] || AUTH_KEY
324332
, source = args['source']
325333
, destination = args['destination']
326334
, stop = args['stop']
@@ -346,7 +354,7 @@ function PN_API(setup) {
346354
if (end) data['end'] = end;
347355
if (limit) data['count'] = limit;
348356

349-
data['auth'] = AUTH_KEY;
357+
data['auth'] = auth_key;
350358

351359
// Compose URL Parts
352360
url = [
@@ -358,7 +366,13 @@ function PN_API(setup) {
358366
// Start (or Stop) Replay!
359367
xdr({
360368
callback : jsonp,
361-
success : function(response) { callback(response) },
369+
success : function(response) {
370+
if (typeof response == 'object' && response['error']) {
371+
err(response);
372+
return;
373+
}
374+
callback(response)
375+
},
362376
fail : function() { callback([ 0, 'Disconnected' ]) },
363377
url : url,
364378
data : data
@@ -398,6 +412,8 @@ function PN_API(setup) {
398412
var callback = callback || args['callback'] || function(){}
399413
, msg = args['message']
400414
, channel = args['channel']
415+
, auth_key = args['auth_key'] || AUTH_KEY
416+
, err = args['error'] || function() {}
401417
, jsonp = jsonp_cb()
402418
, url;
403419

@@ -422,9 +438,16 @@ function PN_API(setup) {
422438
callback : jsonp,
423439
timeout : SECOND * 5,
424440
url : url,
425-
data : { 'uuid' : UUID, 'auth' : AUTH_KEY },
426-
success : function(response){callback(response);publish(1)},
427-
fail : function(){callback([0,'Failed',msg]);publish(1)}
441+
data : { 'uuid' : UUID, 'auth' : auth_key },
442+
success : function(response) {
443+
if (typeof response == 'object' && response['error']) {
444+
err(response);
445+
} else {
446+
callback(response);
447+
}
448+
publish(1);
449+
},
450+
fail : function(response){err(response);publish(1)}
428451
});
429452

430453
// Send Message
@@ -468,6 +491,7 @@ function PN_API(setup) {
468491
var channel = args['channel']
469492
, callback = callback || args['callback']
470493
, callback = callback || args['message']
494+
, auth_key = args['auth_key'] || AUTH_KEY
471495
, connect = args['connect'] || function(){}
472496
, reconnect = args['reconnect'] || function(){}
473497
, disconnect = args['disconnect'] || function(){}
@@ -586,7 +610,7 @@ function PN_API(setup) {
586610
SUB_RECEIVER = null;
587611
SELF['time'](_test_connection);
588612
},
589-
data : { 'uuid' : UUID, 'auth' : AUTH_KEY },
613+
data : { 'uuid' : UUID, 'auth' : auth_key },
590614
url : [
591615
SUB_ORIGIN, 'subscribe',
592616
SUBSCRIBE_KEY, encode(channels),
@@ -598,8 +622,7 @@ function PN_API(setup) {
598622
// Check for Errors
599623
if (!messages || (
600624
typeof messages == 'object' &&
601-
'error' in messages &&
602-
!messages['error'])
625+
messages['error'])
603626
) {
604627
errcb(messages);
605628
return timeout( CONNECT, windowing );
@@ -672,9 +695,10 @@ function PN_API(setup) {
672695
'here_now' : function( args, callback ) {
673696
var callback = args['callback'] || callback
674697
, err = args['error'] || function(){}
698+
, auth_key = args['auth_key'] || AUTH_KEY
675699
, channel = args['channel']
676700
, jsonp = jsonp_cb()
677-
, data = { 'uuid' : UUID, 'auth' : AUTH_KEY };
701+
, data = { 'uuid' : UUID, 'auth' : auth_key };
678702

679703
// Make sure we have a Channel
680704
if (!channel) return error('Missing Channel');
@@ -686,7 +710,13 @@ function PN_API(setup) {
686710
xdr({
687711
callback : jsonp,
688712
data : data,
689-
success : function(response) { callback(response,channel) },
713+
success : function(response) {
714+
if (typeof response == 'object' && response['error']) {
715+
err(response);
716+
return;
717+
}
718+
callback(response)
719+
},
690720
fail : err,
691721
url : [
692722
STD_ORIGIN, 'v2', 'presence',

modern/pubnub.js

+43-13
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function build_url( url_components, url_params ) {
4949
if (!url_params) return url;
5050

5151
each( url_params, function( key, value ) {
52-
params.push(key + "=" + encode_param(value));
52+
(typeof value != 'undefined') && params.push(key + "=" + encode_param(value));
5353
} );
5454

5555
url += "?" + params.join(PARAMSBIT);
@@ -281,6 +281,7 @@ function PN_API(setup) {
281281
, count = args['count'] || args['limit'] || 100
282282
, reverse = args['reverse'] || "false"
283283
, err = args['error'] || function(){}
284+
, auth_key = args['auth_key'] || AUTH_KEY
284285
, channel = args['channel']
285286
, start = args['start']
286287
, end = args['end']
@@ -295,7 +296,7 @@ function PN_API(setup) {
295296
params['stringtoken'] = 'true';
296297
params['count'] = count;
297298
params['reverse'] = reverse;
298-
params['auth'] = AUTH_KEY;
299+
params['auth'] = auth_key;
299300

300301
if (jsonp) params['callback'] = jsonp;
301302
if (start) params['start'] = start;
@@ -305,7 +306,13 @@ function PN_API(setup) {
305306
xdr({
306307
callback : jsonp,
307308
data : params,
308-
success : function(response) { callback(response) },
309+
success : function(response) {
310+
if (typeof response == 'object' && response['error']) {
311+
err(response);
312+
return;
313+
}
314+
callback(response)
315+
},
309316
fail : err,
310317
url : [
311318
STD_ORIGIN, 'v2', 'history', 'sub-key',
@@ -322,6 +329,7 @@ function PN_API(setup) {
322329
*/
323330
'replay' : function(args) {
324331
var callback = callback || args['callback'] || function(){}
332+
, auth_key = args['auth_key'] || AUTH_KEY
325333
, source = args['source']
326334
, destination = args['destination']
327335
, stop = args['stop']
@@ -347,7 +355,7 @@ function PN_API(setup) {
347355
if (end) data['end'] = end;
348356
if (limit) data['count'] = limit;
349357

350-
data['auth'] = AUTH_KEY;
358+
data['auth'] = auth_key;
351359

352360
// Compose URL Parts
353361
url = [
@@ -359,7 +367,13 @@ function PN_API(setup) {
359367
// Start (or Stop) Replay!
360368
xdr({
361369
callback : jsonp,
362-
success : function(response) { callback(response) },
370+
success : function(response) {
371+
if (typeof response == 'object' && response['error']) {
372+
err(response);
373+
return;
374+
}
375+
callback(response)
376+
},
363377
fail : function() { callback([ 0, 'Disconnected' ]) },
364378
url : url,
365379
data : data
@@ -399,6 +413,8 @@ function PN_API(setup) {
399413
var callback = callback || args['callback'] || function(){}
400414
, msg = args['message']
401415
, channel = args['channel']
416+
, auth_key = args['auth_key'] || AUTH_KEY
417+
, err = args['error'] || function() {}
402418
, jsonp = jsonp_cb()
403419
, url;
404420

@@ -423,9 +439,16 @@ function PN_API(setup) {
423439
callback : jsonp,
424440
timeout : SECOND * 5,
425441
url : url,
426-
data : { 'uuid' : UUID, 'auth' : AUTH_KEY },
427-
success : function(response){callback(response);publish(1)},
428-
fail : function(){callback([0,'Failed',msg]);publish(1)}
442+
data : { 'uuid' : UUID, 'auth' : auth_key },
443+
success : function(response) {
444+
if (typeof response == 'object' && response['error']) {
445+
err(response);
446+
} else {
447+
callback(response);
448+
}
449+
publish(1);
450+
},
451+
fail : function(response){err(response);publish(1)}
429452
});
430453

431454
// Send Message
@@ -469,6 +492,7 @@ function PN_API(setup) {
469492
var channel = args['channel']
470493
, callback = callback || args['callback']
471494
, callback = callback || args['message']
495+
, auth_key = args['auth_key'] || AUTH_KEY
472496
, connect = args['connect'] || function(){}
473497
, reconnect = args['reconnect'] || function(){}
474498
, disconnect = args['disconnect'] || function(){}
@@ -587,7 +611,7 @@ function PN_API(setup) {
587611
SUB_RECEIVER = null;
588612
SELF['time'](_test_connection);
589613
},
590-
data : { 'uuid' : UUID, 'auth' : AUTH_KEY },
614+
data : { 'uuid' : UUID, 'auth' : auth_key },
591615
url : [
592616
SUB_ORIGIN, 'subscribe',
593617
SUBSCRIBE_KEY, encode(channels),
@@ -599,8 +623,7 @@ function PN_API(setup) {
599623
// Check for Errors
600624
if (!messages || (
601625
typeof messages == 'object' &&
602-
'error' in messages &&
603-
!messages['error'])
626+
messages['error'])
604627
) {
605628
errcb(messages);
606629
return timeout( CONNECT, windowing );
@@ -673,9 +696,10 @@ function PN_API(setup) {
673696
'here_now' : function( args, callback ) {
674697
var callback = args['callback'] || callback
675698
, err = args['error'] || function(){}
699+
, auth_key = args['auth_key'] || AUTH_KEY
676700
, channel = args['channel']
677701
, jsonp = jsonp_cb()
678-
, data = { 'uuid' : UUID, 'auth' : AUTH_KEY };
702+
, data = { 'uuid' : UUID, 'auth' : auth_key };
679703

680704
// Make sure we have a Channel
681705
if (!channel) return error('Missing Channel');
@@ -687,7 +711,13 @@ function PN_API(setup) {
687711
xdr({
688712
callback : jsonp,
689713
data : data,
690-
success : function(response) { callback(response,channel) },
714+
success : function(response) {
715+
if (typeof response == 'object' && response['error']) {
716+
err(response);
717+
return;
718+
}
719+
callback(response)
720+
},
691721
fail : err,
692722
url : [
693723
STD_ORIGIN, 'v2', 'presence',

0 commit comments

Comments
 (0)