def authenticate()

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


def authenticate():
    try:
        logger.info(f'TaskID: {fargate_task_id}(PID:{pid}) - authenticate(): Request received')
        sftp_hostname = app.config.get('sftp_hostname')
        usr, passwdorpkey = authenticate_request(request)

        sftp_connection = create_sftp_connection(usr, passwdorpkey, sftp_hostname)
        res = type(sftp_connection).__name__

        if res == 'SFTPClient':
            response = jsonify({'message': "Authentication successful."})

            cred = encrypt_username_password_boto(usr + " " + passwdorpkey, kms_key_id)
            access_token = create_access_token(identity=cred)

            # We still need to call these functions to set the
            # JWTs in the cookies
            jwt_exp_time_config = app.config.get('JWT_ACCESS_TOKEN_EXPIRES')
            set_access_cookies(response, access_token, jwt_exp_time_config)

            response.status_code = 200
            logger.info(f'TaskID: {fargate_task_id}(PID:{pid}) - authenticate(): Returning with 200 status code')

            return response

        elif res == 'AuthenticationException':
            response = jsonify({'message': "Authentication Failed"})
            response.status_code = 401
            logger.error(f'TaskID: {fargate_task_id}(PID:{pid}) - authenticate(): Authentication Failure. Returning with 401 status code')
            return response
        else:
            response = jsonify({'message': "Network/Socket Error occurred while making connection to SFTP endpoint"})
            response.status_code = 500  # Internal Server Error
            logger.error(f'TaskID: {fargate_task_id}(PID:{pid}) - authenticate(): Returning with 500 status code')
            return response

    except Exception as e:
        logger.error(f'TaskID: {fargate_task_id}(PID:{pid}) - authenticate(): call to /api/authenticate returned exception {e}')
        return bad_request("Bad or Invalid Request", 500)  # Internal Server Error