| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import typing as t |
| 4 |
|
| 5 |
from globus_sdk import exc |
| 6 |
|
| 7 |
from ._graph_parser import ScopeGraph |
| 8 |
from .representation import Scope |
| 9 |
|
| 10 |
|
| 11 |
class ScopeParser: |
| 12 |
""" |
| 13 |
The ``ScopeParser`` handles the conversion of strings to scopes. |
| 14 |
|
| 15 |
Most interfaces are classmethods, meaning users should prefer usage like |
| 16 |
``ScopeParser.parse("foo")`` |
| 17 |
""" |
| 18 |
|
| 19 |
@classmethod |
| 20 |
def parse(cls, scope_string: str) -> list[Scope]: |
| 21 |
""" |
| 22 |
Parse an arbitrary scope string to a list of scopes. |
| 23 |
|
| 24 |
Zero or more than one scope may be returned, as in the case of an empty string |
| 25 |
or space-delimited scopes. |
| 26 |
|
| 27 |
.. warning:: |
| 28 |
|
| 29 |
Parsing passes through an intermediary representation which treats scopes |
| 30 |
as a graph. This ensures that the behavior of parses matches the treatment |
| 31 |
of scope strings in Globus Auth authorization flows. |
| 32 |
However, this also means that the parsing does not allow for strings which |
| 33 |
represent consent trees with structures in which the same scope appears in |
| 34 |
multiple parts of the tree. |
| 35 |
|
| 36 |
:param scope_string: The string to parse |
| 37 |
""" |
| 38 |
# build the graph intermediate representation |
| 39 |
scope_graph = ScopeGraph.parse(scope_string) |
| 40 |
|
| 41 |
# traverse the graph in a reversed BFS scan |
| 42 |
# |
| 43 |
# this means we'll handle leaf nodes first, and we'll never reach a |
| 44 |
# node before its descendants (dependencies) |
| 45 |
# |
| 46 |
# as we work, build a lookup table for built Scope objects so that we can |
| 47 |
# quickly retrieve elements |
| 48 |
built_scopes: dict[tuple[str, bool], Scope] = {} |
| 49 |
|
| 50 |
for name, optionality in list(scope_graph.breadth_first_walk())[::-1]: |
| 51 |
dependencies: tuple[Scope, ...] = tuple( |
| 52 |
# the lookup in built_scopes here is safe because of the |
| 53 |
# reversed BFS ordering |
| 54 |
built_scopes[(dep_name, dep_optional)] |
| 55 |
for _, dep_name, dep_optional in scope_graph.adjacency_matrix[name] |
| 56 |
) |
| 57 |
|
| 58 |
built_scopes[(name, optionality)] = Scope( |
| 59 |
name, optional=optionality, dependencies=dependencies |
| 60 |
) |
| 61 |
|
| 62 |
# only return the top-level elements from that build process |
| 63 |
# (the roots of the forest-shaped graph) |
| 64 |
return [built_scopes[key] for key in scope_graph.top_level_scopes] |
| 65 |
|
| 66 |
@classmethod |
| 67 |
def merge_scopes(cls, scopes_a: list[Scope], scopes_b: list[Scope]) -> list[Scope]: |
| 68 |
""" |
| 69 |
Given two lists of Scopes, merge them into one list of Scopes by parsing |
| 70 |
them as one combined scope string. |
| 71 |
|
| 72 |
:param scopes_a: list of Scopes to be merged with scopes_b |
| 73 |
:param scopes_b: list of Scopes to be merged with scopes_a |
| 74 |
""" |
| 75 |
# dict of base scope_string: list of scopes with that base scope_string |
| 76 |
return cls.parse( |
| 77 |
" ".join([str(s) for s in scopes_a] + [str(s) for s in scopes_b]) |
| 78 |
) |
| 79 |
|
| 80 |
@classmethod |
| 81 |
def serialize( |
| 82 |
cls, scopes: str | Scope | t.Iterable[str | Scope], *, reject_empty: bool = True |
| 83 |
) -> str: |
| 84 |
""" |
| 85 |
Normalize scopes to a space-separated scope string. |
| 86 |
|
| 87 |
The results of this method are suitable for sending to Globus Auth. |
| 88 |
Scopes are not parsed, merged, or normalized by this method. |
| 89 |
|
| 90 |
:param scopes: A scope string, scope object, or an iterable of scope strings |
| 91 |
and scope objects. |
| 92 |
:param reject_empty: When true (the default), raise an error if the |
| 93 |
scopes serialize to the empty string. |
| 94 |
:returns: A space-separated scope string. |
| 95 |
|
| 96 |
Example usage: |
| 97 |
|
| 98 |
.. code-block:: pycon |
| 99 |
|
| 100 |
>>> ScopeParser.serialize([Scope("foo"), "bar", Scope("qux")]) |
| 101 |
'foo bar qux' |
| 102 |
""" |
| 103 |
scope_iter: t.Iterable[str | Scope] |
| 104 |
if isinstance(scopes, (str, Scope)): |
| 105 |
scope_iter = (scopes,) |
| 106 |
else: |
| 107 |
scope_iter = scopes |
| 108 |
|
| 109 |
result = " ".join(str(scope) for scope in scope_iter) |
| 110 |
if reject_empty and result == "": |
| 111 |
raise exc.GlobusSDKUsageError( |
| 112 |
"'scopes' cannot be the empty string or empty collection." |
| 113 |
) |
| 114 |
return result |