@@ -152,9 +152,19 @@ def vk_login(self, captcha_sid=None, captcha_key=None):
152152
153153 response = self .http .post ('https://login.vk.com/' , values )
154154
155- if 'act=authcheck' in response .url : # TODO: test/fix
156- code , remember_device = self .error_handlers [TWOFACTOR_CODE ]()
157- response = self .twofactor (response , code , remember_device )
155+ if 'onLoginCaptcha(' in response .text :
156+ captcha_sid = search_re (RE_CAPTCHAID , response .text )
157+ captcha = Captcha (self , captcha_sid , self .vk_login )
158+
159+ return self .error_handlers [CAPTCHA_ERROR_CODE ](captcha )
160+
161+ if 'onLoginFailed(4' in response .text :
162+ raise BadPassword ('Bad password' )
163+
164+ if 'act=authcheck' in response .text :
165+ response = self .http .get ('https://vk.com/login?act=authcheck' )
166+
167+ self .twofactor (response )
158168
159169 remixsid = (
160170 self .http .cookies .get ('remixsid' ) or
@@ -173,60 +183,48 @@ def vk_login(self, captcha_sid=None, captcha_key=None):
173183 self .settings .save ()
174184
175185 self .sid = remixsid
176-
177- elif 'onLoginCaptcha(' in response .text :
178- captcha_sid = search_re (RE_CAPTCHAID , response .text )
179- captcha = Captcha (self , captcha_sid , self .vk_login )
180-
181- if self .error_handlers [CAPTCHA_ERROR_CODE ]:
182- return self .error_handlers [CAPTCHA_ERROR_CODE ](captcha )
183- else :
184- raise AuthorizationError ('Authorization error (capcha)' )
185- elif 'onLoginFailed(4' in response .text :
186- raise BadPassword ('Bad password' )
187186 else :
188187 raise AuthorizationError (
189188 'Unknown error. Please send bugreport: https://vk.com/python273'
190189 )
191190
192- self .security_check ()
191+ response = self .security_check ()
193192
194- if 'act=blocked' in response .url : # TODO: text/fix
193+ if 'act=blocked' in response .url :
195194 raise AccountBlocked ('Account is blocked' )
196195
197- def twofactor (self , response , code , remember_device = False ):
196+ def twofactor (self , auth_response ):
198197 """ Двухфакторная аутентификация
199- :param response: запрос, содержащий страницу с приглашением к аутентификации
200- :param code: код, который необходимо ввести для успешной аутентификации
201- :param remember_device: параметр, означающий,
202- стоит ли запоминать это устройство в целях
203- избежания повторного ввода кода(default: False)
198+ :param auth_response: страница с приглашением к аутентификации
204199 """
200+ code , remember_device = self .error_handlers [TWOFACTOR_CODE ]()
205201
206- if code is None :
207- raise TwoFactorError ("Empty code doesn't acceptable" )
208- if len (code ) != 6 :
209- raise TwoFactorError ("Length of code cannot be other than 6." )
202+ auth_hash = search_re (RE_AUTH_HASH , auth_response .text )
210203
211- auth_hash = search_re (RE_AUTH_HASH , response .text )
212- url = 'https://vk.com/al_login.php'
213- if auth_hash :
214- values = {
215- 'act' : 'a_authcheck_code' ,
216- 'code' : code ,
217- 'remember' : int (remember_device ),
218- 'hash' : auth_hash ,
219- }
220- response = self .http .post (url , values , cookies = response .cookies )
221- if url not in response .url :
222- return response
223- raise TwoFactorError ('Incorrect code: %s' % code )
204+ values = {
205+ 'act' : 'a_authcheck_code' ,
206+ 'al' : '1' ,
207+ 'code' : code ,
208+ 'remember' : int (remember_device ),
209+ 'hash' : auth_hash ,
210+ }
211+
212+ response = self .http .post ('https://vk.com/al_login.php' , values )
213+ response_parsed = response .text .split ('<!>' )
214+
215+ if response_parsed [4 ] == '4' : # OK
216+ return self .http .get ('https://vk.com/' + response_parsed [5 ])
217+
218+ elif response_parsed [4 ] == '8' : # Incorrect code
219+ return self .twofactor (auth_response )
220+
221+ raise TwoFactorError ('Two factor authentication failed' )
224222
225223 def security_check (self , response = None ):
226224 if response is None :
227225 response = self .http .get ('https://vk.com/settings' )
228226 if 'security_check' not in response .url :
229- return
227+ return response
230228
231229 phone_prefix = clean_string (search_re (RE_PHONE_PREFIX , response .text ))
232230 phone_postfix = clean_string (search_re (RE_PHONE_POSTFIX , response .text ))
@@ -254,12 +252,12 @@ def security_check(self, response=None):
254252 response = self .http .post ('https://vk.com/login.php' , values )
255253
256254 if response .text .split ('<!>' )[4 ] == '4' :
257- return True
255+ return response
258256
259257 if phone_prefix and phone_postfix :
260258 raise SecurityCheck (phone_prefix , phone_postfix )
261- else :
262- raise SecurityCheck (response = response )
259+
260+ raise SecurityCheck (response = response )
263261
264262 def check_sid (self ):
265263 """ Проверка Cookies remixsid на валидность """
0 commit comments