Skip to content

Commit 85f23ec

Browse files
author
Devendra
committed
making auth key optional in grant and revoke
1 parent 0a23b54 commit 85f23ec

File tree

22 files changed

+492
-224
lines changed

22 files changed

+492
-224
lines changed

core/pubnub-common.js

+6-5
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-
(typeof value != 'undefined') && params.push(key + "=" + encode_param(value));
51+
(typeof value != 'undefined' && value != null && encode_param(value).length > 0) && params.push(key + "=" + encode_param(value));
5252
} );
5353

5454
url += "?" + params.join(PARAMSBIT);
@@ -746,15 +746,16 @@ function PN_API(setup) {
746746
if (!SUBSCRIBE_KEY) return error('Missing Subscribe Key');
747747
if (!PUBLISH_KEY) return error('Missing Publish Key');
748748
if (!SECRET_KEY) return error('Missing Secret Key');
749-
if (!auth_key) return error('Missing Auth Key');
750749

751750
if (jsonp != '0') { data['callback'] = jsonp; }
752751

753752
var timestamp = Math.floor(new Date().getTime() / 1000);
754753

755754
var sign_input = SUBSCRIBE_KEY + "\n" + PUBLISH_KEY + "\n"
756-
+ "grant" + "\n" + "auth=" + encodeURIComponent(auth_key) + "&" + "channel="
757-
+ encodeURIComponent(channel) + "&" + "pnsdk=" + encodeURIComponent(PNSDK) + "&" + "r=" + r + "&" + "timestamp=" + timestamp
755+
+ "grant" + "\n"
756+
+ (((auth_key && encodeURIComponent(auth_key).length > 0)?"auth=" + encodeURIComponent(auth_key) + "&":""))
757+
+ "channel=" + encodeURIComponent(channel) + "&" + "pnsdk=" + encodeURIComponent(PNSDK) + "&"
758+
+ "r=" + r + "&" + "timestamp=" + encodeURIComponent(timestamp)
758759
+ ((ttl > -1)?"&" + "ttl=" + ttl:"")
759760
+ "&" + "w=" + w;
760761
var signature = hmac_SHA256( sign_input, SECRET_KEY );
@@ -767,10 +768,10 @@ function PN_API(setup) {
767768
'r' : r,
768769
'signature' : signature,
769770
'channel' : encodeURIComponent(channel),
770-
'auth' : encodeURIComponent(auth_key),
771771
'timestamp' : timestamp
772772
};
773773
if (ttl > -1) data['ttl'] = ttl
774+
if (auth_key) data['auth'] = encodeURIComponent(auth_key);
774775

775776
xdr({
776777
callback : jsonp,

modern/pubnub.js

+6-5
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-
(typeof value != 'undefined') && params.push(key + "=" + encode_param(value));
52+
(typeof value != 'undefined' && value != null && encode_param(value).length > 0) && params.push(key + "=" + encode_param(value));
5353
} );
5454

5555
url += "?" + params.join(PARAMSBIT);
@@ -747,15 +747,16 @@ function PN_API(setup) {
747747
if (!SUBSCRIBE_KEY) return error('Missing Subscribe Key');
748748
if (!PUBLISH_KEY) return error('Missing Publish Key');
749749
if (!SECRET_KEY) return error('Missing Secret Key');
750-
if (!auth_key) return error('Missing Auth Key');
751750

752751
if (jsonp != '0') { data['callback'] = jsonp; }
753752

754753
var timestamp = Math.floor(new Date().getTime() / 1000);
755754

756755
var sign_input = SUBSCRIBE_KEY + "\n" + PUBLISH_KEY + "\n"
757-
+ "grant" + "\n" + "auth=" + encodeURIComponent(auth_key) + "&" + "channel="
758-
+ encodeURIComponent(channel) + "&" + "pnsdk=" + encodeURIComponent(PNSDK) + "&" + "r=" + r + "&" + "timestamp=" + timestamp
756+
+ "grant" + "\n"
757+
+ (((auth_key && encodeURIComponent(auth_key).length > 0)?"auth=" + encodeURIComponent(auth_key) + "&":""))
758+
+ "channel=" + encodeURIComponent(channel) + "&" + "pnsdk=" + encodeURIComponent(PNSDK) + "&"
759+
+ "r=" + r + "&" + "timestamp=" + encodeURIComponent(timestamp)
759760
+ ((ttl > -1)?"&" + "ttl=" + ttl:"")
760761
+ "&" + "w=" + w;
761762
var signature = hmac_SHA256( sign_input, SECRET_KEY );
@@ -768,10 +769,10 @@ function PN_API(setup) {
768769
'r' : r,
769770
'signature' : signature,
770771
'channel' : encodeURIComponent(channel),
771-
'auth' : encodeURIComponent(auth_key),
772772
'timestamp' : timestamp
773773
};
774774
if (ttl > -1) data['ttl'] = ttl
775+
if (auth_key) data['auth'] = encodeURIComponent(auth_key);
775776

776777
xdr({
777778
callback : jsonp,

modern/pubnub.min.js

+22-22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node.js/pubnub.js

+6-5
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-
(typeof value != 'undefined') && params.push(key + "=" + encode_param(value));
52+
(typeof value != 'undefined' && value != null && encode_param(value).length > 0) && params.push(key + "=" + encode_param(value));
5353
} );
5454

5555
url += "?" + params.join(PARAMSBIT);
@@ -747,15 +747,16 @@ function PN_API(setup) {
747747
if (!SUBSCRIBE_KEY) return error('Missing Subscribe Key');
748748
if (!PUBLISH_KEY) return error('Missing Publish Key');
749749
if (!SECRET_KEY) return error('Missing Secret Key');
750-
if (!auth_key) return error('Missing Auth Key');
751750

752751
if (jsonp != '0') { data['callback'] = jsonp; }
753752

754753
var timestamp = Math.floor(new Date().getTime() / 1000);
755754

756755
var sign_input = SUBSCRIBE_KEY + "\n" + PUBLISH_KEY + "\n"
757-
+ "grant" + "\n" + "auth=" + encodeURIComponent(auth_key) + "&" + "channel="
758-
+ encodeURIComponent(channel) + "&" + "pnsdk=" + encodeURIComponent(PNSDK) + "&" + "r=" + r + "&" + "timestamp=" + timestamp
756+
+ "grant" + "\n"
757+
+ (((auth_key && encodeURIComponent(auth_key).length > 0)?"auth=" + encodeURIComponent(auth_key) + "&":""))
758+
+ "channel=" + encodeURIComponent(channel) + "&" + "pnsdk=" + encodeURIComponent(PNSDK) + "&"
759+
+ "r=" + r + "&" + "timestamp=" + encodeURIComponent(timestamp)
759760
+ ((ttl > -1)?"&" + "ttl=" + ttl:"")
760761
+ "&" + "w=" + w;
761762
var signature = hmac_SHA256( sign_input, SECRET_KEY );
@@ -768,10 +769,10 @@ function PN_API(setup) {
768769
'r' : r,
769770
'signature' : signature,
770771
'channel' : encodeURIComponent(channel),
771-
'auth' : encodeURIComponent(auth_key),
772772
'timestamp' : timestamp
773773
};
774774
if (ttl > -1) data['ttl'] = ttl
775+
if (auth_key) data['auth'] = encodeURIComponent(auth_key);
775776

776777
xdr({
777778
callback : jsonp,

node.js/tests/test.js

+125-1
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,11 @@ describe('Pubnub', function() {
312312
describe('#grant()', function(){
313313
var grant_channel = channel + '-grant';
314314
var auth_key = "abcd";
315+
var sub_key = 'sub-c-a478dd2a-c33d-11e2-883f-02ee2ddab7fe';
315316
var pubnub = PUBNUB.init({
316317
origin : 'pam-beta.pubnub.com',
317318
publish_key : 'pub-c-a2650a22-deb1-44f5-aa87-1517049411d5',
318-
subscribe_key : 'sub-c-a478dd2a-c33d-11e2-883f-02ee2ddab7fe',
319+
subscribe_key : sub_key,
319320
secret_key : 'sec-c-YjFmNzYzMGMtYmI3NC00NzJkLTlkYzYtY2MwMzI4YTJhNDVh'
320321
});
321322

@@ -381,6 +382,67 @@ describe('Pubnub', function() {
381382
},5000);
382383
})
383384

385+
it('should be able to grant read write access without auth key', function(done) {
386+
var grant_channel_5 = grant_channel + '-5';
387+
setTimeout(function() {
388+
pubnub.grant({
389+
channel : grant_channel_5,
390+
read : true,
391+
write : true,
392+
callback : function(response) {
393+
assert.deepEqual(response.status,200);
394+
pubnub.audit({
395+
channel : grant_channel_5,
396+
callback : function(response) {
397+
assert.deepEqual(response.status,200);
398+
assert.deepEqual(response.payload.channels.r,1);
399+
assert.deepEqual(response.payload.channels.w,1);
400+
assert.deepEqual(response.payload.subscribe_key,sub_key);
401+
pubnub.history({
402+
'channel' : grant_channel_5,
403+
'auth_key' : "",
404+
'callback' : function(response) {
405+
assert.ok(true);
406+
pubnub.publish({
407+
'channel' : grant_channel_5,
408+
'auth_key' : "",
409+
'message' : 'Test',
410+
'callback': function(response) {
411+
assert.ok(true);
412+
done();
413+
},
414+
'error' : function(response) {
415+
assert.ok(false);
416+
}
417+
})
418+
},
419+
'error' : function(response) {
420+
console.log(response);
421+
assert.ok(false);
422+
pubnub.publish({
423+
'channel' : grant_channel_5,
424+
'message' : 'Test',
425+
'auth_key' : "",
426+
'callback': function(response) {
427+
assert.ok(true);
428+
done();
429+
},
430+
'error' : function(response) {
431+
assert.ok(false);
432+
done();
433+
}
434+
})
435+
}
436+
437+
});
438+
}
439+
});
440+
441+
}
442+
})
443+
},5000);
444+
})
445+
384446
it('should be able to grant read access revoke write access', function(done) {
385447
var grant_channel_2 = grant_channel + '-2';
386448
setTimeout(function() {
@@ -568,6 +630,68 @@ describe('Pubnub', function() {
568630
})
569631
},5000);
570632
})
633+
it('should be able to revoke read and write access without auth key', function(done) {
634+
var grant_channel_6 = grant_channel + '-6';
635+
setTimeout(function() {
636+
pubnub.grant({
637+
channel : grant_channel_6,
638+
read : false,
639+
write : false,
640+
callback : function(response) {
641+
assert.deepEqual(response.status,200);
642+
pubnub.audit({
643+
channel : grant_channel_6,
644+
callback : function(response) {
645+
assert.deepEqual(response.status,200);
646+
assert.deepEqual(response.payload.channels.r,0);
647+
assert.deepEqual(response.payload.channels.w,0);
648+
assert.deepEqual(response.payload.subscribe_key,sub_key);
649+
pubnub.history({
650+
'channel' : grant_channel_6,
651+
'auth_key' : "",
652+
'callback' : function(response) {
653+
assert.ok(false);
654+
pubnub.publish({
655+
'channel' : grant_channel_6,
656+
'auth_key' : "",
657+
'message' : 'Test',
658+
'callback': function(response) {
659+
assert.ok(false);
660+
done();
661+
},
662+
'error' : function(response) {
663+
assert.ok(true);
664+
done();
665+
}
666+
})
667+
},
668+
'error' : function(response) {
669+
assert.ok(true);
670+
pubnub.publish({
671+
'channel' : grant_channel_6,
672+
'message' : 'Test',
673+
'auth_key' : "",
674+
'callback': function(response) {
675+
assert.ok(false);
676+
done();
677+
},
678+
'error' : function(response) {
679+
assert.ok(true);
680+
done();
681+
}
682+
})
683+
}
684+
685+
});
686+
687+
}
688+
});
689+
690+
}
691+
})
692+
},5000);
693+
})
694+
571695

572696
})
573697
describe('#revoke()', function(){

phonegap/pubnub.js

+6-5
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-
(typeof value != 'undefined') && params.push(key + "=" + encode_param(value));
52+
(typeof value != 'undefined' && value != null && encode_param(value).length > 0) && params.push(key + "=" + encode_param(value));
5353
} );
5454

5555
url += "?" + params.join(PARAMSBIT);
@@ -747,15 +747,16 @@ function PN_API(setup) {
747747
if (!SUBSCRIBE_KEY) return error('Missing Subscribe Key');
748748
if (!PUBLISH_KEY) return error('Missing Publish Key');
749749
if (!SECRET_KEY) return error('Missing Secret Key');
750-
if (!auth_key) return error('Missing Auth Key');
751750

752751
if (jsonp != '0') { data['callback'] = jsonp; }
753752

754753
var timestamp = Math.floor(new Date().getTime() / 1000);
755754

756755
var sign_input = SUBSCRIBE_KEY + "\n" + PUBLISH_KEY + "\n"
757-
+ "grant" + "\n" + "auth=" + encodeURIComponent(auth_key) + "&" + "channel="
758-
+ encodeURIComponent(channel) + "&" + "pnsdk=" + encodeURIComponent(PNSDK) + "&" + "r=" + r + "&" + "timestamp=" + timestamp
756+
+ "grant" + "\n"
757+
+ (((auth_key && encodeURIComponent(auth_key).length > 0)?"auth=" + encodeURIComponent(auth_key) + "&":""))
758+
+ "channel=" + encodeURIComponent(channel) + "&" + "pnsdk=" + encodeURIComponent(PNSDK) + "&"
759+
+ "r=" + r + "&" + "timestamp=" + encodeURIComponent(timestamp)
759760
+ ((ttl > -1)?"&" + "ttl=" + ttl:"")
760761
+ "&" + "w=" + w;
761762
var signature = hmac_SHA256( sign_input, SECRET_KEY );
@@ -768,10 +769,10 @@ function PN_API(setup) {
768769
'r' : r,
769770
'signature' : signature,
770771
'channel' : encodeURIComponent(channel),
771-
'auth' : encodeURIComponent(auth_key),
772772
'timestamp' : timestamp
773773
};
774774
if (ttl > -1) data['ttl'] = ttl
775+
if (auth_key) data['auth'] = encodeURIComponent(auth_key);
775776

776777
xdr({
777778
callback : jsonp,

0 commit comments

Comments
 (0)