| 1 |
import urllib.parse |
| 2 |
import uuid |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
import globus_sdk |
| 7 |
from globus_sdk._missing import MISSING |
| 8 |
from globus_sdk.scopes import TransferScopes |
| 9 |
from globus_sdk.services.auth.flow_managers.native_app import _make_native_app_challenge |
| 10 |
from globus_sdk.testing import load_response |
| 11 |
|
| 12 |
CLIENT_ID = "d0f1d9b0-bd81-4108-be74-ea981664453a" |
| 13 |
|
| 14 |
|
| 15 |
@pytest.fixture |
| 16 |
def native_client(): |
| 17 |
client = globus_sdk.NativeAppAuthClient(client_id=CLIENT_ID) |
| 18 |
with client.retry_config.tune(max_retries=0): |
| 19 |
yield client |
| 20 |
|
| 21 |
|
| 22 |
@pytest.fixture |
| 23 |
def confidential_client(): |
| 24 |
client = globus_sdk.ConfidentialAppAuthClient( |
| 25 |
client_id=CLIENT_ID, client_secret="SECRET_SECRET_HES_GOT_A_SECRET" |
| 26 |
) |
| 27 |
with client.retry_config.tune(max_retries=0): |
| 28 |
yield client |
| 29 |
|
| 30 |
|
| 31 |
# build a nearly-diagonal matrix over |
| 32 |
# |
| 33 |
# domain: str | list[str] |
| 34 |
# identities: uuid | str | list[uuid] | list[str] | list[str | uuid] |
| 35 |
# policies: uuid | str | list[uuid] | list[str] | list[str | uuid] |
| 36 |
# mfa: True | False |
| 37 |
# message: str | None |
| 38 |
# prompt: Literal["prompt"] | None |
| 39 |
# |
| 40 |
# The order of these options is consequential. |
| 41 |
# They are declared in the same order they tuple-unpack in the parametrized test. |
| 42 |
# `None` values, though valid for each parameter, are omitted; |
| 43 |
# they will be tested exactly once by a single all-`None` option test. |
| 44 |
domain_options = ("example.edu", ["example.edu", "example.org"]) |
| 45 |
identity_options = ( |
| 46 |
uuid.UUID(int=0), |
| 47 |
"foo-id", |
| 48 |
[uuid.UUID(int=0), uuid.UUID(int=1)], |
| 49 |
["foo-id", "bar-id"], |
| 50 |
["foo-id", uuid.UUID(int=2)], |
| 51 |
) |
| 52 |
policy_options = ( |
| 53 |
uuid.UUID(int=3), |
| 54 |
"baz-id", |
| 55 |
[uuid.UUID(int=3), uuid.UUID(int=4)], |
| 56 |
["baz-id", "quux-id"], |
| 57 |
["baz-id", uuid.UUID(int=5)], |
| 58 |
) |
| 59 |
message_options = ("Test message",) |
| 60 |
mfa_options = (True, False) |
| 61 |
prompt_options = ("login",) |
| 62 |
# Seed an all-`None` option test, then use a loop to fill in the rest. |
| 63 |
# The number of parameters here must match the test parameters: |
| 64 |
_ALL_SESSION_PARAM_COMBINATIONS = [(MISSING,) * 6] |
| 65 |
for idx, options in enumerate( |
| 66 |
( |
| 67 |
domain_options, |
| 68 |
identity_options, |
| 69 |
policy_options, |
| 70 |
mfa_options, |
| 71 |
message_options, |
| 72 |
prompt_options, |
| 73 |
) |
| 74 |
): |
| 75 |
for option in options: |
| 76 |
parameters = [MISSING] * 6 |
| 77 |
parameters[idx] = option |
| 78 |
_ALL_SESSION_PARAM_COMBINATIONS.append(tuple(parameters)) |
| 79 |
|
| 80 |
|
| 81 |
@pytest.mark.parametrize("flow_type", ("native_app", "confidential_app")) |
| 82 |
# parametrize over both what is and what *is not* passed as a parameter |
| 83 |
@pytest.mark.parametrize( |
| 84 |
[ |
| 85 |
"domain_option", |
| 86 |
"identity_option", |
| 87 |
"policy_option", |
| 88 |
"mfa_option", |
| 89 |
"message_option", |
| 90 |
"prompt_option", |
| 91 |
], |
| 92 |
_ALL_SESSION_PARAM_COMBINATIONS, |
| 93 |
) |
| 94 |
def test_oauth2_get_authorize_url_supports_session_params( |
| 95 |
native_client, |
| 96 |
confidential_client, |
| 97 |
flow_type, |
| 98 |
domain_option, |
| 99 |
identity_option, |
| 100 |
policy_option, |
| 101 |
mfa_option, |
| 102 |
message_option, |
| 103 |
prompt_option, |
| 104 |
): |
| 105 |
if flow_type == "native_app": |
| 106 |
client = native_client |
| 107 |
elif flow_type == "confidential_app": |
| 108 |
client = confidential_client |
| 109 |
else: |
| 110 |
raise NotImplementedError |
| 111 |
|
| 112 |
# get the url... |
| 113 |
client.oauth2_start_flow(redirect_uri="https://example.com", requested_scopes="foo") |
| 114 |
url_res = client.oauth2_get_authorize_url( |
| 115 |
session_required_single_domain=domain_option, |
| 116 |
session_required_identities=identity_option, |
| 117 |
session_required_policies=policy_option, |
| 118 |
session_required_mfa=mfa_option, |
| 119 |
session_message=message_option, |
| 120 |
prompt=prompt_option, |
| 121 |
) |
| 122 |
|
| 123 |
# parse the result... |
| 124 |
parsed_url = urllib.parse.urlparse(url_res) |
| 125 |
parsed_params = urllib.parse.parse_qs(parsed_url.query) |
| 126 |
|
| 127 |
# prepare some helper data... |
| 128 |
expected_params_keys = { |
| 129 |
"session_required_single_domain" if domain_option else None, |
| 130 |
"session_required_identities" if identity_option else None, |
| 131 |
"session_required_policies" if policy_option else None, |
| 132 |
"session_required_mfa" if mfa_option is not MISSING else None, |
| 133 |
"prompt" if prompt_option else None, |
| 134 |
} |
| 135 |
expected_params_keys.discard(None) |
| 136 |
unexpected_query_params = { |
| 137 |
"session_required_single_domain", |
| 138 |
"session_required_identities", |
| 139 |
"session_required_policies", |
| 140 |
"session_required_mfa", |
| 141 |
"prompt", |
| 142 |
} - expected_params_keys |
| 143 |
parsed_params_keys = set(parsed_params.keys()) |
| 144 |
|
| 145 |
# ...and validate! |
| 146 |
assert expected_params_keys <= parsed_params_keys |
| 147 |
assert (unexpected_query_params - parsed_params_keys) == unexpected_query_params |
| 148 |
|
| 149 |
if domain_option is not MISSING: |
| 150 |
strized_option = ( |
| 151 |
",".join(str(x) for x in domain_option) |
| 152 |
if isinstance(domain_option, list) |
| 153 |
else str(domain_option) |
| 154 |
) |
| 155 |
assert parsed_params["session_required_single_domain"] == [strized_option] |
| 156 |
|
| 157 |
if identity_option is not MISSING: |
| 158 |
strized_option = ( |
| 159 |
",".join(str(x) for x in identity_option) |
| 160 |
if isinstance(identity_option, list) |
| 161 |
else str(identity_option) |
| 162 |
) |
| 163 |
assert parsed_params["session_required_identities"] == [strized_option] |
| 164 |
|
| 165 |
if policy_option is not MISSING: |
| 166 |
strized_option = ( |
| 167 |
",".join(str(x) for x in policy_option) |
| 168 |
if isinstance(policy_option, list) |
| 169 |
else str(policy_option) |
| 170 |
) |
| 171 |
assert parsed_params["session_required_policies"] == [strized_option] |
| 172 |
|
| 173 |
if mfa_option is not MISSING: |
| 174 |
strized_option = "True" if mfa_option else "False" |
| 175 |
assert parsed_params["session_required_mfa"] == [strized_option] |
| 176 |
|
| 177 |
if prompt_option is not MISSING: |
| 178 |
assert parsed_params["prompt"] == [prompt_option] |
| 179 |
|
| 180 |
|
| 181 |
def test_oauth2_get_authorize_url_native_defaults(native_client): |
| 182 |
flow_manager = globus_sdk.services.auth.GlobusNativeAppFlowManager( |
| 183 |
native_client, |
| 184 |
TransferScopes.all, |
| 185 |
) |
| 186 |
native_client.current_oauth2_flow_manager = flow_manager |
| 187 |
|
| 188 |
# get url and validate results |
| 189 |
url_res = native_client.oauth2_get_authorize_url() |
| 190 |
parsed_url = urllib.parse.urlparse(url_res) |
| 191 |
assert f"https://{parsed_url.netloc}/" == native_client.base_url |
| 192 |
assert parsed_url.path == "/v2/oauth2/authorize" |
| 193 |
parsed_params = urllib.parse.parse_qs(parsed_url.query) |
| 194 |
assert parsed_params == { |
| 195 |
"client_id": [native_client.client_id], |
| 196 |
"redirect_uri": [native_client.base_url + "v2/web/auth-code"], |
| 197 |
"scope": [str(TransferScopes.all)], |
| 198 |
"state": ["_default"], |
| 199 |
"response_type": ["code"], |
| 200 |
"code_challenge": [flow_manager.challenge], |
| 201 |
"code_challenge_method": ["S256"], |
| 202 |
"access_type": ["online"], |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
def test_oauth2_get_authorize_url_native_custom_params(native_client): |
| 207 |
# starting flow with custom parameters, should not warn because a scope is specified |
| 208 |
flow_manager = globus_sdk.services.auth.GlobusNativeAppFlowManager( |
| 209 |
native_client, |
| 210 |
requested_scopes="scopes", |
| 211 |
redirect_uri="uri", |
| 212 |
state="state", |
| 213 |
verifier=("a" * 43), |
| 214 |
refresh_tokens=True, |
| 215 |
) |
| 216 |
native_client.current_oauth2_flow_manager = flow_manager |
| 217 |
|
| 218 |
# get url_and validate results |
| 219 |
url_res = native_client.oauth2_get_authorize_url() |
| 220 |
verifier, remade_challenge = _make_native_app_challenge("a" * 43) |
| 221 |
parsed_url = urllib.parse.urlparse(url_res) |
| 222 |
assert f"https://{parsed_url.netloc}/" == native_client.base_url |
| 223 |
assert parsed_url.path == "/v2/oauth2/authorize" |
| 224 |
parsed_params = urllib.parse.parse_qs(parsed_url.query) |
| 225 |
assert parsed_params == { |
| 226 |
"client_id": [native_client.client_id], |
| 227 |
"redirect_uri": ["uri"], |
| 228 |
"scope": ["scopes"], |
| 229 |
"state": ["state"], |
| 230 |
"response_type": ["code"], |
| 231 |
"code_challenge": [urllib.parse.quote_plus(remade_challenge)], |
| 232 |
"code_challenge_method": ["S256"], |
| 233 |
"access_type": ["offline"], |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
def test_oauth2_get_authorize_url_confidential_defaults(confidential_client): |
| 238 |
flow_manager = globus_sdk.services.auth.GlobusAuthorizationCodeFlowManager( |
| 239 |
confidential_client, "uri", TransferScopes.all |
| 240 |
) |
| 241 |
confidential_client.current_oauth2_flow_manager = flow_manager |
| 242 |
|
| 243 |
# get url_and validate results |
| 244 |
url_res = confidential_client.oauth2_get_authorize_url() |
| 245 |
parsed_url = urllib.parse.urlparse(url_res) |
| 246 |
assert f"https://{parsed_url.netloc}/" == confidential_client.base_url |
| 247 |
assert parsed_url.path == "/v2/oauth2/authorize" |
| 248 |
parsed_params = urllib.parse.parse_qs(parsed_url.query) |
| 249 |
assert parsed_params == { |
| 250 |
"client_id": [confidential_client.client_id], |
| 251 |
"redirect_uri": ["uri"], |
| 252 |
"scope": [str(TransferScopes.all)], |
| 253 |
"state": ["_default"], |
| 254 |
"response_type": ["code"], |
| 255 |
"access_type": ["online"], |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
def test_oauth2_get_authorize_url_confidential_custom_params(confidential_client): |
| 260 |
# starting flow with specified parameters |
| 261 |
flow_manager = globus_sdk.services.auth.GlobusAuthorizationCodeFlowManager( |
| 262 |
confidential_client, |
| 263 |
requested_scopes="scopes", |
| 264 |
redirect_uri="uri", |
| 265 |
state="state", |
| 266 |
refresh_tokens=True, |
| 267 |
) |
| 268 |
confidential_client.current_oauth2_flow_manager = flow_manager |
| 269 |
|
| 270 |
# get url_and validate results |
| 271 |
url_res = confidential_client.oauth2_get_authorize_url() |
| 272 |
parsed_url = urllib.parse.urlparse(url_res) |
| 273 |
assert f"https://{parsed_url.netloc}/" == confidential_client.base_url |
| 274 |
assert parsed_url.path == "/v2/oauth2/authorize" |
| 275 |
parsed_params = urllib.parse.parse_qs(parsed_url.query) |
| 276 |
assert parsed_params == { |
| 277 |
"client_id": [confidential_client.client_id], |
| 278 |
"redirect_uri": ["uri"], |
| 279 |
"scope": ["scopes"], |
| 280 |
"state": ["state"], |
| 281 |
"response_type": ["code"], |
| 282 |
"access_type": ["offline"], |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
def test_oauth2_exchange_code_for_tokens_native(native_client): |
| 287 |
""" |
| 288 |
Starts a NativeAppFlowManager, Confirms invalid code raises 401 |
| 289 |
Further testing cannot be done without user login credentials |
| 290 |
""" |
| 291 |
load_response(native_client.oauth2_exchange_code_for_tokens, case="invalid_grant") |
| 292 |
|
| 293 |
flow_manager = globus_sdk.services.auth.GlobusNativeAppFlowManager( |
| 294 |
native_client, requested_scopes=TransferScopes.all |
| 295 |
) |
| 296 |
native_client.current_oauth2_flow_manager = flow_manager |
| 297 |
|
| 298 |
with pytest.raises(globus_sdk.AuthAPIError) as excinfo: |
| 299 |
native_client.oauth2_exchange_code_for_tokens("invalid_code") |
| 300 |
assert excinfo.value.http_status == 401 |
| 301 |
assert excinfo.value.code is None |
| 302 |
|
| 303 |
|
| 304 |
def test_oauth2_exchange_code_for_tokens_confidential(confidential_client): |
| 305 |
""" |
| 306 |
Starts an AuthorizationCodeFlowManager, Confirms bad code raises 401 |
| 307 |
Further testing cannot be done without user login credentials |
| 308 |
""" |
| 309 |
load_response( |
| 310 |
confidential_client.oauth2_exchange_code_for_tokens, case="invalid_grant" |
| 311 |
) |
| 312 |
|
| 313 |
flow_manager = globus_sdk.services.auth.GlobusAuthorizationCodeFlowManager( |
| 314 |
confidential_client, "uri", requested_scopes=TransferScopes.all |
| 315 |
) |
| 316 |
confidential_client.current_oauth2_flow_manager = flow_manager |
| 317 |
|
| 318 |
with pytest.raises(globus_sdk.AuthAPIError) as excinfo: |
| 319 |
confidential_client.oauth2_exchange_code_for_tokens("invalid_code") |
| 320 |
assert excinfo.value.http_status == 401 |
| 321 |
assert excinfo.value.code is None |