| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import abc |
| 4 |
import typing as t |
| 5 |
|
| 6 |
from .representation import Scope |
| 7 |
|
| 8 |
|
| 9 |
class ScopeCollection(abc.ABC): |
| 10 |
""" |
| 11 |
The common base for scope collections. |
| 12 |
|
| 13 |
ScopeCollections act as namespaces with attribute access to get scopes. |
| 14 |
|
| 15 |
They can also be iterated to get all of their defined scopes and provide |
| 16 |
the appropriate resource_server string for use in OAuth2 flows. |
| 17 |
""" |
| 18 |
|
| 19 |
@property |
| 20 |
@abc.abstractmethod |
| 21 |
def resource_server(self) -> str: ... |
| 22 |
|
| 23 |
@abc.abstractmethod |
| 24 |
def __iter__(self) -> t.Iterator[Scope]: ... |
| 25 |
|
| 26 |
|
| 27 |
class StaticScopeCollection(ScopeCollection): |
| 28 |
""" |
| 29 |
A static scope collection is a data container which provides various scopes |
| 30 |
as class attributes. |
| 31 |
|
| 32 |
``resource_server`` must be available as a class attribute. |
| 33 |
""" |
| 34 |
|
| 35 |
resource_server: t.ClassVar[str] |
| 36 |
|
| 37 |
def __iter__(self) -> t.Iterator[Scope]: |
| 38 |
for view in (vars(self).values(), vars(self.__class__).values()): |
| 39 |
for value in view: |
| 40 |
if isinstance(value, Scope): |
| 41 |
yield value |
| 42 |
|
| 43 |
|
| 44 |
class DynamicScopeCollection(ScopeCollection): |
| 45 |
""" |
| 46 |
The base type for dynamic scope collections, where the resource server is |
| 47 |
variable. |
| 48 |
|
| 49 |
The default implementation takes the resource server as the only init-time |
| 50 |
parameter. |
| 51 |
|
| 52 |
:param resource_server: The resource_server to use for all scopes attached |
| 53 |
to this scope collection. |
| 54 |
""" |
| 55 |
|
| 56 |
# DynamicScopeCollection classes are expected to provide |
| 57 |
# the scope names which they provide as a classvar |
| 58 |
# these are often properties for dynamic computation |
| 59 |
_scope_names: t.ClassVar[tuple[str, ...]] |
| 60 |
|
| 61 |
def __init__(self, resource_server: str) -> None: |
| 62 |
self._resource_server = resource_server |
| 63 |
|
| 64 |
def __iter__(self) -> t.Iterator[Scope]: |
| 65 |
for name in self._scope_names: |
| 66 |
value = getattr(self, name) |
| 67 |
if isinstance(value, Scope): |
| 68 |
yield value |
| 69 |
|
| 70 |
@property |
| 71 |
def resource_server(self) -> str: |
| 72 |
return self._resource_server |
| 73 |
|
| 74 |
|
| 75 |
def _urn_scope(resource_server: str, scope_name: str) -> Scope: |
| 76 |
""" |
| 77 |
Convert a short name + resource server string to a scope, in the Globus |
| 78 |
Auth URN format. |
| 79 |
|
| 80 |
Example Usage: |
| 81 |
|
| 82 |
>>> _urn_scope("transfer.api.globus.org", "all") |
| 83 |
Scope('urn:globus:auth:scope:transfer.api.globus.org:all') |
| 84 |
|
| 85 |
:param resource_server: The resource server string. |
| 86 |
:param scope_name: The short name for the scope. |
| 87 |
""" |
| 88 |
return Scope(f"urn:globus:auth:scope:{resource_server}:{scope_name}") |
| 89 |
|
| 90 |
|
| 91 |
def _url_scope(resource_server: str, scope_name: str) -> Scope: |
| 92 |
""" |
| 93 |
Convert a short name + resource server string to a scope, in the Globus |
| 94 |
Auth URL format. |
| 95 |
|
| 96 |
Example Usage: |
| 97 |
|
| 98 |
>>> _url_scope("actions.globus.org", "hello_world") |
| 99 |
Scope('https://auth.globus.org/scopes/actions.globus.org/hello_world') |
| 100 |
|
| 101 |
:param resource_server: The resource server string. |
| 102 |
:param scope_name: The short name for the scope. |
| 103 |
""" |
| 104 |
return Scope(f"https://auth.globus.org/scopes/{resource_server}/{scope_name}") |