Skip to content

Commit 2d9c05b

Browse files
committed
Merge pull request #24 from AlexTalker/fix-asserts-two-factor
Fixes and improvements in two-factor authentication
2 parents 71af8c0 + 7616ad7 commit 2d9c05b

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

vk_api/vk_api.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ def __init__(self, login=None, password=None, number=None, sec_number=None,
5656
{'http': 'http://127.0.0.1:8888/',
5757
'https': 'https://127.0.0.1:8888/'}
5858
:param auth_handler: Функция для обработки двухфакторной аутентификации,
59-
обязана возвращать строку с кодом для
60-
прохождения аутентификации
59+
обязана возвращать строку с кодом и булевое значение,
60+
означающее, стоит ли вк запомнить это устройство, для
61+
прохождения аутентификации.
6162
:param captcha_handler: Функция для обработки капчи
6263
:param config_filename: Расположение config файла
6364
@@ -148,8 +149,8 @@ def vk_login(self, captcha_sid=None, captcha_key=None):
148149
remixsid = None
149150

150151
if 'act=authcheck' in response.url:
151-
code = self.error_handlers[TWOFACTOR_CODE]()
152-
response = self.twofactor(response, code)
152+
code, remember_device = self.error_handlers[TWOFACTOR_CODE]()
153+
response = self.twofactor(response, code, remember_device)
153154

154155
if 'remixsid' in self.http.cookies:
155156
remixsid = self.http.cookies['remixsid']
@@ -188,21 +189,27 @@ def vk_login(self, captcha_sid=None, captcha_key=None):
188189
if 'act=blocked' in response.url:
189190
raise AccountBlocked('Account is blocked')
190191

191-
def twofactor(self, response, code):
192+
def twofactor(self, response, code, remember_device=False):
192193
""" Двухфакторная аутентификация
193194
:param reponse: запрос, содержащий страницу с приглашением к аутентификации
194195
:param code: код, который необходимо ввести для успешной аутентификации
196+
:param remember_device: параметр, означающий,
197+
стоит ли запоминать это устройство в целях
198+
избежания повторного ввода кода(default: False)
195199
"""
196-
assert code != None, "Empty code doesn't acceptable"
197-
assert len(code) == 6, "Length of code cannot be other than 6."
200+
201+
if code == None:
202+
raise TwoFactorError("Empty code doesn't acceptable")
203+
if len(code) != 6:
204+
raise TwoFactorError("Length of code cannot be other than 6.")
198205

199206
auth_hash = search_re(RE_AUTH_HASH, response.text)
200207
url = 'https://vk.com/al_login.php'
201208
if auth_hash:
202209
values = {
203210
'act': 'a_authcheck_code',
204211
'code': code,
205-
'remember': 0, # TODO: Fix me(device remembering)
212+
'remember': int(remember_device),
206213
'hash': auth_hash,
207214
}
208215
response = self.http.post(url, values, cookies=response.cookies)

0 commit comments

Comments
 (0)