-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.py
74 lines (49 loc) · 1.63 KB
/
helpers.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
import os
import re
import hmac
import jinja2
import hashlib
import random
from string import letters
from google.appengine.ext import db
# Jinja configuration
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_dir),
autoescape=True)
# Global functions
def render_str(template, **params):
t = jinja_env.get_template(template)
return t.render(params)
# Model keys
def users_key(group='default'):
return db.Key.from_path('users', group)
def blog_key(name='default'):
return db.Key.from_path('blogs', name)
# Validation
USER_RE = re.compile(r"^[a-zA-Z0-9_-]{3,20}$")
PASS_RE = re.compile(r"^.{3,20}$")
EMAIL_RE = re.compile(r'^[\S]+@[\S]+\.[\S]+$')
def valid_username(username):
return username and USER_RE.match(username)
def valid_password(password):
return password and PASS_RE.match(password)
def valid_email(email):
return not email or EMAIL_RE.match(email)
# Authentication
secret = 'fart'
def make_pw_hash(name, password, salt=None):
if not salt:
salt = make_salt()
h = hashlib.sha256(name + password + salt).hexdigest()
return '%s,%s' % (salt, h)
def make_salt(length=5):
return ''.join(random.choice(letters) for x in xrange(length))
def valid_pw(name, password, h):
salt = h.split(',')[0]
return h == make_pw_hash(name, password, salt)
def make_secure_val(val):
return '%s|%s' % (val, hmac.new(secret, val).hexdigest())
def check_secure_val(secure_val):
val = secure_val.split('|')[0]
if secure_val == make_secure_val(val):
return val