forked from pythonitalia/pythonit-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermissions.py
More file actions
52 lines (38 loc) · 1.58 KB
/
permissions.py
File metadata and controls
52 lines (38 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from typing import Any
import jwt
from pythonit_toolkit.headers import SERVICE_JWT_HEADER
from pythonit_toolkit.pastaporto.entities import Credential
from pythonit_toolkit.pastaporto.tokens import decode_service_to_service_token
from strawberry.permission import BasePermission
from strawberry.types import Info
class IsAuthenticated(BasePermission):
message = "Not authenticated"
def has_permission(self, source, info, **kwargs):
return Credential.AUTHENTICATED in info.context.request.auth.scopes
def IsService(allowed_callers: list[str], secret: str, service: str):
if not allowed_callers:
raise ValueError("No callers allowed specified")
if not secret:
raise ValueError("JWT secret cannot be empty")
if not service:
raise ValueError("Current service name cannot be empty")
secret = str(secret)
class _IsService(BasePermission):
message = "Forbidden"
def has_permission(self, source: Any, info: Info, **kwargs) -> bool:
token = info.context.request.headers.get(SERVICE_JWT_HEADER)
for caller in allowed_callers:
try:
decode_service_to_service_token(
token, secret, issuer=caller, audience=service
)
return True
except (
jwt.DecodeError,
jwt.InvalidIssuerError,
jwt.ExpiredSignatureError,
jwt.InvalidAudienceError,
):
pass
return False
return _IsService