-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_helpers.py
More file actions
105 lines (73 loc) · 2.41 KB
/
file_helpers.py
File metadata and controls
105 lines (73 loc) · 2.41 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
import os
import jinja2
import hmac
import logging
import hashlib
import re
import random
import string
from models.ArticleModel import Article
from models.CommentModel import Comment
from models.UserModel import User
from models.LikeModel import Like
# Import google app engine datastore lib
from google.appengine.ext import db
# Sets the location of the templates folder that are contained in the home
# directory of this app.
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
# Envokes jinja2 environment, points it to the templates folder with the
# user input markup automatically escaped.
jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_dir),
autoescape=True)
# Strings
THE_SECRET = "TheSecretIs42"
user_err_string = "Not a valid username."
pass_err_string = "Not a valid password."
pass2_err_string = "Passwords do not match."
email_err_string = "Not a valid email."
def post_exists(function):
@wraps(function)
def wrapper(self, post_id):
article = Article.get_by_id(int(id))
return wrapper
def blog_key(name='default'):
"""
This is the key that defines a single blog and facilitiate multiple
blogs on the same site.
"""
return db.Key.from_path('blogs', name)
# Signups check
def checkUser(user):
user2 = re.compile(r"^[a-zA-Z0-9_-]{3,20}$")
return user2.match(user)
def checkPass(pass1):
pass2 = re.compile(r"^.{3,20}$")
return pass2.match(pass1)
def checkPass2(pass1, pass2):
return pass2 == pass1
def checkEmail(email):
email2 = re.compile(r"^[\S]+@[\S]+.[\S]+$")
return email2.match(email) or email == ""
# Cookie related
def hash_str(s):
return hmac.new(THE_SECRET, s).hexdigest()
def make_secure_val(s):
return "%s|%s" % (s, hash_str(s))
def check_secure_val(h):
val = h.split('|')[0]
if h == make_secure_val(val):
return val
# Password related
def make_salt():
return "".join(random.choice(string.letters) for x in xrange(5))
def make_pw_hash(name, pw, salt=make_salt()):
h = hashlib.sha256(name + pw + salt).hexdigest()
return '%s,%s' % (h, salt)
def valid_pw(name, pw, h):
# This compares input password with the input confirmation password
split = h.split(",")
hash = split[0]
salt = split[1]
logging.warning("h =" + h)
logging.warning("make = " + make_pw_hash(name, pw, salt))
return h == make_pw_hash(name, pw, salt)