Commit 228b056f authored by Florian Duguet's avatar Florian Duguet Committed by ntome

[ldlc - pro] manage captcha and change login form field

Closes: 46991@sibi
parent 938e0d45
......@@ -18,7 +18,7 @@
# along with this weboob module. If not, see <http://www.gnu.org/licenses/>.
from weboob.browser import LoginBrowser, AbstractBrowser, URL, need_login
from weboob.exceptions import BrowserIncorrectPassword
from weboob.exceptions import BrowserIncorrectPassword, NocaptchaQuestion
from .pages import HomePage, LoginPage, ProBillsPage, DocumentsPage
......@@ -46,9 +46,17 @@ class LdlcBrowser(LoginBrowser):
login = URL(r'/Account/LoginPage.aspx', LoginPage)
home = URL(r'/$', HomePage)
def __init__(self, config, *args, **kwargs):
super(LdlcBrowser, self).__init__(*args, **kwargs)
self.config = config
def do_login(self):
self.login.stay_or_go()
self.page.login(self.username, self.password)
sitekey = self.page.get_recaptcha_sitekey()
if sitekey and not self.config['captcha_response'].get():
raise NocaptchaQuestion(website_key=sitekey, website_url=self.login.build())
self.page.login(self.username, self.password, self.config['captcha_response'].get())
if self.login.is_here():
raise BrowserIncorrectPassword(self.page.get_error())
......
......@@ -40,7 +40,8 @@ class LdlcModule(AbstractModule, CapDocument):
CONFIG = BackendConfig(Value('login', label='Email'),
ValueBackendPassword('password', label='Password'),
Value('website', label='Site web', default='part',
choices={'pro': 'Professionnels', 'part': 'Particuliers'}))
choices={'pro': 'Professionnels', 'part': 'Particuliers'}),
Value('captcha_response', label='Réponse captcha', default='', required=False))
PARENT = 'materielnet'
......@@ -50,7 +51,7 @@ class LdlcModule(AbstractModule, CapDocument):
return self.create_browser(self.config['login'].get(), self.config['password'].get(), weboob=self.weboob)
else:
self.BROWSER = LdlcProBrowser
return self.create_browser(self.config['login'].get(), self.config['password'].get())
return self.create_browser(self.config, self.config['login'].get(), self.config['password'].get())
def download_document(self, bill):
if not isinstance(bill, Bill):
......
......@@ -50,10 +50,20 @@ class HomePage(LoggedPage, HTMLPage):
class LoginPage(HTMLPage):
def login(self, username, password):
form = self.get_form(xpath='//form[contains(@action, "/Login/Login")]')
form['Email'] = username
form['Password'] = password
def get_recaptcha_sitekey(self):
return Attr('//div[@class="g-recaptcha"]', 'data-sitekey', default=NotAvailable)(self.doc)
def login(self, username, password, captcha_response=None):
form = self.get_form(id='aspnetForm')
form['__EVENTTARGET'] = 'ctl00$cphMainContent$butConnexion'
form['ctl00$cphMainContent$txbMail'] = username
form['ctl00$cphMainContent$txbPassword'] = password
# remove this, else error message will be empty if there is a wrongpass
del form['ctl00$SaveCookiesChoices']
if captcha_response:
form['g-recaptcha-response'] = captcha_response
form.submit()
def get_error(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment