Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/adal/util.py: 88%
41 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:05 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:05 +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#------------------------------------------------------------------------------
28import sys
29import base64
30try:
31 from urllib.parse import urlparse
32except ImportError:
33 from urlparse import urlparse #pylint: disable=import-error
35import adal
37from .constants import AdalIdParameters
39def is_http_success(status_code):
40 return status_code >= 200 and status_code < 300
42def add_default_request_headers(self, options):
43 if not options.get('headers'):
44 options['headers'] = {}
46 headers = options['headers']
47 if not headers.get('Accept-Charset'):
48 headers['Accept-Charset'] = 'utf-8'
50 #pylint: disable=protected-access
51 headers['client-request-id'] = self._call_context['log_context']['correlation_id']
52 headers['return-client-request-id'] = 'true'
54 headers[AdalIdParameters.SKU] = AdalIdParameters.PYTHON_SKU
55 headers[AdalIdParameters.VERSION] = adal.__version__
56 headers[AdalIdParameters.OS] = sys.platform
57 headers[AdalIdParameters.CPU] = 'x64' if sys.maxsize > 2 ** 32 else 'x86'
59def create_request_options(self, *options):
61 merged_options = {}
63 if options:
64 for i in options:
65 merged_options.update(i)
67 #pylint: disable=protected-access
68 if self._call_context.get('options') and self._call_context['options'].get('http'):
69 merged_options.update(self._call_context['options']['http'])
71 add_default_request_headers(self, merged_options)
72 return merged_options
75def log_return_correlation_id(log, operation_message, response):
76 if response and response.headers and response.headers.get('client-request-id'):
77 log.debug("{} Server returned this correlation_id: {}".format(
78 operation_message,
79 response.headers['client-request-id']))
81def copy_url(url_source):
82 if hasattr(url_source, 'geturl'):
83 return urlparse(url_source.geturl())
84 else:
85 return urlparse(url_source)
87# urlsafe_b64decode requires correct padding. AAD does not include padding so
88# the string needs to be correctly padded before decoding.
89def base64_urlsafe_decode(b64string):
90 b64string += '=' * (4 - ((len(b64string) % 4)))
91 return base64.urlsafe_b64decode(b64string.encode('ascii'))