Commit 4dc02627 authored by thibault douge's avatar thibault douge Committed by Vincent A

[swile] add captcha management

parent ad631f4b
......@@ -28,7 +28,7 @@ from weboob.browser.filters.standard import (
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 @@ from weboob.capabilities.bank import Account, Transaction
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 @@ class SwileBrowser(APIBrowser):
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 @@ class SwileBrowser(APIBrowser):
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)
......
......@@ -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()
......
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