prawn-cake / vk-requests

vk.com requests for humans. API library for vk.com
MIT License
158 stars 22 forks source link

Handling Incorrect phone numbers error #4

Closed anatolykazantsev closed 8 years ago

anatolykazantsev commented 8 years ago

VK returns error page if user failed many times to enter masked numbers from its phone number during verification process. Because of this get_masked_phone_number() function returns empty tuple on new attempts, but caller code doesn't expect it and produces following error:

  File "vk_requests/auth.py", line 248, in require_phone_number
    phone_prefix, phone_suffix = get_masked_phone_number(html)
ValueError: need more than 0 values to unpack

Example of page with Incorrect phone numbers error return by VK:

<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=yes" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="MobileOptimized" content="176" />
<meta name="HandheldFriendly" content="True" />
<base id="base">
<meta name="robots" content="noindex,nofollow" />
<title>Login | VK</title>
<script type="text/javascript">
<!--
...
//-->
</script>
<link type="text/css" rel="stylesheet" href="/css/s_cf.css?222"></link>
<link type="text/css" rel="stylesheet" media="only screen" href="/css/s_yzg.css?161"></link>
<link rel="shortcut icon" href="/images/favicon_vk.ico?3"></link>
<link rel="canonical" href="https://vk.com/login.php?act=security_check&to="></link><link rel="alternate" href="android-app://com.vkontakte.android/vkontakte/m.vk.com/login.php?act=security_check&to=" />
</head>
<body id="vk" class="_ivk _hover">
<div id="vk_utils"></div>
<div id="vk_head" class="mhead">
<div class="hb_wrap"><div class="hb_btn">&nbsp;</div></div>
</div>
<div id="vk_wrap">
<div id="l"></div>
<div id="m"><div id="mhead" class="mhead"><a href="/login?act=do_logout&hash=..." class="hb_wrap mhb_notify mh_nobl" accesskey="#">
<div class="hb_btn">
<em class="mh_btn_label">log out</em>
</div>
</a>
<a href="/" accesskey="*" class="hb_wrap mhb_logo mh_rb">
<div class="hb_btn mhi_logo">&nbsp;</div>
<h1 class="hb_btn mh_header">&nbsp;</h1>
</a></div>
<div id="mcont" class="mcont"><div class="pcont fit_box bl_cont">
<h4 class="sub_header">Security check</h4>
<div class="form_item fi_fat">
<div class="fi_row"><div class="service_msg_box">
<div class="service_msg service_msg_warning">Incorrect numbers. You can repeat the attempt in 3 hours.</div>
</div></div>
<div class="fi_row">You are trying to access <b>...</b> &#39;s account from an unusual place.</div>
<div class="fi_row">In order to confirm ownership of the page, we ask you to enter all the <b>missing digits</b> of the phone number linked to your account.</div>
<div class="fi_row"><a class="button" href="/login?act=do_logout&hash=...">Log out</a></div>
</div>
</div></div>
<div id="mfoot" class="mfoot"></div>
<div class="_cntrs" style="height:0;">
<img width="1" height="1" src="//counter.yadro.ru/hit?..." alt="" align="left" />
<img width="1" height="1" src="//sb.scorecardresearch.com/p?..." alt="" align="left" />
<img width="1" height="1" src="https://r3.mail.ru/k?..." />
<img src="//top-fwz1.mail.ru/counter?..." style="border:0;" height="1" width="1" />
</div></div>
<div id="z"></div>
</div>
<div id="vk_bottom"></div>
<script type="text/javascript">
<!--
parent&&parent!=window&&(document.getElementsByTagName('body')[0].innerHTML='');
//-->
</script>
</body>
</html>

The error message is in

<div class="service_msg service_msg_warning">Incorrect numbers. You can repeat the attempt in 3 hours.</div>

So it seems the code should check the response from VK before calling AuthAPI.require_phone_number() method.

prawn-cake commented 8 years ago

Thanks for reporting. Yes, this case definitely isn't covered. Let me see what we can do.

prawn-cake commented 8 years ago

OK, I've updated develop To check your case again let's do this way:

Now it's covered with unittests. In case of such page warnings it raises VkPageWarningsError, you can handle it as except VkAuthError in your code

prawn-cake commented 8 years ago

Updated in v0.9.2

anatolykazantsev commented 8 years ago

Tested :-). Works ok. Thanks for the fix.