<s> """<STR_LIT>""" <EOL> from django . core . signing import BadSignature <EOL> from django . utils . crypto import get_random_string , constant_time_compare <EOL> from sudo . settings import COOKIE_NAME , COOKIE_AGE , COOKIE_SALT <EOL> def grant_sudo_privileges ( request , max_age = COOKIE_AGE ) : <EOL> """<STR_LIT>""" <EOL> user = getattr ( request , '<STR_LIT:user>' , None ) <EOL> if user is None : <EOL> return <EOL> if not user . is_authenticated ( ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> token = get_random_string ( ) <EOL> request . session [ COOKIE_NAME ] = token <EOL> request . _sudo = True <EOL> request . _sudo_token = token <EOL> request . _sudo_max_age = max_age <EOL> return token <EOL> def revoke_sudo_privileges ( request ) : <EOL> """<STR_LIT>""" <EOL> request . _sudo = False <EOL> if COOKIE_NAME in request . session : <EOL> del request . session [ COOKIE_NAME ] <EOL> def has_sudo_privileges ( request ) : <EOL> """<STR_LIT>""" <EOL> if getattr ( request , '<STR_LIT>' , None ) is None : <EOL> try : <EOL> request . _sudo = ( <EOL> request . user . is_authenticated ( ) and <EOL> constant_time_compare ( <EOL> request . get_signed_cookie ( COOKIE_NAME , salt = COOKIE_SALT , max_age = COOKIE_AGE ) , <EOL> request . session [ COOKIE_NAME ] <EOL> ) <EOL> ) <EOL> except ( KeyError , BadSignature ) : <EOL> request . _sudo = False <EOL> return request . _sudo </s>
