From 19019a3466917e8ab2d8fd17df471aa34a07df1b Mon Sep 17 00:00:00 2001 From: Devendra Date: Fri, 5 Jun 2015 23:31:51 +0530 Subject: pam demo --- python/examples/pam_demo/demo.py | 74 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 python/examples/pam_demo/demo.py diff --git a/python/examples/pam_demo/demo.py b/python/examples/pam_demo/demo.py new file mode 100644 index 0000000..e00914d --- /dev/null +++ b/python/examples/pam_demo/demo.py @@ -0,0 +1,74 @@ + +from gevent.monkey import patch_all +patch_all() + +import sys +from Pubnub import Pubnub +import random +import json + +rand = str(random.randint(1,99999999)) + +def get_unique(s): + return 'str-' + rand + '-' + s + +# public channel +channel_public = get_unique("channel_public") + +# server auth key +server_auth_token = get_unique("server_auth_token") + +#client auth key +client_auth_token = get_unique("client_auth_token") + +#client uuid +client_uuid = get_unique("client_uuid") + +#server uuid +server_uuid = get_unique("server_uuid") + + +#init server object +server = Pubnub(publish_key="pam", subscribe_key="pam", secret_key="pam", auth_key=server_auth_token, uuid=server_uuid) + +#init client object +client = Pubnub(publish_key="pam", subscribe_key="pam", auth_key=client_auth_token, uuid=client_uuid) + +# Grant permission to server auth keys +print(server.grant(channel=channel_public, auth_key=server_auth_token, read=True, write=True)) +print(server.grant(channel=channel_public + '-pnpres', auth_key=server_auth_token, read=True, write=True)) + +# Grant permission to client auth keys +print(server.grant(channel=channel_public, auth_key=client_auth_token, read=True, write=False)) +print(server.grant(channel=channel_public + '-pnpres', auth_key=client_auth_token, read=True, write=False)) + + + +def _server_callback(message, channel): + print(message) + +def _error_callback(error): + print(error) + +#server subscribes to public channel +server.subscribe(channels=channel_public, callback=_server_callback, error=_error_callback) + + + +def _server_presence_callback(message, channel): + print message + if 'action' in message: + if message['action'] == 'join' and message['uuid'] == client_uuid: + print "Only I can see that client with UUID " + message['uuid'] + " has a state of " + json.dumps(message['data']) + +# server subscribes to presence events on public channel +server.presence(channel=channel_public, callback=_server_presence_callback, error=_error_callback) + + + + +def _client_callback(channel, message): + print(message) + +# client subscribes to public channel +client.subscribe(channels=channel_public, state={ "myKey" : get_unique("foo")}, callback=_client_callback, error=_error_callback) -- cgit v1.2.3 From ae7dbdec9a5a9e147cf5e8de160455277d328f39 Mon Sep 17 00:00:00 2001 From: gcohen Date: Fri, 5 Jun 2015 13:28:52 -0700 Subject: Added verbose comments, and added a scenarion to depict the client trying to subscribe on a restricted presence channel. --- python/examples/pam_demo/demo.py | 74 ++++++++++++++++++++++++++++------------ 1 file changed, 52 insertions(+), 22 deletions(-) diff --git a/python/examples/pam_demo/demo.py b/python/examples/pam_demo/demo.py index e00914d..a60730c 100644 --- a/python/examples/pam_demo/demo.py +++ b/python/examples/pam_demo/demo.py @@ -1,4 +1,3 @@ - from gevent.monkey import patch_all patch_all() @@ -13,62 +12,93 @@ def get_unique(s): return 'str-' + rand + '-' + s # public channel +# This is the channel all clients announce themselves on -- or more generally speaking, a channel you expect the client +# to "check-in" on to announce his state + channel_public = get_unique("channel_public") # server auth key +# Only the server has/knows about this auth token. It will be used to grant read on the "check-in" Presence channel + server_auth_token = get_unique("server_auth_token") -#client auth key +# client auth key +# only clients will use this authey -- it does not provide presence channel read access + client_auth_token = get_unique("client_auth_token") -#client uuid +# each client must have a unique id -- a UUID, for presence information/state to bind to + +# client uuid client_uuid = get_unique("client_uuid") -#server uuid +# server uuid server_uuid = get_unique("server_uuid") +# For the demo, we'll implement a SERVER called server, who is the authoritative 'admin' entity in the system +# We'll also implement a CLIENT called client, who is an arbitrary hardware device member of the network -#init server object +# Please swap out the default 'pam' demo keys with your own PAM-enabled keys + +# init server object server = Pubnub(publish_key="pam", subscribe_key="pam", secret_key="pam", auth_key=server_auth_token, uuid=server_uuid) -#init client object +# init client object client = Pubnub(publish_key="pam", subscribe_key="pam", auth_key=client_auth_token, uuid=client_uuid) +# To access a Presence channel with PAM, its format is CHANNELNAME-pnpres + # Grant permission to server auth keys +# grant r/w to public, and r/w public Presence (public-pnpres) + print(server.grant(channel=channel_public, auth_key=server_auth_token, read=True, write=True)) print(server.grant(channel=channel_public + '-pnpres', auth_key=server_auth_token, read=True, write=True)) # Grant permission to client auth keys +# grant r/w to public, and w-only access to public Presence (public-pnpres) print(server.grant(channel=channel_public, auth_key=client_auth_token, read=True, write=False)) -print(server.grant(channel=channel_public + '-pnpres', auth_key=client_auth_token, read=True, write=False)) +print(server.grant(channel=channel_public + '-pnpres', auth_key=client_auth_token, read=False, write=False)) +# Now, we'll run it to watch it work as advertised... +# Define some simple callabcks for the Server and Client -def _server_callback(message, channel): - print(message) +def _server_message_callback(message, channel): + print("Server heard: " + message) + +def _client_callback(channel, message): + print("Client heard: " + message) def _error_callback(error): - print(error) + print("Error: " + error) -#server subscribes to public channel -server.subscribe(channels=channel_public, callback=_server_callback, error=_error_callback) +def _server_presence_callback(message, channel): + print message + if 'action' in message: + if message['action'] == 'join' and message['uuid'] == client_uuid: + print "Server can see that client with UUID " + message['uuid'] + " has a state of " + json.dumps(message['data']) +def _client_presence_callback(message, channel): + print message + if 'action' in message: + if message['action'] == 'join' and message['uuid'] == client_uuid: + print "Client can see that client with UUID " + message['uuid'] + " has a state of " + json.dumps(message['data']) +# server subscribes to public channel +server.subscribe(channels=channel_public, callback=_server_message_callback, error=_error_callback) -def _server_presence_callback(message, channel): - print message - if 'action' in message: - if message['action'] == 'join' and message['uuid'] == client_uuid: - print "Only I can see that client with UUID " + message['uuid'] + " has a state of " + json.dumps(message['data']) +# server subscribes to presence events on public channel +# presence() is a convienence method that subscribes to channel-pnpres with special logic for handling +# presence-event formatted messages -# server subscribes to presence events on public channel +## uncomment out to see server able to read on presence channel server.presence(channel=channel_public, callback=_server_presence_callback, error=_error_callback) +# now if the client tried to subscribe on the presence channel, and therefore, get state info +# he is explicitly denied! - - -def _client_callback(channel, message): - print(message) +## uncomment out to see client not able to read on presence channel +#client.presence(channel=channel_public, callback=_client_presence_callback, error=_error_callback) # client subscribes to public channel client.subscribe(channels=channel_public, state={ "myKey" : get_unique("foo")}, callback=_client_callback, error=_error_callback) -- cgit v1.2.3 From 8983da3e4e18f851ccb4f2695c71083ad2e392af Mon Sep 17 00:00:00 2001 From: Devendra Date: Sat, 6 Jun 2015 16:19:03 +0530 Subject: fixed issue of error callback not getting invoked for presence --- Pubnub.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Pubnub.py b/Pubnub.py index fbb175b..5580a51 100644 --- a/Pubnub.py +++ b/Pubnub.py @@ -768,7 +768,7 @@ class PubnubBase(object): callback=self._return_wrapped_callback(callback), error=self._return_wrapped_callback(error)) - def presence(self, channel, callback, error=None): + def presence(self, channel, callback, error=None, connect=None, disconnect=None, reconnect=None): """Subscribe to presence events on a channel. Only works in async mode @@ -784,7 +784,7 @@ class PubnubBase(object): Returns: None """ - return self.subscribe(channel+'-pnpres', callback=callback) + return self.subscribe(channel+'-pnpres', callback=callback, error=error, connect=connect, disconnect=disconnect, reconnect=reconnect) def presence_group(self, channel_group, callback, error=None): """Subscribe to presence events on a channel group. -- cgit v1.2.3 From dcdafd146508f7f1f1a1868a94827bb4d6c1a3de Mon Sep 17 00:00:00 2001 From: Devendra Date: Tue, 9 Jun 2015 01:13:03 +0530 Subject: adding channel param for error callback --- Pubnub.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Pubnub.py b/Pubnub.py index 5580a51..4709e47 100644 --- a/Pubnub.py +++ b/Pubnub.py @@ -991,6 +991,7 @@ class PubnubBase(object): if ("urlparams" in request): url = url + '?' + "&".join([x + "=" + str(y) for x, y in request[ "urlparams"].items() if y is not None and len(str(y)) > 0]) + #print(url) return url def _channel_registry(self, url=None, params=None, callback=None, error=None): @@ -1684,11 +1685,17 @@ class PubnubCoreAsync(PubnubBase): if channel_list is None: for ch in self.subscriptions: chobj = self.subscriptions[ch] - _invoke(chobj['error'], error) + try: + _invoke(chobj['error'], error, ch) + except TypeError: + _invoke(chobj['error'], error) else: for ch in channel_list: chobj = self.subscriptions[ch] - _invoke(chobj['error'], error) + try: + _invoke(chobj['error'], error, ch) + except TypeError: + _invoke(chobj['error'], error) def _get_channel(): for ch in self.subscriptions: @@ -2107,6 +2114,8 @@ def _requests_request(url, timeout=5): except requests.exceptions.Timeout as error: msg = str(error) return (json.dumps(msg), 0) + #print (resp.text) + #print (resp.status_code) return (resp.text, resp.status_code) -- cgit v1.2.3 From 437124952fd01eac9e8ccc132197ea6f18fdea56 Mon Sep 17 00:00:00 2001 From: Devendra Date: Tue, 9 Jun 2015 01:42:23 +0530 Subject: fixing typo --- Pubnub.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pubnub.py b/Pubnub.py index 4709e47..1cd2a3f 100644 --- a/Pubnub.py +++ b/Pubnub.py @@ -1902,7 +1902,7 @@ class PubnubCoreAsync(PubnubBase): self.leave_channel(channel=channel) # remove channel from STATE - STATE.pop(channel, None) + self.STATE.pop(channel, None) self.CONNECT() -- cgit v1.2.3