Skip to content

Instantly share code, notes, and snippets.

@killua8p
Forked from paulosuzart/tornado_basic_auth.py
Last active February 10, 2023 22:56
Show Gist options
  • Save killua8p/f2b3fac8ce0c854ae8d0 to your computer and use it in GitHub Desktop.
Save killua8p/f2b3fac8ce0c854ae8d0 to your computer and use it in GitHub Desktop.
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