OmniSciDB  340b00dbf6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
omnisci._samlutils Namespace Reference

Functions

def get_saml_response
 

Function Documentation

def omnisci._samlutils.get_saml_response (   idpurl,
  username,
  password,
  userformfield,
  passwordformfield,
  sslverify = True 
)
Obtains the SAML response from an Identity Provider
given the provided username and password.

Parameters
----------
idpurl : str
    The logon page of the SAML Identity Provider
username : str
    SAML Username
password : str
    SAML Password
userformfield : str
    The HTML form ID for the username
passwordformfield : str
    The HTML form ID for the password
sslverify : bool, optional
    Verify TLS certificates, by default True

Definition at line 14 of file _samlutils.py.

14 
15 ):
16  """
17  Obtains the SAML response from an Identity Provider
18  given the provided username and password.
19 
20  Parameters
21  ----------
22  idpurl : str
23  The logon page of the SAML Identity Provider
24  username : str
25  SAML Username
26  password : str
27  SAML Password
28  userformfield : str
29  The HTML form ID for the username
30  passwordformfield : str
31  The HTML form ID for the password
32  sslverify : bool, optional
33  Verify TLS certificates, by default True
34  """
35 
36  session = requests.Session()
37 
38  response = session.get(idpurl, verify=sslverify)
39  initialurl = response.url
40  formaction = initialurl
41  # print(page.content)
42 
43  # Determine if there's an action in the form, if there is,
44  # use it instead of the page URL
45  asearch = re.search(
46  r'<form\s+.*?\s+action' r'\s*=\s*\"(.*?)\".*?<\s*/form>',
47  response.text,
48  re.IGNORECASE | re.DOTALL,
49  )
50 
51  if asearch:
52  formaction = asearch.group(1)
53 
54  # If the action is a path not a URL, build the full
55  if not formaction.lower().startswith('http'):
56  parsedurl = urlparse(idpurl)
57  formaction = parsedurl.scheme + "://" + parsedurl.netloc + formaction
58 
59  # Un-urlencode the URL
60  formaction = unescape(formaction)
61 
62  formpayload = {userformfield: username, passwordformfield: password}
63 
64  response = session.post(formaction, data=formpayload, verify=sslverify)
65 
66  samlresponse = None
67  ssearch = re.search(
68  r'<input\s+.*?\s+name\s*=\s*'
69  r'\"SAMLResponse\".*?\s+value=\"(.*?)\".*?\/>',
70  response.text,
71  re.IGNORECASE | re.DOTALL,
72  )
73  if ssearch:
74  samlresponse = ssearch.group(1)
75  # Remove any whitespace, some providers include
76  # new lines in the response (!)
77  re.sub(r"[\r\n\t\s]*", "", samlresponse)
78 
79  if not samlresponse:
80  raise ValueError('No SAMLResponse found in response.')
81 
82  return samlresponse