Refactor http auth code into own module
This commit is contained in:
parent
8fa1ee4617
commit
129f72bf8e
|
@ -70,6 +70,7 @@ from gajim.common.modules.search import Search
|
|||
from gajim.common.modules.annotations import Annotations
|
||||
from gajim.common.modules.roster_item_exchange import RosterItemExchange
|
||||
from gajim.common.modules.last_activity import LastActivity
|
||||
from gajim.common.modules.http_auth import HTTPAuth
|
||||
from gajim.common.connection_handlers import *
|
||||
from gajim.common.contacts import GC_Contact
|
||||
from gajim.gtkgui_helpers import get_action
|
||||
|
@ -664,6 +665,7 @@ class Connection(CommonConnection, ConnectionHandlers):
|
|||
self.register_module('Annotations', Annotations, self)
|
||||
self.register_module('RosterItemExchange', RosterItemExchange, self)
|
||||
self.register_module('LastActivity', LastActivity, self)
|
||||
self.register_module('HTTPAuth', HTTPAuth, self)
|
||||
|
||||
app.ged.register_event_handler('privacy-list-received', ged.CORE,
|
||||
self._nec_privacy_list_received)
|
||||
|
|
|
@ -1342,8 +1342,6 @@ ConnectionHTTPUpload):
|
|||
Archiving313PreferencesChangedReceivedEvent)
|
||||
app.nec.register_incoming_event(NotificationEvent)
|
||||
|
||||
app.ged.register_event_handler('http-auth-received', ged.CORE,
|
||||
self._nec_http_auth_received)
|
||||
app.ged.register_event_handler('roster-set-received',
|
||||
ged.CORE, self._nec_roster_set_received)
|
||||
app.ged.register_event_handler('private-storage-bookmarks-received',
|
||||
|
@ -1375,8 +1373,6 @@ ConnectionHTTPUpload):
|
|||
ConnectionArchive313.cleanup(self)
|
||||
ConnectionPubSub.cleanup(self)
|
||||
ConnectionHTTPUpload.cleanup(self)
|
||||
app.ged.remove_event_handler('http-auth-received', ged.CORE,
|
||||
self._nec_http_auth_received)
|
||||
app.ged.remove_event_handler('roster-set-received',
|
||||
ged.CORE, self._nec_roster_set_received)
|
||||
app.ged.remove_event_handler('private-storage-bookmarks-received',
|
||||
|
@ -1419,32 +1415,6 @@ ConnectionHTTPUpload):
|
|||
c.setAttr('ver', app.caps_hash[self.name])
|
||||
return p
|
||||
|
||||
def build_http_auth_answer(self, iq_obj, answer):
|
||||
if not self.connection or self.connected < 2:
|
||||
return
|
||||
if answer == 'yes':
|
||||
confirm = iq_obj.getTag('confirm')
|
||||
reply = iq_obj.buildReply('result')
|
||||
if iq_obj.getName() == 'message':
|
||||
reply.addChild(node=confirm)
|
||||
self.connection.send(reply)
|
||||
elif answer == 'no':
|
||||
err = nbxmpp.Error(iq_obj, nbxmpp.protocol.ERR_NOT_AUTHORIZED)
|
||||
self.connection.send(err)
|
||||
|
||||
def _nec_http_auth_received(self, obj):
|
||||
if obj.conn.name != self.name:
|
||||
return
|
||||
if obj.opt in ('yes', 'no'):
|
||||
obj.conn.build_http_auth_answer(obj.stanza, obj.opt)
|
||||
return True
|
||||
|
||||
def _HttpAuthCB(self, con, iq_obj):
|
||||
log.debug('HttpAuthCB')
|
||||
app.nec.push_incoming_event(HttpAuthReceivedEvent(None, conn=self,
|
||||
stanza=iq_obj))
|
||||
raise nbxmpp.NodeProcessed
|
||||
|
||||
def _ErrorCB(self, con, iq_obj):
|
||||
log.debug('ErrorCB')
|
||||
app.nec.push_incoming_event(IqErrorReceivedEvent(None, conn=self,
|
||||
|
@ -2065,7 +2035,6 @@ ConnectionHTTPUpload):
|
|||
con.RegisterHandler('iq', self._PrivateCB, 'result', nbxmpp.NS_PRIVATE)
|
||||
con.RegisterHandler('iq', self._SecLabelCB, 'result',
|
||||
nbxmpp.NS_SECLABEL_CATALOG)
|
||||
con.RegisterHandler('iq', self._HttpAuthCB, 'get', nbxmpp.NS_HTTP_AUTH)
|
||||
con.RegisterHandler('iq', self._CommandExecuteCB, 'set',
|
||||
nbxmpp.NS_COMMANDS)
|
||||
con.RegisterHandler('iq', self._DiscoverInfoGetCB, 'get',
|
||||
|
|
|
@ -180,19 +180,6 @@ class HelperEvent:
|
|||
self.muc_pm = muc_user.getChildren() == []
|
||||
return self.muc_pm
|
||||
|
||||
class HttpAuthReceivedEvent(nec.NetworkIncomingEvent):
|
||||
name = 'http-auth-received'
|
||||
base_network_events = []
|
||||
|
||||
def generate(self):
|
||||
self.opt = app.config.get_per('accounts', self.conn.name, 'http_auth')
|
||||
self.iq_id = self.stanza.getTagAttr('confirm', 'id')
|
||||
self.method = self.stanza.getTagAttr('confirm', 'method')
|
||||
self.url = self.stanza.getTagAttr('confirm', 'url')
|
||||
# In case it's a message with a body
|
||||
self.msg = self.stanza.getTagData('body')
|
||||
return True
|
||||
|
||||
class RosterReceivedEvent(nec.NetworkIncomingEvent):
|
||||
name = 'roster-received'
|
||||
base_network_events = []
|
||||
|
@ -1060,8 +1047,8 @@ class MessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):
|
|||
|
||||
# check if the message is a XEP-0070 confirmation request
|
||||
if self.stanza.getTag('confirm', namespace=nbxmpp.NS_HTTP_AUTH):
|
||||
app.nec.push_incoming_event(HttpAuthReceivedEvent(None,
|
||||
conn=self.conn, stanza=self.stanza))
|
||||
self.conn.get_module('HTTPAuth').answer_request(
|
||||
self.conn, self.stanza)
|
||||
return
|
||||
|
||||
try:
|
||||
|
|
|
@ -0,0 +1,76 @@
|
|||
# This file is part of Gajim.
|
||||
#
|
||||
# Gajim 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; version 3 only.
|
||||
#
|
||||
# Gajim 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 Gajim. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
# XEP-0070: Verifying HTTP Requests via XMPP
|
||||
|
||||
import logging
|
||||
|
||||
import nbxmpp
|
||||
|
||||
from gajim.common import app
|
||||
from gajim.common.nec import NetworkIncomingEvent
|
||||
|
||||
log = logging.getLogger('gajim.c.m.http_auth')
|
||||
|
||||
|
||||
class HTTPAuth:
|
||||
def __init__(self, con):
|
||||
self._con = con
|
||||
self._account = con.name
|
||||
|
||||
self.handlers = [
|
||||
('iq', self.answer_request, 'get', nbxmpp.NS_HTTP_AUTH)
|
||||
]
|
||||
|
||||
def answer_request(self, con, stanza):
|
||||
log.info('Auth request received')
|
||||
auto_answer = app.config.get_per(
|
||||
'accounts', self._account, 'http_auth')
|
||||
if auto_answer in ('yes', 'no'):
|
||||
self.build_http_auth_answer(stanza, auto_answer)
|
||||
raise nbxmpp.NodeProcessed
|
||||
|
||||
iq_id = stanza.getTagAttr('confirm', 'id')
|
||||
method = stanza.getTagAttr('confirm', 'method')
|
||||
url = stanza.getTagAttr('confirm', 'url')
|
||||
# In case it's a message with a body
|
||||
msg = stanza.getTagData('body')
|
||||
|
||||
app.nec.push_incoming_event(
|
||||
HttpAuthReceivedEvent(None, conn=self._con,
|
||||
iq_id=iq_id,
|
||||
method=method,
|
||||
url=url,
|
||||
msg=msg,
|
||||
stanza=stanza))
|
||||
|
||||
raise nbxmpp.NodeProcessed
|
||||
|
||||
def build_http_auth_answer(self, stanza, answer):
|
||||
if answer == 'yes':
|
||||
log.info('Auth request approved')
|
||||
confirm = stanza.getTag('confirm')
|
||||
reply = stanza.buildReply('result')
|
||||
if stanza.getName() == 'message':
|
||||
reply.addChild(node=confirm)
|
||||
self._con.connection.send(reply)
|
||||
elif answer == 'no':
|
||||
log.info('Auth request denied')
|
||||
err = nbxmpp.Error(stanza, nbxmpp.protocol.ERR_NOT_AUTHORIZED)
|
||||
self._con.connection.send(err)
|
||||
|
||||
|
||||
class HttpAuthReceivedEvent(NetworkIncomingEvent):
|
||||
name = 'http-auth-received'
|
||||
base_network_events = []
|
|
@ -173,7 +173,8 @@ class Interface:
|
|||
def handle_event_http_auth(obj):
|
||||
#('HTTP_AUTH', account, (method, url, transaction_id, iq_obj, msg))
|
||||
def response(account, answer):
|
||||
obj.conn.build_http_auth_answer(obj.stanza, answer)
|
||||
obj.conn.get_module('HTTPAuth').build_http_auth_answer(
|
||||
obj.stanza, answer)
|
||||
|
||||
def on_yes(is_checked, obj):
|
||||
response(obj, 'yes')
|
||||
|
|
Loading…
Reference in New Issue