File: session_authenticate.py

package info (click to toggle)
python-xknx 3.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,012 kB
  • sloc: python: 39,710; javascript: 8,556; makefile: 27; sh: 12
file content (70 lines) | stat: -rw-r--r-- 2,276 bytes parent folder | download
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
Module for Serialization and Deserialization of KNX Session Authenticates.

The SESSION_AUTHENTICATE shall be sent by the KNXnet/IP secure client to the
control endpoint of the KNXnet/IP secure server after the Diffie-Hellman handshake
to authenticate the user against the server device.
"""

from __future__ import annotations

from typing import Final

from xknx.exceptions import CouldNotParseKNXIP

from .body import KNXIPBody
from .knxip_enum import KNXIPServiceType


class SessionAuthenticate(KNXIPBody):
    """Representation of a KNX Session Authenticate."""

    SERVICE_TYPE = KNXIPServiceType.SESSION_AUTHENTICATE
    LENGTH: Final = 18

    def __init__(
        self,
        user_id: int = 0x02,
        message_authentication_code: bytes = bytes(16),
    ) -> None:
        """Initialize SessionAuthenticate object."""
        # 00h: Reserved, shall not be used
        # 01h: Management level access
        # 02h - 7Fh: User level access
        # 80h - FFh: Reserved, shall not be used
        # TODO: maybe use an Enum instead of int or raise an error in
        #   to_knx() and handle in RequestResponse class
        self.user_id = user_id
        self.message_authentication_code = message_authentication_code

    def calculated_length(self) -> int:
        """Get length of KNX/IP body."""
        return SessionAuthenticate.LENGTH

    def from_knx(self, raw: bytes) -> int:
        """Parse/deserialize from KNX/IP raw data."""
        if len(raw) != SessionAuthenticate.LENGTH:
            raise CouldNotParseKNXIP("SessionAuthenticate has wrong length")
        self.user_id = raw[1]
        self.message_authentication_code = raw[2:]
        return SessionAuthenticate.LENGTH

    def to_knx(self) -> bytes:
        """Serialize to KNX/IP raw data."""
        return (
            bytes(
                (
                    0x00,  # reserved
                    self.user_id,
                )
            )
            + self.message_authentication_code
        )

    def __repr__(self) -> str:
        """Return object as readable string."""
        return (
            f"<SessionAuthenticate "
            f'user_id="{self.user_id}" '
            f'message_authentication_code="{self.message_authentication_code.hex()}" />'
        )