-
-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(JWT): Custom JWT payload classes (#3692)
* feat(JWT): Custom JWT payload classes
- Loading branch information
1 parent
23ba256
commit 9cff0a4
Showing
7 changed files
with
168 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import dataclasses | ||
import secrets | ||
from typing import Any, Dict | ||
|
||
from litestar import Litestar, Request, get | ||
from litestar.connection import ASGIConnection | ||
from litestar.security.jwt import JWTAuth, Token | ||
|
||
|
||
@dataclasses.dataclass | ||
class CustomToken(Token): | ||
token_flag: bool = False | ||
|
||
|
||
@dataclasses.dataclass | ||
class User: | ||
id: str | ||
|
||
|
||
async def retrieve_user_handler(token: CustomToken, connection: ASGIConnection) -> User: | ||
return User(id=token.sub) | ||
|
||
|
||
TOKEN_SECRET = secrets.token_hex() | ||
|
||
jwt_auth = JWTAuth[User]( | ||
token_secret=TOKEN_SECRET, | ||
retrieve_user_handler=retrieve_user_handler, | ||
token_cls=CustomToken, | ||
) | ||
|
||
|
||
@get("/") | ||
def handler(request: Request[User, CustomToken, Any]) -> Dict[str, Any]: | ||
return {"id": request.user.id, "token_flag": request.auth.token_flag} | ||
|
||
|
||
app = Litestar(middleware=[jwt_auth.middleware]) |
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
Oops, something went wrong.