Mercurial > libervia-backend
comparison src/plugins/plugin_xep_0070.py @ 2094:438a49dbfe87
xep-0070: fixed use of byte string instead of unicode
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 18 Dec 2016 10:16:47 +0100 |
parents | 0694a2611bad |
children | 6a66c8c5a567 |
comparison
equal
deleted
inserted
replaced
2093:cbf3af02e941 | 2094:438a49dbfe87 |
---|---|
15 # GNU Affero General Public License for more details. | 15 # GNU Affero General Public License for more details. |
16 | 16 |
17 # You should have received a copy of the GNU Affero General Public License | 17 # You should have received a copy of the GNU Affero General Public License |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 from sat.core.i18n import _, D_ | 19 from sat.core.i18n import _, D_ |
20 from sat.core.constants import Const as C | |
21 from sat.core.i18n import _ | |
22 from sat.core.log import getLogger | 20 from sat.core.log import getLogger |
23 from twisted.words.protocols.jabber import xmlstream | 21 from twisted.words.protocols.jabber import xmlstream |
24 from twisted.words.protocols import jabber | 22 from twisted.words.protocols import jabber |
25 log = getLogger(__name__) | 23 log = getLogger(__name__) |
26 from sat.tools import xml_tools | 24 from sat.tools import xml_tools |
60 """ | 58 """ |
61 Implementation for XEP 0070. | 59 Implementation for XEP 0070. |
62 """ | 60 """ |
63 | 61 |
64 def __init__(self, host): | 62 def __init__(self, host): |
65 log.info(_("Plugin XEP_0070 initialization")) | 63 log.info(_(u"Plugin XEP_0070 initialization")) |
66 self.host = host | 64 self.host = host |
67 self._dictRequest = dict() | 65 self._dictRequest = dict() |
68 | 66 |
69 def getHandler(self, profile): | 67 def getHandler(self, profile): |
70 return XEP_0070_handler(self, profile) | 68 return XEP_0070_handler(self, profile) |
71 | 69 |
72 def onHttpAuthRequestIQ(self, iq_elt, client): | 70 def onHttpAuthRequestIQ(self, iq_elt, client): |
73 """This method is called on confirmation request received (XEP-0070 #4.5) | 71 """This method is called on confirmation request received (XEP-0070 #4.5) |
72 | |
74 @param iq_elt: IQ element | 73 @param iq_elt: IQ element |
75 @param client: %(doc_client)s""" | 74 @param client: %(doc_client)s |
75 """ | |
76 log.info(_("XEP-0070 Verifying HTTP Requests via XMPP (iq)")) | 76 log.info(_("XEP-0070 Verifying HTTP Requests via XMPP (iq)")) |
77 self._treatHttpAuthRequest(iq_elt, IQ, client) | 77 self._treatHttpAuthRequest(iq_elt, IQ, client) |
78 | 78 |
79 def onHttpAuthRequestMsg(self, msg_elt, client): | 79 def onHttpAuthRequestMsg(self, msg_elt, client): |
80 """This method is called on confirmation request received (XEP-0070 #4.5) | 80 """This method is called on confirmation request received (XEP-0070 #4.5) |
81 | |
81 @param msg_elt: message element | 82 @param msg_elt: message element |
82 @param client: %(doc_client)s""" | 83 @param client: %(doc_client)s |
84 """ | |
83 log.info(_("XEP-0070 Verifying HTTP Requests via XMPP (message)")) | 85 log.info(_("XEP-0070 Verifying HTTP Requests via XMPP (message)")) |
84 self._treatHttpAuthRequest(msg_elt, MSG, client) | 86 self._treatHttpAuthRequest(msg_elt, MSG, client) |
85 | 87 |
86 def _treatHttpAuthRequest(self, elt, stanzaType, client): | 88 def _treatHttpAuthRequest(self, elt, stanzaType, client): |
87 elt.handled = True | 89 elt.handled = True |
89 auth_id = auth_elt['id'] | 91 auth_id = auth_elt['id'] |
90 auth_method = auth_elt['method'] | 92 auth_method = auth_elt['method'] |
91 auth_url = auth_elt['url'] | 93 auth_url = auth_elt['url'] |
92 self._dictRequest[client] = (auth_id, auth_method, auth_url, stanzaType, elt) | 94 self._dictRequest[client] = (auth_id, auth_method, auth_url, stanzaType, elt) |
93 | 95 |
94 confirm_ui = xml_tools.XMLUI("form", title=D_("Auth confirmation"), submit_id='') | 96 confirm_ui = xml_tools.XMLUI("form", title=D_(u"Auth confirmation"), submit_id='') |
95 confirm_ui.addText(D_("{} needs to validate your identity, do you agreeĀ ?".format(auth_url))) | 97 confirm_ui.addText(D_(u"{} needs to validate your identity, do you agreeĀ ?".format(auth_url))) |
96 confirm_ui.addText(D_("Validation code : {}".format(auth_id))) | 98 confirm_ui.addText(D_(u"Validation code : {}".format(auth_id))) |
97 confirm_ui.addText(D_("Please check that this code is the same as on {}".format(auth_url))) | 99 confirm_ui.addText(D_(u"Please check that this code is the same as on {}".format(auth_url))) |
98 confirm_ui.addText("") | 100 confirm_ui.addText(u"") |
99 confirm_ui.addText(D_("Submit to authorize, cancel otherwise.")) | 101 confirm_ui.addText(D_(u"Submit to authorize, cancel otherwise.")) |
100 d = xml_tools.deferredUI(self.host, confirm_ui, chained=False) | 102 d = xml_tools.deferredUI(self.host, confirm_ui, chained=False) |
101 d.addCallback(self._authRequestCallback, client.profile) | 103 d.addCallback(self._authRequestCallback, client.profile) |
102 self.host.actionNew({"xmlui": confirm_ui.toXml()}, profile=client.profile) | 104 self.host.actionNew({u"xmlui": confirm_ui.toXml()}, profile=client.profile) |
103 | 105 |
104 def _authRequestCallback(self, result, profile): | 106 def _authRequestCallback(self, result, profile): |
105 client = self.host.getClient(profile) | 107 client = self.host.getClient(profile) |
106 try: | 108 try: |
107 cancelled = result['cancelled'] | 109 cancelled = result['cancelled'] |
123 authorized = False | 125 authorized = False |
124 | 126 |
125 if authorized: | 127 if authorized: |
126 if (stanzaType == IQ): | 128 if (stanzaType == IQ): |
127 # iq | 129 # iq |
128 log.debug(_("XEP-0070 reply iq")) | 130 log.debug(_(u"XEP-0070 reply iq")) |
129 iq_result_elt = xmlstream.toResponse(elt, 'result') | 131 iq_result_elt = xmlstream.toResponse(elt, 'result') |
130 client.xmlstream.send(iq_result_elt) | 132 client.xmlstream.send(iq_result_elt) |
131 elif (stanzaType == MSG): | 133 elif (stanzaType == MSG): |
132 # message | 134 # message |
133 log.debug(_("XEP-0070 reply message")) | 135 log.debug(_(u"XEP-0070 reply message")) |
134 msg_result_elt = xmlstream.toResponse(elt, 'result') | 136 msg_result_elt = xmlstream.toResponse(elt, 'result') |
135 msg_result_elt.addChild(elt.elements(NS_HTTP_AUTH, 'confirm').next()) | 137 msg_result_elt.addChild(elt.elements(NS_HTTP_AUTH, 'confirm').next()) |
136 client.xmlstream.send(msg_result_elt) | 138 client.xmlstream.send(msg_result_elt) |
137 else: | 139 else: |
138 log.debug(_("XEP-0070 reply error")) | 140 log.debug(_(u"XEP-0070 reply error")) |
139 result_elt = jabber.error.StanzaError("not-authorized").toResponse(elt) | 141 result_elt = jabber.error.StanzaError("not-authorized").toResponse(elt) |
140 client.xmlstream.send(result_elt) | 142 client.xmlstream.send(result_elt) |
141 | 143 |
142 | 144 |
143 class XEP_0070_handler(XMPPHandler): | 145 class XEP_0070_handler(XMPPHandler): |