示例#1
0
class kytos_auth:  # pylint: disable=invalid-name
    """Class to be used as decorator to require authentication."""
    def __init__(self, func):
        """Init method.

        Save the function on the func attribute and bootstrap a new config.
        """
        self.func = func
        self.config = KytosConfig().config
        self.cls = None
        self.obj = None

    def __call__(self, *args, **kwargs):
        """Code run when func is called."""
        if not (self.config.has_option('napps', 'api')
                and self.config.has_option('napps', 'repo')):
            uri = input("Enter the kytos napps server address: ")
            self.config.set('napps', 'api', os.path.join(uri, 'api', ''))
            self.config.set('napps', 'repo', os.path.join(uri, 'repo', ''))

        if not self.config.has_option('auth', 'user'):
            user = input("Enter the username: "******"""Deal with owner class."""
        self.cls = owner
        self.obj = instance

        return self.__call__

    def authenticate(self):
        """Check the user authentication."""
        endpoint = os.path.join(self.config.get('napps', 'api'), 'auth', '')
        username = self.config.get('auth', 'user')
        password = getpass("Enter the password for {}: ".format(username))
        response = requests.get(endpoint, auth=(username, password))
        if response.status_code != 201:
            LOG.error(response.content)
            LOG.error('ERROR: %s: %s', response.status_code, response.reason)
            sys.exit(1)
        else:
            data = response.json()
            KytosConfig().save_token(username, data.get('hash'))
            return data.get('hash')
示例#2
0
    def test_save_token(self):
        """Test save_token method."""
        self.kytos_config.save_token('user', 'token')

        config = KytosConfig(self.config_file).config
        has_token = config.has_option('auth', 'token')
        self.assertTrue(has_token)
示例#3
0
    def test_clear_token(self):
        """Test clear_token method."""
        self.kytos_config.clear_token()

        config = KytosConfig(self.config_file).config
        has_token = config.has_option('auth', 'token')
        self.assertFalse(has_token)