Amin Etesamian Amin Etesamian - 4 years ago 129
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

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:

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,
is raised and request is aborted but if he is authenticated, the
method is not called. BTW, its my first time writing decorator so I might be completely wrong. Thanks for any answers

Answer Source

You need to actually call the function within your wrapper.

f(*args, **kwargs)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download