Skip to content

Instantly share code, notes, and snippets.

@sferich888
Last active January 13, 2023 06:59
Show Gist options
  • Save sferich888/eea6928d2c0e5fe5362f0b45a1183bec to your computer and use it in GitHub Desktop.
Save sferich888/eea6928d2c0e5fe5362f0b45a1183bec to your computer and use it in GitHub Desktop.
Example SAML implementation using Python Requests
from requests.auth import AuthBase
class SAML(AuthBase):
from bs4 import BeautifulSoup
"""Implemeents SSO with RH auth.redhat.com"""
def __init__(self, username, password):
# setup any auth-related data here
self.username = username
self.password = password
def response_hook(self, response, **kwargs):
if response.status_code == 401:
'''RHSSO login page that says 'no kerberos ticket found. click form submit to to be redirected to text based login form'''
soup = BeautifulSoup(response.text, 'html.parser')
_r = response.connection.send(requests.Request('GET', soup.form['action'],
cookies=response.cookies).prepare(), **kwargs)
soup2 = BeautifulSoup(_r.text, 'html.parser')
_r2 = response.connection.send(requests.Request('POST', soup2.form['action'], data={'username': self.username, 'password': self.password}, cookies=response.cookies).prepare(), **kwargs)
soup3 = BeautifulSoup(_r2.text, 'html.parser')
_r3 = response.connection.send(requests.Request('POST', soup3.form['action'], data={soup3.input['name']: soup3.input['value']}).prepare(), **kwargs)
return _r3
def __call__(self, r):
r.register_hook('response', self.response_hook)
return r
Example:
import requests
### from directory import class(SAML)
import getpass
s = requests.Session()
s.auth = SAML(raw_input("username: "), getpass.getpass("password: "))
r = s.get('https://example.domain.com')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment