Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/msal/wstrust_response.py: 39%
31 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:20 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:20 +0000
1#------------------------------------------------------------------------------
2#
3# Copyright (c) Microsoft Corporation.
4# All rights reserved.
5#
6# This code is licensed under the MIT License.
7#
8# Permission is hereby granted, free of charge, to any person obtaining a copy
9# of this software and associated documentation files(the "Software"), to deal
10# in the Software without restriction, including without limitation the rights
11# to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
12# copies of the Software, and to permit persons to whom the Software is
13# furnished to do so, subject to the following conditions :
14#
15# The above copyright notice and this permission notice shall be included in
16# all copies or substantial portions of the Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24# THE SOFTWARE.
25#
26#------------------------------------------------------------------------------
28try:
29 from xml.etree import cElementTree as ET
30except ImportError:
31 from xml.etree import ElementTree as ET
32import re
34from .mex import Mex
37SAML_TOKEN_TYPE_V1 = 'urn:oasis:names:tc:SAML:1.0:assertion'
38SAML_TOKEN_TYPE_V2 = 'urn:oasis:names:tc:SAML:2.0:assertion'
40# http://docs.oasis-open.org/wss-m/wss/v1.1.1/os/wss-SAMLTokenProfile-v1.1.1-os.html#_Toc307397288
41WSS_SAML_TOKEN_PROFILE_V1_1 = "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1"
42WSS_SAML_TOKEN_PROFILE_V2 = "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV2.0"
44def parse_response(body): # Returns {"token": "<saml:assertion ...>", "type": "..."}
45 token = parse_token_by_re(body)
46 if token:
47 return token
48 error = parse_error(body)
49 raise RuntimeError("WsTrust server returned error in RSTR: %s" % (error or body))
51def parse_error(body): # Returns error as a dict. See unit test case for an example.
52 dom = ET.fromstring(body)
53 reason_text_node = dom.find('s:Body/s:Fault/s:Reason/s:Text', Mex.NS)
54 subcode_value_node = dom.find('s:Body/s:Fault/s:Code/s:Subcode/s:Value', Mex.NS)
55 if reason_text_node is not None or subcode_value_node is not None:
56 return {"reason": reason_text_node.text, "code": subcode_value_node.text}
58def findall_content(xml_string, tag):
59 """
60 Given a tag name without any prefix,
61 this function returns a list of the raw content inside this tag as-is.
63 >>> findall_content("<ns0:foo> what <bar> ever </bar> content </ns0:foo>", "foo")
64 [" what <bar> ever </bar> content "]
66 Motivation:
68 Usually we would use XML parser to extract the data by xpath.
69 However the ElementTree in Python will implicitly normalize the output
70 by "hoisting" the inner inline namespaces into the outmost element.
71 The result will be a semantically equivalent XML snippet,
72 but not fully identical to the original one.
73 While this effect shouldn't become a problem in all other cases,
74 it does not seem to fully comply with Exclusive XML Canonicalization spec
75 (https://www.w3.org/TR/xml-exc-c14n/), and void the SAML token signature.
76 SAML signature algo needs the "XML -> C14N(XML) -> Signed(C14N(Xml))" order.
78 The binary extention lxml is probably the canonical way to solve this
79 (https://stackoverflow.com/questions/22959577/python-exclusive-xml-canonicalization-xml-exc-c14n)
80 but here we use this workaround, based on Regex, to return raw content as-is.
81 """
82 # \w+ is good enough for https://www.w3.org/TR/REC-xml/#NT-NameChar
83 pattern = r"<(?:\w+:)?%(tag)s(?:[^>]*)>(.*)</(?:\w+:)?%(tag)s" % {"tag": tag}
84 return re.findall(pattern, xml_string, re.DOTALL)
86def parse_token_by_re(raw_response): # Returns the saml:assertion
87 for rstr in findall_content(raw_response, "RequestSecurityTokenResponse"):
88 token_types = findall_content(rstr, "TokenType")
89 tokens = findall_content(rstr, "RequestedSecurityToken")
90 if token_types and tokens:
91 # Historically, we use "us-ascii" encoding, but it should be "utf-8"
92 # https://stackoverflow.com/questions/36658000/what-is-encoding-used-for-saml-conversations
93 return {"token": tokens[0].encode('utf-8'), "type": token_types[0]}