<s> from django . conf import settings <EOL> from django . db import models <EOL> from django . utils import timezone <EOL> from knox import crypto <EOL> from knox . settings import CONSTANTS , knox_settings <EOL> User = settings . AUTH_USER_MODEL <EOL> class AuthTokenManager ( models . Manager ) : <EOL> def create ( self , user , expires = knox_settings . TOKEN_TTL ) : <EOL> token = crypto . create_token_string ( ) <EOL> salt = crypto . create_salt_string ( ) <EOL> digest = crypto . hash_token ( token , salt ) <EOL> if expires is not None : <EOL> expires = timezone . now ( ) + expires <EOL> auth_token = super ( AuthTokenManager , self ) . create ( digest = digest , salt = salt , user = user , expires = expires ) <EOL> return token <EOL> class AuthToken ( models . Model ) : <EOL> objects = AuthTokenManager ( ) <EOL> digest = models . CharField ( max_length = CONSTANTS . DIGEST_LENGTH , primary_key = True ) <EOL> salt = models . CharField ( max_length = CONSTANTS . SALT_LENGTH , unique = True ) <EOL> user = models . ForeignKey ( User , null = False , blank = False , related_name = "<STR_LIT>" ) <EOL> created = models . DateTimeField ( auto_now_add = True ) <EOL> expires = models . DateTimeField ( null = True , blank = True ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . digest , self . user ) </s>
