-
Notifications
You must be signed in to change notification settings - Fork 8
/
accounts.py
232 lines (200 loc) · 7.85 KB
/
accounts.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
"""An example service authenticating with the Hub.
This serves `/services/accounts/`, authenticated with the Hub, showing the user their own info.
"""
import os
import pwd
import subprocess
from xkcdpass import xkcd_password
from getpass import getuser
from urllib.parse import urlparse
from tornado.ioloop import IOLoop
from tornado.httpserver import HTTPServer
from tornado.web import RequestHandler, Application
from jupyterhub.services.auth import HubAuthenticated
DEFAULT_EMAIL = "brynmawr.edu"
class AccountsHandler(HubAuthenticated, RequestHandler):
hub_users = None # the users allowed to access me (everyone can try)
def get(self):
user_model = self.get_current_user()
self.set_header('content-type', 'text/html')
if user_model is not None and user_model["admin"]:
self.write("""<!DOCTYPE html>
<html>
<body>
<h1>Create Jupyter Accounts</h1>
<p>Each line should be in one of the following forms:</p>
<pre>
username (assumes default email) OR
Real Name, [email protected] OR
Real Name, username (assumes default email) OR
Real Name, [email protected], username
</pre>
<form method="post" action="/services/accounts/">
<textarea rows="30" cols="70" name="usernames"></textarea>
<br><br>
<b>Professor name and email:</b> <input size="50" type="text" name="prof_email"/> <br/>
<b>Send Email:<b> <input type="checkbox" name="Send Email" value="send"></input><br/>
<b>Display Passwords:</b> <input type="checkbox" name="Display Passwords" value="display"></input> <br/> <br/>
<input type="submit" value="Create Accounts">
</form>
</body>
</html>
""")
else:
self.write("Not an admin!")
def post(self):
user_model = self.get_current_user()
usernames = self.get_argument('usernames').split("\n")
prof_email = self.get_argument('prof_email')
display = self.get_argument('Display Passwords', "") == "display"
send_email = self.get_argument('Send Email', "") == "send"
self.set_header('content-type', 'text/html')
if user_model is not None and user_model["admin"]:
self.process_lines(usernames, prof_email, send_email, display)
else:
self.write("Not an admin!")
def process_lines(self, lines, prof_email, send_email, display):
"""
filename is a file:
username OR
username@address OR
Real Name, email+username OR
Real Name, email+username@address OR
Real Name, email@address, username
"""
for line in lines:
line = line.strip()
if line.startswith("#") or line == "":
continue
data = [item.strip() for item in line.split(",")]
if len(data) == 1: # USERNAME/EMAIL
if "@" in data[0]:
email = data[0]
username = email.split("@")[0]
else:
username = data[0]
email = data[0] + "@" + DEFAULT_EMAIL
realname = "Jupyter User"
elif len(data) == 2: # REALNAME, USERNAME/EMAIL
if "@" in data[1]:
realname = data[0]
email = data[1]
username = email.split("@")[0]
else:
realname = data[0]
username = data[1]
email = data[1] + "@" + DEFAULT_EMAIL
elif len(data) == 3: # REALNAME, EMAIL, USERNAME
realname = data[0]
email = data[1]
username = data[2]
else:
self.write("invalid line: " + line + "; skipping! <br/><br/>")
continue
### Now, let's see if there is an account:
if not display:
self.write("processing: %s %s %s... <br/><br/>" % (username, realname, email))
account_info = get_user_info(username)
if account_info:
username = account_info[0]
realname = account_info[4]
self.write("Account exists! username: %s realname: %s <br/><br/>" %(username, realname))
#continue ## Do it anyway
# otherwise, make account
gecos = "%s <%s>" % (realname, email)
password = make_password("-w safe6 -n 4 --min 1 --max=6")
env = {
"username": username,
"gecos": gecos,
"password": password,
"prof_email": prof_email,
"email": email,
}
#print("env:", env)
system('useradd -m -d /home/{username} -c "{gecos}" {username}'.format(**env))
system('echo {username}:{password} | chpasswd'.format(**env))
if display:
self.write("""
<pre>
===============================================
Bryn Mawr College
Jupyter Computer Resource
{prof_email}
-----------------------------------------------
Computer: http://jupyter.brynmawr.edu
User: {gecos}
Username : {username}
Pass phrase : {password}
Email address: {email}
Note: you must type any spaces in pass phrase when you login.
%< ---------------------------------------------
</pre>
""".format(**env))
if send_email and env["email"]:
message = """
Welcome to Computing at Bryn Mawr College!
You are currently enrolled in a course using this resource.
You will be given details in class on how to login and use the system
using the following information.
Computer: https://jupyter.brynmawr.edu/
Username: {username}
Password: {password}
If you have any questions, please check with your instructor
{prof_email}
Thank you!
"""
env["message"] = message.format(**env)
system('echo -e "{message}" | mail -s "Bryn Mawr College - Jupyter Computer Resource" {email}'.format(**env))
def make_password(arg_string=None):
if arg_string is None:
arg_string = "-w safe6 -n 4 --min 1 --max=6"
argv = arg_string.split()
parser = xkcd_password.XkcdPassArgumentParser(prog="xkcdpass")
options = parser.parse_args(argv)
xkcd_password.validate_options(parser, options)
my_wordlist = xkcd_password.generate_wordlist(
wordfile=options.wordfile,
min_length=options.min_length,
max_length=options.max_length,
valid_chars=options.valid_chars)
if options.verbose:
xkcd_password.verbose_reports(my_wordlist, options)
return xkcd_password.generate_xkcdpassword(
my_wordlist,
interactive=options.interactive,
numwords=options.numwords,
acrostic=options.acrostic,
delimiter=options.delimiter)
def get_user_info(username):
"""
Returns pwd.struct_passwd(pw_name='baldwin01',
pw_passwd='x',
pw_uid=1544,
pw_gid=1544,
pw_gecos='Baldwin Student 01',
pw_dir='/home/baldwin01',
pw_shell='/bin/bash')
"""
retval = None
try:
retval = pwd.getpwnam(username)
except KeyError:
retval = None
return retval
def system(command):
#print("COMMAND: ", command)
proc = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
return out.decode().strip()
def main():
app = Application([
(os.environ['JUPYTERHUB_SERVICE_PREFIX'] + '/?', AccountsHandler),
(r'.*', AccountsHandler),
])
http_server = HTTPServer(app)
url = urlparse(os.environ['JUPYTERHUB_SERVICE_URL'])
http_server.listen(url.port, url.hostname)
IOLoop.current().start()
if __name__ == '__main__':
main()