自定义基本身份验证功能不适用于Bottle hook

时间:2018-08-02 02:44:58

标签: python-2.7 bottle

我无法使用bottle.hook('before_request')来完成这项工作,知道基本的身份验证弹出窗口为何从不显示吗?

from bottle import *

auth_enabled = True

@hook('before_request')
def custom_auth_basic():
    def decorator(func):
        def wrapper(*a, **ka):
            if auth_enabled:
                user, password = request.auth or (None, None)
                if user is None or not check_credentials(user, password):
                    err = HTTPError(401, "Access denied")
                    err.add_header('WWW-Authenticate', 'Basic realm="Private"')
                    return err
                return func(*a, **ka)
            else:
                return func(*a, **ka)

        return wrapper
    return decorator

def check_credentials(user, pw):
    username = "test"
    password = "test"
    if pw == password and user == username:
        return True
    return False

@route('/')
def root():
    return Response("Test")


run(host='localhost', port=8080)

我无法使其正常工作,并且说实话,我不完全理解这3个嵌套函数以及如何调用它们。它基于我之前遇到的一个问题:Enable or disable @auth_basic() programmatically

谢谢!

0 个答案:

没有答案