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

builder: always refresh OAuth token after getting 401 #102

Merged
merged 1 commit into from
Jun 30, 2022
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
12 changes: 9 additions & 3 deletions plugins/builder/osbuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,9 @@ def oauth_init(self, client_id: str, secret: str, token_url: str):
oauth = OAuth2(client_id, secret, token_url)
self.http.auth = oauth

def oauth_check(self) -> bool:
def oauth_check(self, force_new_token: bool = False) -> bool:
auth = self.http.auth
if auth and auth.token_expired:
if auth and (auth.token_expired or force_new_token):
auth.fetch_token(self.http)
return True

Expand All @@ -365,7 +365,13 @@ def request(self, method: str, url: str, js: Optional[Dict] = None):
self.oauth_check()
res = self.http.request(method, url, json=js)

if res.status_code == 401 and self.oauth_check():
# If 401 is returned, check if oauth is enabled. If it is, get
# a new access token and then retry the request.
# This is needed because even though we always send the request when
# the token is still usable, it might arrive to the server when it's
# already invalid. This retrying mechanism serves as the last resort
# attempt to get the request through.
if res.status_code == 401 and self.oauth_check(True):
res = self.http.request(method, url, json=js)

return res
Expand Down