<s> from calendar import timegm <EOL> from datetime import datetime <EOL> from django import forms <EOL> from django . contrib . auth import authenticate <EOL> from jwt_auth import settings <EOL> from jwt_auth . compat import User <EOL> jwt_payload_handler = settings . JWT_PAYLOAD_HANDLER <EOL> jwt_encode_handler = settings . JWT_ENCODE_HANDLER <EOL> jwt_decode_handler = settings . JWT_DECODE_HANDLER <EOL> jwt_get_user_id_from_payload = settings . JWT_PAYLOAD_GET_USER_ID_HANDLER <EOL> class JSONWebTokenForm ( forms . Form ) : <EOL> password = forms . CharField ( ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( JSONWebTokenForm , self ) . __init__ ( * args , ** kwargs ) <EOL> self . fields [ self . username_field ] = forms . CharField ( ) <EOL> @ property <EOL> def username_field ( self ) : <EOL> try : <EOL> return User . USERNAME_FIELD <EOL> except AttributeError : <EOL> return '<STR_LIT:username>' <EOL> def clean ( self ) : <EOL> cleaned_data = super ( JSONWebTokenForm , self ) . clean ( ) <EOL> credentials = { <EOL> self . username_field : cleaned_data . get ( self . username_field ) , <EOL> '<STR_LIT:password>' : cleaned_data . get ( '<STR_LIT:password>' ) <EOL> } <EOL> if all ( credentials . values ( ) ) : <EOL> user = authenticate ( ** credentials ) <EOL> if user : <EOL> if not user . is_active : <EOL> msg = '<STR_LIT>' <EOL> raise forms . ValidationError ( msg ) <EOL> payload = jwt_payload_handler ( user ) <EOL> if settings . JWT_ALLOW_REFRESH : <EOL> payload [ '<STR_LIT>' ] = timegm ( <EOL> datetime . utcnow ( ) . utctimetuple ( ) <EOL> ) <EOL> self . object = { <EOL> '<STR_LIT>' : jwt_encode_handler ( payload ) <EOL> } <EOL> else : <EOL> msg = '<STR_LIT>' <EOL> raise forms . ValidationError ( msg ) <EOL> else : <EOL> msg = '<STR_LIT>' <EOL> raise forms . ValidationError ( msg ) </s>
