Skip to content

Commit

Permalink
feat: add token refresh (#50)
Browse files Browse the repository at this point in the history
Signed-off-by: Panos Vagenas <35837085+vagenas@users.noreply.github.com>
  • Loading branch information
vagenas committed Nov 28, 2022
1 parent 38ca016 commit 8526769
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions MAINTAINERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- Michele Dolfi - [@dolfim-ibm](https://github.com/dolfim-ibm)
- Christoph Auer - [@cau-git](https://github.com/cau-git)
- Panos Vagenas - [@vagenas](https://github.com/vagenas)
- Peter Staar - [@PeterStaar-IBM](https://github.com/PeterStaar-IBM)

Maintainers can be contacted at [deepsearch-core@zurich.ibm.com](mailto:deepsearch-core@zurich.ibm.com).
34 changes: 34 additions & 0 deletions deepsearch/cps/client/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,40 @@ def __init__(self, client: CpsApiClient) -> None:
self.elastic = CpsApiElastic(self)
self.data_indices = CpsApiDataIndices(self)

def refresh_token(self, admin: bool = False):
"""Refresh access token
Args:
admin (bool, optional): controls whether an admin token should be requested. Defaults to False.
Raises:
RuntimeError: raised in case API Key or User is invalid
"""
auth_header_val = f"Bearer {self.client.bearer_token_auth.bearer_token}"
user_api_conf = deepsearch.cps.apis.user.Configuration(
host=f"{self.client.config.host}/api/cps/user/v1",
api_key={"Authorization": auth_header_val},
)
user_api_conf.verify_ssl = self.client.config.verify_ssl
user_api_conf.client_side_validation = False

api = deepsearch.cps.apis.user.UsersApi(
api_client=deepsearch.cps.apis.user.ApiClient(configuration=user_api_conf)
)

try:
access_token = api.get_access_token(options={"admin": admin}).access_token
except deepsearch.cps.apis.user.exceptions.ApiException as e:
raise RuntimeError("The API Key or User is invalid.") from e

bearer_token_auth = DeepSearchBearerTokenAuth(bearer_token=access_token)
ds_config = DeepSearchConfig(
host=self.client.config.host,
auth=bearer_token_auth,
verify_ssl=self.client.config.verify_ssl,
)
self.client = CpsApiClient(ds_config)

@classmethod
def default_from_env(cls) -> "CpsApi":
"""
Expand Down

0 comments on commit 8526769

Please sign in to comment.