Skip to content

Commit

Permalink
implement review comment about anon/req token
Browse files Browse the repository at this point in the history
from: #148 (comment)

> And if the basic auth is there, skip over asking for an anon token

as it stands, in case the basic auth are present,
these are exchanged for the request token.

Signed-off-by: tarilabs <matteo.mortari@gmail.com>
  • Loading branch information
tarilabs committed Sep 23, 2024
1 parent 23ee55a commit e164a49
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
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")
token = self.request_token(h)
if token:
self.token = token
Expand Down

0 comments on commit e164a49

Please sign in to comment.