| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import typing as t |
| 4 |
|
| 5 |
from globus_sdk import exc |
| 6 |
from globus_sdk._internal.guards import validators |
| 7 |
from globus_sdk._internal.serializable import Serializable |
| 8 |
|
| 9 |
from ._auth_requirements_error import GARE, GlobusAuthorizationParameters |
| 10 |
|
| 11 |
V = t.TypeVar("V", bound="LegacyAuthRequirementsErrorVariant") |
| 12 |
|
| 13 |
|
| 14 |
class LegacyAuthRequirementsErrorVariant(t.Protocol): |
| 15 |
""" |
| 16 |
Protocol for errors which can be converted to a Globus Auth Requirements Error. |
| 17 |
""" |
| 18 |
|
| 19 |
@classmethod |
| 20 |
def from_dict(cls: type[V], data: dict[str, t.Any]) -> V: |
| 21 |
pass |
| 22 |
|
| 23 |
def to_auth_requirements_error(self) -> GARE: ... |
| 24 |
|
| 25 |
|
| 26 |
class LegacyDependentConsentRequiredAuthError(Serializable): |
| 27 |
""" |
| 28 |
The dependent_consent_required error format emitted by the Globus Auth service. |
| 29 |
""" |
| 30 |
|
| 31 |
def __init__( |
| 32 |
self, |
| 33 |
*, |
| 34 |
error: t.Literal["dependent_consent_required"], |
| 35 |
errors: list[dict[str, str]], |
| 36 |
extra: dict[str, t.Any] | None = None, |
| 37 |
) -> None: |
| 38 |
self.error = _validate_dependent_consent_required_literal("error", error) |
| 39 |
|
| 40 |
try: |
| 41 |
first_error = errors[0] |
| 42 |
if not isinstance(first_error, dict): |
| 43 |
raise TypeError |
| 44 |
except (AttributeError, KeyError, TypeError) as _error: |
| 45 |
msg = "'errors' must be list of errors with at least one error object" |
| 46 |
raise exc.ValidationError(msg) from _error |
| 47 |
self.errors = errors |
| 48 |
self.unapproved_scopes = validators.str_list( |
| 49 |
"unapproved_scopes", first_error.get("unapproved_scopes", []) |
| 50 |
) |
| 51 |
self.extra = extra or {} |
| 52 |
|
| 53 |
def to_auth_requirements_error(self) -> GARE: |
| 54 |
""" |
| 55 |
Return a GlobusAuthRequirementsError representing this error. |
| 56 |
""" |
| 57 |
|
| 58 |
return GARE( |
| 59 |
code="ConsentRequired", |
| 60 |
authorization_parameters=GlobusAuthorizationParameters( |
| 61 |
required_scopes=self.unapproved_scopes, |
| 62 |
), |
| 63 |
) |
| 64 |
|
| 65 |
|
| 66 |
class LegacyConsentRequiredTransferError(Serializable): |
| 67 |
""" |
| 68 |
The ConsentRequired error format emitted by the Globus Transfer service. |
| 69 |
""" |
| 70 |
|
| 71 |
def __init__( |
| 72 |
self, |
| 73 |
*, |
| 74 |
code: t.Literal["ConsentRequired"], |
| 75 |
required_scopes: list[str], |
| 76 |
extra: dict[str, t.Any] | None = None, |
| 77 |
) -> None: |
| 78 |
self.code = _validate_consent_required_literal("code", code) |
| 79 |
self.required_scopes = validators.str_list("required_scopes", required_scopes) |
| 80 |
self.extra = extra or {} |
| 81 |
|
| 82 |
def to_auth_requirements_error(self) -> GARE: |
| 83 |
""" |
| 84 |
Return a GlobusAuthRequirementsError representing this error. |
| 85 |
""" |
| 86 |
return GARE( |
| 87 |
code=self.code, |
| 88 |
authorization_parameters=GlobusAuthorizationParameters( |
| 89 |
required_scopes=self.required_scopes, |
| 90 |
session_message=self.extra.get("message"), |
| 91 |
), |
| 92 |
extra=self.extra, |
| 93 |
) |
| 94 |
|
| 95 |
|
| 96 |
class LegacyConsentRequiredAPError(Serializable): |
| 97 |
""" |
| 98 |
The ConsentRequired error format emitted by the legacy Globus Transfer |
| 99 |
Action Providers. |
| 100 |
""" |
| 101 |
|
| 102 |
def __init__( |
| 103 |
self, |
| 104 |
*, |
| 105 |
code: t.Literal["ConsentRequired"], |
| 106 |
required_scope: str, |
| 107 |
extra: dict[str, t.Any] | None, |
| 108 |
) -> None: |
| 109 |
self.code = _validate_consent_required_literal("code", code) |
| 110 |
self.required_scope = validators.str_("required_scope", required_scope) |
| 111 |
self.extra = extra or {} |
| 112 |
|
| 113 |
def to_auth_requirements_error(self) -> GARE: |
| 114 |
""" |
| 115 |
Return a GlobusAuthRequirementsError representing this error. |
| 116 |
|
| 117 |
Normalizes the required_scope field to a list and uses the description |
| 118 |
to set the session message. |
| 119 |
""" |
| 120 |
return GARE( |
| 121 |
code=self.code, |
| 122 |
authorization_parameters=GlobusAuthorizationParameters( |
| 123 |
required_scopes=[self.required_scope], |
| 124 |
session_message=self.extra.get("description"), |
| 125 |
extra=self.extra.get("authorization_parameters"), |
| 126 |
), |
| 127 |
extra={ |
| 128 |
k: v for k, v in self.extra.items() if k != "authorization_parameters" |
| 129 |
}, |
| 130 |
) |
| 131 |
|
| 132 |
|
| 133 |
class LegacyAuthorizationParameters(Serializable): |
| 134 |
""" |
| 135 |
An Authorization Parameters object that describes all known variants in use by |
| 136 |
Globus services. |
| 137 |
""" |
| 138 |
|
| 139 |
def __init__( |
| 140 |
self, |
| 141 |
*, |
| 142 |
session_message: str | None = None, |
| 143 |
session_required_identities: list[str] | None = None, |
| 144 |
session_required_policies: str | list[str] | None = None, |
| 145 |
session_required_single_domain: str | list[str] | None = None, |
| 146 |
session_required_mfa: bool | None = None, |
| 147 |
prompt: t.Literal["login"] | None = None, |
| 148 |
extra: dict[str, t.Any] | None = None, |
| 149 |
) -> None: |
| 150 |
self.session_message = validators.opt_str("session_message", session_message) |
| 151 |
self.session_required_identities = validators.opt_str_list( |
| 152 |
"session_required_identities", session_required_identities |
| 153 |
) |
| 154 |
self.session_required_policies = validators.opt_str_list_or_commasep( |
| 155 |
"session_required_policies", session_required_policies |
| 156 |
) |
| 157 |
self.session_required_single_domain = validators.opt_str_list_or_commasep( |
| 158 |
"session_required_single_domain", session_required_single_domain |
| 159 |
) |
| 160 |
self.session_required_mfa = validators.opt_bool( |
| 161 |
"session_required_mfa", session_required_mfa |
| 162 |
) |
| 163 |
if prompt in [None, "login"]: |
| 164 |
self.prompt = prompt |
| 165 |
else: |
| 166 |
raise exc.ValidationError("'prompt' must be 'login' or null") |
| 167 |
self.extra = extra or {} |
| 168 |
|
| 169 |
def to_authorization_parameters( |
| 170 |
self, |
| 171 |
) -> GlobusAuthorizationParameters: |
| 172 |
""" |
| 173 |
Return a normalized GlobusAuthorizationParameters instance representing |
| 174 |
these parameters. |
| 175 |
|
| 176 |
Normalizes fields that may have been provided |
| 177 |
as comma-delimited strings to lists of strings. |
| 178 |
""" |
| 179 |
return GlobusAuthorizationParameters( |
| 180 |
session_message=self.session_message, |
| 181 |
session_required_identities=self.session_required_identities, |
| 182 |
session_required_mfa=self.session_required_mfa, |
| 183 |
session_required_policies=self.session_required_policies, |
| 184 |
session_required_single_domain=self.session_required_single_domain, |
| 185 |
prompt=self.prompt, |
| 186 |
extra=self.extra, |
| 187 |
) |
| 188 |
|
| 189 |
|
| 190 |
class LegacyAuthorizationParametersError(Serializable): |
| 191 |
""" |
| 192 |
Defines an Authorization Parameters error that describes all known variants |
| 193 |
in use by Globus services. |
| 194 |
""" |
| 195 |
|
| 196 |
DEFAULT_CODE = "AuthorizationRequired" |
| 197 |
|
| 198 |
def __init__( |
| 199 |
self, |
| 200 |
*, |
| 201 |
authorization_parameters: dict[str, t.Any] | LegacyAuthorizationParameters, |
| 202 |
code: str | None = None, |
| 203 |
extra: dict[str, t.Any] | None = None, |
| 204 |
) -> None: |
| 205 |
# Apply default, if necessary |
| 206 |
self.code = validators.str_("code", code or self.DEFAULT_CODE) |
| 207 |
self.authorization_parameters = validators.instance_or_dict( |
| 208 |
"authorization_parameters", |
| 209 |
authorization_parameters, |
| 210 |
LegacyAuthorizationParameters, |
| 211 |
) |
| 212 |
self.extra = extra or {} |
| 213 |
|
| 214 |
def to_auth_requirements_error(self) -> GARE: |
| 215 |
""" |
| 216 |
Return a GlobusAuthRequirementsError representing this error. |
| 217 |
""" |
| 218 |
authorization_parameters = ( |
| 219 |
self.authorization_parameters.to_authorization_parameters() |
| 220 |
) |
| 221 |
return GARE( |
| 222 |
authorization_parameters=authorization_parameters, |
| 223 |
code=self.code, |
| 224 |
extra=self.extra, |
| 225 |
) |
| 226 |
|
| 227 |
|
| 228 |
def _validate_consent_required_literal( |
| 229 |
name: str, value: t.Any |
| 230 |
) -> t.Literal["ConsentRequired"]: |
| 231 |
if value == "ConsentRequired": |
| 232 |
return "ConsentRequired" |
| 233 |
raise exc.ValidationError(f"'{name}' must be the string 'ConsentRequired'") |
| 234 |
|
| 235 |
|
| 236 |
def _validate_dependent_consent_required_literal( |
| 237 |
name: str, value: t.Any |
| 238 |
) -> t.Literal["dependent_consent_required"]: |
| 239 |
if value == "dependent_consent_required": |
| 240 |
return "dependent_consent_required" |
| 241 |
msg = f"'{name}' must be the string 'dependent_consent_required'" |
| 242 |
raise exc.ValidationError(msg) |