'How to expire access token using flask-jwt-extend when new login is initiated

I am creating the access token using the flask-jwt-extended and storing it into httpcookie only.

My requirement is if user logins to the site using credentials, I will generate one token and store it in httpcookie, so my protected route will be accessible through that access token.

If the same user will login to the site using the different system with the same credentials, then I want to expire the access token which I have created first, so when user will try to access the protected route it will be forbidden, it means, that it will logout the user explicitly from the first system.

Below is the code using which I am creating the access token and storing it in a cookie.

Note: in my case i am using imspin as an identity.

    class NewJwtAuthentication(Resourse):
       def post(self):
        requestData = request.get_json()
        LogRecordings().request(requestData)
        imspin = requestData['imspin']
        useremail = requestData['useremail']
        if requestData['imspin'] == '' or requestData['useremail'] == '':
            emptyDataResponse()
        getstudentdetails = fetchAllWhere(DB_Queries.SelectAllUsingPinAndEmail, imspin,useremail)
        if getstudentdetails :
            #redirect(url_for('loginsuccess'))
            access_token = create_access_token(identity=imspin)
            refresh_token = create_refresh_token(identity=imspin)
            d = jwt.decode(access_token, '48e48e4e1796b1de856737ca2418dd43', algorithms='HS256')
            expiration_time = d['exp']
            expires_on=datetime.fromtimestamp(expiration_time).strftime('%Y-%m-%d %H:%M:%S')
            print(expires_on)
            print(access_token)
            print(refresh_token)
            resp = make_response({'message': 'Login Successfull',
                                  'csrf_token':flask_jwt_extended.get_csrf_token(encoded_token=access_token),
                                  'csrf_refresh_token':flask_jwt_extended.get_csrf_token(encoded_token=refresh_token),
                                  'accesstoken_expiry_time':expires_on,
                                  'imspin':imspin})
            set_access_cookies(resp, access_token)
            set_refresh_cookies(resp, refresh_token)
            #print(flask_jwt_extended.get_csrf_token(encoded_token=access_token))
            #set_refresh_cookies(response, refresh_token)
            return resp
        else:
            return make_response({'message':'could not verify' })

any help would be highly appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source