Skip to content

Commit

Permalink
Adjust API calls so that calls to DUO work
Browse files Browse the repository at this point in the history
  • Loading branch information
pcmxgti committed Nov 8, 2023
1 parent 7dd7e62 commit afcd0b6
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions tokendito/duo.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def prepare_info(selected_okta_factor):
return duo_info


def api_post(url, params=None, headers=None, payload=None):
def api_post(url, params=None, headers=None, payload=None, return_json=True):
"""Error handling and response parsing wrapper for Duo POSTs.
:param url: The URL being connected to.
Expand All @@ -54,7 +54,9 @@ def api_post(url, params=None, headers=None, payload=None):
:param payload: Request body.
:return response: Response to the API request.
"""
response = HTTP_client.post(url, params=params, headers=headers, data=payload, return_json=True)
response = HTTP_client.post(
url, params=params, headers=headers, data=payload, return_json=return_json
)
return response


Expand All @@ -71,7 +73,7 @@ def get_sid(duo_info):

url = f"https://{duo_info['host']}/frame/web/v1/auth"
logger.debug(f"Calling Duo {urlparse(url).path} with params {params.keys()}")
duo_auth_response = api_post(url, params=params)
duo_auth_response = api_post(url, params=params, return_json=False)

try:
duo_auth_redirect = urlparse(f"{unquote(duo_auth_response.url)}").query
Expand Down Expand Up @@ -124,10 +126,9 @@ def parse_mfa_challenge(mfa_challenge):
:return txid: Duo transaction ID.
"""
try:
mfa_challenge = mfa_challenge.json()
mfa_status = mfa_challenge["stat"]
txid = mfa_challenge["response"]["txid"]
except (TypeError, ValueError) as err:
except (TypeError, ValueError, AttributeError) as err:
logger.error(f"The Duo API returned a non-json response: {err}")
sys.exit(1)
except KeyError as key_error:
Expand Down Expand Up @@ -185,7 +186,7 @@ def get_mfa_response(mfa_result):
:return verify_mfa: json response from mfa api
"""
try:
verify_mfa = mfa_result.json()["response"]
verify_mfa = mfa_result["response"]
except KeyError as key_error:
logger.error(f"The mfa challenge response is missing a required parameter: {key_error}")
logger.debug(json.dumps(mfa_result.json()))
Expand Down Expand Up @@ -266,7 +267,7 @@ def factor_callback(duo_info, verify_mfa):
factor_callback = api_post(factor_callback_url, payload={"sid": duo_info["sid"]})

try:
sig_response = f"{factor_callback.json()['response']['cookie']}:{duo_info['tile_sig']}"
sig_response = f"{factor_callback['response']['cookie']}:{duo_info['tile_sig']}"
except Exception as sig_error:
logger.error("There was an error getting your application signature. Please try again.")
logger.debug(f"from Duo: {sig_error}")
Expand Down Expand Up @@ -331,5 +332,5 @@ def authenticate(selected_okta_factor):
}

# Send Okta callback and return payload
api_post(duo_info["okta_callback_url"], payload=payload)
api_post(duo_info["okta_callback_url"], payload=payload, return_json=False)
return payload

0 comments on commit afcd0b6

Please sign in to comment.