Skip to content

Commit

Permalink
update user agent parametter for Tokendito version
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando Aureliano da Silva Maia committed Oct 2, 2023
1 parent 7c1d6d6 commit bfc8194
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions tokendito/okta.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import requests
from tokendito import duo
from tokendito import user
from tokendito import __version__
from http_client import HTTPClient

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -83,7 +84,7 @@ def api_error_code_parser(status=None):
return message


def get_auth_properties(userid=None, url=None, user_agent="Tokendito"):
def get_auth_properties(userid=None, url=None, user_agent=f"Tokendito/{__version__}"):
"""
Make a call to the Okta webfinger endpoint to retrieve authentication properties.
Expand Down Expand Up @@ -126,30 +127,45 @@ def get_auth_properties(userid=None, url=None, user_agent="Tokendito"):



def get_saml_request(auth_properties):
def get_saml_request(auth_properties, user_agent=f"Tokendito/{__version__}"):
"""
Get a SAML Request object from the Service Provider, to be submitted to the IdP.
:param auth_properties: dict with the IdP ID and type.
:returns: dict with post_url, relay_state, and base64 encoded saml request.
"""

# Create an HTTP client instance with the specified user agent.
http_client = HTTPClient(user_agent)

# Prepare the headers for the request to retrieve the SAML request.
headers = {"accept": "text/html,application/xhtml+xml,application/xml"}

# Build the URL based on the metadata and ID provided in the auth properties.
base_url = user.get_base_url(auth_properties["metadata"])
url = f"{base_url}/sso/idps/{auth_properties['id']}"

logger.debug(f"Getting SAML request from {url}")
response = user.request_wrapper("GET", url, headers=headers)

# Make a GET request using the HTTP client to retrieve the SAML request.
response = http_client.get(url, headers=headers)

# Extract the required parameters from the SAML request.
saml_request = {
"base_url": user.get_base_url(extract_form_post_url(response.text)),
"post_url": extract_form_post_url(response.text),
"relay_state": extract_saml_relaystate(response.text),
"request": extract_saml_request(response.text, raw=True),
}

# Mask sensitive data in the logs for security.
user.add_sensitive_value_to_be_masked(saml_request["request"])

logger.debug(f"SAML request is {saml_request}")
return saml_request



def send_saml_request(saml_request, cookies):
"""Submit SAML request to IdP, and get the response back.
Expand Down

0 comments on commit bfc8194

Please sign in to comment.