/
login.py
executable file
·220 lines (212 loc) · 7.04 KB
/
login.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# version 0.1
import cgi
import Cookie
import hashlib
import MySQLdb
import os
import re
import urllib
import urllib2
import settings
def trusted_users():
trusted_users = []
conn = MySQLdb.connect(host='sql-s7',
db='metawiki_p',
read_default_file='/home/mzmcbride/.my.cnf')
cursor = conn.cursor()
cursor.execute('''
/* login.py SLOW_OK */
SELECT DISTINCT
pl_title
FROM pagelinks
JOIN page
ON pl_from = page_id
WHERE page_namespace = 0
AND page_title = 'Toolserver/watcher'
AND pl_namespace IN (2,3);
''')
for row in cursor.fetchall():
trusted_users.append(re.sub('_', ' ', row[0]))
cursor.close()
conn.close()
return trusted_users
secret_key = settings.secret_key
login_status = 'You\'re not logged in.'
form = cgi.FieldStorage()
# Some debugging code
try:
logout_input = form["logout"].value
except:
logout_input = '0'
try:
language_input = form["language"].value
except:
language_input = None
try:
project_input = form["project"].value
except:
project_input = None
try:
user_input = form["user"].value
except:
user_input = None
try:
password_input = form["password"].value
except:
password_input = None
try:
tool_input = form["tool"].value
except:
tool_input = 'watcher'
if user_input is None and logout_input == '0':
if os.environ.has_key('QUERY_STRING') and tool_input == 'watcher':
if form.getvalue('titles') is not None:
return_string = '?' + os.environ['QUERY_STRING']
else:
return_string = ''
else:
return_string = ''
print """\
Content-Type: text/html;charset=utf-8\n
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="../style.css?2" type="text/css" />
<title>login</title>
</head>
<body>
<div class="header" id="main-title"><a href="/~mzmcbride/cgi-bin/login.py" title="login">login</a></div>
<form action="/~mzmcbride/cgi-bin/login.py%s" method="post">
<table id="input" class="inner-table">
<tr>
<th colspan="2" class="header">Input your <a href="/~magnus/tusc.php" title="Toolserver User Screening Control">TUSC</a> info.</th>
</tr>
<tr>
<th>Language</th>
<th style="padding:.3em;">
<input id="language" name="language" style="width:100%%;" />
</th>
</tr>
<tr>
<th>Project</th>
<th style="padding:.3em;">
<input id="project" name="project" style="width:100%%;" />
</th>
</tr>
<tr>
<th>Username</th>
<th style="padding:.3em;">
<input id="username" name="user" style="width:100%%;" />
</th>
</tr>
<tr>
<th>Password</th>
<th style="padding:.3em;">
<input id="password" type="password" name="password" style="width:100%%;" />
</th>
</tr>
<tr>
<th>Tool</th>
<th style="padding:.3em;">
<select id="tool" name="tool" style="width:100%%;">
<option value="watcher" selected="selected">watcher</option>
</select>
</th>
</tr>
<tr>
<td colspan="2" id="input-cell">
<input id="go-button" type="submit" value="Login!" style="width:100%%; margin:0" />
</td>
</tr>
</table>
</form>""" % return_string
elif (language_input
and project_input
and user_input
and password_input
and tool_input):
url = 'https://toolserver.org/~magnus/tusc.php'
values = {'language' : language_input,
'project' : project_input,
'user' : user_input,
'password' : password_input,
'botmode': '1',
'check': '1'}
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
valid_user_check = response.read()
if valid_user_check == '1':
if user_input in trusted_users(): # FIXME Need to check for global account
m = hashlib.md5()
m.update(user_input)
m.update(tool_input)
m.update(secret_key)
session = m.hexdigest()
cookie = Cookie.SimpleCookie()
cookie['mz_chocolate_chip'] = session
cookie['mz_chocolate_chip']['max-age'] = 60**5
cookie['mz_chocolate_chip']['path'] = '/~mzmcbride/watcher/'
if tool_input == 'watcher' and form.getvalue('titles') is not None:
return_message = ' Proceed to <a href="/~mzmcbride/watcher/?db=%s&titles=%s" title="watcher">watcher</a>?' % (form.getvalue('db'), form.getvalue('titles'))
else:
return_message = ' Proceed to <a href="/~mzmcbride/watcher/" title="watcher">watcher</a>?'
login_status = 'You\'re now logged in!%s' % return_message
print cookie
else:
login_status = 'Sorry, you\'re not on the <a href="//meta.wikimedia.org/wiki/Toolserver/watcher" title="access list">access list</a>.'
elif valid_user_check == '0':
login_status = 'Sorry, there\'s something wrong with your <a href="/~magnus/tusc.php" title="Toolserver User Screening Control">TUSC</a> info.'
else:
login_status = 'Sorry, there was an error. It was likely due to setting the wrong language or project. Please remember that sites like Wikimedia Commons use language: commons and project: wikimedia.'
print """\
Content-Type: text/html;charset=utf-8\n
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="../style.css?2" type="text/css" />
<title>login</title>
</head>
<body>
<div class="header" id="main-title"><a href="/~mzmcbride/cgi-bin/login.py" title="login">login</a></div>
<div style="margin:0 auto; width:50%%; text-align:center;">%s</div>""" % (login_status)
else:
if logout_input == '1':
if tool_input == 'watcher':
if form.getvalue('titles') is not None:
return_message = ' Return to <a href="/~mzmcbride/watcher/?db=%s&titles=%s" title="watcher">watcher</a>?' % (form.getvalue('db'), form.getvalue('titles'))
else:
return_message = ' Return to <a href="/~mzmcbride/cgi-bin/watcher.py" title="watcher">watcher</a>?'
else:
return_message = ''
login_status = 'You\'re now logged out. Congratulations.%s' % return_message
cookie = Cookie.SimpleCookie()
cookie['mz_chocolate_chip'] = 'invalid'
cookie['mz_chocolate_chip']['max-age'] = 0
cookie['mz_chocolate_chip']['path'] = '/~mzmcbride/watcher/'
print cookie
else:
login_status = 'Sorry, some piece of your login credentials is missing.'
print """\
Content-Type: text/html;charset=utf-8\n
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="../style.css?2" type="text/css" />
<title>login</title>
</head>
<body>
<div class="header" id="main-title"><a href="/~mzmcbride/cgi-bin/login.py" title="login">login</a></div>
<div style="margin:0 auto; width:50%%; text-align:center;">%s</div>""" % (login_status)
print """\
<div id="footer">
<div id="meta-info">
public domain<!--
--> <b>·</b> <!--
--><a href="//en.wikipedia.org/w/index.php?title=User_talk:MZMcBride&action=edit§ion=new" title="Report a bug">bugs</a>
</div>
</div>
</body>
</html>"""