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: set OAuth token creation time before we fetch it #101

Merged
merged 3 commits into from
Jun 29, 2022
Merged
Changes from 1 commit
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
11 changes: 8 additions & 3 deletions plugins/builder/osbuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,13 @@ class OAuth2(requests.auth.AuthBase):
"""

class Token:
def __init__(self, data):
def __init__(self, data, created):
self.data = data["access_token"]
self.type = data["token_type"]
self.expires_in = int(data["expires_in"])
self.scope = data.get("scope")

self.created = time.time()
self.created = created

@property
def expired(self) -> bool:
Expand All @@ -299,6 +299,11 @@ def token_expired(self) -> bool:
return not self.token or self.token.expired

def fetch_token(self, http: requests.Session):
# Set token creation time here. If we set it after the request was
# fulfilled, it would be offsetted by the time it took the token to
# get from the server here, which can result into us refreshing
# the token late.
token_created = time.time()
data = {
"grant_type": "client_credentials",
"client_id": self.id,
Expand All @@ -312,7 +317,7 @@ def fetch_token(self, http: requests.Session):
raise koji.GenericError(msg) from None

token_data = res.json()
self.token = self.Token(token_data)
self.token = self.Token(token_data, token_created)

def __call__(self, r: requests.Request):
"""Called by requests to obtain authorization"""
Expand Down