diff --git a/README.md b/README.md index 7853991..de2463f 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,14 @@ c = GlobusClient() topic = "topic-" + c.subject_openid[-12:] print(c.register_topic(topic)) print(c.list_topics()) + +# registration_status = c.register_topic(topic) +# print(registration_status) +# assert registration_status["status"] in ["no-op", "success"] + +# registered_topics = c.list_topics() +# print(registered_topics) +# assert topic in registered_topics["topics"] ``` Register a topic also creates it, if the topic previously does not exist. @@ -119,6 +127,26 @@ print(c.retrieve_key()) ``` ### Advanced Usage +#### Key Migration +In case you want to use the same credential (OpenID, secret key) on the second macine, but a call to create_key() invalidates all previous keys so you cannot rely on this API for generating a new key for the new machine, while keeping the old key on the old machine valid. Starting at 0.0.19, you can call the following code to retrieve the active secret key on the first machine and store it to the second machine, so that both machines hold valid keys. + +On the first machine, call: +```python +from diaspora_event_sdk import Client as GlobusClient +c = GlobusClient() +print(c.get_secret_key()) # note down the secret key +``` + +On the second machine, call: +```python +from diaspora_event_sdk import Client as GlobusClient +from diaspora_event_sdk import block_until_ready + +c = GlobusClient() +c.put_secret_key("") +assert block_until_ready() # should unblock immediately +``` +Note that a call to create_key() on any machine would invalidate the current key, on both machines. #### Password Refresh In case that you need to invalidate all previously issued passwords and generate a new one, call the `create_key` method from the `Client` class diff --git a/diaspora_event_sdk/sdk/client.py b/diaspora_event_sdk/sdk/client.py index 5ab7581..6106ef7 100644 --- a/diaspora_event_sdk/sdk/client.py +++ b/diaspora_event_sdk/sdk/client.py @@ -45,10 +45,7 @@ def create_key(self): tokens = self.login_manager._token_storage.get_token_data( DIASPORA_RESOURCE_SERVER ) - tokens["access_key"], tokens["secret_key"] = ( - resp["access_key"], - resp["secret_key"], - ) + tokens["secret_key"] = resp["secret_key"] with self.login_manager._access_lock: self.login_manager._token_storage._connection.executemany( "REPLACE INTO token_storage(namespace, resource_server, token_data_json) " @@ -72,11 +69,41 @@ def retrieve_key(self): tokens = self.login_manager._token_storage.get_token_data( DIASPORA_RESOURCE_SERVER ) - if tokens is None or "access_key" not in tokens or "secret_key" not in tokens: + if tokens is None or "secret_key" not in tokens: return self.create_key() else: return {"username": self.subject_openid, "password": tokens["secret_key"]} + @requires_login + def get_secret_key(self): + tokens = self.login_manager._token_storage.get_token_data( + DIASPORA_RESOURCE_SERVER + ) + if tokens is None or "secret_key" not in tokens: + return None + else: + return tokens["secret_key"] + + @requires_login + def put_secret_key(self, secret_key): + tokens = self.login_manager._token_storage.get_token_data( + DIASPORA_RESOURCE_SERVER + ) + tokens["secret_key"] = secret_key + with self.login_manager._access_lock: + self.login_manager._token_storage._connection.executemany( + "REPLACE INTO token_storage(namespace, resource_server, token_data_json) " + "VALUES(?, ?, ?)", + [ + ( + self.login_manager._token_storage.namespace, + DIASPORA_RESOURCE_SERVER, + json.dumps(tokens), + ) + ], + ) + self.login_manager._token_storage._connection.commit() + @requires_login def list_topics(self): """ diff --git a/diaspora_event_sdk/version.py b/diaspora_event_sdk/version.py index f18e5d0..a11f0b4 100644 --- a/diaspora_event_sdk/version.py +++ b/diaspora_event_sdk/version.py @@ -1 +1 @@ -__version__ = "0.0.18" +__version__ = "0.0.19" diff --git a/test.py b/test.py new file mode 100644 index 0000000..8dba0ac --- /dev/null +++ b/test.py @@ -0,0 +1,6 @@ +from diaspora_event_sdk import Client as GlobusClient +from diaspora_event_sdk import block_until_ready +c = GlobusClient() +assert block_until_ready() + +