File: gateway.py

package info (click to toggle)
slidge 0.3.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,516 kB
  • sloc: python: 20,548; xml: 518; sh: 57; javascript: 27; makefile: 14
file content (73 lines) | stat: -rw-r--r-- 2,256 bytes parent folder | download | duplicates (2)
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
71
72
73
"""
The gateway
"""

from typing import Optional

from slixmpp import JID

from slidge import BaseGateway, FormField, GatewayUser
from slidge.command.register import RegistrationType

from .legacy_client import SuperDuperClient
from .util import ASSETS_DIR


class Gateway(BaseGateway):
    """
    This is instantiated once by the slidge entrypoint.

    By customizing the class attributes, we customize the registration process,
    and display name of the component.
    """

    COMPONENT_NAME = "The great legacy network (slidge)"
    COMPONENT_AVATAR = ASSETS_DIR / "slidge-color.png"
    COMPONENT_TYPE = "whatsapp"
    REGISTRATION_INSTRUCTIONS = (
        "Register to this fake service by using 'slidger' as username, and any "
        "password you want. Then you will need to enter '666' as the 2FA code."
    )
    REGISTRATION_TYPE = RegistrationType.TWO_FACTOR_CODE
    REGISTRATION_FIELDS = [
        FormField(var="username", label="User name", required=True),
        FormField(var="password", label="Password", required=True, private=True),
    ]
    GROUPS = True
    MARK_ALL_MESSAGES = True

    LEGACY_CONTACT_ID_TYPE = int

    async def validate(
        self, user_jid: JID, registration_form: dict[str, Optional[str]]
    ):
        """
        This function receives the values of the form defined in
        :attr:`REGISTRATION_FIELDS`. Here, since we set
        :attr:`REGISTRATION_TYPE` to "2FA", if this method does not raise any
        exception, the wannabe user will be prompted for their 2FA code.

        :param user_jid:
        :param registration_form:
        :return:
        """
        await SuperDuperClient.send_2fa(
            registration_form["username"],
            registration_form["password"],
        )

    async def validate_two_factor_code(self, user: GatewayUser, code: str):
        """
        This function receives the 2FA code entered by the aspiring user.

        It should raise something if the 2FA does not permit logging in to the
        legacy service.

        :param user:
        :param code:
        """
        await SuperDuperClient.validate_2fa(
            user.legacy_module_data["username"],
            user.legacy_module_data["password"],
            code,
        )