forked from binary-com/binary-static
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
392 lines (346 loc) · 15.2 KB
/
client.js
File metadata and controls
392 lines (346 loc) · 15.2 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
const CookieStorage = require('./storage').CookieStorage;
const LocalStore = require('./storage').LocalStore;
const url_for = require('./url').url_for;
const default_redirect_url = require('./url').default_redirect_url;
const japanese_client = require('../common_functions/country_base').japanese_client;
const Cookies = require('../../lib/js-cookie');
const moment = require('moment');
const Client = (function () {
const client_object = {};
let landing_company_response = {};
const parseLoginIDList = function(string) {
if (!string) return [];
return string.split('+').sort().map(function(str) {
const items = str.split(':');
const id = items[0];
return {
id : id,
real : items[1] === 'R',
disabled : items[2] === 'D',
financial : /^MF/.test(id),
non_financial: /^MLT/.test(id),
};
});
};
const init = function () {
const loginid = Cookies.get('loginid');
client_object.loginid_array = parseLoginIDList(Cookies.get('loginid_list') || '');
const is_logged_in = (
loginid &&
client_object.loginid_array.length > 0 &&
get_storage_value('tokens')
);
set_storage_value('email', Cookies.get('email'));
set_storage_value('loginid', loginid);
set_storage_value('is_logged_in', is_logged_in);
set_storage_value('residence', Cookies.get('residence'));
};
const validate_loginid = function() {
const loginid_list = Cookies.get('loginid_list');
const client_id = Cookies.get('loginid');
if (!client_id || !loginid_list) return;
const valid_loginids = new RegExp('^(MX|MF|VRTC|MLT|CR|FOG|VRTJ|JP)[0-9]+$', 'i');
if (!valid_loginids.test(client_id)) {
send_logout_request();
}
const accIds = loginid_list.split('+');
accIds.forEach(function(acc_id) {
if (!valid_loginids.test(acc_id.split(':')[0])) {
send_logout_request();
}
});
};
const redirect_if_is_virtual = function(redirectPage) {
const is_virtual = get_boolean('is_virtual');
if (is_virtual) {
window.location.href = url_for(redirectPage || '');
}
return is_virtual;
};
const redirect_if_login = function() {
if (get_boolean('is_logged_in')) {
window.location.href = default_redirect_url();
}
return get_boolean('is_logged_in');
};
const set_storage_value = function(key, value) {
client_object[key] = value;
return LocalStore.set('client.' + key, value);
};
// use this function to get variables that have values
const get_storage_value = function(key) {
return client_object[key] || LocalStore.get('client.' + key) || '';
};
// use this function to get variables that are a boolean
const get_boolean = function(value) {
return JSON.parse(get_storage_value(value) || false);
};
const check_storage_values = function(origin) {
let is_ok = true;
// currencies
if (!get_storage_value('currencies')) {
BinarySocket.send({
payout_currencies: 1,
passthrough : {
handler: 'client',
origin : origin || '',
},
});
is_ok = false;
}
if (get_boolean('is_logged_in')) {
if (
!get_boolean('is_virtual') &&
Cookies.get('residence') &&
!get_boolean('has_reality_check')
) {
BinarySocket.send({
landing_company: Cookies.get('residence'),
passthrough : {
handler: 'client',
origin : origin || '',
},
});
is_ok = false;
}
}
// website TNC version
if (!LocalStore.get('website.tnc_version')) {
BinarySocket.send({ website_status: 1 });
}
return is_ok;
};
const response_authorize = function(response) {
const authorize = response.authorize;
if (!Cookies.get('email')) {
set_cookie('email', authorize.email);
set_storage_value('email', authorize.email);
}
set_storage_value('session_start', parseInt(moment().valueOf() / 1000));
set_storage_value('is_virtual', authorize.is_virtual);
set_storage_value('landing_company_name', authorize.landing_company_name);
set_storage_value('landing_company_fullname', authorize.landing_company_fullname);
set_storage_value('currency', authorize.currency);
check_storage_values();
client_object.values_set = true;
activate_by_client_type();
};
const check_tnc = function() {
if (/user\/tnc_approvalws/.test(window.location.href) ||
/terms-and-conditions/.test(window.location.href) ||
get_boolean('is_virtual') ||
sessionStorage.getItem('check_tnc') !== 'check') {
return;
}
const client_tnc_status = get_storage_value('tnc_status'),
website_tnc_version = LocalStore.get('website.tnc_version');
if (client_tnc_status && website_tnc_version && client_tnc_status !== website_tnc_version) {
sessionStorage.setItem('tnc_redirect', window.location.href);
window.location.href = url_for('user/tnc_approvalws');
}
};
const set_check_tnc = function () {
if (!$('body').hasClass('BlueTopBack')) {
sessionStorage.setItem('check_tnc', 'check');
localStorage.removeItem('client.tnc_status');
localStorage.removeItem('website.tnc_version');
}
};
const clear_storage_values = function() {
// clear all client values from local storage
Object.keys(localStorage).forEach(function(c) {
if (/^client\.(?!(tokens$))/.test(c)) {
LocalStore.set(c, '');
}
});
set_check_tnc();
sessionStorage.setItem('currencies', '');
};
const get_token = function(client_loginid) {
let token;
const tokens = get_storage_value('tokens');
if (client_loginid && tokens) {
const tokensObj = JSON.parse(tokens);
if (tokensObj.hasOwnProperty(client_loginid) && tokensObj[client_loginid]) {
token = tokensObj[client_loginid];
}
}
return token;
};
const add_token = function(client_loginid, token) {
if (!client_loginid || !token || get_token(client_loginid)) {
return false;
}
const tokens = get_storage_value('tokens');
const tokensObj = tokens && tokens.length > 0 ? JSON.parse(tokens) : {};
tokensObj[client_loginid] = token;
set_storage_value('tokens', JSON.stringify(tokensObj));
return true;
};
const set_cookie = function(cookieName, Value, domain) {
const cookie_expire = new Date();
cookie_expire.setDate(cookie_expire.getDate() + 60);
const cookie = new CookieStorage(cookieName, domain);
cookie.write(Value, cookie_expire, true);
};
const process_new_account = function(client_email, client_loginid, token, virtual_client) {
if (!client_email || !client_loginid || !token) {
return;
}
// save token
add_token(client_loginid, token);
// set cookies
set_cookie('email', client_email);
set_cookie('login', token);
set_cookie('loginid', client_loginid);
set_cookie('loginid_list', virtual_client ? client_loginid + ':V:E' : client_loginid + ':R:E+' + Cookies.get('loginid_list'));
// set local storage
localStorage.setItem('GTM_newaccount', '1');
localStorage.setItem('active_loginid', client_loginid);
window.location.href = default_redirect_url();
};
const can_upgrade_gaming_to_financial = function(data) {
return (data.hasOwnProperty('financial_company') && data.financial_company.shortcode === 'maltainvest');
};
const can_upgrade_virtual_to_financial = function(data) {
return (data.hasOwnProperty('financial_company') && !data.hasOwnProperty('gaming_company') && data.financial_company.shortcode === 'maltainvest');
};
const can_upgrade_virtual_to_japan = function(data) {
return (data.hasOwnProperty('financial_company') && !data.hasOwnProperty('gaming_company') && data.financial_company.shortcode === 'japan');
};
const has_gaming_financial_enabled = function() {
let has_financial = false,
has_gaming = false,
looping_user;
for (let i = 0; i < client_object.loginid_array.length; i++) {
looping_user = client_object.loginid_array[i];
if (looping_user.financial && !looping_user.disabled && !looping_user.non_financial) {
has_financial = true;
} else if (!looping_user.financial && !looping_user.disabled && looping_user.non_financial) {
has_gaming = true;
}
}
return has_gaming && has_financial;
};
const activate_by_client_type = function() {
$('.by_client_type').addClass('invisible');
if (get_boolean('is_logged_in')) {
if (!client_object.values_set) {
return;
}
$('#client-logged-in').addClass('gr-centered');
$('.client_logged_in').removeClass('invisible');
if (!get_boolean('is_virtual')) {
// control-class is a fake class, only used to counteract ja-hide class
$('.by_client_type.client_real').not((japanese_client() ? '.ja-hide' : '.control-class')).removeClass('invisible').show();
$('#topbar').addClass('primary-color-dark')
.removeClass('secondary-bg-color');
if (!/^CR/.test(get_storage_value('loginid'))) {
$('#payment-agent-section').addClass('invisible')
.hide();
}
if (has_gaming_financial_enabled()) {
$('#account-transfer-section').removeClass('invisible');
}
} else {
$('.by_client_type.client_virtual').removeClass('invisible')
.show();
$('#topbar').addClass('secondary-bg-color')
.removeClass('primary-color-dark');
}
} else {
$('.by_client_type.client_logged_out').removeClass('invisible')
.show();
$('#topbar').removeClass('secondary-bg-color')
.addClass('primary-color-dark');
}
};
const send_logout_request = function(showLoginPage) {
if (showLoginPage) {
sessionStorage.setItem('showLoginPage', 1);
}
BinarySocket.send({ logout: '1' });
};
const do_logout = function(response) {
if (response.logout !== 1) return;
Client.clear_storage_values();
LocalStore.remove('client.tokens');
LocalStore.set('reality_check.ack', 0);
sessionStorage.removeItem('client_status');
const cookies = ['login', 'loginid', 'loginid_list', 'email', 'settings', 'reality_check', 'affiliate_token', 'affiliate_tracking', 'residence'];
const domains = [
'.' + document.domain.split('.').slice(-2).join('.'),
'.' + document.domain,
];
let parent_path = window.location.pathname.split('/', 2)[1];
if (parent_path !== '') {
parent_path = '/' + parent_path;
}
cookies.forEach(function(c) {
const regex = new RegExp(c);
Cookies.remove(c, { path: '/', domain: domains[0] });
Cookies.remove(c, { path: '/', domain: domains[1] });
Cookies.remove(c);
if (regex.test(document.cookie) && parent_path) {
Cookies.remove(c, { path: parent_path, domain: domains[0] });
Cookies.remove(c, { path: parent_path, domain: domains[1] });
Cookies.remove(c, { path: parent_path });
}
});
localStorage.removeItem('risk_classification');
localStorage.removeItem('risk_classification.response');
window.location.reload();
};
// type can take one or more params, separated by comma
// e.g. one param = 'authenticated', two params = 'unwelcome, authenticated'
// match_type can be `any` `all`, by default is `any`
// should be passed when more than one param in type.
// `any` will return true if any of the params in type are found in client status
// `all` will return true if all of the params in type are found in client status
const status_detected = function(type, match_type) {
let client_status = sessionStorage.getItem('client_status');
if (!client_status || client_status.length === 0) return false;
const require_auth = /\,/.test(type) ? type.split(/, */) : [type];
client_status = client_status.split(',');
match_type = match_type && match_type === 'all' ? 'all' : 'any';
for (let i = 0; i < require_auth.length; i++) {
if (match_type === 'any' && (client_status.indexOf(require_auth[i]) > -1)) return true;
if (match_type === 'all' && (client_status.indexOf(require_auth[i]) < 0)) return false;
}
return (match_type !== 'any');
};
const get_set_landing_company = function(response) { // eslint-disable-line consistent-return
if (response) landing_company_response = response;
else return landing_company_response;
};
return {
init : init,
validate_loginid : validate_loginid,
redirect_if_is_virtual: redirect_if_is_virtual,
redirect_if_login : redirect_if_login,
set_value : set_storage_value,
get_value : get_storage_value,
get_boolean : get_boolean,
check_storage_values : check_storage_values,
response_authorize : response_authorize,
check_tnc : check_tnc,
set_check_tnc : set_check_tnc,
clear_storage_values : clear_storage_values,
get_token : get_token,
add_token : add_token,
set_cookie : set_cookie,
process_new_account : process_new_account,
can_upgrade_gaming_to_financial : can_upgrade_gaming_to_financial,
can_upgrade_virtual_to_financial: can_upgrade_virtual_to_financial,
can_upgrade_virtual_to_japan : can_upgrade_virtual_to_japan,
activate_by_client_type : activate_by_client_type,
send_logout_request: send_logout_request,
do_logout : do_logout,
status_detected : status_detected,
landing_company : get_set_landing_company,
};
})();
module.exports = {
Client : Client,
validate_loginid: Client.validate_loginid,
};