| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import logging |
| 4 |
import typing as t |
| 5 |
|
| 6 |
from globus_sdk._internal import guards |
| 7 |
|
| 8 |
log = logging.getLogger(__name__) |
| 9 |
|
| 10 |
|
| 11 |
class ErrorInfo: |
| 12 |
""" |
| 13 |
Errors may contain "containers" of data which are testable (define ``__bool__``). |
| 14 |
When they have data, they should ``bool()`` as ``True`` |
| 15 |
""" |
| 16 |
|
| 17 |
_has_data: bool |
| 18 |
|
| 19 |
def __bool__(self) -> bool: |
| 20 |
return self._has_data |
| 21 |
|
| 22 |
def __str__(self) -> str: |
| 23 |
if self: |
| 24 |
attrmap = ", ".join( |
| 25 |
[f"{k}={v}" for k, v in self.__dict__.items() if not k.startswith("_")] |
| 26 |
) |
| 27 |
else: |
| 28 |
attrmap = ":" |
| 29 |
return f"{self.__class__.__name__}({attrmap})" |
| 30 |
|
| 31 |
|
| 32 |
class AuthorizationParameterInfo(ErrorInfo): |
| 33 |
""" |
| 34 |
AuthorizationParameterInfo objects may contain information about the |
| 35 |
'authorization_parameters' of an error. They test as truthy when the error has valid |
| 36 |
'authorization_parameters' data. |
| 37 |
|
| 38 |
:ivar session_message: A message from the server |
| 39 |
:vartype session_message: str, optional |
| 40 |
|
| 41 |
:ivar session_required_identities: A list of identity IDs as strings which are being |
| 42 |
requested by the server |
| 43 |
:vartype session_required_identities: list of str, optional |
| 44 |
|
| 45 |
:ivar session_required_single_domain: A list of domains which are being requested by |
| 46 |
the server ("single domain" because the user should choose one) |
| 47 |
:vartype session_required_single_domain: list of str, optional |
| 48 |
|
| 49 |
:ivar session_required_policies: A list of policies required for the session. |
| 50 |
:vartype session_required_policies: list of str, optional |
| 51 |
|
| 52 |
:ivar session_required_mfa: Whether MFA is required for the session. |
| 53 |
:vartype session_required_mfa: bool, optional |
| 54 |
|
| 55 |
**Examples** |
| 56 |
|
| 57 |
>>> try: |
| 58 |
>>> ... # something |
| 59 |
>>> except GlobusAPIError as err: |
| 60 |
>>> # get a parsed AuthorizationParameterInfo object, and check if it's truthy |
| 61 |
>>> authz_params = err.info.authorization_parameters |
| 62 |
>>> if not authz_params: |
| 63 |
>>> raise |
| 64 |
>>> # whatever handling code is desired... |
| 65 |
>>> print("got authz params:", authz_params) |
| 66 |
""" |
| 67 |
|
| 68 |
def __init__(self, error_data: dict[str, t.Any]) -> None: |
| 69 |
# data is there if this key is present and it is a dict |
| 70 |
self._has_data = isinstance(error_data.get("authorization_parameters"), dict) |
| 71 |
data = t.cast( |
| 72 |
t.Dict[str, t.Any], error_data.get("authorization_parameters", {}) |
| 73 |
) |
| 74 |
|
| 75 |
self.session_message: str | None = self._parse_session_message(data) |
| 76 |
|
| 77 |
self.session_required_identities: list[str] | None = ( |
| 78 |
self._parse_session_required_identities(data) |
| 79 |
) |
| 80 |
|
| 81 |
self.session_required_single_domain: list[str] | None = ( |
| 82 |
self._parse_session_required_single_domain(data) |
| 83 |
) |
| 84 |
|
| 85 |
self.session_required_policies: list[str] | None = ( |
| 86 |
self._parse_session_required_policies(data) |
| 87 |
) |
| 88 |
|
| 89 |
self.session_required_mfa = self._parse_session_required_mfa(data) |
| 90 |
|
| 91 |
def _parse_session_message(self, data: dict[str, t.Any]) -> str | None: |
| 92 |
session_message = data.get("session_message") |
| 93 |
if isinstance(session_message, str): |
| 94 |
return session_message |
| 95 |
elif session_message is not None: |
| 96 |
self._warn_type("session_message", "str", session_message) |
| 97 |
return None |
| 98 |
|
| 99 |
def _parse_session_required_identities( |
| 100 |
self, data: dict[str, t.Any] |
| 101 |
) -> list[str] | None: |
| 102 |
session_required_identities = data.get("session_required_identities") |
| 103 |
if guards.is_list_of(session_required_identities, str): |
| 104 |
return session_required_identities |
| 105 |
elif session_required_identities is not None: |
| 106 |
self._warn_type( |
| 107 |
"session_required_identities", |
| 108 |
"list[str]", |
| 109 |
session_required_identities, |
| 110 |
) |
| 111 |
return None |
| 112 |
|
| 113 |
def _parse_session_required_single_domain( |
| 114 |
self, data: dict[str, t.Any] |
| 115 |
) -> list[str] | None: |
| 116 |
session_required_single_domain = data.get("session_required_single_domain") |
| 117 |
if guards.is_list_of(session_required_single_domain, str): |
| 118 |
return session_required_single_domain |
| 119 |
elif session_required_single_domain is not None: |
| 120 |
self._warn_type( |
| 121 |
"session_required_single_domain", |
| 122 |
"list[str]", |
| 123 |
session_required_single_domain, |
| 124 |
) |
| 125 |
return None |
| 126 |
|
| 127 |
def _parse_session_required_policies( |
| 128 |
self, data: dict[str, t.Any] |
| 129 |
) -> list[str] | None: |
| 130 |
session_required_policies = data.get("session_required_policies") |
| 131 |
if isinstance(session_required_policies, str): |
| 132 |
return session_required_policies.split(",") |
| 133 |
elif guards.is_list_of(session_required_policies, str): |
| 134 |
return session_required_policies |
| 135 |
elif session_required_policies is not None: |
| 136 |
self._warn_type( |
| 137 |
"session_required_policies", "list[str]|str", session_required_policies |
| 138 |
) |
| 139 |
return None |
| 140 |
|
| 141 |
def _parse_session_required_mfa(self, data: dict[str, t.Any]) -> bool | None: |
| 142 |
session_required_mfa = data.get("session_required_mfa") |
| 143 |
if isinstance(session_required_mfa, bool): |
| 144 |
return session_required_mfa |
| 145 |
elif session_required_mfa is not None: |
| 146 |
self._warn_type("session_required_mfa", "bool", session_required_mfa) |
| 147 |
return None |
| 148 |
|
| 149 |
def _warn_type(self, key: str, expected: str, got: t.Any) -> None: |
| 150 |
log.warning( |
| 151 |
f"During ErrorInfo instantiation, got unexpected type for '{key}'. " |
| 152 |
f"Expected '{expected}'. Got '{got}'" |
| 153 |
) |
| 154 |
|
| 155 |
|
| 156 |
class ConsentRequiredInfo(ErrorInfo): |
| 157 |
""" |
| 158 |
ConsentRequiredInfo objects contain required consent information for an error. They |
| 159 |
test as truthy if the error was marked as a ConsentRequired error. |
| 160 |
|
| 161 |
:ivar required_scopes: A list of scopes requested by the server |
| 162 |
:vartype required_scopes: list of str, optional |
| 163 |
""" |
| 164 |
|
| 165 |
def __init__(self, error_data: dict[str, t.Any]) -> None: |
| 166 |
# data is only considered parseable if this error has the code 'ConsentRequired' |
| 167 |
has_code = error_data.get("code") == "ConsentRequired" |
| 168 |
data = error_data if has_code else {} |
| 169 |
self.required_scopes = self._parse_required_scopes(data) |
| 170 |
|
| 171 |
# but the result is only considered valid if both parts are present |
| 172 |
self._has_data = has_code and bool(self.required_scopes) |
| 173 |
|
| 174 |
def _parse_required_scopes(self, data: dict[str, t.Any]) -> list[str]: |
| 175 |
if guards.is_list_of(data.get("required_scopes"), str): |
| 176 |
return t.cast("list[str]", data["required_scopes"]) |
| 177 |
elif isinstance(data.get("required_scope"), str): |
| 178 |
return [data["required_scope"]] |
| 179 |
return [] |
| 180 |
|
| 181 |
|
| 182 |
class ErrorInfoContainer: |
| 183 |
""" |
| 184 |
This is a wrapper type which contains various error info objects for parsed error |
| 185 |
data. It is attached to API errors as the ``.info`` attribute. |
| 186 |
|
| 187 |
:ivar authorization_parameters: A parsed AuthorizationParameterInfo object |
| 188 |
:ivar consent_required: A parsed ConsentRequiredInfo object |
| 189 |
""" |
| 190 |
|
| 191 |
def __init__(self, error_data: dict[str, t.Any] | None) -> None: |
| 192 |
self.authorization_parameters = AuthorizationParameterInfo(error_data or {}) |
| 193 |
self.consent_required = ConsentRequiredInfo(error_data or {}) |
| 194 |
|
| 195 |
def __str__(self) -> str: |
| 196 |
return f"{self.authorization_parameters}|{self.consent_required}" |