Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/sharepy/auth/spol.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from getpass import getpass
from datetime import datetime, timedelta
from urllib.parse import urlparse
from xml.sax.saxutils import escape
import xml.etree.ElementTree as et

Expand All @@ -21,7 +22,15 @@ def __init__(self, *args, **kwargs):

def login(self, site):
"""Perform authentication steps"""
self.site = site
# Having to add a scheme here as it's pre-removed but needed to force urllib to
# do the right thing.
parsed_site = urlparse("https://" + site)
self.fqdn = parsed_site.netloc
self.site_path = ""
if parsed_site.path:
self.site_path = parsed_site.path
if not self.site_path.endswith("/"):
self.site_path += "/"
self._get_token()
self._get_cookie()
self._get_digest()
Expand All @@ -34,7 +43,7 @@ def _get_token(self):
password = self.password or getpass('Enter your password: ')
saml = templates.load('spol-token.saml').format(username=escape(self.username),
password=escape(password),
site=self.site)
site=self.fqdn)

# Request security token from Microsoft Online
response = requests.post(self.login_url, data=saml)
Expand All @@ -53,13 +62,13 @@ def _get_token(self):

def _get_cookie(self):
"""Request access cookie from sharepoint site"""
cookie_url = f'https://{self.site}/_forms/default.aspx?wa=wsignin1.0'
response = requests.post(cookie_url, data=self.token, headers={'Host': self.site})
cookie_url = f'https://{self.fqdn}/_forms/default.aspx?wa=wsignin1.0'
response = requests.post(cookie_url, data=self.token, headers={'Host': self.fqdn})

# Create access cookie from returned headers
cookie = self._buildcookie(response.cookies)
# Verify access by requesting page
test_url = f'https://{self.site}/_api/web'
test_url = f'https://{self.fqdn}/_api/web'
response = requests.get(test_url, headers={'Cookie': cookie})

if response.status_code == requests.codes.ok:
Expand All @@ -71,7 +80,7 @@ def _get_digest(self):
"""Check and refresh sites cookie and request digest"""
if self.expire <= datetime.now():
# Request site context info from SharePoint site
digest_url = f'https://{self.site}/_api/contextinfo'
digest_url = f'https://{self.fqdn}{self.site_path}_api/contextinfo'
response = requests.post(digest_url, data='', headers={'Cookie': self.cookie})
# Parse digest text and timeout from XML
try:
Expand Down