Skip to content

Commit

Permalink
Adds route to create test user for dev purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
ewainberg committed Jan 28, 2025
1 parent 528ee92 commit 1ceada6
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: MIT
# Copyright (c) 2024 Collegiate Cyber Defense Club
import logging
import os
import uuid
from typing import Optional
from urllib.parse import urlparse
Expand Down Expand Up @@ -397,3 +398,53 @@ async def logout(request: Request):
@app.get("/favicon.ico", include_in_schema=False)
async def favicon():
return FileResponse("./app/static/favicon.ico")


# This check is a little hacky and needs to be documented in the dev environment set up
# If it's run under docker, the -e flag should set the env variable, but if its local you have to set it yourself
# Use 'export ENV=development' to set the env variable
if os.getenv("ENV") == "development":

@app.get("/dev/user")
async def create_dev_user(request: Request, session: Session = Depends(get_session)):
if request.client.host not in ["127.0.0.1", "localhost"]:
return Errors.generate(
request,
403,
"Forbidden",
essay="This endpoint is only available on localhost.",
)

# Generate random user data
user_id = uuid.uuid4()
discord_id = str(uuid.uuid4())

user = UserModel(
id=user_id,
discord_id=discord_id,
)

discord_user = DiscordModel(username=f"devuser-{user_id}", email=f"[email protected]", user_id=user_id, user=user)

session.add(user)
session.commit()
session.refresh(user)

session.add(discord_user)
session.commit()
session.refresh(discord_user)

# Create JWT token for the user
bearer = Authentication.create_jwt(user)
rr = RedirectResponse("/profile", status_code=status.HTTP_302_FOUND)
max_age = Settings().jwt.lifetime_sudo
rr.set_cookie(
key="token",
value=bearer,
httponly=True,
samesite="lax",
secure=False,
max_age=max_age,
)

Check warning

Code scanning / CodeQL

Failure to use secure cookies Medium

Cookie is added without the Secure attribute properly set.

return rr

0 comments on commit 1ceada6

Please sign in to comment.