Created
November 2, 2010 19:52
-
-
Save paulosuzart/660185 to your computer and use it in GitHub Desktop.
Sample (working) for basic http auth on tornado
This file contains 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
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