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