def refresh_expiring_jwts()

in source/backend/src/transfer_sftp_backend.py [0:0]


def refresh_expiring_jwts(response):
    try:
        request_str = str(request)
        allowed_endpoints = ["listchildnodes", "numberofchildnodes", "upload", "download", "delete", "rename", "createfolder"]

        # Only create access tokens for specific endpoints
        if any(item in request_str for item in allowed_endpoints):
            logger.debug(f'TaskID: {fargate_task_id}(PID:{pid}) - refresh_expiring_jwts(): {request_str}')
            exp_timestamp = get_raw_jwt()["exp"]
            now = datetime.datetime.now(timezone.utc)
            target_timestamp = datetime.datetime.timestamp(now + timedelta(seconds=120))
            if target_timestamp > exp_timestamp:
                logger.debug(f'TaskID: {fargate_task_id}(PID:{pid}) - refresh_expiring_jwts(): Setting new access token')
                jwt_exp_time_config = app.config.get('JWT_ACCESS_TOKEN_EXPIRES')
                access_token = create_access_token(identity=get_jwt_identity())
                set_access_cookies(response, access_token, jwt_exp_time_config)
            else:
                logger.debug(f'TaskID: {fargate_task_id}(PID:{pid}) - refresh_expiring_jwts(): Target expiration time is not within the 2 mins of original expiration time')
        return response
    except (RuntimeError,KeyError):
        # Case where there is not a valid JWT. Just return the original respone
        logger.debug(f'TaskID: {fargate_task_id}(PID:{pid}) - refresh_expiring_jwts(): In EXCEPT CLAUSE')
        return response