<s> """<STR_LIT>""" <EOL> import functools <EOL> import inspect <EOL> import logging <EOL> from tornado import gen <EOL> from tornado . concurrent import Future <EOL> from . exceptions import HTTPError <EOL> def auth_required ( validator ) : <EOL> """<STR_LIT>""" <EOL> def _auth_decorator ( handler ) : <EOL> if inspect . isclass ( handler ) : <EOL> return _wrap_class ( handler , validator ) <EOL> return _auth_required ( handler , validator ) <EOL> return _auth_decorator <EOL> def _wrap_class ( request_handler , validator ) : <EOL> """<STR_LIT>""" <EOL> METHODS = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> for name in METHODS : <EOL> method = getattr ( request_handler , name ) <EOL> setattr ( request_handler , name , _auth_required ( method , validator ) ) <EOL> return request_handler <EOL> def _get_token ( request ) : <EOL> """<STR_LIT>""" <EOL> token = request . headers . get ( '<STR_LIT>' ) <EOL> if not token : <EOL> message = '<STR_LIT>' <EOL> logging . warning ( message ) <EOL> raise HTTPError ( <NUM_LIT> , message ) <EOL> return token <EOL> def _auth_required ( method , validator ) : <EOL> """<STR_LIT>""" <EOL> @ gen . coroutine <EOL> @ functools . wraps ( method ) <EOL> def wrapper ( self , * args , ** kwargs ) : <EOL> token = _get_token ( self . request ) <EOL> valid_token = yield validator ( token ) <EOL> if not valid_token : <EOL> message = '<STR_LIT>' . format ( token ) <EOL> logging . warning ( message ) <EOL> raise HTTPError ( <NUM_LIT> , message ) <EOL> result = method ( self , * args , ** kwargs ) <EOL> if isinstance ( result , Future ) : <EOL> result = yield result <EOL> raise gen . Return ( result ) <EOL> return wrapper <EOL> def authorized ( validator ) : <EOL> """<STR_LIT>""" <EOL> def _authorized_decorator ( method ) : <EOL> @ gen . coroutine <EOL> def wrapper ( self , * args , ** kwargs ) : <EOL> token = _get_token ( self . request ) <EOL> authorized = yield validator ( token , ** kwargs ) <EOL> if not authorized : <EOL> message = '<STR_LIT>' . format ( token ) <EOL> logging . warning ( message ) <EOL> raise HTTPError ( <NUM_LIT> , message ) <EOL> result = method ( self , * args , ** kwargs ) <EOL> if isinstance ( result , Future ) : <EOL> result = yield result <EOL> raise gen . Return ( result ) <EOL> return wrapper <EOL> return _authorized_decorator </s>
