diff --git a/src/pyrad3/__init__.py b/src/pyrad3/__init__.py index b64c7e0..e6a9dc0 100644 --- a/src/pyrad3/__init__.py +++ b/src/pyrad3/__init__.py @@ -20,4 +20,4 @@ __copyright__ = "Copyright 2020 Istvan Ruzman" __version__ = "0.1.0" __docformat__ = "restructuredtext en" -__all__ = ["client", "code", "dictionary", "packet", "tools", "utils"] +__all__ = ["client", "dictionary", "packet", "tools", "types", "utils"] diff --git a/src/pyrad3/code.py b/src/pyrad3/code.py deleted file mode 100644 index 74432e3..0000000 --- a/src/pyrad3/code.py +++ /dev/null @@ -1,35 +0,0 @@ -# Copyright 2020 Istvan Ruzman -# SPDX-License-Identifier: MIT OR Apache-2.0 - - -"""Valid RADIUS codes (registered in IANA) - -Currently not all RADIUS codes are contained, because -we don't support them (yet). -""" - -from enum import IntEnum - - -class Code(IntEnum): - """Valid RADIUS codes (registered in IANA)""" - - AccessRequest = 1 - AccessAccept = 2 - AccessReject = 3 - AccountingRequest = 4 - AccountingResponse = 5 - AccountingInterim = 6 - PasswordRequest = 7 - PasswordAck = 8 - PasswordReject = 9 - AccountingMessage = 10 - AccessChallenge = 11 - StatusServer = 12 - StatusClient = 13 - DisconnectRequest = 40 - DisconnectACK = 41 - DisconnectNAK = 42 - CoARequest = 43 - CoAACK = 44 - CoANAK = 45 diff --git a/src/pyrad3/dictionary.py b/src/pyrad3/dictionary.py index bcfe480..8674da6 100644 --- a/src/pyrad3/dictionary.py +++ b/src/pyrad3/dictionary.py @@ -7,11 +7,11 @@ Classes and Types to parse and represent a RADIUS dictionary. """ import logging -from dataclasses import dataclass -from enum import Enum, IntEnum, auto from os.path import dirname, isabs, join, normpath from typing import IO, Dict, Generator, List, Optional, Sequence, Tuple, Union +from pyrad3.types import Attribute, Datatype, Encrypt, Vendor + LOG = logging.getLogger(__name__) @@ -24,32 +24,6 @@ INTEGER_TYPES: Dict[str, Tuple[int, int]] = { } -class Datatype(Enum): - """Possible Datatypes for ATTRIBUTES""" - - string = auto() - octets = auto() - date = auto() - abinary = auto() - byte = auto() - short = auto() - integer = auto() - signed = auto() - integer64 = auto() - ipaddr = auto() - ipv4prefix = auto() - ipv6addr = auto() - ipv6prefix = auto() - comboip = auto() - ifid = auto() - ether = auto() - concat = auto() - tlv = auto() - extended = auto() - longextended = auto() - evs = auto() - - class ParseError(Exception): """RADIUS Dictionary Parser Error""" @@ -71,41 +45,6 @@ class ParseError(Exception): return f"{self.file}{line}: ParseError: {self.msg}" -class Encrypt(IntEnum): - """Enum for different RADIUS Encryption types.""" - - NoEncrpytion = 0 - RadiusCrypt = 1 - SaltCrypt = 2 - AscendCrypt = 3 - - -@dataclass -class Attribute: # pylint: disable=too-many-instance-attributes - """RADIUS Attribute definition""" - - name: str - code: int - datatype: Datatype - values: Dict[Union[int, str], Union[int, str]] - has_tag: bool = False - encrypt: Encrypt = Encrypt(0) - is_sub_attr: bool = False - # vendor = Dictionary - - -@dataclass -class Vendor: - """Representation of a vendor""" - - name: str - code: int - tlength: int - llength: int - continuation: bool - attrs: Dict[Union[int, Tuple[int, ...]], Attribute] - - def dict_parser( filename: str, rad_dict: IO ) -> Generator[Tuple[int, List[str]], None, None]: diff --git a/src/pyrad3/packet.py b/src/pyrad3/packet.py index f3e0a61..603ac2e 100644 --- a/src/pyrad3/packet.py +++ b/src/pyrad3/packet.py @@ -9,8 +9,8 @@ from collections import OrderedDict from secrets import token_bytes from typing import Any, Dict, Optional, Sequence, Tuple, Union -from pyrad3.code import Code from pyrad3.host import Host +from pyrad3.types import Code from pyrad3.utils import ( Attribute, PacketError, diff --git a/src/pyrad3/types.py b/src/pyrad3/types.py new file mode 100644 index 0000000..6beaad5 --- /dev/null +++ b/src/pyrad3/types.py @@ -0,0 +1,98 @@ +# Copyright 2020 Istvan Ruzman +# SPDX-License-Identifier: MIT OR Apache-2.0 + + +"""Valid RADIUS codes (registered in IANA) + +Currently not all RADIUS codes are contained, because +we don't support them (yet). +""" + +from dataclasses import dataclass +from enum import Enum, IntEnum, auto +from typing import Dict, Tuple, Union + + +class Code(IntEnum): + """Valid RADIUS codes (registered in IANA)""" + + AccessRequest = 1 + AccessAccept = 2 + AccessReject = 3 + AccountingRequest = 4 + AccountingResponse = 5 + AccountingInterim = 6 + PasswordRequest = 7 + PasswordAck = 8 + PasswordReject = 9 + AccountingMessage = 10 + AccessChallenge = 11 + StatusServer = 12 + StatusClient = 13 + DisconnectRequest = 40 + DisconnectACK = 41 + DisconnectNAK = 42 + CoARequest = 43 + CoAACK = 44 + CoANAK = 45 + + +class Datatype(Enum): + """Possible Datatypes for ATTRIBUTES""" + + string = auto() + octets = auto() + date = auto() + abinary = auto() + byte = auto() + short = auto() + integer = auto() + signed = auto() + integer64 = auto() + ipaddr = auto() + ipv4prefix = auto() + ipv6addr = auto() + ipv6prefix = auto() + comboip = auto() + ifid = auto() + ether = auto() + concat = auto() + tlv = auto() + extended = auto() + longextended = auto() + evs = auto() + + +class Encrypt(IntEnum): + """Enum for different RADIUS Encryption types.""" + + NoEncrpytion = 0 + RadiusCrypt = 1 + SaltCrypt = 2 + AscendCrypt = 3 + + +@dataclass +class Attribute: # pylint: disable=too-many-instance-attributes + """RADIUS Attribute definition""" + + name: str + code: int + datatype: Datatype + values: Dict[Union[int, str], Union[int, str]] + has_tag: bool = False + encrypt: Encrypt = Encrypt(0) + is_sub_attr: bool = False + # vendor = Dictionary + + +@dataclass +class Vendor: + """Representation of a vendor""" + + name: str + code: int + tlength: int + llength: int + continuation: bool + attrs: Dict[Union[int, Tuple[int, ...]], Attribute] diff --git a/src/pyrad3/utils.py b/src/pyrad3/utils.py index 4071ab2..cf9081d 100644 --- a/src/pyrad3/utils.py +++ b/src/pyrad3/utils.py @@ -9,9 +9,9 @@ import struct from collections import namedtuple from typing import List, Optional, Tuple, Union -from pyrad3.code import Code -from pyrad3.dictionary import Attribute as DictAttr, Datatype, Dictionary +from pyrad3.dictionary import Dictionary from pyrad3.tools import decode_attr +from pyrad3.types import Attribute as DictAttr, Code, Datatype RANDOM_GENERATOR = secrets.SystemRandom() MD5 = hashlib.md5