Skip to content

Instantly share code, notes, and snippets.

@kurohai
Forked from juntalis/spauth.py
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save kurohai/b14b08074805734b4949 to your computer and use it in GitHub Desktop.

Select an option

Save kurohai/b14b08074805734b4949 to your computer and use it in GitHub Desktop.
import os
import requests
import re
from pprint import pprint
from htmlentitydefs import name2codepoint
from simplejson import loads
class spauth:
def __init__(self, url=None, username=None, password=None):
self.options = dict()
self.options['username'] = username
self.options['password'] = password
self.options['endpoint'] = self.format_login(url)
self.token = self.request_token()
result = requests.post(self.options['endpoint'], self.token)
self.cookie = 'FedAuth=' + result.cookies['FedAuth'] + '; rtFa=' + result.cookies['rtFa']
def format_login(self, url):
if url.endswith('/'):
url.replace('/', '')
if not url.endswith('_forms/default.aspx?wa=wsignin1.0'):
url = url + '/_forms/default.aspx?wa=wsignin1.0'
return url
def unescape(self, s):
name2codepoint['#39'] = 39
return re.sub('&(%s);' % '|'.join(name2codepoint),
lambda m: unichr(name2codepoint[m.group(1)]), s)
def generate_saml(self):
# SAML.xml from https://github.com/lstak/node-sharepoint
content = '<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><s:Header><a:Action s:mustUnderstand="1">http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue</a:Action><a:ReplyTo><a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address></a:ReplyTo><a:To s:mustUnderstand="1">https://login.microsoftonline.com/extSTS.srf</a:To><o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><o:UsernameToken><o:Username>[username]</o:Username><o:Password>[password]</o:Password></o:UsernameToken></o:Security></s:Header><s:Body><t:RequestSecurityToken xmlns:t="http://schemas.xmlsoap.org/ws/2005/02/trust"><wsp:AppliesTo xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"><a:EndpointReference><a:Address>[endpoint]</a:Address></a:EndpointReference></wsp:AppliesTo><t:KeyType>http://schemas.xmlsoap.org/ws/2005/05/identity/NoProofKey</t:KeyType><t:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</t:RequestType><t:TokenType>urn:oasis:names:tc:SAML:1.0:assertion</t:TokenType></t:RequestSecurityToken></s:Body></s:Envelope>'
for k in self.options.keys():
content = content.replace('['+k+']', self.options[k])
return content
def request_token(self):
saml = self.generate_saml()
result = requests.post('https://login.microsoftonline.com/extSTS.srf', data=saml)
return self.unescape(result.text.split('<wsse:BinarySecurityToken Id="Compact0">')[1].split('</wsse:BinarySecurityToken>')[0])
def get(self, url):
return requests.get(url, headers={'Cookie':self.cookie, 'Accept':'application/json'})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment