Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions obstore/python/obstore/auth/earthdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,16 @@ def _refresh_with_token(self, token: str) -> Mapping[str, str]:

def _refresh_with_basic_auth(
self,
auth: tuple[str, str] | None,
auth: tuple[str, str] | None = None,
) -> Mapping[str, str]:
with self.session.get(self._credentials_url, allow_redirects=False) as r:
r.raise_for_status()
location = r.headers["location"]

if (location := self.session.get_redirect_target(r)) is None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The := operator here always confuses me because it makes me think that location is scoped just to this if block, while we're using location later 🙃

# We were NOT redirected, indicating that we requested a refresh
# prior to expiry of our previous token, so we simply received
# a new token directly.
return r.json()

# We were redirected, so we must use basic auth credentials with the
# redirect location. If the host of the redirect is the same host we have
Expand Down Expand Up @@ -250,7 +255,7 @@ class NasaEarthdataAsyncCredentialProvider:
Examples:
```py
import obstore
from obstore.auth.earthdata import NasaEarthdataCredentialProvider
from obstore.auth.earthdata import NasaEarthdataAsyncCredentialProvider

# Obtain an S3 credentials URL and an S3 data/download URL, typically
# via metadata returned from a NASA CMR collection or granule query.
Expand Down Expand Up @@ -360,14 +365,18 @@ async def _refresh_with_token(self, token: str) -> Mapping[str, str]:

async def _refresh_with_basic_auth(
self,
auth: aiohttp.BasicAuth | None,
auth: aiohttp.BasicAuth | None = None,
) -> Mapping[str, str]:
async with self.session.get(
self._credentials_url,
allow_redirects=False,
raise_for_status=True,
) as r:
location = r.headers["location"]
if (location := r.headers.get("location")) is None:
# We were NOT redirected, indicating that we requested a refresh
# prior to expiry of our previous token, so we simply received
# a new token directly.
return await r.json(content_type=None)

# We were redirected, so we must use basic auth credentials with the
# redirect location. If the host of the redirect is the same host we have
Expand Down
Loading