File: auth_api.py

package info (click to toggle)
python-starline 0.1.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 112 kB
  • sloc: python: 375; makefile: 7
file content (72 lines) | stat: -rw-r--r-- 2,854 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
71
72
"""Auth StarLine API."""
import hashlib
import logging
from .base_api import BaseApi

_LOGGER = logging.getLogger(__name__)


class StarlineAuth(BaseApi):
    """Auth StarLine API class."""

    def get_app_code(self, app_id: str, app_secret: str) -> str:
        """Get application code for getting application token."""

        url = "https://id.starline.ru/apiV3/application/getCode/"
        payload = {
            "appId": app_id,
            "secret": hashlib.md5(app_secret.encode(self._encoding)).hexdigest(),
        }
        response = self._get(url, params=payload)
        if response is None:
            raise Exception("Failed to get application code")

        if int(response["state"]) == 1:
            app_code = response["desc"]["code"]
            _LOGGER.debug("Application code: {}".format(app_code))
            return app_code

        raise Exception("Invalid response state: {}", response["state"])

    def get_app_token(self, app_id: str, app_secret: str, app_code: str) -> str:
        """Get application token for authentication."""

        url = "https://id.starline.ru/apiV3/application/getToken/"
        payload = {
            "appId": app_id,
            "secret": hashlib.md5((app_secret + app_code).encode(self._encoding)).hexdigest(),
        }
        response = self._get(url, params=payload)
        if response is None:
            raise Exception("Failed to get application token")

        if int(response["state"]) == 1:
            app_token = response["desc"]["token"]
            _LOGGER.debug("Application token: {}".format(app_token))
            return app_token

        raise Exception("Invalid response state: {}", response["state"])

    def get_slid_user_token(self, app_token: str, user_login: str, user_password: str, sms_code: str = None, captcha_sid: str = None, captcha_code: str = None) -> (bool, dict):
        """Authenticate user by login, password and application token."""

        url = "https://id.starline.ru/apiV3/user/login/"
        payload = {"token": app_token}
        data = {
            "login": user_login,
            "pass": hashlib.sha1(user_password.encode(self._encoding)).hexdigest(),
        }
        if sms_code is not None:
            data["smsCode"] = sms_code
        if (captcha_sid is not None) and (captcha_code is not None):
            data["captchaSid"] = captcha_sid
            data["captchaCode"] = captcha_code
        response = self._post(url, params=payload, data=data)
        if response is None:
            raise Exception("Failed to get user token")

        state = int(response["state"])
        if (state == 1) or (state == 2) or (state == 0 and "captchaSid" in response["desc"]) or (state == 0 and "phone" in response["desc"]):
            return state, response["desc"]

        raise Exception("Invalid response state: {}", state)