Skip to content

Instantly share code, notes, and snippets.

@gilesdring
Created October 7, 2016 18:50
Show Gist options
  • Save gilesdring/2955480425c8a10a4a3f6569ad8f08d8 to your computer and use it in GitHub Desktop.
Save gilesdring/2955480425c8a10a4a3f6569ad8f08d8 to your computer and use it in GitHub Desktop.
Simple python listener to dump a SAML Response
#!/usr/bin/env python3
'''
Dummy SAML listener to dump the SAMLResponse key from the request body
Start with `python3 saml_dump.py`
'''
import base64
import urllib.parse
from http.server import HTTPServer, BaseHTTPRequestHandler
def parse_content(content):
return urllib.parse.parse_qs(content)
class MyHandler(BaseHTTPRequestHandler):
def _dumpit(self):
if self.path == '/favicon.ico':
self.send_response(404)
return
self.send_response(200)
try:
content_length = int(self.headers['Content-Length'])
except TypeError:
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.wfile.write(b"No content")
return
raw_content = self.rfile.read(content_length).decode('utf-8')
post_data = parse_content(raw_content)
try:
raw_saml = post_data['SAMLResponse'][0]
except KeyError:
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.wfile.write(b"No SAML response!")
return
saml_response = base64.b64decode(raw_saml).decode('utf-8')
self.send_header('Content-type', 'application/xml')
self.end_headers()
self.wfile.write(saml_response.encode('utf-8'))
return
def do_GET(self):
self._dumpit()
def do_POST(self):
self._dumpit()
def run(server_class=HTTPServer, handler_class=MyHandler):
server_address = ('', 8000)
httpd = server_class(server_address, handler_class)
httpd.serve_forever()
if __name__ == '__main__':
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment