Skip to content

Commit

Permalink
channel group support admin api's only
Browse files Browse the repository at this point in the history
  • Loading branch information
Devendra committed Nov 25, 2014
1 parent f97718c commit 99b1453
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 40 deletions.
26 changes: 21 additions & 5 deletions Pubnub.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import time
import hashlib
import uuid as uuid_lib
import random
import sys
from base64 import urlsafe_b64encode
from base64 import encodestring, decodestring
Expand Down Expand Up @@ -320,6 +321,9 @@ def _pam_sign(self, msg):
sha256
).digest())

def set_u(self, u=False):
self.u = u

def _pam_auth(self, query, apicode=0, callback=None, error=None):

if 'timestamp' not in query:
Expand All @@ -332,6 +336,10 @@ def _pam_auth(self, query, apicode=0, callback=None, error=None):
if 'channel' in query and not query['channel']:
del query['channel']

if 'channel-group' in query and not query['channel-group']:
del query['channel-group']


params = "&".join([
x + "=" + quote(
str(query[x]), safe=""
Expand Down Expand Up @@ -362,8 +370,8 @@ def set_auth_key(self, auth_key):
def get_auth_key(self):
return auth_key

def grant(self, channel=None, auth_key=False, read=True,
write=True, ttl=5, callback=None, error=None):
def grant(self, channel=None, channel_group=None, auth_key=False, read=False,
write=False, manage=False, ttl=5, callback=None, error=None):
"""Method for granting permissions.
This function establishes subscribe and/or write permissions for
Expand Down Expand Up @@ -437,14 +445,16 @@ def grant(self, channel=None, auth_key=False, read=True,

return self._pam_auth({
'channel' : channel,
'channel-group' : channel_group,
'auth' : auth_key,
'r' : read and 1 or 0,
'w' : write and 1 or 0,
'm' : manage and 1 or 0,
'ttl' : ttl,
'pnsdk' : self.pnsdk
}, callback=callback, error=error)

def revoke(self, channel=None, auth_key=None, ttl=1, callback=None, error=None):
def revoke(self, channel=None, channel_group=None, auth_key=None, ttl=1, callback=None, error=None):
"""Method for revoking permissions.
Args:
Expand Down Expand Up @@ -501,14 +511,15 @@ def revoke(self, channel=None, auth_key=None, ttl=1, callback=None, error=None):

return self._pam_auth({
'channel' : channel,
'channel-group' : channel_group,
'auth' : auth_key,
'r' : 0,
'w' : 0,
'ttl' : ttl,
'pnsdk' : self.pnsdk
}, callback=callback, error=error)

def audit(self, channel=None, auth_key=None, callback=None, error=None):
def audit(self, channel=None, channel_group=None, auth_key=None, callback=None, error=None):
"""Method for fetching permissions from pubnub servers.
This method provides a mechanism to reveal existing PubNub Access Manager attributes
Expand Down Expand Up @@ -564,6 +575,7 @@ def audit(self, channel=None, auth_key=None, callback=None, error=None):

return self._pam_auth({
'channel' : channel,
'channel-group' : channel_group,
'auth' : auth_key,
'pnsdk' : self.pnsdk
}, 1, callback=callback, error=error)
Expand Down Expand Up @@ -869,6 +881,9 @@ def _encode(self, request):
]) for bit in request]

def getUrl(self, request):

if self.u is True and "urlparams" in request:
request['urlparams']['u'] = str(random.randint(1, 100000000000))
## Build URL
url = self.origin + '/' + "/".join([
"".join([' ~`!@#$%^&*()+=[]\\{}|;\':",./<>?'.find(ch) > -1 and
Expand All @@ -878,7 +893,7 @@ def getUrl(self, request):
if ("urlparams" in request):
url = url + '?' + "&".join([x + "=" + str(y) for x, y in request[
"urlparams"].items() if y is not None])
print url

return url

def _channel_registry(self, url=None, params=None, callback=None, error=None):
Expand Down Expand Up @@ -1015,6 +1030,7 @@ def __init__(
self._tt_lock = _tt_lock
self._channel_list_lock = _channel_list_lock
self._connect = lambda: None
self.u = None

def get_channel_list(self, channels):
channel = ''
Expand Down
16 changes: 11 additions & 5 deletions python/examples/cr.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,25 @@
import sys
from Pubnub import Pubnub

publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo'
subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'demo'
secret_key = len(sys.argv) > 3 and sys.argv[3] or 'demo'
publish_key = len(sys.argv) > 1 and sys.argv[1] or 'pam'
subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'pam'
secret_key = len(sys.argv) > 3 and sys.argv[3] or 'pam'
cipher_key = len(sys.argv) > 4 and sys.argv[4] or ''
ssl_on = len(sys.argv) > 5 and bool(sys.argv[5]) or False

## -----------------------------------------------------------------------
## Initiate Pubnub State
## -----------------------------------------------------------------------
pubnub = Pubnub(publish_key=publish_key, subscribe_key=subscribe_key,
secret_key=secret_key, cipher_key=cipher_key, ssl_on=ssl_on)
secret_key=secret_key, cipher_key=cipher_key, ssl_on=ssl_on, auth_key="abcd")
channel = 'hello_world'

def callback(message):
print(message)

print pubnub.revoke(channel_group='dev:abcd', auth_key="abcd")
print pubnub.audit(channel_group="dev:abcd")
print pubnub.grant(channel_group='dev:abcd', read=True, write=True, manage=True, auth_key="abcd")
print pubnub.channel_group_list_namespaces()
print pubnub.channel_group_list_groups(namespace='aaa')
print pubnub.channel_group_list_groups(namespace='foo')
Expand All @@ -35,11 +38,14 @@ def callback(message):
print pubnub.channel_group_list_channels(channel_group='dev:abcd')


pubnub.revoke(channel_group='dev:abcd', auth_key="abcd", callback=callback, error=callback)
pubnub.audit(channel_group="dev:abcd", callback=callback, error=callback)
pubnub.grant(channel_group='dev:abcd', read=True, write=True, manage=True, auth_key="abcd", callback=callback, error=callback)
pubnub.channel_group_list_namespaces(callback=callback, error=callback)
pubnub.channel_group_list_groups(namespace='aaa', callback=callback, error=callback)
pubnub.channel_group_list_groups(namespace='foo', callback=callback, error=callback)
pubnub.channel_group_list_channels(channel_group='dev:abcd', callback=callback, error=callback)
pubnub.channel_group_add_channel(channel_group='dev:abcd', channel="hi", callback=callback, error=callback)
pubnub.channel_group_list_channels(channel_group='dev:abcd', callback=callback, error=callback)
pubnub.channel_group_remove_channel(channel_group='dev:abcd', channel="hi", callback=callback, error=callback)
pubnub.channel_group_list_channels(channel_group='dev:abcd', callback=callback, error=callback)
pubnub.channel_group_list_channels(channel_group='dev:abcd', callback=callback, error=callback)
107 changes: 107 additions & 0 deletions python/tests/test_cg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
from Pubnub import Pubnub
import time
import random


pubnub = Pubnub("demo","demo")
pubnub.set_u(True)

def rand_str(s):
return str(s) + '-' + str(random.randint(1, 100000000000))


def test_1():
channel = rand_str('channel')
channel2 = rand_str('channel')
channel_group = rand_str('group')
channel_group2 = rand_str('group')
namespace = rand_str('ns')

resp = pubnub.channel_group_add_channel(channel_group=namespace + ':' + channel_group, channel=channel)
assert resp['status'] == 200
assert resp['message'] == 'OK'
assert resp['error'] == False

resp = pubnub.channel_group_add_channel(channel_group=namespace + ':' + channel_group, channel=channel2)
assert resp['status'] == 200
assert resp['message'] == 'OK'
assert resp['error'] == False

resp = pubnub.channel_group_add_channel(channel_group=namespace + ':' + channel_group2, channel=channel)
assert resp['status'] == 200
assert resp['message'] == 'OK'
assert resp['error'] == False

resp = pubnub.channel_group_add_channel(channel_group=namespace + ':' + channel_group2, channel=channel2)
assert resp['status'] == 200
assert resp['message'] == 'OK'
assert resp['error'] == False


resp = pubnub.channel_group_list_channels(channel_group=namespace + ':' + channel_group)
assert channel in resp['payload']['channels']
assert channel2 in resp['payload']['channels']
assert len(resp['payload']['channels']) == 2

resp = pubnub.channel_group_remove_channel(channel_group=namespace + ':' + channel_group, channel=channel2)
print resp
assert resp['status'] == 200
assert resp['message'] == 'OK'
assert resp['error'] == False

resp = pubnub.channel_group_list_channels(channel_group=namespace + ':' + channel_group)
print resp
assert channel in resp['payload']['channels']
assert len(resp['payload']['channels']) == 1


resp = pubnub.channel_group_list_channels(channel_group=namespace + ':' + channel_group2)
assert channel in resp['payload']['channels']
assert channel2 in resp['payload']['channels']
assert len(resp['payload']['channels']) == 2

resp = pubnub.channel_group_remove_channel(channel_group=namespace + ':' + channel_group2, channel=channel2)
print resp
assert resp['status'] == 200
assert resp['message'] == 'OK'
assert resp['error'] == False

resp = pubnub.channel_group_list_channels(channel_group=namespace + ':' + channel_group2)
print resp
assert channel in resp['payload']['channels']
assert len(resp['payload']['channels']) == 1



resp = pubnub.channel_group_list_groups(namespace=namespace)
assert channel_group in resp['payload']['groups']
assert channel_group2 in resp['payload']['groups']
assert len(resp['payload']['groups']) == 2

resp = pubnub.channel_group_remove_group(channel_group=namespace + ':' + channel_group2)
print resp
assert resp['status'] == 200
assert resp['message'] == 'OK'
assert resp['error'] == False


resp = pubnub.channel_group_list_groups(namespace=namespace)
assert channel_group in resp['payload']['groups']
assert len(resp['payload']['groups']) == 1


resp = pubnub.channel_group_list_namespaces()
assert namespace in resp['payload']['namespaces']

resp = pubnub.channel_group_remove_namespace(namespace=namespace)
print resp
assert resp['status'] == 200
assert resp['message'] == 'OK'
assert resp['error'] == False

resp = pubnub.channel_group_list_namespaces()
assert namespace not in resp['payload']['namespaces']




Loading

0 comments on commit 99b1453

Please sign in to comment.