Add self resouce provider
Some checks failed
/ test (push) Successful in 6s
/ build (push) Failing after 14s

This commit is contained in:
phil 2025-02-04 02:27:32 +01:00
parent dc181bd3a8
commit af49242192
7 changed files with 77 additions and 29 deletions

View file

@ -2,6 +2,7 @@ from datetime import datetime
import logging
from httpx import AsyncClient
from jwt.exceptions import ExpiredSignatureError, InvalidTokenError
from .models import User
@ -22,15 +23,20 @@ async def get_resource(resource_id: str, user: User) -> dict:
# but this has to be refined for production
required_scope = f"get:{resource_id}"
# Check if the required scope is in the scopes allowed in userinfo
if user.has_scope(required_scope):
await process(user, resource_id, resp)
else:
## For the showcase, giving a explanation.
## Alternatively, raise HTTP_401_UNAUTHORIZED
resp["sorry"] = (
f"No scope {required_scope} in the access token "
+ "but it is required for accessing this resource."
)
try:
if user.has_scope(required_scope):
await process(user, resource_id, resp)
else:
## For the showcase, giving a explanation.
## Alternatively, raise HTTP_401_UNAUTHORIZED
resp["sorry"] = (
f"No scope {required_scope} in the access token "
+ "but it is required for accessing this resource."
)
except ExpiredSignatureError:
resp["sorry"] = "The token's signature has expired"
except InvalidTokenError:
resp["sorry"] = "The token is invalid"
return resp