kuon.steam.steam_login   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
eloc 56
dl 0
loc 106
ccs 0
cts 39
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A SteamLogin.__init__() 0 13 1
A SteamLogin.login() 0 8 1
A SteamLogin.rsa_key() 0 16 2
A SteamLogin.set_session_cookie() 0 11 1
A SteamLogin.visit_transfer_urls() 0 9 2
A SteamLogin.set_sessionid_cookies() 0 8 1
A SteamLogin.login_data() 0 18 1
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
from base64 import b64encode
4
from time import time
5
6
import requests
7
import rsa
8
9
from kuon.steam.common import SteamUrls
10
from kuon.steam.steam_guard import SteamGuard
11
12
13
class SteamLogin(SteamGuard):
14
    """Class providing the login functionality of the steam mobile client"""
15
16
    def __init__(self, *args, **kwargs) -> None:
17
        """Initializing function
18
19
        :type args: list
20
        :type kwargs: dict
21
        """
22
        super().__init__(*args, **kwargs)
23
24
        self._session = requests.Session()
25
        self._rsa_data = {}
26
27
        self.login()
28
        self.set_sessionid_cookies()
29
30
    def login(self) -> None:
31
        """Login into the set account
32
33
        :return:
34
        """
35
        response = self._session.post(SteamUrls.STORE + '/login/dologin', data=self.login_data).json()
36
        self.visit_transfer_urls(response)
37
        return response['success']
38
39
    @property
40
    def rsa_key(self) -> dict:
41
        """Retrieve the public key modulus and exponent and generate an RSA public key from it
42
43
        :return:
44
        """
45
        if not self._rsa_data:
46
            response = self._session.post(SteamUrls.STORE + '/login/getrsakey/', data={'username': self._user}).json()
47
            rsa_mod = int(response['publickey_mod'], 16)
48
            rsa_exp = int(response['publickey_exp'], 16)
49
            rsa_timestamp = response['timestamp']
50
            self._rsa_data = {
51
                'rsa_key': rsa.PublicKey(rsa_mod, rsa_exp),
52
                'rsa_tstamp': rsa_timestamp
53
            }
54
        return self._rsa_data
55
56
    @property
57
    def login_data(self) -> dict:
58
        """Retrieve the form login data
59
60
        :return:
61
        """
62
        return {
63
            'username': self._user,
64
            'password': b64encode(rsa.encrypt(self._password.encode(encoding="utf-8"), self.rsa_key['rsa_key'])),
65
            'twofactorcode': self.generate_one_time_code,
66
            'emailauth': '',
67
            'loginfriendlyname': '',
68
            'captchagid': '-1',
69
            'captcha_text': '',
70
            'emailsteamid': '',
71
            'rsatimestamp': self.rsa_key['rsa_tstamp'],
72
            'remember_login': 'false',
73
            'donotcache': str(int(time() * 1000))
74
        }
75
76
    def visit_transfer_urls(self, response: dict) -> None:
77
        """Visit the transfer urls to create the session
78
79
        :type response: dict
80
        :return:
81
        """
82
        parameters = response.get('transfer_parameters')
83
        for url in response['transfer_urls']:
84
            self._session.post(url, parameters)
85
86
    def set_sessionid_cookies(self) -> None:
87
        """Create the newly created session for the community and the store url
88
89
        :return:
90
        """
91
        session_id = self._session.cookies.get_dict()['sessionid']
92
        self.set_session_cookie(SteamUrls.COMMUNITY[8:], session_id)
93
        self.set_session_cookie(SteamUrls.STORE[8:], session_id)
94
95
    def set_session_cookie(self, domain: str, session_id: str) -> None:
96
        """Set the passed cookie for the passed domain
97
98
        :param domain:
99
        :param session_id:
100
        :return:
101
        """
102
        self._session.cookies.set(**{
103
            "name": "sessionid",
104
            "value": session_id,
105
            "domain": domain
106
        })
107