Commit 0feef324 authored by Thibault Douge's avatar Thibault Douge Committed by hydrargyrum
Browse files

[infomaniak] handle SCA

parent 7fe77b03
......@@ -22,7 +22,9 @@
from __future__ import unicode_literals
from woob.browser import LoginBrowser, URL, need_login
from woob.exceptions import BrowserIncorrectPassword
from woob.browser.exceptions import ServerError
from woob.exceptions import BrowserIncorrectPassword, BrowserQuestion
from woob.tools.value import Value
from .pages import LoginPage, SubscriptionsPage, DocumentsPage
......@@ -34,11 +36,35 @@ class InfomaniakBrowser(LoginBrowser):
profile = URL(r'/v3/api/proxypass/profile', SubscriptionsPage)
documents = URL(r'/v3/api/invoicing/(?P<subid>.*)/invoices', DocumentsPage)
def __init__(self, config, *args, **kwargs):
self.config = config
kwargs['username'] = self.config['login'].get()
kwargs['password'] = self.config['password'].get()
super(InfomaniakBrowser, self).__init__(*args, **kwargs)
def do_login(self):
self.login.go(data={'login': self.username, 'password': self.password})
try:
if self.config['otp'].get():
self.login.go(
data={
'login': self.username,
'password': self.password,
'double_auth_code': self.config['otp'].get(),
}
)
else:
self.login.go(data={'login': self.username, 'password': self.password})
except ServerError as e:
if e.response.status_code == 500:
page = LoginPage(self, e.response)
# first for the wrongpass, second for the otp failed
error_msgs = ["Invalid login or password", "The authentication code is incorrect"]
if page.get_error() in error_msgs:
raise BrowserIncorrectPassword(page.get_error())
raise
if not self.page.logged:
raise BrowserIncorrectPassword()
if self.page.has_otp and not self.config['otp'].get():
raise BrowserQuestion(Value('otp', label='Enter the OTP'))
@need_login
def iter_subscription(self):
......
......@@ -28,7 +28,8 @@
)
from woob.capabilities.base import find_object
from woob.tools.backend import Module, BackendConfig
from woob.tools.value import ValueBackendPassword
from woob.tools.value import ValueBackendPassword, ValueTransient
from .browser import InfomaniakBrowser
......@@ -46,6 +47,7 @@ class InfomaniakModule(Module, CapDocument):
CONFIG = BackendConfig(
ValueBackendPassword('login', label='Email de connexion', masked=False),
ValueBackendPassword('password', label='Mot de passe'),
ValueTransient('otp'),
)
BROWSER = InfomaniakBrowser
......@@ -53,7 +55,7 @@ class InfomaniakModule(Module, CapDocument):
accepted_document_types = (DocumentTypes.BILL,)
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_subscription(self):
return self.browser.iter_subscription()
......
......@@ -35,8 +35,17 @@
class LoginPage(JsonPage):
@property
def logged(self):
if self.doc['data'].get('need_double_auth'):
return False
return self.doc['result'] == 'success'
@property
def has_otp(self):
return self.doc['data']['default_method']
def get_error(self):
return self.doc['error']['description']
class SubscriptionsPage(LoggedPage, JsonPage):
@method
......
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