-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Database 1. AsyncAttrs declared in base class instead of children. 2. expire_on_commit=False, no need to refresh session * UUID replaces str for Base id For authorize we log the user_id and not the token. * Created dependencies logic for auth
- Loading branch information
1 parent
cc38dad
commit 2feb78e
Showing
16 changed files
with
103 additions
and
65 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,36 @@ | ||
from fastapi import APIRouter, Depends | ||
from uuid import UUID | ||
|
||
from fastapi import APIRouter | ||
|
||
from app.database.dependencies import sessDep | ||
from app.models.auth.functions import authorize, authorize_and_load, authorize_limited | ||
from app.models.auth.token import TokenDecode | ||
from app.models.auth.dependencies import authDep, authDepLimit, authLoadDep | ||
from app.models.post import Post | ||
from app.models.post.schemas import PostIn, PostOut | ||
from app.models.user import User | ||
|
||
router = APIRouter(prefix="/post", tags=["Post"]) | ||
|
||
|
||
@router.post("", response_model=PostOut, status_code=201) | ||
async def create_post( | ||
post_in: PostIn, | ||
async_session: sessDep, | ||
token: TokenDecode = Depends(authorize), | ||
): | ||
async def create_post(post_in: PostIn, async_session: sessDep, token: authDep): | ||
return await Post(**post_in.model_dump(), user_id=token.id).save(async_session) | ||
|
||
|
||
@router.get("", response_model=list[PostOut], status_code=200) | ||
async def get_posts(user: User = Depends(authorize_and_load)): | ||
async def get_posts(user: authLoadDep): | ||
return await user.awaitable_attrs.posts | ||
|
||
|
||
@router.get("/{id}", response_model=PostOut, status_code=200) | ||
async def get_post( | ||
async_session: sessDep, id: str, token: TokenDecode = Depends(authorize) | ||
): | ||
async def get_post(async_session: sessDep, id: UUID, token: authDep): | ||
return await Post.find(async_session, id=id, user_id=token.id, raise_=True) | ||
|
||
|
||
@router.get("/rate-limited/{id}", response_model=PostOut, status_code=200) | ||
async def get_post_rate_limited( | ||
async_session: sessDep, id: str, token: TokenDecode = Depends(authorize_limited) | ||
): | ||
async def get_post_rate_limited(async_session: sessDep, id: UUID, token: authDepLimit): | ||
return await Post.find(async_session, id=id, user_id=token.id, raise_=True) | ||
|
||
|
||
@router.delete("/{id}", status_code=204) | ||
async def delete_post( | ||
async_session: sessDep, id: str, token: TokenDecode = Depends(authorize) | ||
): | ||
async def delete_post(async_session: sessDep, id: UUID, token: authDep): | ||
post = await Post.find(async_session, id=id, user_id=token.id, raise_=True) | ||
await post.delete(async_session) # type: ignore | ||
await post.delete(async_session) |
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
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
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from typing import Annotated | ||
|
||
from fastapi import Depends, Security | ||
|
||
from app.models.auth.functions import ( | ||
authenticate, | ||
authenticate_and_token, | ||
authorize, | ||
authorize_and_load, | ||
authorize_limited, | ||
) | ||
from app.models.auth.role import Role | ||
from app.models.auth.schemas import TokenDecode | ||
from app.models.user import User | ||
|
||
authDep = Annotated[TokenDecode, Depends(authorize)] | ||
|
||
userDep = Annotated[User, Depends(authenticate)] | ||
|
||
authLoadDep = Annotated[User, Depends(authorize_and_load)] | ||
|
||
authTokenDep = Annotated[User, Depends(authenticate_and_token)] | ||
|
||
resetLoadDep = Annotated[User, Security(authorize_and_load, scopes=[Role.RESET])] | ||
|
||
authDepLimit = Annotated[TokenDecode, Depends(authorize_limited)] |
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
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
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
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