Skip to content

Commit

Permalink
upload_tokens: Fix another detached database session issue
Browse files Browse the repository at this point in the history
  • Loading branch information
barthalion committed Feb 5, 2025
1 parent 05b2fea commit f3f8aff
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions backend/app/routes/upload_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ def create_upload_token(
issued_to = login.user
expires_at = issued_at + datetime.timedelta(days=180)

# Create the row in the database
token = models.UploadToken(
comment=request.comment,
app_id=app_id,
Expand All @@ -183,11 +182,30 @@ def create_upload_token(
issued_to=issued_to.id,
expires_at=expires_at,
)

with get_db("writer") as db:
db.session.add(token)
db.session.commit()
token_id = token.id

encoded = jwt.encode(
{
"jti": f"backend_{token_id}",
"sub": "build",
"scope": request.scopes,
"repos": request.repos,
"prefixes": [app_id],
"iat": issued_at,
"exp": expires_at,
"token_type": "app",
},
base64.b64decode(config.settings.flat_manager_build_secret),
algorithm="HS256",
)

if config.settings.flat_manager_build_token_prefix is not None:
encoded = config.settings.flat_manager_build_token_prefix + encoded

if app_metadata := get_json_key(f"apps:{app_id}"):
app_name = app_metadata["name"]
else:
Expand All @@ -212,25 +230,6 @@ def create_upload_token(

worker.send_email_new.send(payload)

# Create the JWT
encoded = jwt.encode(
{
"jti": jti(token),
"sub": "build",
"scope": request.scopes,
"repos": request.repos,
"prefixes": [app_id],
"iat": issued_at,
"exp": expires_at,
"token_type": "app",
},
base64.b64decode(config.settings.flat_manager_build_secret),
algorithm="HS256",
)

if config.settings.flat_manager_build_token_prefix is not None:
encoded = config.settings.flat_manager_build_token_prefix + encoded

return NewTokenResponse(
token=encoded,
details=_token_response(token, issued_to.display_name),
Expand Down

0 comments on commit f3f8aff

Please sign in to comment.