forked from micahg/plugin.video.snnow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcogeco.py
164 lines (123 loc) · 4.77 KB
/
cogeco.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
import urllib, urllib2, re
from cookies import Cookies
class Cogeco:
"""
@class Cogeco
MSO class to handle authorization with the Cogeco MSO
"""
@staticmethod
def getID():
return 'Cogeco'
@staticmethod
def authorize(streamProvider, username, password):
"""
Perform authorization with Cogeco
@param streamProvider the stream provider object.
@param username the username to authenticate with
@param password the password to authenticate with
"""
uri = streamProvider.getAuthURI("Cogeco")
jar = Cookies.getCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))#,
#urllib2.HTTPHandler(debuglevel=1),
#urllib2.HTTPSHandler(debuglevel=1))
# TODO: move this into a method that can be reused.. multiple calls..
try:
resp = opener.open(uri)
except:
print "Unable to redirect to auth page."
return None
Cookies.saveCookieJar(jar)
html = resp.read()
# TODO: this could be made a function to to parse and return the value based on an expression
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 SAML request."
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 Cogeco.postAuthSaml(username, password, saml, relay, action)
@staticmethod
def postAuthSaml(username, password, saml, relay, 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=\"(.*?)\"', html, re.MULTILINE)
if not action:
print "Unable to find action form"
return None
action = "https://customer-services.cogeco.com" + action.group(1)
return Cogeco.postLogin(username, password, action)
@staticmethod
def postLogin(username, password, url):
jar = Cookies.getCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
values = {
'username' : username,
'password' : password
}
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)
saml = re.search('<input.*?name=\"SAMLResponse\".*?value=\"(.*?)\"', html, re.MULTILINE)
if not saml:
print "Unable to find SAML response."
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 Cogeco.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 Cogeco.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