From f18efda31b78c7f1b2ededa97f9ff2fa0a0f0cb4 Mon Sep 17 00:00:00 2001 From: jesopo Date: Sun, 26 Apr 2020 01:08:47 +0100 Subject: [PATCH] isinstance for SASL types, so EXTERNAL doesn't have blank user/pass --- ircrobots/params.py | 26 +++++++++++--------------- ircrobots/sasl.py | 8 ++++---- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/ircrobots/params.py b/ircrobots/params.py index f0fc1e5..9c8d54f 100644 --- a/ircrobots/params.py +++ b/ircrobots/params.py @@ -2,23 +2,19 @@ from typing import Optional from dataclasses import dataclass class SASLParams(object): - def __init__(self, - mechanism: str, - username: str="", - password: str=""): - self.mechanism = mechanism.upper() - self.username = username - self.password = password + mechanism: str -class SASLUserPass(SASLParams): - def __init__(self, username: str, password: str): - super().__init__("USERPASS", username, password) -class SASLSCRAM(SASLParams): - def __init__(self, username: str, password: str): - super().__init__("SCRAM", username, password) +@dataclass +class _SASLUserPass(SASLParams): + username: str + password: str + +class SASLUserPass(_SASLUserPass): + mechanism = "USERPASS" +class SASLSCRAM(_SASLUserPass): + mechanism = "SCRAM" class SASLExternal(SASLParams): - def __init__(self): - super().__init__("EXTERNAL") + mechanism = "EXTERNAL" @dataclass class STSPolicy(object): diff --git a/ircrobots/sasl.py b/ircrobots/sasl.py index 028b744..e60d787 100644 --- a/ircrobots/sasl.py +++ b/ircrobots/sasl.py @@ -6,7 +6,7 @@ from ircstates.numerics import * from .matching import ResponseOr, Responses, Response, ANY from .contexts import ServerContext -from .params import SASLParams +from .params import SASLParams, SASLUserPass, SASLSCRAM, SASLExternal from .scram import SCRAMContext SASL_SCRAM_MECHANISMS = [ @@ -47,11 +47,11 @@ def _b64db(s: str) -> bytes: class SASLContext(ServerContext): async def from_params(self, params: SASLParams) -> SASLResult: - if params.mechanism == "USERPASS": + if isinstance(params, SASLUserPass): return await self.userpass(params.username, params.password) - elif params.mechanism == "SCRAM": + elif isinstance(params, SASLSCRAM): return await self.scram(params.username, params.password) - elif params.mechanism == "EXTERNAL": + elif isinstance(params, SASLExternal): return await self.external() else: raise SASLUnknownMechanismError(