~mvo/ubuntu-sso-client/lp711413

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# -*- coding: utf-8 -*-
#
# Copyright 2009-2012 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.
"""Main module implementation specific for darwin (OS X).

This module should never import from the multiplatform one (main/__init__.py),
but the other way around. Likewise, this module should *not* have any logic
regarding error processing or decision making about when to send a given
signal.

Also, most of the logging is being made in the main module to avoid
duplication between the different platform implementations.

"""

import os
import os.path

from dirspec import basedir
from twisted.internet import defer

import ubuntu_sso
from ubuntu_sso.logger import setup_logging
from ubuntu_sso.utils.ipc import BaseClient
from ubuntu_sso.main.perspective_broker import (
    SSO_SERVICE_NAME,
    SSOLoginClient,
    CredentialsManagementClient,
    UbuntuSSOProxyBase,
    )

# Invalid name for signals that are CamelCase
# pylint: disable=C0103


logger = setup_logging("ubuntu_sso.main.darwin")
SSO_INSTALL_PATH = 'SSOInstallPath'


def get_sso_domain_socket():
    """Compute the domain socket for the sso ipc."""
    path = os.path.join(basedir.xdg_cache_home, 'sso', 'ipc')
    return path


def get_activation_cmdline(name):
    """Find the path to the executable callback."""
    # TODO: We have to either use launchd to launch the different services
    # on demand or us LaunchServices to handle running the app via an URL.
    ubuntu_sso_pkg_dir = os.path.dirname(os.path.dirname(ubuntu_sso.__file__))
    ubuntu_sso_bin_dir = os.path.join(ubuntu_sso_pkg_dir, 'bin')
    ubuntu_sso_bin = os.path.join(ubuntu_sso_bin_dir, 'ubuntu-sso-login')
    ubuntuone_client_bin_dir = os.path.join(ubuntu_sso_pkg_dir,
                                            '../ubuntuone-client/bin')
    ubuntuone_client_bin = os.path.join(ubuntuone_client_bin_dir, name)
    if os.path.exists(ubuntuone_client_bin):
        return ubuntuone_client_bin
    return ubuntu_sso_bin


class DescriptionFactory(object):
    """Factory that provides the server and client descriptions."""

    client_description_pattern = 'unix:path=%s'
    server_description_pattern = 'unix:%s'

    def __init__(self):
        """Create a new instance."""
        self.domain = get_sso_domain_socket()
        self.server = self.server_description_pattern % self.domain
        self.client = self.client_description_pattern % self.domain


class UbuntuSSOProxy(UbuntuSSOProxyBase):
    """Object that exposes the diff referenceable objects."""

    name = SSO_SERVICE_NAME

    @property
    def description(self):
        """Get the description on which the SSO pb is running."""
        return DescriptionFactory()

    @property
    def cmdline(self):
        """Get the command line to activate an executable."""
        return get_activation_cmdline(self.name)


class UbuntuSSOClient(BaseClient):
    """Base client that provides remote access to the sso API."""

    name = SSO_SERVICE_NAME

    clients = {
        'sso_login': SSOLoginClient,
        'cred_manager': CredentialsManagementClient,
    }

    service_name = UbuntuSSOProxy.name
    service_description = UbuntuSSOProxy.description
    service_cmdline = UbuntuSSOProxy.cmdline


@defer.inlineCallbacks
def get_sso_client():
    """Get a client to access the SSO service."""
    result = UbuntuSSOClient()
    yield result.connect()
    defer.returnValue(result)