Skip to content

Commit

Permalink
get_secret_key, put_secret_key, readme
Browse files Browse the repository at this point in the history
  • Loading branch information
haochenpan committed Dec 12, 2023
1 parent 7cf9049 commit c519d35
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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("<secret-key-from-first-machine>")
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
Expand Down
37 changes: 32 additions & 5 deletions diaspora_event_sdk/sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) "
Expand All @@ -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):
"""
Expand Down
2 changes: 1 addition & 1 deletion diaspora_event_sdk/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.18"
__version__ = "0.0.19"
6 changes: 6 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -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()


0 comments on commit c519d35

Please sign in to comment.