move dataclasses to a dedicated file

This commit is contained in:
Istvan Ruzman
2020-09-03 13:56:57 +02:00
parent 2f151119c3
commit 93286edbb8
6 changed files with 104 additions and 102 deletions

View File

@@ -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"]

View File

@@ -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

View File

@@ -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]:

View File

@@ -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,

98
src/pyrad3/types.py Normal file
View File

@@ -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]

View File

@@ -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