Skip to content

Instantly share code, notes, and snippets.

@horatio-sans-serif
Created August 23, 2010 06:38
Show Gist options
  • Save horatio-sans-serif/544933 to your computer and use it in GitHub Desktop.
Save horatio-sans-serif/544933 to your computer and use it in GitHub Desktop.
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