This repository has been archived by the owner on Jul 7, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): add database method for retrieving by field value
Signed-off-by: CyberFlame <[email protected]>
- Loading branch information
1 parent
68d8499
commit f7d3767
Showing
5 changed files
with
85 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,21 @@ | ||
from datetime import UTC, datetime | ||
from datetime import UTC, datetime, timedelta | ||
from enum import Enum | ||
from typing import Annotated, Tuple, Type | ||
|
||
import sentry_sdk | ||
from fastapi import Depends, FastAPI, Request, status | ||
from fastapi import Depends, FastAPI, Request, status, HTTPException | ||
from fastapi.responses import HTMLResponse, RedirectResponse, Response | ||
from fastapi.staticfiles import StaticFiles | ||
from fastapi.security import OAuth2PasswordBearer | ||
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm | ||
from fastapi.templating import Jinja2Templates | ||
from sqlalchemy.exc import IntegrityError, NoResultFound | ||
from sqlalchemy.ext.asyncio import AsyncSession | ||
from pydantic import BaseModel | ||
|
||
import models | ||
import utils | ||
from models import Base, Game | ||
from models import Base, Game, PydanticUser, Token | ||
from utils import ACCESS_TOKEN_EXPIRE_MINUTES, authenticate_user, create_access_token, get_current_active_user | ||
|
||
sentry_sdk.init( | ||
dsn="https://[email protected]/4504873492480000", | ||
|
@@ -206,5 +207,42 @@ async def get_match(request: Request, match_id: int, session: Session): | |
:param match_id: | ||
:return: | ||
""" | ||
match = await db.retrieve(session, models.Match, match_id) | ||
try: | ||
match = await db.retrieve(session, models.Match, match_id) | ||
except NoResultFound: | ||
# TODO: adjust with a proper page regarding no match found with id | ||
return Response(status_code=status.HTTP_404_NOT_FOUND) | ||
return templates.TemplateResponse("matches.html", {"request": request, "id": match_id, "match": match}) | ||
|
||
|
||
@app.post("/token", response_model=Token) | ||
async def login_for_access_token( | ||
form_data: Annotated[OAuth2PasswordRequestForm, Depends()], | ||
session: Session, | ||
): | ||
user = await authenticate_user(session, form_data.username, form_data.password) | ||
if not user: | ||
raise HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail="Incorrect username or password", | ||
headers={"WWW-Authenticate": "Bearer"}, | ||
) | ||
access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES) | ||
access_token = create_access_token( | ||
data={"sub": user.username}, expires_delta=access_token_expires | ||
) | ||
return {"access_token": access_token, "token_type": "bearer"} | ||
|
||
|
||
@app.get("/users/me/", response_model=PydanticUser) | ||
async def read_users_me( | ||
current_user: Annotated[PydanticUser, Depends(get_current_active_user)] | ||
): | ||
return current_user | ||
|
||
|
||
@app.get("/users/me/items/") | ||
async def read_own_items( | ||
current_user: Annotated[PydanticUser, Depends(get_current_active_user)] | ||
): | ||
return [{"item_id": "Foo", "owner": current_user.username}] |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ | |
""" | ||
|
||
from .db_utils import * # noqa F401 | ||
from .auth import * # noqa F401 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters