1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
import abc
from enum import Enum
from typing import Optional
from .abc import AbstractConnection
class AuthBase:
value: Optional[str]
def __init__(self, connector: AbstractConnection):
self.connector = connector
self.value = None
@abc.abstractmethod
def encode(self) -> str:
raise NotImplementedError
def marshal(self) -> str:
if self.value is None:
self.value = self.encode()
return self.value
class PlainAuth(AuthBase):
def encode(self) -> str:
return (
"\x00"
+ (self.connector.url.user or "guest")
+ "\x00"
+ (self.connector.url.password or "guest")
)
class ExternalAuth(AuthBase):
def encode(self) -> str:
return ""
class AuthMechanism(Enum):
PLAIN = PlainAuth
EXTERNAL = ExternalAuth
|