isinstance for SASL types, so EXTERNAL doesn't have blank user/pass

This commit is contained in:
jesopo 2020-04-26 01:08:47 +01:00
parent 067fbbc531
commit f18efda31b
2 changed files with 15 additions and 19 deletions

View file

@ -2,23 +2,19 @@ from typing import Optional
from dataclasses import dataclass from dataclasses import dataclass
class SASLParams(object): class SASLParams(object):
def __init__(self, mechanism: str
mechanism: str,
username: str="",
password: str=""):
self.mechanism = mechanism.upper()
self.username = username
self.password = password
class SASLUserPass(SASLParams): @dataclass
def __init__(self, username: str, password: str): class _SASLUserPass(SASLParams):
super().__init__("USERPASS", username, password) username: str
class SASLSCRAM(SASLParams): password: str
def __init__(self, username: str, password: str):
super().__init__("SCRAM", username, password) class SASLUserPass(_SASLUserPass):
mechanism = "USERPASS"
class SASLSCRAM(_SASLUserPass):
mechanism = "SCRAM"
class SASLExternal(SASLParams): class SASLExternal(SASLParams):
def __init__(self): mechanism = "EXTERNAL"
super().__init__("EXTERNAL")
@dataclass @dataclass
class STSPolicy(object): class STSPolicy(object):

View file

@ -6,7 +6,7 @@ from ircstates.numerics import *
from .matching import ResponseOr, Responses, Response, ANY from .matching import ResponseOr, Responses, Response, ANY
from .contexts import ServerContext from .contexts import ServerContext
from .params import SASLParams from .params import SASLParams, SASLUserPass, SASLSCRAM, SASLExternal
from .scram import SCRAMContext from .scram import SCRAMContext
SASL_SCRAM_MECHANISMS = [ SASL_SCRAM_MECHANISMS = [
@ -47,11 +47,11 @@ def _b64db(s: str) -> bytes:
class SASLContext(ServerContext): class SASLContext(ServerContext):
async def from_params(self, params: SASLParams) -> SASLResult: async def from_params(self, params: SASLParams) -> SASLResult:
if params.mechanism == "USERPASS": if isinstance(params, SASLUserPass):
return await self.userpass(params.username, params.password) return await self.userpass(params.username, params.password)
elif params.mechanism == "SCRAM": elif isinstance(params, SASLSCRAM):
return await self.scram(params.username, params.password) return await self.scram(params.username, params.password)
elif params.mechanism == "EXTERNAL": elif isinstance(params, SASLExternal):
return await self.external() return await self.external()
else: else:
raise SASLUnknownMechanismError( raise SASLUnknownMechanismError(