-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
82 lines (59 loc) · 1.37 KB
/
utils.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
import hashlib
import hmac
import re
from config import *
USER_RE = re.compile(r"^[a-zA-Z0-9_-]{3,20}$")
PASSWORD_RE = re.compile(r"^.{3,20}$")
EMAIL_RE = re.compile(r'^[\S]+@[\S]+\.[\S]+$')
GROUP_RE = re.compile(r"^[a-zA-Z0-9_-]{3,20}$")
def render_str(template, **params):
"""
Return a string rendered by a Jinja2 template
"""
t = JINJA_ENV.get_template(template)
return t.render(params)
def hash_str(s):
"""
Return an HMAC hash of the string s using the secret
"""
return hmac.new(SECRET, s).hexdigest()
def make_secure_val(s):
"""
Return a secure value for cookie
"""
return "%s|%s" % (s, hash_str(s))
def check_secure_val(h):
"""
Check the integrity of a secure cookie
"""
val = h.split('|')[0]
if h == make_secure_val(val):
return val
def is_username_valid(username):
"""
Check if username is valid
"""
return username and USER_RE.match(username)
def is_password_valid(password):
"""
Check if password is valid
"""
return password and PASSWORD_RE.match(password)
def is_group_valid(group):
"""
Check if group name is valid
"""
return group and GROUP_RE.match(group)
def encrypt(password):
"""
Encrypt the password with SHA256
"""
return hashlib.sha256(password).hexdigest()
class UserError(Exception):
pass
class ValidationError(Exception):
pass
class InvalidCredentialsError(Exception):
pass
class AuthenticationError(Exception):
pass