-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathshawgo.py
189 lines (148 loc) · 5.69 KB
/
shawgo.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import urllib, urllib2, re
from cookies import Cookies
from urlparse import urlparse
class ShawGo:
"""
@class ShawGo
MSO class to handle authorization with the Shaw MSO
"""
@staticmethod
def getID():
return 'ShawGo'
@staticmethod
def authorize(streamProvider, username, password):
"""
Perform authorization with ShawGo
@param streamProvider the stream provider object. Needs to handle the
getAuthURI.
@param username the username to authenticate with
@param password the password to authenticate with
"""
uri = streamProvider.getAuthURI('ShawGo')
jar = Cookies.getCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))#,
#urllib2.HTTPHandler(debuglevel=1),
#urllib2.HTTPSHandler(debuglevel=1))
try:
resp = opener.open(uri)
except:
print "Unable get OAUTH location"
return None
Cookies.saveCookieJar(jar)
html = resp.read()
action = re.search('<form.*?action=\"(.*?)"', html, re.MULTILINE)
if not action:
print "Unable to find action form"
return None
action = action.group(1)
saml = re.search('<input.*?name=\"SAMLRequest\".*?value=\"(.*?)\"', html, re.MULTILINE)
if not saml:
print "Unable to find SAMLRequest."
return None
saml = saml.group(1)
relay = re.search('<input.*?name=\"RelayState\".*?value=\"(.*?)\"', html, re.MULTILINE)
if not relay:
print "Unable to find relay state."
return None
relay = relay.group(1)
return ShawGo.getAuthn(username, password, saml, relay, action)
@staticmethod
def getAuthn(username, password, saml, relay, url):
"""
Perform OAuth
@param username the username
@param password the password
@param saml the SAML request (form data)
@param relay the relay state (form data)
@param url the entitlement URL
"""
jar = Cookies.getCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
values = {'SAMLRequest' : saml,
'RelayState' : relay }
try:
resp = opener.open(url, urllib.urlencode(values))
except urllib2.URLError, e:
print e.args
return None
Cookies.saveCookieJar(jar)
html = resp.read()
action = re.search('<form.*?action=\"(.*?)".*?id=\"form3">', html, re.MULTILINE)
if not action:
print "Unable to find action form"
return None
action = action.group(1)
# ooc is username, email is shawemail, account number is shawdirect
idp = 'shawocc'
if username.isdigit():
idp = 'ShawDirect'
elif "@" in username:
idp = 'ShawEmail'
# rejig the URL
o = urlparse(url)
newurl = o.scheme + "://" + o.netloc + action
return ShawGo.getEntitlement(username.translate(None, "-"), password, saml, relay, idp, newurl)
@staticmethod
def getEntitlement(username, password, saml, relay, idp, url):
"""
Get entitlement
@param username the username
@param password the password
@param saml the SAML request (form data)
@param relay the relay state (form data)
@param idp the logon type (form data)
@param url the entitlement URL
"""
jar = Cookies.getCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
values = {'pf.username' : username,
'selectedPCV': idp,
'pf.pass' : password,
'pf.ok' : '',
'pf.cancel': ''}
try:
resp = opener.open(url, urllib.urlencode(values))
except urllib2.URLError, e:
print e.args
Cookies.saveCookieJar(jar)
html = resp.read()
action = re.search('<form.*?action=\"(.*?)"', html, re.MULTILINE)
if not action:
print "Unable to find action form"
return None
action = action.group(1)
relay = re.search('<input.*?name=\"RelayState\".*?value=\"(.*?)\"', html, re.MULTILINE)
if not relay:
print "Unable to find relay state."
return None
relay = relay.group(1)
# this is stupid -- you should use beautiful soup or something
idx = html.find("name=\"SAMLResponse\"")
html = html[idx:len(html)]
idx = html.find("value=\"") + 7
html = html[idx:len(html)]
idx = html.find("\"")
saml = html[0:idx].replace('\n', '')
return ShawGo.getSAMLAssertionConsumer(saml, relay, action)
@staticmethod
def getSAMLAssertionConsumer(saml, relay, url):
jar = Cookies.getCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
values = {'SAMLResponse' : saml,
'RelayState' : relay }
try:
resp = opener.open(url, urllib.urlencode(values))
except urllib2.URLError, e:
print e.args
Cookies.saveCookieJar(jar)
return ShawGo.completeBackgroundLogin()
@staticmethod
def completeBackgroundLogin():
jar = Cookies.getCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
try:
resp = opener.open('https://sp.auth.adobe.com/adobe-services/completeBackgroundLogin')
except urllib2.URLError, e:
print e.args
Cookies.saveCookieJar(jar)
return True