简体   繁体   中英

Trouble understanding decorator

I want to check if the user is authenticated before he/she can access the method, so I wrote a decorator named authorize but only the decorator code is executed and even if the user is authenticated, the method is not called after it. here are the method and the decorator codes:

@authorize
def post(self, **kw):
    # store data in database after authentication done using @authorize


def authorize(f):
    def wrapper(*args, **kwargs):
        secret_key = config.get('auth_secret_key')
        auth_message = config.get('auth_message')
        if 'HTTP_TOKEN' not in request.environ:
            abort(401, detail='Authentication failed', passthrough='json')
        gibberish = request.environ['HTTP_TOKEN']
        if triple_des(secret_key).decrypt(gibberish, padmode=2).decode() != auth_message:
            abort(401, detail='Authentication failed', passthrough='json')
    return wrapper

If user has authentication problem, 401 is raised and request is aborted but if he is authenticated, the post method is not called. BTW, its my first time writing decorator so I might be completely wrong. Thanks for any answers

您实际上需要在包装器中调用该函数。

f(*args, **kwargs)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM