예제 #1
0
class CredentialsManagement(Referenceable, SignalBroadcaster):
    """Object that manages credentials.

    Every exposed method in this class requires one mandatory argument:

        - 'app_name': the name of the application. Will be displayed in the
        GUI header, plus it will be used to find/build/clear tokens.

    And accepts another parameter named 'args', which is a dictionary that
    can contain the following:

        - 'help_text': an explanatory text for the end-users, will be
        shown below the header. This is an optional free text field.

        - 'ping_url': the url to open after successful token retrieval. If
        defined, the email will be attached to the url and will be pinged
        with a OAuth-signed request.

        - 'tc_url': the link to the Terms and Conditions page. If defined,
        the checkbox to agree to the terms will link to it.

        - 'window_id': the id of the window which will be set as a parent
        of the GUI. If not defined, no parent will be set.

    """

    __metaclass__ = RemoteMeta

    # calls that will be accessible remotely
    remote_calls = [
        'find_credentials',
        'clear_credentials',
        'store_credentials',
        'register',
        'shutdown',
        'login',
        'login_email_password',
    ]

    def __init__(self, timeout_func, shutdown_func, *args, **kwargs):
        super(CredentialsManagement, self).__init__(*args, **kwargs)
        self.root = CredentialsManagementRoot(timeout_func, shutdown_func,
                                              self.emit_credentials_found,
                                              self.emit_credentials_error,
                                              self.emit_authorization_denied)

    def _process_failure(self, failure, app_name):
        """Process the 'failure' and emit CredentialsError."""
        self.emit_credentials_error(app_name, except_to_errdict(failure.value))

    def shutdown(self):
        """If no ongoing requests, call self.shutdown_func."""
        logger.debug('shutdown!, ref_count is %r.', self.root.ref_count)
        self.root.shutdown()

    def emit_authorization_denied(self, app_name):
        """Signal thrown when the user denies the authorization."""
        self.root.ref_count -= 1
        logger.info('%s: emitting AuthorizationDenied with app_name "%s".',
                    self.__class__.__name__, app_name)
        self.emit_signal('on_authorization_denied', app_name)

    def emit_credentials_found(self, app_name, credentials):
        """Signal thrown when the credentials are found."""
        self.root.ref_count -= 1
        logger.info('%s: emitting CredentialsFound with app_name "%s".',
                    self.__class__.__name__, app_name)
        self.emit_signal('on_credentials_found', app_name, credentials)

    def emit_credentials_not_found(self, app_name):
        """Signal thrown when the credentials are not found."""
        self.root.ref_count -= 1
        logger.info('%s: emitting CredentialsNotFound with app_name "%s".',
                    self.__class__.__name__, app_name)
        self.emit_signal('on_credentials_not_found', app_name)

    def emit_credentials_cleared(self, app_name):
        """Signal thrown when the credentials were cleared."""
        self.root.ref_count -= 1
        logger.info('%s: emitting CredentialsCleared with app_name "%s".',
                    self.__class__.__name__, app_name)
        self.emit_signal('on_credentials_cleared', app_name)

    def emit_credentials_stored(self, app_name):
        """Signal thrown when the credentials were cleared."""
        self.root.ref_count -= 1
        logger.info('%s: emitting CredentialsStored with app_name "%s".',
                    self.__class__.__name__, app_name)
        self.emit_signal('on_credentials_stored', app_name)

    def emit_credentials_error(self, app_name, error_dict):
        """Signal thrown when there is a problem getting the credentials."""
        self.root.ref_count -= 1
        logger.error('%s: emitting CredentialsError with app_name "%s" and '
                     'error_dict %r.', self.__class__.__name__, app_name,
                     error_dict)
        self.emit_signal('on_credentials_error', app_name, error_dict)

    def find_credentials(self, app_name, args):
        """Look for the credentials for an application.

        - 'app_name': the name of the application which credentials are
        going to be removed.

        - 'args' is a dictionary, currently not used.

        """

        def success_cb(credentials):
            """Find credentials and notify using signals."""
            if credentials is not None and len(credentials) > 0:
                self.emit_credentials_found(app_name, credentials)
            else:
                self.emit_credentials_not_found(app_name)

        self.root.find_credentials(app_name, args, success_cb,
                                   self._process_failure)

    def clear_credentials(self, app_name, args):
        """Clear the credentials for an application.

        - 'app_name': the name of the application which credentials are
        going to be removed.

        - 'args' is a dictionary, currently not used.

        """
        self.root.clear_credentials(app_name, args,
                            lambda _: self.emit_credentials_cleared(app_name),
                            self._process_failure)

    def store_credentials(self, app_name, args):
        """Store the token for an application.

        - 'app_name': the name of the application which credentials are
        going to be stored.

        - 'args' is the dictionary holding the credentials. Needs to provide
        the following mandatory keys: 'token', 'token_key', 'consumer_key',
        'consumer_secret'.

        """
        self.root.store_credentials(app_name, args,
                            lambda _: self.emit_credentials_stored(app_name),
                            self._process_failure)

    def register(self, app_name, args):
        """Get credentials if found else prompt GUI to register."""
        self.root.register(app_name, args)

    def login(self, app_name, args):
        """Get credentials if found else prompt GUI to login."""
        self.root.login(app_name, args)

    def login_email_password(self, app_name, args):
        """Get credentials if found, else login."""
        self.root.login_email_password(app_name, args)
예제 #2
0
class CredentialsManagement(dbus.service.Object):
    """DBus object that manages credentials.

    Every exposed method in this class requires one mandatory argument:

        - 'app_name': the name of the application. Will be displayed in the
        GUI header, plus it will be used to find/build/clear tokens.

    And accepts another parameter named 'args', which is a dictionary that
    can contain the following:

        - 'help_text': an explanatory text for the end-users, will be
        shown below the header. This is an optional free text field.

        - 'ping_url': the url to open after successful token retrieval. If
        defined, the email will be attached to the url and will be pinged
        with a OAuth-signed request.

        - 'tc_url': the link to the Terms and Conditions page. If defined,
        the checkbox to agree to the terms will link to it.

        - 'window_id': the id of the window which will be set as a parent
        of the GUI. If not defined, no parent will be set.

    """

    def __init__(self, timeout_func, shutdown_func, *args, **kwargs):
        super(CredentialsManagement, self).__init__(*args, **kwargs)
        self.root = CredentialsManagementRoot(timeout_func, shutdown_func,
                                              self.CredentialsFound,
                                              self.CredentialsError,
                                              self.AuthorizationDenied)

    # Operator not preceded by a space (fails with dbus decorators)
    # pylint: disable=C0322

    def _process_failure(self, failure, app_name):
        """Process the 'failure' and emit CredentialsError."""
        self.CredentialsError(app_name, except_to_errdict(failure.value))

    def shutdown(self):
        """If no ongoing requests, call self.shutdown_func."""
        logger.debug('shutdown!, ref_count is %r.', self.root.ref_count)
        self.root.shutdown()

    @dbus.service.signal(DBUS_CREDENTIALS_IFACE, signature='s')
    def AuthorizationDenied(self, app_name):
        """Signal thrown when the user denies the authorization."""
        self.root.ref_count -= 1
        logger.info('%s: emitting AuthorizationDenied with app_name "%s".',
                    self.__class__.__name__, app_name)

    @dbus.service.signal(DBUS_CREDENTIALS_IFACE, signature='sa{ss}')
    def CredentialsFound(self, app_name, credentials):
        """Signal thrown when the credentials are found."""
        self.root.ref_count -= 1
        logger.info('%s: emitting CredentialsFound with app_name "%s".',
                    self.__class__.__name__, app_name)

    @dbus.service.signal(DBUS_CREDENTIALS_IFACE, signature='s')
    def CredentialsNotFound(self, app_name):
        """Signal thrown when the credentials are not found."""
        self.root.ref_count -= 1
        logger.info('%s: emitting CredentialsNotFound with app_name "%s".',
                    self.__class__.__name__, app_name)

    @dbus.service.signal(DBUS_CREDENTIALS_IFACE, signature='s')
    def CredentialsCleared(self, app_name):
        """Signal thrown when the credentials were cleared."""
        self.root.ref_count -= 1
        logger.info('%s: emitting CredentialsCleared with app_name "%s".',
                    self.__class__.__name__, app_name)

    @dbus.service.signal(DBUS_CREDENTIALS_IFACE, signature='s')
    def CredentialsStored(self, app_name):
        """Signal thrown when the credentials were cleared."""
        self.root.ref_count -= 1
        logger.info('%s: emitting CredentialsStored with app_name "%s".',
                    self.__class__.__name__, app_name)

    @dbus.service.signal(DBUS_CREDENTIALS_IFACE, signature='sa{ss}')
    def CredentialsError(self, app_name, error_dict):
        """Signal thrown when there is a problem getting the credentials."""
        self.root.ref_count -= 1
        logger.error('%s: emitting CredentialsError with app_name "%s" and '
                     'error_dict %r.', self.__class__.__name__, app_name,
                     error_dict)

    @dbus.service.method(dbus_interface=DBUS_CREDENTIALS_IFACE,
                         in_signature='sa{ss}', out_signature='')
    def find_credentials(self, app_name, args):
        """Look for the credentials for an application.

        - 'app_name': the name of the application which credentials are
        going to be removed.

        - 'args' is a dictionary, currently not used.

        """

        def success_cb(credentials):
            """Find credentials and notify using signals."""
            if credentials is not None and len(credentials) > 0:
                self.CredentialsFound(app_name, credentials)
            else:
                self.CredentialsNotFound(app_name)

        self.root.find_credentials(app_name, args, success_cb,
                                   self._process_failure)

    @dbus.service.method(dbus_interface=DBUS_CREDENTIALS_IFACE,
                         in_signature='sa{ss}', out_signature='')
    def clear_credentials(self, app_name, args):
        """Clear the credentials for an application.

        - 'app_name': the name of the application which credentials are
        going to be removed.

        - 'args' is a dictionary, currently not used.

        """
        self.root.clear_credentials(app_name, args,
                                lambda _: self.CredentialsCleared(app_name),
                                self._process_failure)

    @dbus.service.method(dbus_interface=DBUS_CREDENTIALS_IFACE,
                         in_signature='sa{ss}', out_signature='')
    def store_credentials(self, app_name, args):
        """Store the token for an application.

        - 'app_name': the name of the application which credentials are
        going to be stored.

        - 'args' is the dictionary holding the credentials. Needs to provide
        the following mandatory keys: 'token', 'token_key', 'consumer_key',
        'consumer_secret'.

        """
        self.root.store_credentials(app_name, args,
                                lambda _: self.CredentialsStored(app_name),
                                self._process_failure)

    @dbus.service.method(dbus_interface=DBUS_CREDENTIALS_IFACE,
                         in_signature='sa{ss}', out_signature='')
    def register(self, app_name, args):
        """Get credentials if found else prompt GUI to register."""
        self.root.register(app_name, args)

    @dbus.service.method(dbus_interface=DBUS_CREDENTIALS_IFACE,
                         in_signature='sa{ss}', out_signature='')
    def login(self, app_name, args):
        """Get credentials if found else prompt GUI to login."""
        self.root.login(app_name, args)

    @dbus.service.method(dbus_interface=DBUS_CREDENTIALS_IFACE,
                         in_signature='sa{ss}', out_signature='')
    def login_email_password(self, app_name, args):
        """Get credentials if found, else login using email and password.

        - 'args' should contain at least the follwing keys: 'email' and
        'password'. Those will be used to issue a new SSO token, which will be
        returned trough the CredentialsFound signal.

        """
        self.root.login_email_password(app_name, args)