-
Notifications
You must be signed in to change notification settings - Fork 0
/
authentication.py
345 lines (259 loc) · 12.7 KB
/
authentication.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import flask_login as login
from configuration import app
from models import User
import os
from flask import request, send_file
from utils import error_response, success_data_jsonify, invalid_format
from flask import request
from constants import AUTHORIZATION_HEADER_FIELD, ADMIN_HEADER_FIELD
from passbook.models import Pass, Barcode, EventTicket, Location, BarcodeFormat
from configuration import MENLOHACKS_PASSBOOK_KEY_FILENAME
import datetime
import hashlib
@app.route('/user/ticket/<username>')
def get_ticket_admin(username):
if ADMIN_HEADER_FIELD not in request.headers:
return error_response(title="No password",
message='To check in a user, please provide the admin password',
code=401)
password = request.headers[ADMIN_HEADER_FIELD]
if password != os.environ['ADMIN_PASSWORD']:
return error_response(title="Invalid password",
message='To check in a user, please provide the admin password',
code=401)
user = User.objects(username=username).first()
if user is None:
return error_response(title="No user",
message='A user does not exist with this username',
code=404)
return generate_pass(user)
@app.route('/get_users')
def get_users():
if ADMIN_HEADER_FIELD not in request.headers:
return error_response(title="No password",
message='To check in a user, please provide the admin password',
code=401)
password = request.headers[ADMIN_HEADER_FIELD]
if password != os.environ['ADMIN_PASSWORD']:
return error_response(title="Invalid password",
message='To check in a user, please provide the admin password',
code=401)
users = User.objects(checked_in=True)
return success_data_jsonify([user.name for user in users])
@app.route('/user/ticket')
def get_ticket():
user = current_user()
if user is None:
return error_response(title="No user is currently logged in",
message="In get your ticket, you must be logged in",
code=401)
return generate_pass(user)
def generate_pass(user):
cardInfo = EventTicket()
cardInfo.addPrimaryField('name', user.name, 'Name')
cardInfo.addHeaderField('header', 'March 14-15, 2020', 'Menlo School') #change date for menlohacks vi
if user.school:
cardInfo.addSecondaryField('loc', user.school, 'School')
cardInfo.addSecondaryField('email', user.username, 'Email')
organizationName = 'MenloHacks'
passTypeIdentifier = 'pass.com.menlohacks.menlohacksv' #change passTypeIdentifier
teamIdentifier = 'YWVU2284UC'
passfile = Pass(cardInfo,
passTypeIdentifier=passTypeIdentifier,
organizationName=organizationName,
teamIdentifier=teamIdentifier)
passfile.labelColor = 'rgb(255,255,255)'
passfile.foregroundColor = 'rgb(255,255,255)'
passfile.relevantDate = '2019-03-09T10:00-11:00'
latitude = 37.453240
longitude = -122.191278
location = Location(latitude, longitude)
location.distance = 600
passfile.serialNumber = hashlib.sha256(user.username).hexdigest()
passfile.locations = [location]
passfile.barcode = Barcode(message=user.username, format=BarcodeFormat.QR)
dir = os.path.dirname(__file__)
passfile.addFile('icon.png', open(os.path.join(dir, 'passbook/icon.png'), 'r'))
passfile.addFile('[email protected]', open(os.path.join(dir, 'passbook/[email protected]'), 'r'))
passfile.addFile('[email protected]', open(os.path.join(dir, 'passbook/[email protected]'), 'r'))
passfile.addFile('logo.png', open(os.path.join(dir, 'passbook/logo.png'), 'r'))
passfile.addFile('[email protected]', open(os.path.join(dir, 'passbook/[email protected]'), 'r'))
passfile.addFile('[email protected]', open(os.path.join(dir, 'passbook/[email protected]'), 'r'))
passfile.addFile('background.png', open(os.path.join(dir, 'passbook/background.png'), 'r'))
passfile.addFile('[email protected]', open(os.path.join(dir, 'passbook/[email protected]'), 'r'))
passfile.addFile('[email protected]', open(os.path.join(dir, 'passbook/[email protected]'), 'r'))
key_path = 'secure/' + MENLOHACKS_PASSBOOK_KEY_FILENAME
key_filename = os.path.join(dir, key_path)
cert_filename = os.path.join(dir, 'passbook/certificate.pem') #change name once you make new certification
wwdr_filename = os.path.join(dir, 'passbook/WWDR.pem')
password = os.environ['PASSBOOK_PASSWORD']
# https://github.com/devartis/passbook/issues/1 to get cert and pem
file = passfile.create(cert_filename, key_filename, wwdr_filename, password)
file.seek(0)
return send_file(file, as_attachment=True,
attachment_filename='pass.pkpass',
mimetype='application/vnd.apple.pkpass')
@app.route('/user/checkin', methods=['POST'])
def check_in_user():
json = request.get_json()
if json is None:
return invalid_format()
if ADMIN_HEADER_FIELD not in request.headers:
return error_response(title="No password",
message='To check in a user, please provide the admin password',
code=401)
if 'username' not in json:
return error_response(title="No username provided",
message='To check in a user, please provide the username of the user to check in',
code=400)
username = json['username']
password = request.headers[ADMIN_HEADER_FIELD]
if str(os.environ['ADMIN_PASSWORD']) != str(password):
print(password)
print(password == "6XjMGH7d2VRXdRsENgZTKeBk")
print(len(password))
print(os.environ["ADMIN_PASSWORD"])
print(len(os.environ["ADMIN_PASSWORD"]))
print(os.environ["ADMIN_PASSWORD"] == "6XjMGH7d2VRXdRsENgZTKeBk")
return error_response(title="Invalid password",
message='To check in a user, please provide the correct shared password',
code=401)
user = User.objects(username=username).first()
if user is None:
return error_response(title="User does not exist",
message='The specified user does not exist.',
code=404)
if user.checked_in:
return error_response(title="Already checked in",
message='The user specified is already checked in. Please see an organizer.',
code=409)
user.check_in_times.append(datetime.datetime.now())
user.checked_in = True
user.save()
return success_data_jsonify(user.check_in_dictionary_representation(), code=200)
@app.route('/user/checkout', methods=['POST'])
def check_out_user():
json = request.get_json()
if json is None:
return invalid_format()
if ADMIN_HEADER_FIELD not in request.headers:
return error_response(title="No password",
message='To check in a user, please provide the admin password',
code=401)
if 'username' not in json:
return error_response(title="No username provided",
message='To check in a user, please provide the username of the user to check in',
code=400)
username = json['username']
password = request.headers[ADMIN_HEADER_FIELD]
if os.environ['ADMIN_PASSWORD'] != password:
return error_response(title="Invalid password",
message='To check in a user, please provide the correct shared password',
code=401)
user = User.objects(username=username).first()
if user is None:
return error_response(title="User does not exist",
message='The specified user does not exist.',
code=404)
if not user.checked_in:
return error_response(title="Never checked in",
message='The user specified never checked in. Please see an organizer.',
code=409)
user.check_out_times.append(datetime.datetime.now())
user.checked_in = False
user.save()
return success_data_jsonify(user.check_in_dictionary_representation(), code=200)
@app.route('/user/create', methods=['POST'])
def create_user():
json = request.get_json()
if json is None:
return invalid_format()
if 'username' not in json:
return error_response(title="No email provided",
message="To create an account, please provide your email",
code=400)
if 'password' not in json:
return error_response(title="No password provided",
message="To create an account, please provide a password",
code=400)
if 'name' not in json:
return error_response(title="No name provided",
message="To create an account, please provide your full name",
code=400)
username = request.json['username']
password = request.json['password']
name = request.json['name']
if len(username) == 0:
return error_response(title="Email is empty",
message="To create an account, please provide your email",
code=400)
if len(password) == 0:
return error_response(title="Password is empty",
message="To create an account, please provide a password",
code=400)
if len(name) == 0:
return error_response(title="Name is empty",
message="To create an account, please provide your full name",
code=400)
user = User.objects(username=username).first()
if user is not None:
return error_response(title="User already exists",
message="A user with this email address already exists. If you already have an account, plesae login instead.",
code=400)
user = User()
user.username = username
user.set_password(password)
user.name = name
user.save()
token = user.generate_auth_token()
return success_data_jsonify({'token' : token}, code=201)
@app.route('/user/login', methods=['POST'])
def login():
json = request.get_json()
if json is None:
return invalid_format()
if 'username' not in json:
return error_response(title='No email provided',
message='To login, please provide an email',
code=400)
if 'password' not in json:
return error_response(title='No password provided',
message='To login, please provide your password',
code=400)
username = json['username']
password = json['password']
if len(username) == 0:
return error_response(title="Email is empty",
message="To login, please provide your email",
code=400)
if len(password) == 0:
return error_response(title="Password is empty",
message="To login, please provide a password",
code=400)
user = User.objects(username=username).first()
if user is None:
return error_response(title="No user exists with the specified email",
message="If you mis-entered your email please try again. If you do not have an account, please find an organizer.",
code=400)
if User.validate_login(user.hashed_password, password):
token = user.generate_auth_token()
dictionary = user.dictionary_representation()
dictionary['token'] = token
return success_data_jsonify(dictionary)
else:
return error_response(title="Invalid password",
message="The password you entered is not valid. If you forgot your password, please find an organizer.",
code=401)
from flask import render_template, url_for
@app.route('/admin/login',methods=['GET','POST'])
def admin_login():
if request.method == 'GET':
return render_template('login.html')
# return redirect(url_for('index'))
def current_user():
if request is None:
return None
if AUTHORIZATION_HEADER_FIELD in request.headers:
token = request.headers[AUTHORIZATION_HEADER_FIELD]
return User.verify_auth_token(token)
return None