alignak_backend_client/client.py
def login(self, username, password, generate='enabled', proxies=None):
        """
        Log into the backend and get the token

        generate parameter may have following values:
        - enabled: require current token (default)
        - force: force new token generation
        - disabled

        if login is:
        - accepted, returns True
        - refused, returns False

        In case of any error, raises a BackendException

        :param username: login name
        :type username: str
        :param password: password
        :type password: str
        :param generate: Can have these values: enabled | force | disabled
        :type generate: str
        :param proxies: dict of proxy (http and / or https)
        :type proxies: dict
        :return: return True if authentication is successfull, otherwise False
        :rtype: bool
        """
        logger.debug("login for: %s with generate: %s", username, generate)

        if not username or not password:
            raise BackendException(BACKEND_ERROR, "Missing mandatory parameters")

        if proxies:
            for key in proxies.keys():
                try:
                    assert key in PROXY_PROTOCOLS
                except AssertionError:
                    raise BackendException(BACKEND_ERROR, "Wrong proxy protocol ", key)
        self.proxies = proxies

        endpoint = 'login'
        json = {u'username': username, u'password': password}
        if generate == 'force':
            json['action'] = 'generate'
            logger.debug("Asking for generating new token")

        response = self.get_response(method='POST', endpoint=endpoint, json=json)
        if response.status_code == 401:
            logger.error("Backend refused login with params %s", json)
            self.set_token(token=None)
            return False

        resp = self.decode(response=response)

        if 'token' in resp:
            self.set_token(token=resp['token'])
            return True
        if generate == 'force':  # pragma: no cover - need specific backend tests
            self.set_token(token=None)
            raise BackendException(BACKEND_ERROR, "Token not provided")
        if generate == 'disabled':  # pragma: no cover - need specific backend tests
            logger.error("Token disabled ... to be implemented!")
            return False
        if generate == 'enabled':  # pragma: no cover - need specific backend tests
            logger.warning("Token enabled, but none provided, require new token generation")
            return self.login(username, password, 'force')

        return False
def login(self, username, password, generate='enabled', proxies=None):
        """
        Log into the backend and get the token

        generate parameter may have following values:
        - enabled: require current token (default)
        - force: force new token generation
        - disabled

        if login is:
        - accepted, returns True
        - refused, returns False

        In case of any error, raises a BackendException

        :param username: login name
        :type username: str
        :param password: password
        :type password: str
        :param generate: Can have these values: enabled | force | disabled
        :type generate: str
        :param proxies: dict of proxy (http and / or https)
        :type proxies: dict
        :return: return True if authentication is successfull, otherwise False
        :rtype: bool
        """
        logger.debug("login for: %s with generate: %s", username, generate)

        if not username or not password:
            raise BackendException(BACKEND_ERROR, "Missing mandatory parameters")

        if proxies:
            for key in proxies.keys():
                try:
                    assert key in PROXY_PROTOCOLS
                except AssertionError:
                    raise BackendException(BACKEND_ERROR, "Wrong proxy protocol ", key)
        self.proxies = proxies

        endpoint = 'login'
        json = {u'username': username, u'password': password}
        if generate == 'force':
            json['action'] = 'generate'
            logger.debug("Asking for generating new token")

        response = self.get_response(method='POST', endpoint=endpoint, json=json)
        if response.status_code == 401:
            logger.error("Backend refused login with params %s", json)
            self.set_token(token=None)
            return False

        resp = self.decode(response=response)

        if 'token' in resp:
            self.set_token(token=resp['token'])
            return True
        if generate == 'force':  # pragma: no cover - need specific backend tests
            self.set_token(token=None)
            raise BackendException(BACKEND_ERROR, "Token not provided")
        if generate == 'disabled':  # pragma: no cover - need specific backend tests
            logger.error("Token disabled ... to be implemented!")
            return False
        if generate == 'enabled':  # pragma: no cover - need specific backend tests
            logger.warning("Token enabled, but none provided, require new token generation")
            return self.login(username, password, 'force')

        return False
def login ( self , username , password , generate = 'enabled' , proxies = None ) : logger . debug ( "login for: %s with generate: %s" , username , generate ) if not username or not password : raise BackendException ( BACKEND_ERROR , "Missing mandatory parameters" ) if proxies : for key in proxies . keys ( ) : try : assert key in PROXY_PROTOCOLS except AssertionError : raise BackendException ( BACKEND_ERROR , "Wrong proxy protocol " , key ) self . proxies = proxies endpoint = 'login' json = { u'username' : username , u'password' : password } if generate == 'force' : json [ 'action' ] = 'generate' logger . debug ( "Asking for generating new token" ) response = self . get_response ( method = 'POST' , endpoint = endpoint , json = json ) if response . status_code == 401 : logger . error ( "Backend refused login with params %s" , json ) self . set_token ( token = None ) return False resp = self . decode ( response = response ) if 'token' in resp : self . set_token ( token = resp [ 'token' ] ) return True if generate == 'force' : # pragma: no cover - need specific backend tests self . set_token ( token = None ) raise BackendException ( BACKEND_ERROR , "Token not provided" ) if generate == 'disabled' : # pragma: no cover - need specific backend tests logger . error ( "Token disabled ... to be implemented!" ) return False if generate == 'enabled' : # pragma: no cover - need specific backend tests logger . warning ( "Token enabled, but none provided, require new token generation" ) return self . login ( username , password , 'force' ) return False
Log into the backend and get the token

        generate parameter may have following values:
        - enabled: require current token (default)
        - force: force new token generation
        - disabled

        if login is:
        - accepted, returns True
        - refused, returns False

        In case of any error, raises a BackendException

        :param username: login name
        :type username: str
        :param password: password
        :type password: str
        :param generate: Can have these values: enabled | force | disabled
        :type generate: str
        :param proxies: dict of proxy (http and / or https)
        :type proxies: dict
        :return: return True if authentication is successfull, otherwise False
        :rtype: bool
Log into the backend and get the token