From c3ff6097da05d42a6967bab742288e43570196e4 Mon Sep 17 00:00:00 2001 From: Hillel Arnold Date: Wed, 17 Aug 2022 11:58:05 -0400 Subject: [PATCH 1/2] add authorize_oauth method --- electronbonder/client.py | 17 +++++++++++++++++ requirements.txt | 1 + tests/test_client.py | 19 ++++++++++++++++++- 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/electronbonder/client.py b/electronbonder/client.py index 3528205..ae61f70 100755 --- a/electronbonder/client.py +++ b/electronbonder/client.py @@ -1,8 +1,10 @@ import json from urllib.parse import urlparse +from oauthlib.oauth2 import BackendApplicationClient from requests import Session from requests.adapters import HTTPAdapter +from requests_oauthlib import OAuth2Session from six import add_metaclass from urllib3.util.retry import Retry @@ -90,6 +92,21 @@ def authorize(self): session_token) return session_token + def authorize_oauth(self): + """Authorizes the client using a configured OAuth provider.""" + try: + client = BackendApplicationClient( + client_id=self.config["oauth_client_id"]) + oauth = OAuth2Session(client=client) + token = oauth.fetch_token( + token_url=f"{self.config['oauth_client_baseurl']}/oauth2/token", + client_id=self.config["oauth_client_id"], + client_secret=self.config["oauth_client_secret"]) + except Exception as e: + raise ElectronBondAuthError( + f"Failed to authorize OAuth with message: {str(e)}") + self.session.headers["Authorization"] = token["access_token"] + def get_paged(self, url, *args, **kwargs): """get list of json objects from urls of paged items""" params = {"page": 1} diff --git a/requirements.txt b/requirements.txt index 536a1e9..e646cf2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ requests~=2.28 +requests-oauthlib~=1.3 six~=1.16 diff --git a/tests/test_client.py b/tests/test_client.py index 62cdc3f..9396096 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -5,6 +5,9 @@ from electronbonder.client import ElectronBond, ElectronBondAuthError BASEURL = "http://localhost:8007" +OAUTH_BASEURL = "https://awscognito.com" +OAUTH_CLIENT_ID = "123456789" +OAUTH_CLIENT_SECRET = "987654321" PATH = "custom/path" @@ -14,7 +17,10 @@ def setUp(self): self.client = ElectronBond( baseurl=BASEURL, username="username", - password="password") + password="password", + oauth_client_baseurl=OAUTH_BASEURL, + oauth_client_id=OAUTH_CLIENT_ID, + oauth_client_secret=OAUTH_CLIENT_SECRET) @patch("requests.Session.post") def test_authorize(self, mock_post): @@ -29,6 +35,17 @@ def test_authorize(self, mock_post): mock_post.return_value.status_code = 404 self.client.authorize() + @patch("requests_oauthlib.OAuth2Session.fetch_token") + def test_authorize_oauth(self, mock_token): + token = "12345" + mock_token.return_value = {"access_token": token} + self.client.authorize_oauth() + self.assertEqual(self.client.session.headers["Authorization"], token) + mock_token.assert_called_with( + token_url=f"{OAUTH_BASEURL}/oauth2/token", + client_id=OAUTH_CLIENT_ID, + client_secret=OAUTH_CLIENT_SECRET) + @patch("requests.Session.get") def test_get_paged(self, mock_get): list(self.client.get_paged(PATH)) From 1782140e5e54af46cdd0ff1819542aec89c0d431 Mon Sep 17 00:00:00 2001 From: Hillel Arnold Date: Wed, 17 Aug 2022 12:02:18 -0400 Subject: [PATCH 2/2] version bump --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index e89bc68..9eebf55 100755 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ long_description_content_type="text/markdown", author="Rockefeller Archive Center", author_email="archive@rockarch.org", - version="1.1", + version="2.0", license='MIT', packages=find_packages(), zip_safe=False,