-
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.
- Loading branch information
Showing
4 changed files
with
73 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from functools import wraps | ||
from http import HTTPStatus | ||
from typing import Any, Awaitable, Callable, Dict, List, Optional | ||
|
||
from zara.login.jwt import verify_jwt | ||
from zara.types.http import Http | ||
|
||
|
||
def auth_required( | ||
permissions: Optional[List[str]] = None, | ||
roles: Optional[List[str]] = None, | ||
): | ||
def decorator(func: Callable[..., Awaitable[None]]): | ||
@wraps(func) | ||
async def wrapper(scope: Dict[str, Any], receive: Callable, send: Callable): | ||
headers = dict(scope.get("headers", [])) | ||
authorization = headers.get(b"authorization", b"").decode() | ||
|
||
if not authorization.startswith("Bearer "): | ||
await send(Http.Response.Start(status=HTTPStatus.FORBIDDEN)) | ||
await send( | ||
Http.Response.Detail( | ||
message="Authorization header missing or malformed." | ||
) | ||
) | ||
return | ||
|
||
token = authorization[7:] | ||
jwt_payload = verify_jwt(token) | ||
if jwt_payload is None: | ||
await send(Http.Response.Start(status=HTTPStatus.FORBIDDEN)) | ||
await send(Http.Response.Detail(message="Invalid token")) | ||
return | ||
|
||
user_permissions = jwt_payload.get("permissions", []) | ||
user_roles = jwt_payload.get("roles", []) | ||
|
||
if permissions: | ||
if not all(p in user_permissions for p in permissions): | ||
await send(Http.Response.Start(status=HTTPStatus.FORBIDDEN)) | ||
await send(Http.Response.Detail(message="Insufficient permissions")) | ||
return | ||
|
||
if roles: | ||
if not any(r in user_roles for r in roles): | ||
await send(Http.Response.Start(status=HTTPStatus.FORBIDDEN)) | ||
await send(Http.Response.Detail(message="Insufficient roles")) | ||
return | ||
|
||
await func(scope, receive, send) | ||
|
||
return wrapper | ||
|
||
return decorator |
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
Empty file.