Amin Etesamian Amin Etesamian - 1 month ago 5
Python Question

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

Answer

You need to actually call the function within your wrapper.

f(*args, **kwargs)
Comments