Last active
December 16, 2015 22:20
-
-
Save pauricthelodger/1b8d52d9f006a9b6e32b 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
APP_ROOT = os.path.abspath(os.path.dirname(__file__)) | |
DATABASE = "sqlite:///%s" % os.path.join(APP_ROOT, 'wiki.sqlite') |
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 request, HTTPError | |
from modelapi import * | |
def check_login(username, password): | |
pw_hash = sha256_uhex(password) | |
usr = User.get_by(name=username.decode('utf-8')) | |
if usr is None: | |
return False | |
return usr.password == pw_hash | |
def auth_required(check_func=check_login, realm='bottle-authentication'): | |
""" | |
Decorator for basic authentication. | |
"check_func" has to be a callable object with two | |
arguments ("username" and "password") and has to return | |
a bool value if login was sucessful or not. | |
""" | |
def decorator(view): | |
def wrapper(*args, **kwargs): | |
try: | |
user, password = request.auth | |
except (TypeError, AttributeError): | |
# catch AttributeError because of bug in bottle | |
auth = False | |
else: | |
auth = check_func(user, password) | |
if auth: | |
return view(*args, **kwargs) | |
return HTTPError(401, 'Access denied!', | |
header={'WWW-Authenticate': 'Basic realm="%s"' % realm}) | |
return wrapper | |
return decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment