Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: improve anon/auth token logic #148

Merged
merged 13 commits into from
Sep 24, 2024
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ env
__pycache__
.python-version
.venv
.vscode
build
dist
22 changes: 12 additions & 10 deletions oras/auth/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,18 @@ def authenticate_request(

h = auth_utils.parse_auth_header(authHeaderRaw)

# First try to request an anonymous token
logger.debug("No Authorization, requesting anonymous token")
anon_token = self.request_anonymous_token(h)
if anon_token:
logger.debug("Successfully obtained anonymous token!")
self.token = anon_token
headers["Authorization"] = "Bearer %s" % self.token
return headers, True

# Next try for logged in token
# if no basic auth, try by request an anonymous token
if not hasattr(self, "_basic_auth"):
logger.debug("No Basic Auth found, requesting anonymous token")
anon_token = self.request_anonymous_token(h)
if anon_token:
logger.debug("Successfully obtained anonymous token!")
self.token = anon_token
headers["Authorization"] = "Bearer %s" % self.token
return headers, True

# try using auth token
logger.debug("requesting Auth Token")
tarilabs marked this conversation as resolved.
Show resolved Hide resolved
token = self.request_token(h)
if token:
self.token = token
Expand Down
4 changes: 3 additions & 1 deletion oras/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,9 @@ def do_request(
:param stream: stream the responses
:type stream: bool
"""
# Make the request and return to calling function, unless requires auth
# Make the request and return to calling function, but attempt to use auth token if previously obtained
if headers is not None and isinstance(self.auth, oras.auth.TokenAuth):
headers.update(self.auth.get_auth_header())
response = self.session.request(
method,
url,
Expand Down
Loading