Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _perform_dynamic_instance_discovery(self):
discovery_endpoint = self._create_instance_discovery_endpoint_from_template(
AADConstants.WORLD_WIDE_AUTHORITY)
get_options = util.create_request_options(self)
operation = "Instance Discovery"
self._log.debug("Attempting instance discover at: %(discovery_endpoint)s",
{"discovery_endpoint": discovery_endpoint.geturl()})
try:
resp = requests.get(discovery_endpoint.geturl(), headers=get_options['headers'],
verify=self._call_context.get('verify_ssl', None),
proxies=self._call_context.get('proxies', None))
util.log_return_correlation_id(self._log, operation, resp)
except Exception:
self._log.exception("%(operation)s request failed",
{"operation": operation})
raise
if resp.status_code == 429:
resp.raise_for_status() # Will raise requests.exceptions.HTTPError
if not util.is_http_success(resp.status_code):
return_error_string = u"{} request returned http error: {}".format(operation,
resp.status_code)
error_response = ""
if resp.text:
return_error_string = u"{} and server response: {}".format(return_error_string,
resp.text)
try:
error_response = resp.json()
operation = "Get token with device code"
max_times_for_retry = math.floor(expires_in/refresh_internal)
for _ in range(int(max_times_for_retry)):
if self._cancel_polling_request:
raise AdalError('Polling_Request_Cancelled')
resp = requests.post(
token_url.geturl(),
data=url_encoded_code_request, headers=post_options['headers'],
proxies=self._call_context.get('proxies', None),
verify=self._call_context.get('verify_ssl', None))
if resp.status_code == 429:
resp.raise_for_status() # Will raise requests.exceptions.HTTPError
util.log_return_correlation_id(self._log, operation, resp)
wire_response = {}
if not util.is_http_success(resp.status_code):
# on error, the body should be json already
wire_response = json.loads(resp.text)
error = wire_response.get(OAuth2.DeviceCodeResponseParameters.ERROR)
if error == 'authorization_pending':
time.sleep(refresh_internal)
continue
elif error:
raise AdalError('Unexpected polling state {}'.format(error),
wire_response)
else:
try:
return self._validate_token_response(resp.text)
def get_user_code_info(self, oauth_parameters):
device_code_url = self._create_device_code_url()
url_encoded_code_request = urlencode(oauth_parameters)
post_options = util.create_request_options(self, _REQ_OPTION)
operation = "Get Device Code"
try:
resp = requests.post(device_code_url.geturl(),
data=url_encoded_code_request,
headers=post_options['headers'],
verify=self._call_context.get('verify_ssl', None),
proxies=self._call_context.get('proxies', None),
timeout=self._call_context.get('timeout', None))
util.log_return_correlation_id(self._log, operation, resp)
except Exception:
self._log.exception("%(operation)s request failed", {"operation": operation})
raise
if util.is_http_success(resp.status_code):
user_code_info = self._handle_get_device_code_response(resp.text)
user_code_info['correlation_id'] = resp.headers.get('client-request-id')
return user_code_info
else:
if resp.status_code == 429:
resp.raise_for_status() # Will raise requests.exceptions.HTTPError
return_error_string = _ERROR_TEMPLATE.format(operation, resp.status_code)
error_response = ""
if resp.text:
return_error_string = u"{} and server response: {}".format(return_error_string,
resp.text)
def discover(self):
options = util.create_request_options(self, {'headers': {'Content-Type': 'application/soap+xml'}})
try:
operation = "Mex Get"
resp = requests.get(self._url, headers=options['headers'],
verify=self._call_context.get('verify_ssl', None),
proxies=self._call_context.get('proxies', None))
util.log_return_correlation_id(self._log, operation, resp)
except Exception:
self._log.exception(
"%(operation)s request failed", {"operation": operation})
raise
if resp.status_code == 429:
resp.raise_for_status() # Will raise requests.exceptions.HTTPError
if not util.is_http_success(resp.status_code):
return_error_string = u"{} request returned http error: {}".format(operation, resp.status_code)
error_response = ""
if resp.text:
return_error_string = u"{} and server response: {}".format(return_error_string, resp.text)
try:
error_response = resp.json()
except ValueError:
pass
def get_token(self, oauth_parameters):
token_url = self._create_token_url()
url_encoded_token_request = urlencode(oauth_parameters)
post_options = util.create_request_options(self, _REQ_OPTION)
operation = "Get Token"
try:
resp = requests.post(token_url.geturl(),
data=url_encoded_token_request,
headers=post_options['headers'],
verify=self._call_context.get('verify_ssl', None),
proxies=self._call_context.get('proxies', None),
timeout=self._call_context.get('timeout', None))
util.log_return_correlation_id(self._log, operation, resp)
except Exception:
self._log.exception("%(operation)s request failed", {"operation": operation})
raise
if util.is_http_success(resp.status_code):
return self._handle_get_token_response(resp.text)
else:
if resp.status_code == 429:
resp.raise_for_status() # Will raise requests.exceptions.HTTPError
return_error_string = _ERROR_TEMPLATE.format(operation, resp.status_code)
error_response = ""
if resp.text:
return_error_string = u"{} and server response: {}".format(return_error_string,
resp.text)
try:
error_response = resp.json()