Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set expiration time to sample django jwt token

I am trying to create a manual token and I would like to add expiration time.from here =>Documentation

here=>

from rest_framework_simplejwt.tokens import RefreshToken


refresh = RefreshToken.for_user(user)

refresh.set_exp(lifetime=datetime.timedelta(days=10))
# refresh.lifetime = datetime.timedelta(days=10)

return Response ({            
                    'access': str(refresh.access_token),'refresh':str(refresh),"status":"success"
                    })

here is setting.py=>

JWT_AUTH = {
    # how long the original token is valid for
    'ACCESS_TOKEN_LIFETIME': datetime.timedelta(days=2),

    # allow refreshing of tokens
    'JWT_ALLOW_REFRESH': True,

    # this is the maximum time AFTER the token was issued that
    # it can be refreshed.  exprired tokens can't be refreshed.
    'REFRESH_TOKEN_LIFETIME': datetime.timedelta(days=7),
}

but why this access token is expired after 5 min even I added 10 days? How can I add expiration time?

This method is created for authenticating with email and password. because default authentication is using user id and password. Is there any way to authenticate with email and password in drf sample jwt?

like image 523
Loran Avatar asked Nov 23 '25 03:11

Loran


2 Answers

Hey you can decide to use django-rest-framework-simplejwt library or rest_framework_jwt

For django-rest-framework-simplejwt use this way in your settings.py

SIMPLE_JWT = {
'ACCESS_TOKEN_LIFETIME': timedelta(days=10),
'REFRESH_TOKEN_LIFETIME': timedelta(days=20),
'ROTATE_REFRESH_TOKENS': False,
'BLACKLIST_AFTER_ROTATION': True,

'ALGORITHM': 'HS256',
'SIGNING_KEY': settings.SECRET_KEY,
'VERIFYING_KEY': None,
'AUDIENCE': None,
'ISSUER': None,

'AUTH_HEADER_TYPES': ('Bearer',),
'USER_ID_FIELD': 'id',
'USER_ID_CLAIM': 'user_id',

'AUTH_TOKEN_CLASSES': ('rest_framework_simplejwt.tokens.AccessToken',),
'TOKEN_TYPE_CLAIM': 'token_type',

'JTI_CLAIM': 'jti',
'TOKEN_USER_CLASS': 'rest_framework_simplejwt.models.TokenUser',

'SLIDING_TOKEN_REFRESH_EXP_CLAIM': 'refresh_exp',
'SLIDING_TOKEN_LIFETIME': timedelta(days=10),
'SLIDING_TOKEN_REFRESH_LIFETIME': timedelta(days=20),
}

For rest_framework_jwt use this way in your settings.py

JWT_AUTH = {
'JWT_ENCODE_HANDLER':
'rest_framework_jwt.utils.jwt_encode_handler',

'JWT_DECODE_HANDLER':
'rest_framework_jwt.utils.jwt_decode_handler',

'JWT_PAYLOAD_HANDLER':
'rest_framework_jwt.utils.jwt_payload_handler',

'JWT_PAYLOAD_GET_USER_ID_HANDLER':
'rest_framework_jwt.utils.jwt_get_user_id_from_payload_handler',

'JWT_RESPONSE_PAYLOAD_HANDLER':
'rest_framework_jwt.utils.jwt_response_payload_handler',

'JWT_SECRET_KEY': settings.SECRET_KEY,
'JWT_GET_USER_SECRET_KEY': None,
'JWT_PUBLIC_KEY': None,
'JWT_PRIVATE_KEY': None,
'JWT_ALGORITHM': 'HS256',
'JWT_VERIFY': True,
'JWT_VERIFY_EXPIRATION': True,
'JWT_LEEWAY': 0,
'JWT_EXPIRATION_DELTA': datetime.timedelta(days=10),
'JWT_AUDIENCE': None,
'JWT_ISSUER': None,

'JWT_ALLOW_REFRESH': False,
'JWT_REFRESH_EXPIRATION_DELTA': datetime.timedelta(days=30),

'JWT_AUTH_HEADER_PREFIX': 'JWT',
'JWT_AUTH_COOKIE': None,

}
like image 82
Andongwisye Mwamengo Avatar answered Nov 25 '25 18:11

Andongwisye Mwamengo


you have an error because you are updating the refresh time, you have to access the access_token

def get_tokens_for_user(user):

    refresh = RefreshToken.for_user(user)
    access_token = refresh.access_token
    access_token.set_exp(lifetime=timedelta(days=10))

    return {
        'refresh': str(refresh),
        'access': str(access_token),
    }
like image 39
Juan Diego Ramirez Avatar answered Nov 25 '25 18:11

Juan Diego Ramirez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!