This repository was archived by the owner on Feb 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 543
Expand file tree
/
Copy pathappId.js
More file actions
160 lines (138 loc) · 5.26 KB
/
appId.js
File metadata and controls
160 lines (138 loc) · 5.26 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
import { LiveApi } from 'binary-live-api';
import {
addToken,
removeToken,
getTokenList,
removeAllTokens,
get as getStorage,
set as setStorage,
} from '../common/utils/storageManager';
import { parseQueryString, isProduction, getExtension } from '../common/utils/tools';
import { getLanguage } from './lang';
import AppIdMap from './appIdResolver';
import Elevio from './elevio';
import GTM from './gtm';
export const AppConstants = Object.freeze({
STORAGE_ACTIVE_TOKEN: 'activeToken',
});
const hostName = document.location.hostname;
const queryToObjectArray = queryStr => {
const tokens = [];
Object.keys(queryStr).forEach(o => {
if (!/\d$/.test(o)) return;
const index = parseInt(o.slice(-1));
let key = o.slice(0, -1);
key = key === 'acct' ? 'accountName' : key; // Make it consistent with storageManage naming
if (index <= tokens.length) {
tokens[index - 1][key] = queryStr[o];
} else {
tokens.push({});
tokens[index - 1][key] = queryStr[o];
}
});
return tokens;
};
export const oauthLogin = (done = () => 0) => {
const queryStr = parseQueryString();
const tokenObjectList = queryToObjectArray(queryStr);
if (tokenObjectList.length) {
$('#main').hide();
addTokenIfValid(tokenObjectList[0].token, tokenObjectList).then(() => {
const accounts = getTokenList();
if (accounts.length) {
setStorage(AppConstants.STORAGE_ACTIVE_TOKEN, accounts[0].token);
}
document.location = 'bot.html';
});
} else {
done();
}
};
export const getCustomEndpoint = () => ({
url : getStorage('config.server_url'),
appId: getStorage('config.app_id'),
});
const isRealAccount = () => {
const accountList = JSON.parse(getStorage('tokenList') || '{}');
const activeToken = getStorage(AppConstants.STORAGE_ACTIVE_TOKEN) || [];
let activeAccount = null;
let isReal = false;
try {
activeAccount = accountList.filter(account => account.token === activeToken);
isReal = !activeAccount[0].accountName.startsWith('VRT');
} catch (e) {} // eslint-disable-line no-empty
return isReal;
};
const getDomainAppId = () => AppIdMap[hostName.replace(/^www./, '')];
export const getDefaultEndpoint = () => ({
url : isRealAccount() ? 'green.binaryws.com' : 'blue.binaryws.com',
appId: getStorage('config.default_app_id') || getDomainAppId() || 1169,
});
const generateOAuthDomain = () => {
const endpointUrl = getCustomEndpoint().url;
if (endpointUrl) {
return endpointUrl;
} else if (isProduction()) {
return `oauth.binary.${getExtension()}`;
}
return 'oauth.binary.com';
};
export const getServerAddressFallback = () => getCustomEndpoint().url || getDefaultEndpoint().url;
export const getAppIdFallback = () => getCustomEndpoint().appId || getDefaultEndpoint().appId;
export const getWebSocketURL = () => `wss://${getServerAddressFallback()}/websockets/v3`;
export const generateWebSocketURL = serverUrl => `wss://${serverUrl}/websockets/v3`;
export const getOAuthURL = () =>
`https://${generateOAuthDomain()}/oauth2/authorize?app_id=${getAppIdFallback()}&l=${getLanguage().toUpperCase()}`;
const options = {
apiUrl : getWebSocketURL(),
language: getLanguage().toUpperCase(),
appId : getAppIdFallback(),
};
export const generateLiveApiInstance = () => new LiveApi(options);
export const generateTestLiveApiInstance = overrideOptions => new LiveApi(Object.assign({}, options, overrideOptions));
export async function addTokenIfValid(token, tokenObjectList) {
const api = generateLiveApiInstance();
try {
const { authorize } = await api.authorize(token);
const { landing_company_name: lcName } = authorize;
const {
landing_company_details: { has_reality_check: hasRealityCheck },
} = await api.getLandingCompanyDetails(lcName);
addToken(token, authorize, !!hasRealityCheck, ['iom', 'malta'].includes(lcName) && authorize.country === 'gb');
const { account_list: accountList } = authorize;
if (accountList.length > 1) {
tokenObjectList.forEach(tokenObject => {
if (tokenObject.token !== token) {
const account = accountList.filter(o => o.loginid === tokenObject.accountName);
if (account.length) {
addToken(tokenObject.token, account[0], false, false);
}
}
});
}
} catch (e) {
removeToken(tokenObjectList[0].token);
Elevio.logoutUser();
/* eslint-disable-next-line no-unused-expressions */
GTM?.setVisitorId?.();
throw e;
}
return api.disconnect();
}
export const logoutAllTokens = () =>
new Promise(resolve => {
const api = generateLiveApiInstance();
const tokenList = getTokenList();
const logout = () => {
removeAllTokens();
api.disconnect();
resolve();
};
if (tokenList.length === 0) {
logout();
} else {
api.authorize(tokenList[0].token).then(() => {
api.logOut().then(logout, logout);
}, logout);
}
});