Nicolas Frandeboeuf
Committed by ntome
Builds for 1 pipeline failed in 3 minutes 42 seconds

[paypal] Fix paypal module

......@@ -79,7 +79,6 @@ class Paypal(LoginBrowser):
super(Paypal, self).__init__(*args, **kwargs)
def do_login(self):
raise BrowserUnavailable()
assert isinstance(self.username, basestring)
assert isinstance(self.password, basestring)
......@@ -88,6 +87,7 @@ class Paypal(LoginBrowser):
response = self.open(self.page.get_script_url())
token, csrf, key, value, sessionID, cookie = self.page.get_token_and_csrf(response.text)
self.session.cookies.update({'xppcts': cookie})
data = {}
data['ads_token_js'] = token
......@@ -114,7 +114,8 @@ class Paypal(LoginBrowser):
self.detect_account_type()
def detect_account_type(self):
self.page.detect_account_type()
if self.page:
self.page.detect_account_type()
@need_login
def get_accounts(self):
......
......@@ -96,6 +96,9 @@ class LoginPage(HTMLPage):
csrf = re.search(r"%s'([^']+)'" % re.escape("'&_csrf='+encodeURIComponent("), cleaner_code).group(1)
key, value = re.findall(r"'(\w+)','(\w+)'", cleaner_code)[-1]
# Remove setCookie function content
cleaner_code = re.sub(r"'setCookie'.*(?=,'removeCookie')", "'setCookie':function(){}", cleaner_code)
# Detect the name of the function that computes the token, detect the
# variable that stores the result and store it as a global.
get_token_func_name = re.search(r"ads_token_js='\+encodeURIComponent\((\w+)\)", cleaner_code).group(1)
......@@ -107,64 +110,6 @@ class LoginPage(HTMLPage):
cleaner_code = cleaner_code.replace(loop_func_name + "();", "")
cleaner_code = cleaner_code.replace("data;", "return;")
# Simulate a browser environment
simulate_browser_code = """
if (!document.createAttribute) {
document.createAttribute = null;
}
if (!document.domain) {
document.domain = "paypal.com";
}
if (!document.styleSheets) {
document.styleSheets = null;
}
if (!document.characterSet) {
document.characterSet = "UTF-8";
}
if (!document.documentElement) {
document.documentElement = {};
}
if (!window.innerWidth || !window.innerHeight) {
window.innerWidth = 1280;
window.innerHeight = 800;
}
if (typeof(screen) === "undefined") {
var screen = window.screen = {
width: 1280,
height: 800
};
}
if (typeof(history) === "undefined") {
var history = window.history = {};
}
if (typeof(location) === "undefined") {
var location = window.location = {
host: "paypal.com"
};
}
var XMLHttpRequest = function() {};
XMLHttpRequest.prototype.onreadystatechange = function(){};
XMLHttpRequest.prototype.open = function(){};
XMLHttpRequest.prototype.setRequestHeader = function(){};
XMLHttpRequest.prototype.send = function(){};
window.XMLHttpRequest = XMLHttpRequest;
if (!navigator.appName) {
navigator.appName = "Netscape";
}
navigator.userAgent = "Mozilla/5.0 (X11; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0";
"""
# Add a function that returns the token
cleaner_code += """
function GET_ADS_JS_TOKEN()
......@@ -173,7 +118,11 @@ class LoginPage(HTMLPage):
}
"""
token = str(Javascript(simulate_browser_code + cleaner_code).call("GET_ADS_JS_TOKEN"))
try:
token = str(Javascript(cleaner_code, None, "paypal.com").call("GET_ADS_JS_TOKEN"))
except:
raise BrowserUnavailable()
return token, csrf, key, value, sessionID, cookie
def login(self, login, password, ):
......
......@@ -26,38 +26,67 @@ from weboob.tools.log import getLogger
class Javascript(object):
HEADER = """
function btoa(str) {
var buffer
;
if (str instanceof Buffer) {
buffer = str;
} else {
buffer = new Buffer(str.toString(), 'binary');
function btoa(str) {
var buffer;
if (str instanceof Buffer) {
buffer = str;
} else {
buffer = new Buffer(str.toString(), 'binary');
}
return buffer.toString('base64');
}
function atob(str) {
return new Buffer(str, 'base64').toString('binary');
}
return buffer.toString('base64');
}
function atob(str) {
return new Buffer(str, 'base64').toString('binary');
}
document = {};
/* JS code checks that some PhantomJS globals aren't defined on the
* global window object; put an empty window object, so that all these
* tests fail.
* It then tests the user agent against some known scrappers; just put
* the default Tor user agent in there.
*/
window = {};
navigator = {
userAgent: "Mozilla/5.0 (Windows NT 6.1; rv:52.0) Gecko/20100101 Firefox/52.0"
};
document = {
createAttribute: null,
styleSheets: null,
characterSet: "UTF-8",
documentElement: {}
};
history = {};
screen = {
width: 1280,
height: 800
};
var XMLHttpRequest = function() {};
XMLHttpRequest.prototype.onreadystatechange = function(){};
XMLHttpRequest.prototype.open = function(){};
XMLHttpRequest.prototype.setRequestHeader = function(){};
XMLHttpRequest.prototype.send = function(){};
/* JS code checks that some PhantomJS globals aren't defined on the
* global window object; put an empty window object, so that all these
* tests fail.
* It then tests the user agent against some known scrappers; just put
* the default Tor user agent in there.
*/
window = {
document: document,
history: history,
screen: screen,
XMLHttpRequest: XMLHttpRequest,
innerWidth: 1280,
innerHeight: 800,
close: function(){}
};
navigator = {
userAgent: "Mozilla/5.0 (X11; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0",
appName: "Netscape"
};
"""
def __init__(self, script, logger=None):
def __init__(self, script, logger=None, domain=""):
try:
import execjs
except ImportError:
......@@ -66,7 +95,19 @@ class Javascript(object):
self.runner = execjs.get()
self.logger = getLogger('js', logger)
self.ctx = self.runner.compile(self.HEADER + script)
window_emulator = self.HEADER
if domain:
window_emulator += "document.domain = '" + domain + "';"
window_emulator += """
if (typeof(location) === "undefined") {
var location = window.location = {
host: document.domain
};
}
"""
self.ctx = self.runner.compile(window_emulator + script)
def call(self, *args, **kwargs):
retval = self.ctx.call(*args, **kwargs)
......