| 1 |
from allauth.idp.oidc.models import Token |
| 2 |
|
| 3 |
|
| 4 |
def _is_scope_granted( |
| 5 |
scope: str | list[str] | list[list[str]], |
| 6 |
granted_scope: list[str], |
| 7 |
) -> bool: |
| 8 |
if isinstance(scope, str): |
| 9 |
return scope in granted_scope |
| 10 |
if not isinstance(scope, list): |
| 11 |
raise ValueError |
| 12 |
if len(scope) == 0: |
| 13 |
return True |
| 14 |
list_of_list_of_scopes: list[list[str]] |
| 15 |
if isinstance(scope[0], str): |
| 16 |
list_of_list_of_scopes = [scope] # type: ignore |
| 17 |
else: |
| 18 |
list_of_list_of_scopes = scope # type: ignore |
| 19 |
for list_of_scopes in list_of_list_of_scopes: |
| 20 |
if all(s in granted_scope for s in list_of_scopes): |
| 21 |
return True |
| 22 |
return False |
| 23 |
|
| 24 |
|
| 25 |
def is_scope_granted( |
| 26 |
scope: ( |
| 27 |
None |
| 28 |
| str |
| 29 |
| list[str] |
| 30 |
| list[list[str]] |
| 31 |
| dict[str, str | list[str] | list[list[str]]] |
| 32 |
), |
| 33 |
token: Token | None, |
| 34 |
method: str | None = None, |
| 35 |
) -> bool: |
| 36 |
if scope is None: |
| 37 |
return True |
| 38 |
if isinstance(scope, dict): |
| 39 |
if not method: |
| 40 |
return False |
| 41 |
scope = scope.get(method) |
| 42 |
granted_scope = token.get_scopes() if token else [] |
| 43 |
assert scope is not None # nosec |
| 44 |
return _is_scope_granted(scope, granted_scope) |