forked from JonathanHolvey/sharepy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.py
170 lines (144 loc) · 6.76 KB
/
session.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import os
import re
import requests
import xml.etree.ElementTree as et
import pickle
from getpass import getpass
from datetime import datetime, timedelta
from xml.sax.saxutils import escape
# XML namespace URLs
ns = {
"wsse": "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
"psf": "http://schemas.microsoft.com/Passport/SoapServices/SOAPFault",
"d": "http://schemas.microsoft.com/ado/2007/08/dataservices",
"S": "http://www.w3.org/2003/05/soap-envelope"
}
def connect(site, username=None, password=None, auth_tld=None):
return SharePointSession(site, username, password, auth_tld)
def load(filename="sp-session.pkl"):
"""Load and return saved session object"""
session = SharePointSession()
session.__dict__.update(pickle.load(open(filename, "rb")))
if session._redigest() or session._spauth():
print("Connected to {} as {}".format(session.site, session.username))
# Re-save session to prevent it going stale
try:
session.save(filename)
except:
pass
return session
class SharePointSession(requests.Session):
"""A SharePy Requests session.
Provide session authentication to SharePoint Online sites
in addition to standard functionality provided by Requests.
Basic Usage::
>>> import sharepy
>>> s = sharepy.connect("example.sharepoint.com")
>>> s.get("https://exemple.sharepoint.com/_api/web/lists")
<Response [200]>
"""
def __init__(self, site=None, username=None, password=None, auth_tld=None):
super().__init__()
if site is not None:
self.site = re.sub(r"^https?://", "", site)
self.auth_tld = auth_tld or "com"
self.expire = datetime.now()
# Request credentials from user
self.username = username or input("Enter your username: ")
self.password = password
if self._spauth():
self._redigest()
self.headers.update({
"Accept": "application/json; odata=verbose",
"Content-type": "application/json; odata=verbose"
})
def _spauth(self):
"""Authorise SharePoint session by generating session cookie"""
# Load SAML request template
with open(os.path.join(os.path.dirname(__file__), "saml-template.xml"), "r") as file:
saml = file.read()
# Insert username and password into SAML request after escaping special characters
password = self.password or getpass("Enter your password: ")
saml = saml.format(username=escape(self.username),
password=escape(password),
site=self.site)
# Request security token from Microsoft Online
print("Requesting security token...\r", end="")
auth_domain = "login.microsoftonline." + self.auth_tld
try:
response = requests.post("https://{}/extSTS.srf".format(auth_domain), data=saml)
except requests.exceptions.ConnectionError:
print("Could not connect to", auth_domain)
return
# Parse and extract token from returned XML
try:
root = et.fromstring(response.text)
except et.ParseError:
print("Token request failed. The server did not send a valid response")
return
# Extract token from returned XML
token = root.find(".//wsse:BinarySecurityToken", ns)
# Check for errors and print error messages
if token is None or root.find(".//S:Fault", ns) is not None:
print("{}: {}".format(root.find(".//S:Text", ns).text,
root.find(".//psf:text", ns).text).strip().strip("."))
return
# Request access token from sharepoint site
print("Requesting access cookie... \r", end="")
response = requests.post("https://" + self.site + "/_forms/default.aspx?wa=wsignin1.0",
data=token.text, headers={"Host": self.site})
# Create access cookie from returned headers
cookie = self._buildcookie(response.cookies)
# Verify access by requesting page
response = requests.get("https://" + self.site + "/_api/web", headers={"Cookie": cookie})
if response.status_code == requests.codes.ok:
self.headers.update({"Cookie": cookie})
self.cookie = cookie
print("Authentication successful ")
return True
else:
print("Authentication failed ")
def _redigest(self):
"""Check and refresh site's request form digest"""
if self.expire <= datetime.now():
# Request site context info from SharePoint site
response = requests.post("https://" + self.site + "/_api/contextinfo",
data="", headers={"Cookie": self.cookie})
# Parse digest text and timeout from XML
try:
root = et.fromstring(response.text)
self.digest = root.find(".//d:FormDigestValue", ns).text
timeout = int(root.find(".//d:FormDigestTimeoutSeconds", ns).text)
self.headers.update({"Cookie": self._buildcookie(response.cookies)})
except:
print("Digest request failed")
return
# Calculate digest expiry time
self.expire = datetime.now() + timedelta(seconds=timeout)
return self.digest
def save(self, filename="sp-session.pkl"):
"""Serialise session object and save to file"""
mode = "r+b" if os.path.isfile(filename) else "wb"
pickle.dump(self.__dict__, open(filename, mode))
def post(self, url, *args, **kwargs):
"""Make POST request and include authorisation headers"""
if "headers" not in kwargs.keys():
kwargs["headers"] = {}
kwargs["headers"]["Authorization"] = "Bearer " + self._redigest()
return super().post(url, *args, **kwargs)
def getfile(self, url, *args, **kwargs):
"""Stream download of specified URL and output to file"""
# Extract file name from request URL if not provided as keyword argument
filename = kwargs.pop("filename", re.search(r"[^/]+$", url).group(0))
kwargs["stream"] = True
# Request file in stream mode
response = self.get(url, *args, **kwargs)
# Save to output file
if response.status_code == requests.codes.ok:
with open(filename, "wb") as file:
for chunk in response:
file.write(chunk)
return response
def _buildcookie(self, cookies):
"""Create session cookie from response cookie dictionary"""
return "rtFa=" + cookies["rtFa"] + "; FedAuth=" + cookies["FedAuth"]