Created
August 23, 2010 06:38
-
-
Save horatio-sans-serif/544933 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bottle import get, run, debug, send_file, request, hook, abort, post | |
import os, redis, hashlib | |
r = redis.Redis() | |
OPEN_URLS = ( '/', '/users/new' ) | |
@hook('before_request') | |
def require_authenticated_requests(): | |
""" | |
Store users in Redis. | |
emails => hash of email address to user id | |
users:$id => hash of user info; passhash, salt | |
""" | |
if request.fullpath in OPEN_URLS: return | |
auth = request.auth | |
if not auth: abort(403) | |
uid = r.hget('emails', auth[0]) | |
if not uid: abort(401, 'Invalid user') | |
passhash, salt = r.hmget('users:%d' % int(uid), [ 'passhash', 'salt' ]) | |
if not passhash or not salt: abort(401, 'Invalid user') | |
if hashlib.sha1(salt+auth[1]).hexdigest() != passhash: abort(401, 'Invalid password') | |
request.userid = int(uid) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment