-
Notifications
You must be signed in to change notification settings - Fork 2
/
lastpass.js
122 lines (104 loc) · 2.71 KB
/
lastpass.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// TODO: Remove this!
var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
var lastpass = (function (){
var original_ajax = $.ajax;
function override_function(f, new_f) {
return (function () {
var original_this = this;
var original_arguments = arguments;
new_f({
arguments: original_arguments,
original: f,
call_original: function () {
return f.apply(original_this, original_arguments);
}
});
});
}
function override_ajax_callbacks(on_success, on_error) {
$.ajax = override_function(original_ajax, function(ajax_context) {
ajax_context.arguments[0].success = override_function(
ajax_context.arguments[0].success,
on_success
);
ajax_context.arguments[0].error = override_function(
ajax_context.arguments[0].error,
on_error
);
return ajax_context.call_original();
});
}
function restore_ajax() {
$ajax = original_ajax;
}
function call_ios(call, parameters) {
if (typeof parameters === 'undefined') {
parameters = '';
}
var url = 'lastpass.' + call + ':' + parameters;
if (is_chrome) {
console.log(url);
} else {
window.location.href = url;
}
}
var exported_api = {
key: '',
hash: '',
database: '',
download: function (email, password) {
$('#email').val(email);
$('#password').val(password);
override_ajax_callbacks(
function (context) {
var response = $(context.arguments[0]);
console.log(context.arguments[0]);
var errors;
if (response.find('ok').size() > 0) {
exported_api.key = btoa(g_local_key);
exported_api.hash = g_hash;
call_ios('logged-in');
exported_api.receive();
} else if ((errors = response.find('error')).size() > 0) {
errors.each(function() {
var error = $(this);
var iterations;
var message;
if ((iterations = error.attr('iterations')) > 0) {
$('#iterations').val(iterations);
login();
} else if (message = error.attr('message')) {
call_ios('login-failed', message);
} else {
call_ios('login-failed', 'Incorrect email or password.')
}
return; // Only test the first one
});
} else {
call_ios('login-failed', 'Incorrect email or password.')
}
},
function (context) {
call_ios('login-failed', 'Cannot connect to the server.');
}
);
login();
},
receive: function() {
override_ajax_callbacks(
function (context) {
exported_api.database = context.arguments[0];
call_ios('downloaded');
},
function (context) {
call_ios('download-failed');
}
);
getaccts();
}
};
alert = function (message) {
call_ios('alert', message);
}
return exported_api;
})();