Skip to content

Instantly share code, notes, and snippets.

@DevOpsCow
Created February 10, 2016 21:13
Show Gist options
  • Save DevOpsCow/d374300304d7beef8a03 to your computer and use it in GitHub Desktop.
Save DevOpsCow/d374300304d7beef8a03 to your computer and use it in GitHub Desktop.
import json
import requests
import kerberos
# To generate the kerberos ticket to attach it to the headers
class KerberosTicket:
def __init__(self, service):
__, krb_context = kerberos.authGSSClientInit(service)
kerberos.authGSSClientStep(krb_context, "")
self._krb_context = krb_context
self.auth_header = ("Negotiate "+kerberos.authGSSClientResponse(krb_context))
def verify_response(self, auth_header):
# Handle comma-separated lists of authentication fields
for field in auth_header.split(","):
kind, __, details = field.strip().partition(" ")
if kind.lower() == "negotiate":
auth_details = details.strip()
break
else:
raise ValueError("Negotiate not found in %s" % auth_header)
# Finish the Kerberos handshake
krb_context = self._krb_context
if krb_context is None:
raise RuntimeError("Ticket already used for verification")
self._krb_context = None
kerberos.authGSSClientStep(krb_context, auth_details)
kerberos.authGSSClientClean(krb_context)
# Build the kerberos ticket and build the headers
krbTicket = KerberosTicket("HTTP@MY_REST_API.COM")
dictHeaders = {"Authorization": krbTicket.auth_header, 'Accept': 'application/json', 'Content-Type': 'application/json'}
# make the request
strResourceBody = json.dumps({'test':'yes','beer':'always'})
strResourceURL = 'http://my_rest_api.com/endpoint'
responseResourcePost = requests.post(url=strResourceURL, verify=False, headers=dictHeaders, data=strResourceBody)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment