Skip to content

Instantly share code, notes, and snippets.

@paulosuzart
Created November 2, 2010 19:52
Show Gist options
  • Save paulosuzart/660185 to your computer and use it in GitHub Desktop.
Save paulosuzart/660185 to your computer and use it in GitHub Desktop.
Sample (working) for basic http auth on tornado
def authenticated(auth):
def decore(f):
def _request_auth(handler):
handler.set_header('WWW-Authenticate', 'Basic realm=tmr')
handler.set_status(401)
handler.finish()
return False
@functools.wraps(f)
def new_f(*args):
handler = args[0]
auth_header = handler.request.headers.get('Authorization')
if auth_header is None:
return _request_auth(handler)
if not auth_header.startswith('Basic '):
return _request_auth(handler)
auth_decoded = base64.decodestring(auth_header[6:])
username, password = auth_decoded.split(':', 2)
if (auth(username, password)):
f(*args, username=username)
else:
_request_auth(handler)
return new_f
return decore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment