|
| 1 | +# -------------------------------------------------------------------------- |
| 2 | +# |
| 3 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 4 | +# |
| 5 | +# The MIT License (MIT) |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +# of this software and associated documentation files (the ""Software""), to deal |
| 9 | +# in the Software without restriction, including without limitation the rights |
| 10 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +# copies of the Software, and to permit persons to whom the Software is |
| 12 | +# furnished to do so, subject to the following conditions: |
| 13 | +# |
| 14 | +# The above copyright notice and this permission notice shall be included in |
| 15 | +# all copies or substantial portions of the Software. |
| 16 | +# |
| 17 | +# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | +# THE SOFTWARE. |
| 24 | +# |
| 25 | +# -------------------------------------------------------------------------- |
| 26 | +import base64 |
| 27 | +import time |
| 28 | +from unittest.mock import Mock |
| 29 | + |
| 30 | +from azure.core.credentials import AccessToken |
| 31 | +from azure.core.pipeline import AsyncPipeline |
| 32 | +from azure.mgmt.core.policies import AsyncARMChallengeAuthenticationPolicy |
| 33 | +from azure.core.pipeline.transport import HttpRequest |
| 34 | + |
| 35 | +import pytest |
| 36 | + |
| 37 | +pytestmark = pytest.mark.asyncio |
| 38 | + |
| 39 | + |
| 40 | +async def test_claims_challenge(): |
| 41 | + """AsyncAsyncARMChallengeAuthenticationPolicy should pass claims from an authentication challenge to its credential""" |
| 42 | + |
| 43 | + first_token = AccessToken("first", int(time.time()) + 3600) |
| 44 | + second_token = AccessToken("second", int(time.time()) + 3600) |
| 45 | + tokens = (t for t in (first_token, second_token)) |
| 46 | + |
| 47 | + expected_claims = '{"access_token": {"essential": "true"}' |
| 48 | + expected_scope = "scope" |
| 49 | + |
| 50 | + challenge = 'Bearer authorization_uri="https://localhost", error=".", error_description=".", claims="{}"'.format( |
| 51 | + base64.b64encode(expected_claims.encode()).decode() |
| 52 | + ) |
| 53 | + responses = (r for r in (Mock(status_code=401, headers={"WWW-Authenticate": challenge}), Mock(status_code=200))) |
| 54 | + |
| 55 | + async def send(request): |
| 56 | + res = next(responses) |
| 57 | + if res.status_code == 401: |
| 58 | + expected_token = first_token.token |
| 59 | + else: |
| 60 | + expected_token = second_token.token |
| 61 | + assert request.headers["Authorization"] == "Bearer " + expected_token |
| 62 | + |
| 63 | + return res |
| 64 | + |
| 65 | + async def get_token(*scopes, **kwargs): |
| 66 | + assert scopes == (expected_scope,) |
| 67 | + return next(tokens) |
| 68 | + |
| 69 | + credential = Mock(get_token=Mock(wraps=get_token)) |
| 70 | + transport = Mock(send=Mock(wraps=send)) |
| 71 | + policies = [AsyncARMChallengeAuthenticationPolicy(credential, expected_scope)] |
| 72 | + pipeline = AsyncPipeline(transport=transport, policies=policies) |
| 73 | + |
| 74 | + response = await pipeline.run(HttpRequest("GET", "https://localhost")) |
| 75 | + |
| 76 | + assert response.http_response.status_code == 200 |
| 77 | + assert transport.send.call_count == 2 |
| 78 | + assert credential.get_token.call_count == 2 |
| 79 | + credential.get_token.assert_called_with(expected_scope, claims=expected_claims) |
| 80 | + with pytest.raises(StopIteration): |
| 81 | + next(tokens) |
| 82 | + with pytest.raises(StopIteration): |
| 83 | + next(responses) |
| 84 | + |
| 85 | + |
| 86 | +async def test_multiple_claims_challenges(): |
| 87 | + """ARMChallengeAuthenticationPolicy should not attempt to handle a response having multiple claims challenges""" |
| 88 | + |
| 89 | + expected_header = ",".join( |
| 90 | + ( |
| 91 | + 'Bearer realm="", authorization_uri="https://login.microsoftonline.com/common/oauth2/authorize", client_id="00000003-0000-0000-c000-000000000000", error="insufficient_claims", claims="eyJhY2Nlc3NfdG9rZW4iOiB7ImZvbyI6ICJiYXIifX0="', |
| 92 | + 'Bearer authorization_uri="https://login.windows-ppe.net/", error="invalid_token", error_description="User session has been revoked", claims="eyJhY2Nlc3NfdG9rZW4iOnsibmJmIjp7ImVzc2VudGlhbCI6dHJ1ZSwgInZhbHVlIjoiMTYwMzc0MjgwMCJ9fX0="', |
| 93 | + ) |
| 94 | + ) |
| 95 | + |
| 96 | + async def send(request): |
| 97 | + return Mock(status_code=401, headers={"WWW-Authenticate": expected_header}) |
| 98 | + |
| 99 | + async def get_token(*_, **__): |
| 100 | + return AccessToken("***", 42) |
| 101 | + |
| 102 | + transport = Mock(send=Mock(wraps=send)) |
| 103 | + credential = Mock(get_token=Mock(wraps=get_token)) |
| 104 | + policies = [AsyncARMChallengeAuthenticationPolicy(credential, "scope")] |
| 105 | + pipeline = AsyncPipeline(transport=transport, policies=policies) |
| 106 | + |
| 107 | + response = await pipeline.run(HttpRequest("GET", "https://localhost")) |
| 108 | + |
| 109 | + assert transport.send.call_count == 1 |
| 110 | + assert credential.get_token.call_count == 1 |
| 111 | + |
| 112 | + # the policy should have returned the error response because it was unable to handle the challenge |
| 113 | + assert response.http_response.status_code == 401 |
| 114 | + assert response.http_response.headers["WWW-Authenticate"] == expected_header |
0 commit comments