-
-
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): Customised token verification (#3695)
- Loading branch information
1 parent
8cdc43d
commit 44819d0
Showing
10 changed files
with
687 additions
and
19 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,28 @@ | ||
import dataclasses | ||
from typing import Any, List, Optional, Sequence, Union | ||
|
||
from litestar.security.jwt.token import JWTDecodeOptions, Token | ||
|
||
|
||
@dataclasses.dataclass | ||
class CustomToken(Token): | ||
@classmethod | ||
def decode_payload( | ||
cls, | ||
encoded_token: str, | ||
secret: str, | ||
algorithms: List[str], | ||
issuer: Optional[List[str]] = None, | ||
audience: Union[str, Sequence[str], None] = None, | ||
options: Optional[JWTDecodeOptions] = None, | ||
) -> Any: | ||
payload = super().decode_payload( | ||
encoded_token=encoded_token, | ||
secret=secret, | ||
algorithms=algorithms, | ||
issuer=issuer, | ||
audience=audience, | ||
options=options, | ||
) | ||
payload["sub"] = payload["sub"].split("@", maxsplit=1)[1] | ||
return payload |
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,32 @@ | ||
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 User: | ||
id: str | ||
|
||
|
||
async def retrieve_user_handler(token: Token, connection: ASGIConnection) -> User: | ||
return User(id=token.sub) | ||
|
||
|
||
jwt_auth = JWTAuth[User]( | ||
token_secret=secrets.token_hex(), | ||
retrieve_user_handler=retrieve_user_handler, | ||
accepted_audiences=["https://api.testserver.local"], | ||
accepted_issuers=["https://auth.testserver.local"], | ||
) | ||
|
||
|
||
@get("/") | ||
def handler(request: Request[User, Token, Any]) -> Dict[str, Any]: | ||
return {"id": request.user.id} | ||
|
||
|
||
app = Litestar([handler], 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
Oops, something went wrong.