Skip to content

Commit 317e844

Browse files
committed
Fix bug in refresh access token
1 parent 4c6a102 commit 317e844

File tree

6 files changed

+95
-7
lines changed

6 files changed

+95
-7
lines changed

manual_tests/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
These tests are designed to be run manually against the API.

manual_tests/__init__.py

Whitespace-only changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from aiohttp import ClientSession
2+
from environs import Env
3+
4+
from pdap_access_manager.AccessManager import AccessManager, RequestInfo, RequestType, Namespaces
5+
6+
7+
async def test_access_manager_refresh_access_token():
8+
env = Env()
9+
env.read_env()
10+
11+
async with ClientSession() as session:
12+
access_manager = AccessManager(
13+
email=env.str("PDAP_EMAIL"),
14+
password=env.str("PDAP_PASSWORD"),
15+
session=session,
16+
)
17+
url = access_manager.build_url(
18+
namespace=Namespaces.PERMISSIONS,
19+
)
20+
jwt_header_pre_refresh = await access_manager.jwt_header()
21+
await access_manager.refresh_access_token()
22+
jwt_header_post_refresh = await access_manager.jwt_header()
23+
assert jwt_header_pre_refresh != jwt_header_post_refresh

pdap_access_manager/AccessManager.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class RequestType(Enum):
2323
class Namespaces(Enum):
2424
AUTH = "auth"
2525
LOCATIONS = "locations"
26+
PERMISSIONS = "permissions"
2627

2728

2829
class CustomHTTPException(Exception):
@@ -60,6 +61,10 @@ def url_with_query_params(self) -> str:
6061
url.query_params.update(self.params)
6162
return url.to_text()
6263

64+
def authorization_from_token(token: str) -> dict:
65+
return {
66+
"Authorization": f"Bearer {token}"
67+
}
6368

6469
DEFAULT_PDAP_API_URL = "https://data-sources.pdap.io/api"
6570

@@ -147,12 +152,10 @@ async def refresh_access_token(self):
147152
namespace=Namespaces.AUTH,
148153
subdomains=["refresh-session"],
149154
)
150-
refresh_token = await self.refresh_token
151155
rqi = RequestInfo(
152156
type_=RequestType.POST,
153157
url=url,
154-
json_={"refresh_token": refresh_token},
155-
headers=await self.jwt_header()
158+
headers=await self.refresh_jwt_header()
156159
)
157160
rsi = await self.make_request(rqi, allow_retry=False)
158161
data = rsi.data
@@ -176,6 +179,7 @@ async def make_request(self, ri: RequestInfo, allow_retry: bool = True) -> Respo
176179
)
177180
except ClientResponseError as e:
178181
if e.status == 401 and allow_retry: # Unauthorized, token expired?
182+
print("401 error, refreshing access token...")
179183
await self.refresh_access_token()
180184
return await self.make_request(ri, allow_retry=False)
181185
else:
@@ -213,9 +217,15 @@ async def jwt_header(self) -> dict:
213217
Returns: Dictionary of Bearer Authorization with JWT key
214218
"""
215219
access_token = await self.access_token
216-
return {
217-
"Authorization": f"Bearer {access_token}"
218-
}
220+
return authorization_from_token(access_token)
221+
222+
async def refresh_jwt_header(self) -> dict:
223+
"""
224+
Retrieve JWT header
225+
Returns: Dictionary of Bearer Authorization with JWT key
226+
"""
227+
refresh_token = await self.refresh_token
228+
return authorization_from_token(refresh_token)
219229

220230
async def api_key_header(self) -> dict:
221231
"""

poetry.lock

Lines changed: 54 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ boltons = "^25.0.0"
2525
[tool.poetry.group.dev.dependencies]
2626
pytest = "^8.3.5"
2727
pytest-asyncio = "^0.26.0"
28+
environs = "^14.1.1"
2829

2930
[build-system]
3031
requires = ["poetry-core"]

0 commit comments

Comments
 (0)