summaryrefslogtreecommitdiff
path: root/src/leap/bonafide/zmq_service.py
blob: f2cf123ffac33ac1cab503cbbba68b74a197c9b6 (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# -*- coding: utf-8 -*-
# zmq_service.py
# Copyright (C) 2015 LEAP
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY 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/>.

"""
Bonafide ZMQ Service.
"""

from leap.bonafide import config
from leap.bonafide._protocol import BonafideProtocol, COMMANDS

from txzmq import ZmqEndpoint, ZmqFactory, ZmqREPConnection

from twisted.application import service
from twisted.internet import reactor
from twisted.python import log


class BonafideZMQService(service.Service):

    def __init__(self):
        self._bonafide = BonafideProtocol()
        self._conn = None

        self.service_hooks = {}

    def startService(self):
        zf = ZmqFactory()
        e = ZmqEndpoint("bind", config.ENDPOINT)

        self._conn = _BonafideZmqREPConnection(zf, e, self._bonafide, self)
        reactor.callWhenRunning(self._conn.do_greet)
        super(BonafideZMQService, self).startService()

    def stopService(self):
        super(BonafideZMQService, self).stopService()

    def register_hook(self, kind, trigger):
        self.service_hooks[kind] = trigger



class _BonafideZmqREPConnection(ZmqREPConnection):

    def __init__(self, zf, e, bonafide, service):
        # XXX passing a ref to the service,
        # to be able to access sibling services
        ZmqREPConnection.__init__(self, zf, e)
        self._bonafide = bonafide
        self._service = service

    def get_sibling_service(self, kind):
        return self._service.parent.getServiceNamed(kind)

    def get_hooked_service(self, kind):
        hooks = self._service.service_hooks
        if kind in hooks:
            return self.get_sibling_service(hooks[kind])

    def do_greet(self):
        print "Starting Bonafide service"

    def do_bye(self):
        print "Bonafide service stopped. Have a nice day."
        reactor.stop()

    def defer_reply(self, response, msgId):
        reactor.callLater(0, self.reply, msgId, str(response))

    def log_err(self, failure, msgId):
        log.err(failure)
        self.defer_reply("ERROR: %r" % failure, msgId)

    def gotMessage(self, msgId, *parts):

        cmd = parts[0]

        if cmd == "shutdown":
            self.do_shutdown(msgId)

        if cmd not in COMMANDS + ("get_soledad",):
            response = 'INVALID COMMAND'
            self.defer_reply(response, msgId)

        elif cmd == 'signup':
            self.do_signup(parts, msgId)

        elif cmd == 'authenticate':
            self.do_authenticate(parts, msgId)

        elif cmd == 'logout':
            self.do_logout(self, parts, msgId)

        elif cmd == 'stats':
            self.do_stats(msgId)

    def do_shutdown(self, msgId):
        self.defer_reply('ok, shutting down', msgId)
        reactor.callLater(1, self.do_bye)

    def do_signup(self, parts, msgId):
        username, password = parts[1], parts[2]
        d = self._bonafide.do_signup(username, password)
        d.addCallback(lambda response: self.defer_reply(
            'REGISTERED -> %s' % response), msgId)
        d.addErrback(self.log_err, msgId)

    def do_authenticate(self, parts, msgId):

        username, password = parts[1], parts[2]

        def notify_passphrase_entry(username, password):
            this_hook = 'on_passphrase_entry'
            hooked_service = self.get_hooked_service(this_hook)
            if hooked_service:
                hooked_service.notify_hook(
                    this_hook, username=username, password=password)

        def notify_bonafide_auth_hook(result):
            this_hook = 'on_bonafide_auth'
            token, uuid = result
            hooked_service = self.get_hooked_service(this_hook)
            if hooked_service:
                hooked_service.notify_hook(
                    this_hook,
                    username=username, token=token, uuid=uuid,
                    password=password)
            return result

        # XXX I still have doubts from where it's best to trigger this.
        # We probably should wait for BOTH deferreds and
        # handle local and remote authentication success together
        # (and fail if either one fails). Going with fire-and-forget for
        # now, but needs needs improvement.

        notify_passphrase_entry(username, password)

        d = self._bonafide.do_authenticate(username, password)
        d.addCallback(notify_bonafide_auth_hook)
        d.addCallback(lambda response: self.defer_reply(
            'TOKEN, UUID: %s' % str(response), msgId))
        d.addErrback(self.log_err, msgId)

    def do_logout(self, parts, msgId):
        username, password = parts[1], parts[2]
        d = self._bonafide.do_logout(username, password)
        d.addCallback(lambda response: self.defer_reply(
            'LOGOUT -> ok'), msgId)
        d.addErrback(self.log_err, msgId)

    def do_stats(self, msgId):
        response = self._bonafide.do_stats()
        self.defer_reply(response, msgId)