This repository has been archived by the owner on Sep 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathsso.py
executable file
·321 lines (287 loc) · 11.5 KB
/
sso.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/usr/bin/python
#
# Copyright (C) 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This code is not supported by Google
#
""" Simple web server for testing Cookie Sites.
This is a simple web server for testing Forms Authentication and
Cookie Sites in a Google Search Appliance. Configure the Admin Console
as follows:
Crawl and Index
Crawl URLs:
Start crawling from: http://www.mycompany.com:8080/
Follow patterns: http://www.mycompany.com:8080/
Cookie Sites/Forms Authentication:
URL of the login page: http://www.mycompany.com:8080/secure
URL pattern for this rule: http://www.mycompany.com:8080/
Click "Create a New Cookie/Forms Authentication Rule"
Enter username "crawler" and password "crawler". Submit form.
Click "Save Cookie Rule and Close Window".
For Legacy Authn:
Serving > Forms Authentication
URL: http://www.mycompany.com:8080/secure
Cookie name: sso
If you want to use External Login you can check the
"Always redirect to external login server" option and point
the appliance to http://myhost.mydomain.com:8080/externalLogin
Note that the external login script will not work when
calling sso.py with the test_cookie_path option.
You must also set the --cookied_domain and --gsa_host options
to something like
--cookie_domain=".mydomain.com"
--gsa_host="search.mydomain.com"
For Security Manager (if using the Universal Login Form, not silent authentication):
Use the Default credential group.
Set up an Auth Mechanism as follows:
Credential Group: Default
Redirect to a form when sample URL check fails: checked
Mechanism name: test
Sample URL: http://www.mycompany.com:8080/secure
Options:
--cookie_cracking Return X-Username: and X-Groups for "/secure"
--cookie_domain= Specifies Cookie Domain.
Required if you access sso.py's login form directly.
-- gsa_host= The Fully Qualified Domain Name of the GSA.
Required if you use Legacy AuthN and no Referrer presents.
--test_cookie_path Mimics an Oblix server's cookie handling
--test_bug_950572 Tests whether bug has been fixed that
prevents more than one cookie being handled
--test_meta_refresh Tests whether appliance handles meta refresh
--use_ssl Run server as HTTP
This script requires the cherrypy v3 to be installed (v2 gives an error
since quickstart is not available).
"""
__author__ = '[email protected] (John Lowry)'
import cherrypy
import urllib
import sys
import time
import getopt
from socket import gethostname
FORM_COOKIE = "form"
SSO_COOKIE = "sso"
class Sso(object):
def __init__(self, cookie_cracking, cookie_domain, gsa_host, test_cookie_path, protocol, test_bug_950572, test_meta_refresh, delay):
self.cookie_cracking = cookie_cracking
self.test_cookie_path = test_cookie_path
self.protocol = protocol
self.test_bug_950572 = test_bug_950572
self.test_meta_refresh = test_meta_refresh
self.delay = delay
self.cookie_domain = ""
if cookie_domain:
self.cookie_domain = cookie_domain
self.search_host = ""
if gsa_host:
self.search_host = gsa_host;
def index(self):
secure_links = "Additional secure links: "
for i in range(1000):
secure_links += "<a href=\"secure?param=%s\">.</a>" % (i)
return ("<a href=\"public\">public</a><br>"
"<a href=\"secure\">secure</a><br>"
"<a href=\"authorized\">authorized</a><br>"
"<a href=\"logout\">logout</a><br>"
"%s" % (secure_links))
def form(self, path="/"):
if self.test_cookie_path and cherrypy.request.cookie.has_key(FORM_COOKIE):
form_cookie = cherrypy.request.cookie[FORM_COOKIE].value
self.redirect("login", "bad_cookie")
else:
return ("<h2>Login form</h2>"
"<form action=/login method=POST>"
"<input type=hidden name=path value=\"%s\">"
"Username: <input type=text name=login><br>"
"Password: <input type=password name=password><br>"
"<input type=submit>"
"</form>") % (path)
def get_host(self):
return cherrypy.request.headers["host"]
def login(self, login=None, password=None, path="", msg=None):
# print cherrypy.request.headers
if self.test_cookie_path:
if msg != None:
return "You got message: %s" % (msg)
if cherrypy.request.cookie.has_key(FORM_COOKIE):
form_cookie = cherrypy.request.cookie[FORM_COOKIE].value
# Firefox sends sso_cookie but Admin Console in 4.6.4.G.70
# and 5.0 does not due to bug #950572.
if self.test_bug_950572:
sso_cookie = cherrypy.request.cookie[SSO_COOKIE].value
if sso_cookie != "1":
return "Wrong value for %s cookie." % (SSO_COOKIE)
else:
return "You did not send a required cookie."
if login != None and login.strip() != "":
cherrypy.response.cookie[SSO_COOKIE] = urllib.quote(login.strip())
if self.cookie_domain:
cherrypy.response.cookie[SSO_COOKIE]['domain'] = self.cookie_domain
self.redirect(path)
else:
self.redirect("form?path=%s" % (path))
def redirect(self, path, msg=None):
cherrypy.response.status = 302
if path.startswith("http://") or path.startswith("https://"):
location = path
elif msg == None:
location = "%s://%s/%s" % (self.protocol, self.get_host(), path)
else:
location = "%s://%s/%s?msg=%s" % (self.protocol, self.get_host(), path, msg)
cherrypy.response.headers["location"] = location
def authenticate(self, path):
if cherrypy.request.cookie.has_key(SSO_COOKIE):
return cherrypy.request.cookie[SSO_COOKIE].value
else:
if self.test_cookie_path:
uri = "obrareq?path=%s" % (path)
else:
uri = "form?path=%s" % (path)
location = "%s://%s/%s" % (self.protocol, self.get_host(), uri)
raise cherrypy.HTTPRedirect(location)
def externalLogin (self, returnPath=None):
if not returnPath:
return "Need to specify returnPath"
if returnPath.startswith ("http://") or returnPath.startswith("https://"):
# Security Manager sets returnPath with the host part
path = returnPath
else:
# Legacy AuthN sets returnPath without the host part
#
if cherrypy.request.headers.has_key("Referer"):
returnHost = cherrypy.request.headers["Referer"].split('/')[2]
else:
returnHost = self.search_host
path = "http://" + returnHost + returnPath
self.authenticate(urllib.quote(path))
self.redirect(path)
def obrareq(self, path):
cherrypy.response.cookie[FORM_COOKIE] = "1"
cherrypy.response.cookie[FORM_COOKIE]["path"] = "/login"
self.redirect("form?path=%s" % (path))
def public(self):
return "Anyone can view this page. No authentication is required"
def secure(self, redirected=None, param=None):
# print cherrypy.request.headers
login = self.authenticate("secure")
if self.cookie_cracking:
cherrypy.response.headers["X-Username"] = login
cherrypy.response.headers["X-Groups"] = "ssopygroup"
refresh = ""
if self.test_meta_refresh:
this_url = "%s://%s/secure?redirected=1" % (self.protocol, self.get_host())
if not redirected:
refresh = """<meta http-equiv="Refresh" content ="0;URL=%s">""" % this_url
if self.delay:
time.sleep(self.delay)
return ("<html><head>%s</head><body>"
"You must be authenticated to view this page."
"You are authenticated as "%s". "
"The value of param is '%s'."
"</body></html>") % (refresh, login, param)
def authorized(self):
login = self.authenticate("authorized")
if login == "crawler":
return ("You must be authorized to view this page."
"You are authenticated as "%s".") % (login)
elif login == None or login == "":
self.redirect("form?path=authorized")
else:
raise cherrypy.HTTPError(401, "Unauthorized")
def logout(self):
cherrypy.response.cookie[SSO_COOKIE] = ""
if self.cookie_domain:
cherrypy.response.cookie[SSO_COOKIE]['domain'] = self.cookie_domain
cherrypy.response.cookie[SSO_COOKIE]['expires'] = 0
if self.test_cookie_path:
cherrypy.response.cookie[FORM_COOKIE] = ""
cherrypy.response.cookie[FORM_COOKIE]['expires'] = 0
return "You are logged out. Return to the <a href=/>index</a>."
def testcookiepath(self, value=None):
if value != None and value == "1":
self.test_cookie_path = True
elif value != None and value == "0":
self.test_cookie_path = False
return str(self.test_cookie_path)
def set_cookie_domain(self, value=None):
if value != None:
self.cookie_domain = value
return str(self.cookie_domain)
def set_search_host(self, value=None):
if value != None:
self.search_host = value
return str(self.search_host)
login.exposed = True
form.exposed = True
public.exposed = True
secure.exposed = True
index.exposed = True
authorized.exposed = True
logout.exposed = True
obrareq.exposed = True
testcookiepath.exposed = True
externalLogin.exposed = True
set_cookie_domain.exposed = True
set_search_host.exposed = True
def main(argv):
pass
if __name__ == '__main__':
cookie_cracking = False
cookie_domain = None
gsa_host = None
test_cookie_path = False
protocol = "http"
test_bug_950572 = False
test_meta_refresh = False
delay = 0
# By default cherrypy runs on localhost
# But we would normally want to run on the external interface
hostname = gethostname()
try:
opts, args = getopt.getopt(sys.argv[1:], None, ["cookie_cracking", "cookie_domain=", "gsa_host=",
"test_cookie_path", "use_ssl",
"test_bug_950572", "test_meta_refresh",
"port=", "delay=", "host="])
except getopt.GetoptError:
print "Invalid arguments"
sys.exit(1)
for opt, arg in opts:
if opt == "--cookie_cracking":
cookie_cracking = True
if opt=="--cookie_domain":
cookie_domain = arg
if opt == "--gsa_host":
gsa_host = arg
if opt == "--test_cookie_path":
test_cookie_path = True
if opt == "--use_ssl":
cherrypy.config.update({"global": {
"server.ssl_certificate": "ssl.crt",
"server.ssl_private_key": "ssl.key", }})
protocol = "https"
if opt == "--test_bug_950572":
test_bug_950572 = True
if opt == "--test_meta_refresh":
test_meta_refresh = True
if opt == "--port":
# By default, runs on port 8080
port = int(arg)
cherrypy.config.update({"global": { "server.socket_port": port }})
if opt == "--delay":
delay = int(arg)
if opt == "--host":
hostname = arg
cherrypy.config.update({"global": { "server.socket_host": hostname }})
cherrypy.quickstart(Sso(cookie_cracking, cookie_domain, gsa_host, test_cookie_path, protocol, test_bug_950572, test_meta_refresh, delay))