-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.py
46 lines (35 loc) · 1.19 KB
/
user.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
#!/usr/bin/python
import random
import hashlib
from string import letters
from google.appengine.ext import db
def users_key(group='default'):
return db.Key.from_path("users", group)
class User(db.Model):
name = db.StringProperty(required=True)
pw_hash = db.StringProperty(required=True)
email = db.StringProperty()
@classmethod
def by_name(cls, name):
u = User.all().filter('name =', name).get()
return u
@classmethod
def by_id(cls, user_id):
return User.get_by_id(user_id, parent=users_key())
@classmethod
def signup(cls, username, pw, email=None):
pw_hash = User.make_pw_hash(username, pw)
user = User(name=username, pw_hash=pw_hash, email=email)
@classmethod
def make_salt(cls, length=5):
return ''.join(random.choice(letters) for x in xrange(length))
@classmethod
def make_pw_hash(cls, name, pw, salt=None):
if not salt:
salt = User.make_salt()
h = hashlib.sha256(name + pw + salt).hexdigest()
return "%s,%s" % (salt, h)
@classmethod
def valid_hash(cls, name, pw, h):
salt = h.split(',')[0]
return h == User.make_pw_hash(name, pw, salt)