From eba2c387f0a92731be0893298d87e405812c36bf Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Sun, 14 Aug 2022 12:43:33 +0100 Subject: [PATCH] Implement API for authentication management actions --- api/views.py | 33 +++++++++++++++++++++++++++++++++ commands/getstr.py | 4 ++-- core/bot.py | 12 +++++++++--- modules/helpers.py | 19 ++++++++++++++++++- modules/regproc.py | 42 +++++++++++++++++++++++++++++++++++++----- 5 files changed, 99 insertions(+), 11 deletions(-) diff --git a/api/views.py b/api/views.py index 842c186..78e2b51 100644 --- a/api/views.py +++ b/api/views.py @@ -213,6 +213,39 @@ class API(object): network["records"] = userinfo.getNumWhoEntries(net) return dumps(network) + @app.route("/irc/network/auth/", methods=["POST"]) + @login_required + def irc_network_recheckauth(self, request): + try: + data = loads(request.content.read()) + except JSONDecodeError: + return "Invalid JSON" + if "func" not in data: + return dumps({"success": False, "reason": "no function specified."}) + func = data["func"] + + if "net" not in data: + return dumps({"success": False, "reason": "no net specified."}) + net = data["net"] + if not net or net == "None": + nets = main.network.keys() + else: + if net not in main.network.keys(): + return dumps({"success": False, "reason": "no such net."}) + nets = [net] + for net_name in nets: + conns = helpers.get_connected_relays(net_name) + if not conns: + return dumps({"success": False, "reason": f"failed to get instances for {net_name}."}) + if func == "recheckauth": + for conn in conns: + conn.regPing() + elif func == "resetauth": + for conn in conns: + conn.authenticated = False + conn.regPing() + return dumps({"success": True}) + @app.route("/irc/network//", methods=["DELETE"]) @login_required def irc_network_delete(self, request, net): diff --git a/commands/getstr.py b/commands/getstr.py index 205088e..c4fe6a9 100644 --- a/commands/getstr.py +++ b/commands/getstr.py @@ -1,7 +1,7 @@ import main - from utils.get import getRelay + class GetstrCommand: def __init__(self, *args): self.getstr(*args) @@ -11,7 +11,7 @@ class GetstrCommand: if length == 3: net = spl[1] num = spl[2] - if not net in main.network.keys(): + if net not in main.network.keys(): failure("Network does not exist: %s" % net) return if not num.isdigit(): diff --git a/core/bot.py b/core/bot.py index 36fc7c4..5b2b04d 100644 --- a/core/bot.py +++ b/core/bot.py @@ -55,7 +55,7 @@ class IRCBot(IRCClient): self.isconnected = False self.channels = [] self.net = net - self.authenticated = not regproc.needToRegister(self.net) + self.authenticated = not regproc.needToAuth(self.net) self.num = num self.buffer = "" self.name = net + str(num) @@ -621,8 +621,9 @@ class IRCBot(IRCClient): def regPing(self, negativepass=None): if self.authenticated: return - if not regproc.needToRegister(self.net): + if not regproc.needToAuth(self.net): self.authenticated = True + return sinst = regproc.substitute(self.net, self.num) if not sinst: error(f"regPing() {self.net}: registration ping failed for {self.num}") @@ -649,7 +650,12 @@ class IRCBot(IRCClient): if sinst["negative"]: self._negativePass = None self.msg(sinst["entity"], sinst["negativemsg"]) - debug(f"regPing() {self.net}: sent negativemsg '{sinst['negativemsg']}' to {sinst['entity']} - {self.num}") + debug( + ( + f"regPing() {self.net}: sent negativemsg " + f"'{sinst['negativemsg']}' to {sinst['entity']} - {self.num}" + ) + ) return else: self._negativePass = True diff --git a/modules/helpers.py b/modules/helpers.py index 8d419ae..f2a8690 100644 --- a/modules/helpers.py +++ b/modules/helpers.py @@ -1,6 +1,7 @@ import main from modules import chankeep -from utils.logging.debug import debug + +# from utils.logging.debug import debug def get_first_relay(net): @@ -53,3 +54,19 @@ def get_active_relays(net): if name in main.IRCPool.keys(): active_insts.append(main.IRCPool[name]) return active_insts + + +def get_connected_relays(net): + """ + Get all connected instances for the network. + :param net: the network + :return: list of active instances + :rtype: list of IRCPool instances + """ + active_nums = chankeep.getConnectedRelays(net) + active_insts = [] + for num in active_nums: + name = net + str(num) + if name in main.IRCPool.keys(): + active_insts.append(main.IRCPool[name]) + return active_insts diff --git a/modules/regproc.py b/modules/regproc.py index 364b375..edb411c 100644 --- a/modules/regproc.py +++ b/modules/regproc.py @@ -21,6 +21,13 @@ def needToRegister(net): return False +def needToAuth(net): + networkObj = main.network[net] + if networkObj.auth == "none": + return False + return True + + def selectInst(net): if net in main.irc.keys(): inst = deepcopy(main.irc[net]) @@ -159,28 +166,53 @@ def registerTest(c): confirmRegistration( c["net"], c["num"], negativepass=False ) # Not passed negative check, report back - debug(f"registerTest() {net} - {num} not passed negative:checknegativemsg check, {sinst['checknegativemsg']} present in message") + debug( + ( + f"registerTest() {net} - {num} not passed negative:checknegativemsg " + f"check, {sinst['checknegativemsg']} present in message" + ) + ) return if sinst["checkendnegative"] in c["msg"]: confirmRegistration( c["net"], c["num"], negativepass=True ) # Passed the negative check, report back - debug(f"registerTest() {net} - {num} passed negative:checkendnegative check, {sinst['checkendnegative']} present in message") + debug( + ( + f"registerTest() {net} - {num} passed negative:checkendnegative " + f"check, {sinst['checkendnegative']} present in message" + ) + ) return if sinst["ping"]: if sinst["checkmsg2"] in c["msg"] and c["nick"] == sinst["entity"]: confirmRegistration(c["net"], c["num"]) - debug(f"registerTest() {net} - {num} passed ping:checkmsg2 check, {sinst['checkmsg2']} present in message") + debug( + ( + f"registerTest() {net} - {num} passed ping:checkmsg2 " + f"check, {sinst['checkmsg2']} present in message" + ) + ) return if sinst["checktype"] == "msg": if sinst["checkmsg"] in c["msg"]: confirmRegistration(c["net"], c["num"]) - debug(f"registerTest() {net} - {num} passed checktype:msg:checkmsg check, {sinst['checkmsg']} present in message") + debug( + ( + f"registerTest() {net} - {num} passed checktype:msg:checkmsg check, " + f"{sinst['checkmsg']} present in message" + ) + ) return elif sinst["checktype"] == "mode": if c["type"] == "self": if c["mtype"] == "mode": if sinst["checkmode"] in c["mode"] and c["status"] is True: confirmRegistration(c["net"], c["num"]) - debug(f"registerTest() {net} - {num} passed checktype:mode:checkmost check, {sinst['checkmode']} present in mode") + debug( + ( + f"registerTest() {net} - {num} passed checktype:mode:checkmost check, " + f"{sinst['checkmode']} present in mode" + ) + ) return