Example #1
0
class SSOCredentials(Referenceable, SignalBroadcaster):
    """DBus object that gets credentials, and login/registers if needed."""

    __metaclass__ = RemoteMeta

    # calls that will be accessible remotely
    remote_calls = [
        'find_credentials',
        'login_or_register_to_get_credentials',
        'login_to_get_credentials',
        'clear_token',
    ]

    def __init__(self, *args, **kwargs):
        super(SSOCredentials, self).__init__()
        self.root = SSOCredentialsRoot()

    def _process_error(self, app_name, error_dict):
        """Process the 'error_dict' and emit CredentialsError."""
        msg = error_dict.get(ERROR_KEY, 'No error message given.')
        detail = error_dict.get(ERROR_DETAIL_KEY, 'No detailed error given.')
        self.emit_credentials_error(app_name, msg, detail)

    def emit_authorization_denied(self, app_name):
        """Signal thrown when the user denies the authorization."""
        logger.info('SSOCredentials: emitting AuthorizationDenied with '
                    'app_name "%s"', 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."""
        logger.info('SSOCredentials: emitting CredentialsFound with '
                    'app_name "%s"', app_name)
        self.emit_signal('on_credentials_found', app_name, credentials)

    def emit_credentials_error(self, app_name, error_message, detailed_error):
        """Signal thrown when there is a problem finding the credentials."""
        logger.error('SSOCredentials: emitting CredentialsError with app_name '
                     '"%s" and error_message %r', app_name, error_message)
        self.emit_signal('on_credentials_error', app_name, error_message,
                         detailed_error)

    def find_credentials(self, app_name, callback=NO_OP, errback=NO_OP):
        """Get the credentials from the keyring or {} if not there."""
        self.root.find_credentials(app_name, remote_handler(callback),
                                   remote_handler(errback))

    def login_or_register_to_get_credentials(self, app_name,
                                             terms_and_conditions_url,
                                             help_text, window_id,
                                             ui_module='ubuntu_sso.qt.gui'):
        """Get credentials if found else prompt GUI to login or register.

        'app_name' will be displayed in the GUI.
        'terms_and_conditions_url' will be the URL pointing to T&C.
        'help_text' is an explanatory text for the end-users, will be shown
         below the headers.
        'window_id' is the id of the window which will be set as a parent of
         the GUI. If 0, no parent will be set.

        """
        self.root.login_or_register_to_get_credentials(app_name,
                                                terms_and_conditions_url,
                                                help_text, window_id,
                                                self.emit_credentials_found,
                                                self._process_error,
                                                self.emit_authorization_denied,
                                                ui_module=ui_module)

    def login_to_get_credentials(self, app_name, help_text, window_id):
        """Get credentials if found else prompt GUI just to login

        'app_name' will be displayed in the GUI.
        'help_text' is an explanatory text for the end-users, will be shown
         before the login fields.
        'window_id' is the id of the window which will be set as a parent of
         the GUI. If 0, no parent will be set.

        """
        self.root.login_to_get_credentials(app_name, help_text, window_id,
                                           self.emit_credentials_found,
                                           self._process_error,
                                           self.emit_authorization_denied,
                                           ui_module='ubuntu_sso.qt.gui')

    def clear_token(self, app_name, callback=NO_OP, errback=NO_OP):
        """Clear the token for an application from the keyring.

        'app_name' is the name of the application.
        """
        self.root.clear_token(app_name, remote_handler(callback),
                              remote_handler(errback))
Example #2
0
class SSOCredentials(dbus.service.Object):
    """DBus object that gets credentials, and login/registers if needed.

    This class is Deprecated. DO NOT USE, use CredentialsManagement instead.

    """

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

    def __init__(self, *args, **kwargs):
        dbus.service.Object.__init__(self, *args, **kwargs)
        self.root = SSOCredentialsRoot()
        warnings.warn('%r DBus object is deprecated, please use %r instead.' %
                      (DBUS_IFACE_CRED_NAME, DBUS_CREDENTIALS_IFACE),
                      DeprecationWarning)

    def _process_error(self, app_name, error_dict):
        """Process the 'error_dict' and emit CredentialsError."""
        msg = error_dict.get(ERROR_KEY, 'No error message given.')
        detail = error_dict.get(ERROR_DETAIL_KEY, 'No detailed error given.')
        self.CredentialsError(app_name, msg, detail)

    @dbus.service.signal(DBUS_IFACE_CRED_NAME, signature="s")
    def AuthorizationDenied(self, app_name):
        """Signal thrown when the user denies the authorization."""
        logger.info('SSOCredentials: emitting AuthorizationDenied with '
                    'app_name "%s"', app_name)

    @dbus.service.signal(DBUS_IFACE_CRED_NAME, signature="sa{ss}")
    def CredentialsFound(self, app_name, credentials):
        """Signal thrown when the credentials are found."""
        logger.info('SSOCredentials: emitting CredentialsFound with '
                    'app_name "%s"', app_name)

    @dbus.service.signal(DBUS_IFACE_CRED_NAME, signature="sss")
    def CredentialsError(self, app_name, error_message, detailed_error):
        """Signal thrown when there is a problem finding the credentials."""
        logger.error('SSOCredentials: emitting CredentialsError with app_name '
                     '"%s" and error_message %r', app_name, error_message)

    @dbus.service.method(dbus_interface=DBUS_IFACE_CRED_NAME,
                         in_signature="s", out_signature="a{ss}",
                         async_callbacks=("callback", "errback"))
    def find_credentials(self, app_name, callback=NO_OP, errback=NO_OP):
        """Get the credentials from the keyring or {} if not there."""
        self.root.find_credentials(app_name, callback, errback)

    @dbus.service.method(dbus_interface=DBUS_IFACE_CRED_NAME,
                         in_signature="sssx", out_signature="")
    def login_or_register_to_get_credentials(self, app_name,
                                             terms_and_conditions_url,
                                             help_text, window_id):
        """Get credentials if found else prompt GUI to login or register.

        'app_name' will be displayed in the GUI.
        'terms_and_conditions_url' will be the URL pointing to T&C.
        'help_text' is an explanatory text for the end-users, will be shown
         below the headers.
        'window_id' is the id of the window which will be set as a parent of
         the GUI. If 0, no parent will be set.

        """
        self.root.login_or_register_to_get_credentials(app_name,
                                               terms_and_conditions_url,
                                               help_text, window_id,
                                               self.CredentialsFound,
                                               self._process_error,
                                               self.AuthorizationDenied,
                                               ui_module='ubuntu_sso.gtk.gui')

    @dbus.service.method(dbus_interface=DBUS_IFACE_CRED_NAME,
                         in_signature="ssx", out_signature="")
    def login_to_get_credentials(self, app_name, help_text, window_id):
        """Get credentials if found else prompt GUI just to login

        'app_name' will be displayed in the GUI.
        'help_text' is an explanatory text for the end-users, will be shown
         before the login fields.
        'window_id' is the id of the window which will be set as a parent of
         the GUI. If 0, no parent will be set.

        """
        self.root.login_to_get_credentials(app_name, help_text, window_id,
                                           self.CredentialsFound,
                                           self._process_error,
                                           self.AuthorizationDenied,
                                           ui_module='ubuntu_sso.gtk.gui')

    @dbus.service.method(dbus_interface=DBUS_IFACE_CRED_NAME,
                         in_signature='s', out_signature='',
                         async_callbacks=("callback", "errback"))
    def clear_token(self, app_name, callback=NO_OP, errback=NO_OP):
        """Clear the token for an application from the keyring.

        'app_name' is the name of the application.
        """
        self.root.clear_token(app_name, callback, errback)