This repository has been archived by the owner on Jan 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
/
auth.js
208 lines (191 loc) · 6.04 KB
/
auth.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
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
/* globals browser */
/* globals main, makeUuid, deviceInfo, analytics, catcher, defaultSentryDsn, communication */
window.auth = (function () {
let exports = {};
let registrationInfo;
let initialized = false;
let authHeader = null;
let sentryPublicDSN = null;
let abTests = {};
catcher.watchPromise(browser.storage.local.get(["registrationInfo", "abTests"]).then((result) => {
if (result.abTests) {
abTests = result.abTests;
}
if (result.registrationInfo) {
registrationInfo = result.registrationInfo;
} else {
registrationInfo = generateRegistrationInfo();
console.info("Generating new device authentication ID", registrationInfo);
return browser.storage.local.set({registrationInfo});
}
}));
exports.getDeviceId = function () {
return registrationInfo && registrationInfo.deviceId;
};
function generateRegistrationInfo() {
let info = {
deviceId: "anon" + makeUuid() + "",
secret: makeUuid()+"",
registered: false
};
return info;
}
function register() {
return new Promise((resolve, reject) => {
let registerUrl = main.getBackend() + "/api/register";
let req = new XMLHttpRequest();
req.open("POST", registerUrl);
req.setRequestHeader("content-type", "application/x-www-form-urlencoded");
req.onload = catcher.watchFunction(() => {
if (req.status == 200) {
console.info("Registered login");
initialized = true;
saveAuthInfo(JSON.parse(req.responseText));
resolve(true);
analytics.sendEvent("registered");
} else {
console.warn("Error in response:", req.responseText);
reject(new Error("Bad response: " + req.status));
}
});
req.send(uriEncode({
deviceId: registrationInfo.deviceId,
secret: registrationInfo.secret,
deviceInfo: JSON.stringify(deviceInfo())
}));
});
}
function login(options) {
let { ownershipCheck, noRegister } = options || {};
return new Promise((resolve, reject) => {
let loginUrl = main.getBackend() + "/api/login";
let req = new XMLHttpRequest();
req.open("POST", loginUrl);
req.onload = catcher.watchFunction(() => {
if (req.status == 404) {
if (noRegister) {
resolve(false);
} else {
resolve(register());
}
} else if (req.status >= 300) {
console.warn("Error in response:", req.responseText);
reject(new Error("Could not log in: " + req.status));
} else if (req.status === 0) {
let error = new Error("Could not log in, server unavailable");
analytics.sendEvent("login-failed");
reject(error);
} else {
initialized = true;
let jsonResponse = JSON.parse(req.responseText);
console.info("Screenshots logged in");
analytics.sendEvent("login");
saveAuthInfo(jsonResponse);
if (ownershipCheck) {
resolve({isOwner: jsonResponse.isOwner});
} else {
resolve(true);
}
}
});
req.onerror = catcher.watchFunction(() => {
let exc = new Error("Connection failed");
exc.url = loginUrl;
exc.popupMessage = "CONNECTION_ERROR";
reject(exc);
});
req.setRequestHeader("content-type", "application/x-www-form-urlencoded");
req.send(uriEncode({
deviceId: registrationInfo.deviceId,
secret: registrationInfo.secret,
deviceInfo: JSON.stringify(deviceInfo()),
ownershipCheck
}));
});
}
function saveAuthInfo(responseJson) {
if (responseJson.sentryPublicDSN) {
sentryPublicDSN = responseJson.sentryPublicDSN;
}
if (responseJson.authHeader) {
authHeader = responseJson.authHeader;
if (!registrationInfo.registered) {
registrationInfo.registered = true;
catcher.watchPromise(browser.storage.local.set({registrationInfo}));
}
}
if (responseJson.abTests) {
abTests = responseJson.abTests;
catcher.watchPromise(browser.storage.local.set({abTests}));
}
}
function uriEncode(obj) {
let s = [];
for (let key in obj) {
if (obj[key] !== undefined) {
s.push(`${encodeURIComponent(key)}=${encodeURIComponent(obj[key])}`);
}
}
return s.join("&");
}
exports.getDeviceId = function () {
return registrationInfo.deviceId;
};
exports.authHeaders = function () {
let initPromise = Promise.resolve();
if (! initialized) {
initPromise = login();
}
return initPromise.then(() => {
if (authHeader) {
return {"x-screenshots-auth": authHeader};
} else {
console.warn("No auth header available");
return {};
}
});
};
exports.getSentryPublicDSN = function () {
return sentryPublicDSN || defaultSentryDsn;
};
exports.getAbTests = function () {
return abTests;
};
exports.isRegistered = function () {
return registrationInfo.registered;
};
exports.setDeviceInfoFromOldAddon = function (newDeviceInfo) {
if (! (newDeviceInfo.deviceId && newDeviceInfo.secret)) {
throw new Error("Bad deviceInfo");
}
if (registrationInfo.deviceId === newDeviceInfo.deviceId &&
registrationInfo.secret === newDeviceInfo.secret) {
// Probably we already imported the information
return Promise.resolve(false);
}
let newInfo = {
deviceId: newDeviceInfo.deviceId,
secret: newDeviceInfo.secret,
registered: true
};
initialized = false;
return browser.storage.local.set({registrationInfo: newInfo}).then(() => {
return true;
});
};
communication.register("getAuthInfo", (sender, ownershipCheck) => {
let info = registrationInfo;
let done = Promise.resolve();
if (info.registered) {
done = login({ownershipCheck}).then((result) => {
if (result && result.isOwner) {
info.isOwner = true;
}
});
}
return done.then(() => {
return info;
});
});
return exports;
})();