Mercurial > libervia-backend
comparison sat/plugins/plugin_xep_0070.py @ 2624:56f94936df1e
code style reformatting using black
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 27 Jun 2018 20:14:46 +0200 |
parents | 26edcf3a30eb |
children | a55a14c3cbf4 |
comparison
equal
deleted
inserted
replaced
2623:49533de4540b | 2624:56f94936df1e |
---|---|
19 from sat.core.i18n import _, D_ | 19 from sat.core.i18n import _, D_ |
20 from sat.core.constants import Const as C | 20 from sat.core.constants import Const as C |
21 from sat.core.log import getLogger | 21 from sat.core.log import getLogger |
22 from twisted.words.protocols.jabber import xmlstream | 22 from twisted.words.protocols.jabber import xmlstream |
23 from twisted.words.protocols import jabber | 23 from twisted.words.protocols import jabber |
24 | |
24 log = getLogger(__name__) | 25 log = getLogger(__name__) |
25 from sat.tools import xml_tools | 26 from sat.tools import xml_tools |
26 | 27 |
27 from wokkel import disco, iwokkel | 28 from wokkel import disco, iwokkel |
28 from zope.interface import implements | 29 from zope.interface import implements |
30 | |
29 try: | 31 try: |
30 from twisted.words.protocols.xmlstream import XMPPHandler | 32 from twisted.words.protocols.xmlstream import XMPPHandler |
31 except ImportError: | 33 except ImportError: |
32 from wokkel.subprotocols import XMPPHandler | 34 from wokkel.subprotocols import XMPPHandler |
33 | 35 |
34 | 36 |
35 NS_HTTP_AUTH = 'http://jabber.org/protocol/http-auth' | 37 NS_HTTP_AUTH = "http://jabber.org/protocol/http-auth" |
36 | 38 |
37 IQ = 'iq' | 39 IQ = "iq" |
38 IQ_GET = '/'+IQ+'[@type="get"]' | 40 IQ_GET = "/" + IQ + '[@type="get"]' |
39 IQ_HTTP_AUTH_REQUEST = IQ_GET + '/confirm[@xmlns="' + NS_HTTP_AUTH + '"]' | 41 IQ_HTTP_AUTH_REQUEST = IQ_GET + '/confirm[@xmlns="' + NS_HTTP_AUTH + '"]' |
40 | 42 |
41 MSG = 'message' | 43 MSG = "message" |
42 MSG_GET = '/'+MSG+'[@type="normal"]' | 44 MSG_GET = "/" + MSG + '[@type="normal"]' |
43 MSG_HTTP_AUTH_REQUEST = MSG_GET + '/confirm[@xmlns="' + NS_HTTP_AUTH + '"]' | 45 MSG_HTTP_AUTH_REQUEST = MSG_GET + '/confirm[@xmlns="' + NS_HTTP_AUTH + '"]' |
44 | 46 |
45 | 47 |
46 PLUGIN_INFO = { | 48 PLUGIN_INFO = { |
47 C.PI_NAME: "XEP-0070 Plugin", | 49 C.PI_NAME: "XEP-0070 Plugin", |
49 C.PI_TYPE: "XEP", | 51 C.PI_TYPE: "XEP", |
50 C.PI_PROTOCOLS: ["XEP-0070"], | 52 C.PI_PROTOCOLS: ["XEP-0070"], |
51 C.PI_DEPENDENCIES: [], | 53 C.PI_DEPENDENCIES: [], |
52 C.PI_MAIN: "XEP_0070", | 54 C.PI_MAIN: "XEP_0070", |
53 C.PI_HANDLER: "yes", | 55 C.PI_HANDLER: "yes", |
54 C.PI_DESCRIPTION: _("""Implementation of HTTP Requests via XMPP""") | 56 C.PI_DESCRIPTION: _("""Implementation of HTTP Requests via XMPP"""), |
55 } | 57 } |
56 | 58 |
57 | 59 |
58 class XEP_0070(object): | 60 class XEP_0070(object): |
59 """ | 61 """ |
86 log.info(_("XEP-0070 Verifying HTTP Requests via XMPP (message)")) | 88 log.info(_("XEP-0070 Verifying HTTP Requests via XMPP (message)")) |
87 self._treatHttpAuthRequest(msg_elt, MSG, client) | 89 self._treatHttpAuthRequest(msg_elt, MSG, client) |
88 | 90 |
89 def _treatHttpAuthRequest(self, elt, stanzaType, client): | 91 def _treatHttpAuthRequest(self, elt, stanzaType, client): |
90 elt.handled = True | 92 elt.handled = True |
91 auth_elt = elt.elements(NS_HTTP_AUTH, 'confirm').next() | 93 auth_elt = elt.elements(NS_HTTP_AUTH, "confirm").next() |
92 auth_id = auth_elt['id'] | 94 auth_id = auth_elt["id"] |
93 auth_method = auth_elt['method'] | 95 auth_method = auth_elt["method"] |
94 auth_url = auth_elt['url'] | 96 auth_url = auth_elt["url"] |
95 self._dictRequest[client] = (auth_id, auth_method, auth_url, stanzaType, elt) | 97 self._dictRequest[client] = (auth_id, auth_method, auth_url, stanzaType, elt) |
96 | 98 |
97 confirm_ui = xml_tools.XMLUI("form", title=D_(u"Auth confirmation"), submit_id='') | 99 confirm_ui = xml_tools.XMLUI("form", title=D_(u"Auth confirmation"), submit_id="") |
98 confirm_ui.addText(D_(u"{} needs to validate your identity, do you agreeĀ ?".format(auth_url))) | 100 confirm_ui.addText( |
101 D_(u"{} needs to validate your identity, do you agreeĀ ?".format(auth_url)) | |
102 ) | |
99 confirm_ui.addText(D_(u"Validation code : {}".format(auth_id))) | 103 confirm_ui.addText(D_(u"Validation code : {}".format(auth_id))) |
100 confirm_ui.addText(D_(u"Please check that this code is the same as on {}".format(auth_url))) | 104 confirm_ui.addText( |
105 D_(u"Please check that this code is the same as on {}".format(auth_url)) | |
106 ) | |
101 confirm_ui.addText(u"") | 107 confirm_ui.addText(u"") |
102 confirm_ui.addText(D_(u"Submit to authorize, cancel otherwise.")) | 108 confirm_ui.addText(D_(u"Submit to authorize, cancel otherwise.")) |
103 d = xml_tools.deferredUI(self.host, confirm_ui, chained=False) | 109 d = xml_tools.deferredUI(self.host, confirm_ui, chained=False) |
104 d.addCallback(self._authRequestCallback, client.profile) | 110 d.addCallback(self._authRequestCallback, client.profile) |
105 self.host.actionNew({u"xmlui": confirm_ui.toXml()}, profile=client.profile) | 111 self.host.actionNew({u"xmlui": confirm_ui.toXml()}, profile=client.profile) |
106 | 112 |
107 def _authRequestCallback(self, result, profile): | 113 def _authRequestCallback(self, result, profile): |
108 client = self.host.getClient(profile) | 114 client = self.host.getClient(profile) |
109 try: | 115 try: |
110 cancelled = result['cancelled'] | 116 cancelled = result["cancelled"] |
111 except KeyError: | 117 except KeyError: |
112 cancelled = False | 118 cancelled = False |
113 | 119 |
114 authorized = False | 120 authorized = False |
115 | 121 |
117 auth_id, auth_method, auth_url, stanzaType, elt = self._dictRequest[client] | 123 auth_id, auth_method, auth_url, stanzaType, elt = self._dictRequest[client] |
118 del self._dictRequest[client] | 124 del self._dictRequest[client] |
119 authorized = False | 125 authorized = False |
120 else: | 126 else: |
121 try: | 127 try: |
122 auth_id, auth_method, auth_url, stanzaType, elt = self._dictRequest[client] | 128 auth_id, auth_method, auth_url, stanzaType, elt = self._dictRequest[ |
129 client | |
130 ] | |
123 del self._dictRequest[client] | 131 del self._dictRequest[client] |
124 authorized = True | 132 authorized = True |
125 except KeyError: | 133 except KeyError: |
126 authorized = False | 134 authorized = False |
127 | 135 |
128 if authorized: | 136 if authorized: |
129 if (stanzaType == IQ): | 137 if stanzaType == IQ: |
130 # iq | 138 # iq |
131 log.debug(_(u"XEP-0070 reply iq")) | 139 log.debug(_(u"XEP-0070 reply iq")) |
132 iq_result_elt = xmlstream.toResponse(elt, 'result') | 140 iq_result_elt = xmlstream.toResponse(elt, "result") |
133 client.send(iq_result_elt) | 141 client.send(iq_result_elt) |
134 elif (stanzaType == MSG): | 142 elif stanzaType == MSG: |
135 # message | 143 # message |
136 log.debug(_(u"XEP-0070 reply message")) | 144 log.debug(_(u"XEP-0070 reply message")) |
137 msg_result_elt = xmlstream.toResponse(elt, 'result') | 145 msg_result_elt = xmlstream.toResponse(elt, "result") |
138 msg_result_elt.addChild(elt.elements(NS_HTTP_AUTH, 'confirm').next()) | 146 msg_result_elt.addChild(elt.elements(NS_HTTP_AUTH, "confirm").next()) |
139 client.send(msg_result_elt) | 147 client.send(msg_result_elt) |
140 else: | 148 else: |
141 log.debug(_(u"XEP-0070 reply error")) | 149 log.debug(_(u"XEP-0070 reply error")) |
142 result_elt = jabber.error.StanzaError("not-authorized").toResponse(elt) | 150 result_elt = jabber.error.StanzaError("not-authorized").toResponse(elt) |
143 client.send(result_elt) | 151 client.send(result_elt) |
150 self.plugin_parent = plugin_parent | 158 self.plugin_parent = plugin_parent |
151 self.host = plugin_parent.host | 159 self.host = plugin_parent.host |
152 self.profile = profile | 160 self.profile = profile |
153 | 161 |
154 def connectionInitialized(self): | 162 def connectionInitialized(self): |
155 self.xmlstream.addObserver(IQ_HTTP_AUTH_REQUEST, self.plugin_parent.onHttpAuthRequestIQ, client=self.parent) | 163 self.xmlstream.addObserver( |
156 self.xmlstream.addObserver(MSG_HTTP_AUTH_REQUEST, self.plugin_parent.onHttpAuthRequestMsg, client=self.parent) | 164 IQ_HTTP_AUTH_REQUEST, |
165 self.plugin_parent.onHttpAuthRequestIQ, | |
166 client=self.parent, | |
167 ) | |
168 self.xmlstream.addObserver( | |
169 MSG_HTTP_AUTH_REQUEST, | |
170 self.plugin_parent.onHttpAuthRequestMsg, | |
171 client=self.parent, | |
172 ) | |
157 | 173 |
158 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): | 174 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): |
159 return [disco.DiscoFeature(NS_HTTP_AUTH)] | 175 return [disco.DiscoFeature(NS_HTTP_AUTH)] |
160 | 176 |
161 def getDiscoItems(self, requestor, target, nodeIdentifier=''): | 177 def getDiscoItems(self, requestor, target, nodeIdentifier=""): |
162 return [] | 178 return [] |