<s> from datetime import date <EOL> from django . conf import settings <EOL> from django . utils . http import int_to_base36 , base36_to_int <EOL> from django . utils . crypto import constant_time_compare , salted_hmac <EOL> from django . utils import six <EOL> class PasswordResetTokenGenerator ( object ) : <EOL> """<STR_LIT>""" <EOL> def make_token ( self , user ) : <EOL> """<STR_LIT>""" <EOL> return self . _make_token_with_timestamp ( user , self . _num_days ( self . _today ( ) ) ) <EOL> def check_token ( self , user , token ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> ts_b36 , hash = token . split ( "<STR_LIT:->" ) <EOL> except ValueError : <EOL> return False <EOL> try : <EOL> ts = base36_to_int ( ts_b36 ) <EOL> except ValueError : <EOL> return False <EOL> if not constant_time_compare ( self . _make_token_with_timestamp ( user , ts ) , token ) : <EOL> return False <EOL> if ( self . _num_days ( self . _today ( ) ) - ts ) > settings . PASSWORD_RESET_TIMEOUT_DAYS : <EOL> return False <EOL> return True <EOL> def _make_token_with_timestamp ( self , user , timestamp ) : <EOL> ts_b36 = int_to_base36 ( timestamp ) <EOL> key_salt = "<STR_LIT>" <EOL> login_timestamp = user . last_login . replace ( microsecond = <NUM_LIT:0> , tzinfo = None ) <EOL> value = ( six . text_type ( user . pk ) + user . password + <EOL> six . text_type ( login_timestamp ) + six . text_type ( timestamp ) ) <EOL> hash = salted_hmac ( key_salt , value ) . hexdigest ( ) [ : : <NUM_LIT:2> ] <EOL> return "<STR_LIT>" % ( ts_b36 , hash ) <EOL> def _num_days ( self , dt ) : <EOL> return ( dt - date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) ) . days <EOL> def _today ( self ) : <EOL> return date . today ( ) <EOL> default_token_generator = PasswordResetTokenGenerator ( ) </s>
