From 4dc026276ef685a88311bcb55ca74dcff9f2732f Mon Sep 17 00:00:00 2001 From: thibault douge Date: Wed, 10 Jun 2020 18:14:14 +0200 Subject: [PATCH] [swile] add captcha management --- modules/swile/browser.py | 15 +++++++++++---- modules/swile/module.py | 8 +++----- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/modules/swile/browser.py b/modules/swile/browser.py index 35305e44ae..8172118790 100644 --- a/modules/swile/browser.py +++ b/modules/swile/browser.py @@ -28,7 +28,7 @@ from weboob.capabilities.base import empty from weboob.browser.filters.json import Dict from weboob.browser.exceptions import ClientError -from weboob.exceptions import BrowserIncorrectPassword +from weboob.exceptions import BrowserIncorrectPassword, NocaptchaQuestion from weboob.browser.browsers import APIBrowser from weboob.capabilities.bank import Account, Transaction @@ -37,7 +37,7 @@ class SwileBrowser(APIBrowser): BASEURL = 'https://customer-api.swile.co' - def __init__(self, login, password, *args, **kwargs): + def __init__(self, config, *args, **kwargs): """SwileBrowser needs login and password to fetch Swile API""" super(SwileBrowser, self).__init__(*args, **kwargs) # self.session.headers are the HTTP headers for Swile API requests @@ -47,9 +47,10 @@ def __init__(self, login, password, *args, **kwargs): self.credentials = { 'client_id': "533bf5c8dbd05ef18fd01e2bbbab3d7f69e3511dd08402862b5de63b9a238923", 'grant_type': "password", - 'username': login, - 'password': password, + 'username': config['login'].get(), + 'password': config['password'].get(), } + self.config = config def _auth(self): """Authenticate to Swile API using self.credentials. @@ -57,9 +58,15 @@ def _auth(self): and response's json payload is returned unwrapped into dictionary. """ try: + if self.config['captcha_response'].get(): + self.credentials['recaptcha'] = self.config['captcha_response'].get() response = self.open('/oauth/token', data=self.credentials) except ClientError as e: json = e.response.json() + # if the captcha's response is not completed the error is + # 426 Client Error: Upgrade Required + if e.response.status_code == 426 and not self.config['captcha_response'].get(): + raise NocaptchaQuestion(website_url='https://app.swile.co/signin', website_key='6LceI-EUAAAAACrBsmKCmllNdk1-H5U7G7NOTzmj') if e.response.status_code == 401: message = json['error_description'] raise BrowserIncorrectPassword(message) diff --git a/modules/swile/module.py b/modules/swile/module.py index cbb6ad9e4a..74cdb547b6 100644 --- a/modules/swile/module.py +++ b/modules/swile/module.py @@ -20,7 +20,7 @@ from __future__ import unicode_literals from weboob.tools.backend import Module, BackendConfig -from weboob.tools.value import ValueBackendPassword +from weboob.tools.value import ValueBackendPassword, ValueTransient from weboob.capabilities.bank import CapBank from .browser import SwileBrowser @@ -41,13 +41,11 @@ class SwileModule(Module, CapBank): CONFIG = BackendConfig( ValueBackendPassword('login', label='E-mail', masked=False), ValueBackendPassword('password', label='Mot de passe'), + ValueTransient('captcha_response', label='Captcha Response'), ) def create_default_browser(self): - return self.create_browser( - self.config['login'].get(), - self.config['password'].get(), - ) + return self.create_browser(self.config) def iter_accounts(self): return self.browser.get_account() -- GitLab