This repository was archived by the owner on Mar 1, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathgaeusers.py
More file actions
386 lines (334 loc) · 11.6 KB
/
Copy pathgaeusers.py
File metadata and controls
386 lines (334 loc) · 11.6 KB
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# -*- coding: utf-8 -*-
import os
import random
import re
import hashlib
from google.appengine.ext import db
from google.appengine.api import mail
from google.appengine.ext.webapp import template
from google.appengine.api import memcache
class Users(db.Model):
email = db.StringProperty(required=True)
password = db.StringProperty()
active = db.BooleanProperty()
created = db.DateTimeProperty(auto_now_add=True)
logins = db.IntegerProperty()
lastlogin = db.DateTimeProperty(auto_now=True)
language = db.StringProperty(default='en')
class ActiveLinks(db.Model):
user = db.StringProperty()
link = db.StringProperty()
class PasswordLinks(db.Model):
user = db.StringProperty()
link = db.StringProperty()
class GaeUsers():
"""
GaeUsers
"""
def __init__(self, options):
"""
Init gaeusers with options.
Parameters
----------
options - dict
appid
mailstring
crypt
crypt_rounds
password_salt
"""
self.backlink = 'https://' + options['appid'] + '.appspot.com/conform?link='
self.passlink = 'https://' + options['appid'] + '.appspot.com/setpassword?link='
self.password_salt = options['password_salt']
self.crypt_rounds = options['crypt_rounds']
self.mailstring = options['mailstring']
self.crypt = options['crypt']
def check_userkey(self, key):
"""
Check if user key exists in memcache.
Parameters
----------
key: key in memcache
"""
return memcache.get(key)
def set_userkey(self, email, db_key, set_cache):
"""
Set user key.
"""
userkey = email + '_' + str(db_key)
userkey = self.crypt_string(userkey)
if set_cache == True:
memcache.add(userkey, str(db_key))
return userkey
def get_useremail(self, key):
"""
Get user email.
Parameters
----------
key: key in memcache
Returns
-------
Email from the user as string.
"""
key = memcache.get(key)
u_query = db.GqlQuery("SELECT * FROM Users WHERE __key__ = :1", db.Key(key))
uresult = u_query.get()
return str(uresult.email)
def get_passworduser(self, link):
"""
Get user who want to change password.
Parameters
----------
link: link in data store
Returns
-------
Returns the user key as string or None.
"""
u_query = db.GqlQuery("SELECT * FROM PasswordLinks WHERE link = :1", str(link))
uresult = u_query.get()
if uresult:
return str(uresult.user)
else:
return None
def get_user(self, key):
"""
Get a user from data store.
Parameters
----------
key: key in the data store
Returns
-------
Users data store object.
"""
u_query = db.GqlQuery("SELECT * FROM Users WHERE __key__ = :1", db.Key(key))
uresult = u_query.get()
return uresult
def crypt_string(self, string):
"""
Crypt string
Parameters
----------
string: string to be crypt
Returns
-------
A crypt string.
"""
if self.crypt == 'md5':
for i in range(self.crypt_rounds):
crypt_string = hashlib.md5(self.password_salt + string).hexdigest()
return crypt_string
elif self.crypt == 'sha1':
for i in range(self.crypt_rounds):
crypt_string = hashlib.sha1(self.password_salt + string).hexdigest()
return crypt_string
def register(self, email, password, repassword, lang, subject):
"""
Register a user.
Parameters
----------
email: email to register
password: password to register
repassword: repeat of password to register
lang: current language
subject: email subject
Returns
-------
dict - Response
register
error
key
"""
if email != '':
check = False
if len(email) > 5:
if re.match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", email) != None:
if password == repassword:
check = True
else:
return dict(register=False, error="not equal")
if check == False:
return dict(register=False, error="not valid")
else:
query = db.GqlQuery("SELECT * FROM Users WHERE email= :1", email)
result = query.get()
if result:
return dict(register=False, error="present")
else:
password = self.crypt_string(password)
u = Users(email = email, password = password, active = False, language = lang)
u.put()
link_key = str( random.randrange(0, 10001, 2) + 1000 )
link_key = self.crypt_string(link_key)
link = self.backlink + link_key
l = ActiveLinks(user=str(u.key()),link=link_key)
l.put()
userkey = self.set_userkey(email, str(u.key()), False)
tpl = 'templates/emails/activate_' + lang + '.html'
messagebody = template.render(tpl, {'link': link})
mail.send_mail(sender=self.mailstring, to="<"+email+">", subject=subject, body=messagebody)
return dict(register=True, key=userkey)
else:
return dict(register=False, error="empty")
def lose_password(self, email, lang, subject):
"""
Lose password request.
Parameters
----------
email: email from the requesting user
lang: selected languageto select the right template
subject: mail subject
Returns
-------
dict - Response
response
"""
u_query = db.GqlQuery("SELECT * FROM Users WHERE email = :1", email)
uresult = u_query.get()
if uresult:
p_query = db.GqlQuery("SELECT * FROM PasswordLinks WHERE user = :1", str(uresult.key()))
presult = p_query.get()
if presult:
link_key = presult.link
else:
link_key = str( random.randrange(0, 10001, 2) + 1000 )
link_key = self.crypt_string(link_key)
l = PasswordLinks(user=str(uresult.key()), link=link_key)
l.put()
link = self.passlink + link_key
tpl = 'templates/emails/losepassword_' + lang + '.html'
messagebody = template.render(tpl, {'link':link})
mail.send_mail(sender=self.mailstring, to="<"+uresult.email+">", subject=subject, body=messagebody)
return dict(response=True)
else:
return dict(response="empty")
def set_password(self, link, password, repassword):
"""
Set a password.
Parameters
----------
password: the new password
repassword: the new password as repeat
Returns
-------
dict - Response
response
"""
query = db.GqlQuery("SELECT * FROM PasswordLinks WHERE link = :1", link)
result = query.get()
if result:
if password != repassword:
return dict(response='not equal')
else:
u_query = db.GqlQuery("SELECT * FROM Users WHERE __key__ = :1", db.Key(result.user))
uresult = u_query.get()
password = self.crypt_string(password)
uresult.password = password
uresult.put()
db.delete(result)
return dict(response='set')
else:
return dict(response='not present')
def change_password(self, userkey, passwordold, newpassword, renewpassword):
"""
Change a password.
Parameters
----------
passwordold: the current password
newpassword: the new password
renewpassword: the new password as repeat
Returns
-------
dict - Response
response
"""
key = memcache.get(userkey)
u_query = db.GqlQuery("SELECT * FROM Users WHERE __key__ = :1", db.Key(key))
uresult = u_query.get()
passwordold = self.crypt_string(passwordold)
if uresult.password == passwordold:
if newpassword == renewpassword:
newpassword = self.crypt_string(newpassword)
uresult.password = newpassword
uresult.put()
return dict(response="change")
else:
return dict(response="not equal")
else:
return dict(response="wrong")
def conform(self, link):
"""
Conform registration and activate the user.
Parameters
----------
link: link send via email
Returns
-------
dict - Response
response
"""
query = db.GqlQuery("SELECT * FROM ActiveLinks WHERE link = :1", link)
result = query.fetch(1)
if result:
u_query = db.GqlQuery("SELECT * FROM Users WHERE __key__ = :1", db.Key(result[0].user))
uresult = u_query.get()
uresult.active = True
db.put(uresult)
db.delete(result)
return dict(response=True)
else:
return dict(response=False)
def login(self, email, password):
"""
Authorize a user with credentials.
Parameters
----------
email: Email from user
password: Password from user
Returns
-------
dict - Response
login
key
"""
if email != '' and password != '':
password = self.crypt_string(password)
query = db.GqlQuery("SELECT * FROM Users WHERE email = :1 AND password = :2", email, password)
result = query.get()
if result:
if result.active == True:
if email == result.email:
if result.logins != None:
result.logins = result.logins + 1
else:
result.logins = 1
result.put()
userkey = self.set_userkey(email, str(result.key()), True)
return dict(login=True, key=userkey)
else:
return dict(login=False)
else:
return dict(login="not active")
else:
return dict(login="unknown")
else:
return dict(login="empty")
def logout(self, key):
"""
Delete key from memcache.
Parameters
----------
key: key in memcache to delete
"""
memcache.delete(key)
def deleteaccount(self, key):
"""
Delete a user account.
Parameters
----------
key: key in memcache for the user to delete
"""
userkey = memcache.get(key)
u_query = db.GqlQuery("SELECT * FROM Users WHERE __key__ = :1", db.Key(userkey))
user = u_query.get()
db.delete(user)
memcache.delete(key)