propro-utils 1.7.20 → 1.7.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -1
- package/src/server/index.js +4 -25
- package/src/server/middleware/cookieUtils.js +21 -115
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "propro-utils",
|
|
3
|
-
"version": "1.7.
|
|
3
|
+
"version": "1.7.22",
|
|
4
4
|
"description": "Auth middleware for propro-auth",
|
|
5
5
|
"main": "src/index.js",
|
|
6
|
+
"private": false,
|
|
6
7
|
"scripts": {
|
|
7
8
|
"test": "node --experimental-vm-modules node_modules/.bin/jest",
|
|
8
9
|
"test:watch": "node --experimental-vm-modules ./node_modules/.bin/jest --coverage --verbose --watchAll",
|
package/src/server/index.js
CHANGED
|
@@ -6,7 +6,6 @@ const {
|
|
|
6
6
|
const {
|
|
7
7
|
setAuthCookies,
|
|
8
8
|
clearAuthCookies,
|
|
9
|
-
prepAuthCookies,
|
|
10
9
|
} = require('./middleware/cookieUtils');
|
|
11
10
|
const { checkIfUserExists } = require('../../middlewares/account_info');
|
|
12
11
|
const authValidation = require('../../middlewares/access_token');
|
|
@@ -94,35 +93,15 @@ class AuthMiddleware {
|
|
|
94
93
|
const response = await this.proxyToAuthServer(req, `/api/v1/auth/login`);
|
|
95
94
|
|
|
96
95
|
const { account, tokens } = response.data;
|
|
97
|
-
|
|
96
|
+
console.log('account:', account);
|
|
98
97
|
const user = await checkIfUserExists(account.accountId);
|
|
99
|
-
|
|
98
|
+
console.log('user:', user);
|
|
100
99
|
|
|
101
100
|
if (returnTokens === 'true') {
|
|
102
101
|
res.status(response.status).json({ account, user, tokens });
|
|
103
102
|
} else {
|
|
104
103
|
const { tokens, urlToRedirect } = response.data;
|
|
105
|
-
|
|
106
|
-
const cookies = prepAuthCookies(tokens, account, user, this.options.appUrl);
|
|
107
|
-
Object.entries(cookies).forEach(
|
|
108
|
-
([name, config]) => {
|
|
109
|
-
try{
|
|
110
|
-
console.log(domain,'Setting cookie 1:', {name, value: config.value, commonAttributes, config})
|
|
111
|
-
res.cookie(name, config.value, {
|
|
112
|
-
...commonAttributes,
|
|
113
|
-
...config,
|
|
114
|
-
});
|
|
115
|
-
// console.log('Setting cookie 2:', {response})
|
|
116
|
-
} catch (error) {
|
|
117
|
-
console.error('Error setting cookie: Object.entries', {
|
|
118
|
-
error: error.message,
|
|
119
|
-
stack: error.stack,
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
);
|
|
124
|
-
console.log('cookies:', cookies);
|
|
125
|
-
// setAuthCookies(res, tokens, account, user, this.options.appUrl);
|
|
104
|
+
setAuthCookies(res, tokens, account, user, this.options.appUrl);
|
|
126
105
|
res.status(response.status).json({ urlToRedirect });
|
|
127
106
|
}
|
|
128
107
|
} catch (error) {
|
|
@@ -187,7 +166,7 @@ class AuthMiddleware {
|
|
|
187
166
|
throw new Error('User not found');
|
|
188
167
|
}
|
|
189
168
|
|
|
190
|
-
setAuthCookies(res, tokens, account, user, this.options.appUrl);
|
|
169
|
+
await setAuthCookies(res, tokens, account, user, this.options.appUrl);
|
|
191
170
|
|
|
192
171
|
res.redirect(formatRedirectUrl(this.options.appUrl));
|
|
193
172
|
} catch (error) {
|
|
@@ -72,7 +72,6 @@ const setChromeExtensionCookie = details => {
|
|
|
72
72
|
}
|
|
73
73
|
});
|
|
74
74
|
} catch (error) {
|
|
75
|
-
console.log('Error setting cookie: setChromeExtensionCookie', error);
|
|
76
75
|
// Not in extension context
|
|
77
76
|
resolve(null);
|
|
78
77
|
}
|
|
@@ -159,46 +158,31 @@ const setAuthCookies = async (res, tokens, account, user, appUrl) => {
|
|
|
159
158
|
try {
|
|
160
159
|
Object.entries({ ...httpOnlyCookies, ...regularCookies }).forEach(
|
|
161
160
|
([name, config]) => {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
...config,
|
|
167
|
-
});
|
|
168
|
-
// console.log('Setting cookie 2:', {response})
|
|
169
|
-
} catch (error) {
|
|
170
|
-
console.error('Error setting cookie: Object.entries', {
|
|
171
|
-
error: error.message,
|
|
172
|
-
stack: error.stack,
|
|
173
|
-
});
|
|
174
|
-
}
|
|
161
|
+
res.cookie(name, config.value, {
|
|
162
|
+
...commonAttributes,
|
|
163
|
+
...config,
|
|
164
|
+
});
|
|
175
165
|
}
|
|
176
166
|
);
|
|
177
167
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
// return res.cookie(name, config.value, {
|
|
195
|
-
// ...commonAttributes,
|
|
196
|
-
// ...config,
|
|
197
|
-
// url: `https://${domain || 'propro.so'}`,
|
|
198
|
-
// });
|
|
199
|
-
// });
|
|
168
|
+
const extensionCookiePromises = Object.entries({
|
|
169
|
+
...httpOnlyCookies,
|
|
170
|
+
...regularCookies,
|
|
171
|
+
}).map(([name, config]) => {
|
|
172
|
+
return setChromeExtensionCookie({
|
|
173
|
+
url: `https://${domain || 'propro.so'}`,
|
|
174
|
+
name,
|
|
175
|
+
value: config.value,
|
|
176
|
+
secure: true,
|
|
177
|
+
httpOnly: !!config.httpOnly,
|
|
178
|
+
sameSite: 'no_restriction',
|
|
179
|
+
path: '/',
|
|
180
|
+
expirationDate: Math.floor((Date.now() + config.maxAge) / 1000),
|
|
181
|
+
domain: domain?.startsWith('.') ? domain : `.${domain || 'propro.so'}`,
|
|
182
|
+
});
|
|
183
|
+
});
|
|
200
184
|
|
|
201
|
-
|
|
185
|
+
await Promise.allSettled(extensionCookiePromises);
|
|
202
186
|
|
|
203
187
|
console.log('Auth cookies set successfully', {
|
|
204
188
|
domain,
|
|
@@ -217,83 +201,6 @@ const setAuthCookies = async (res, tokens, account, user, appUrl) => {
|
|
|
217
201
|
}
|
|
218
202
|
};
|
|
219
203
|
|
|
220
|
-
const prepAuthCookies = async (tokens, account, user, appUrl) => {
|
|
221
|
-
if (!tokens?.refresh?.token || !tokens?.access?.token) {
|
|
222
|
-
throw new Error('Invalid tokens object');
|
|
223
|
-
}
|
|
224
|
-
if (!account) {
|
|
225
|
-
throw new Error('Invalid account object');
|
|
226
|
-
}
|
|
227
|
-
if (!user) {
|
|
228
|
-
throw new Error('Invalid user object');
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
const currentDateTime = new Date();
|
|
232
|
-
const refreshMaxAge =
|
|
233
|
-
new Date(tokens.refresh.expires).getTime() - currentDateTime.getTime();
|
|
234
|
-
const accessMaxAge =
|
|
235
|
-
new Date(tokens.access.expires).getTime() - currentDateTime.getTime();
|
|
236
|
-
|
|
237
|
-
// Domain configuration
|
|
238
|
-
let domain;
|
|
239
|
-
try {
|
|
240
|
-
domain = appUrl ? new URL(appUrl).hostname : undefined;
|
|
241
|
-
if (domain?.includes('mapmap.app')) {
|
|
242
|
-
domain = '.mapmap.app';
|
|
243
|
-
}
|
|
244
|
-
if (domain?.includes('localhost')) {
|
|
245
|
-
domain = undefined;
|
|
246
|
-
}
|
|
247
|
-
if (domain?.includes('propro.so')) {
|
|
248
|
-
domain = 'propro.so';
|
|
249
|
-
}
|
|
250
|
-
} catch (error) {
|
|
251
|
-
console.error('Invalid appUrl:', { error, appUrl });
|
|
252
|
-
domain = undefined;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
const commonAttributes = {
|
|
256
|
-
secure: true,
|
|
257
|
-
sameSite: 'None',
|
|
258
|
-
domain,
|
|
259
|
-
path: '/',
|
|
260
|
-
};
|
|
261
|
-
|
|
262
|
-
const httpOnlyCookies = {
|
|
263
|
-
'x-refresh-token': {
|
|
264
|
-
value: tokens.refresh.token,
|
|
265
|
-
maxAge: refreshMaxAge,
|
|
266
|
-
httpOnly: true,
|
|
267
|
-
},
|
|
268
|
-
'x-access-token': {
|
|
269
|
-
value: tokens.access.token,
|
|
270
|
-
maxAge: accessMaxAge,
|
|
271
|
-
httpOnly: true,
|
|
272
|
-
},
|
|
273
|
-
};
|
|
274
|
-
|
|
275
|
-
const sanitizedUser = sanitizeUser(user);
|
|
276
|
-
const sanitizedAccount = { ...account };
|
|
277
|
-
delete sanitizedAccount.passwordHistory;
|
|
278
|
-
|
|
279
|
-
const regularCookies = {
|
|
280
|
-
user: {
|
|
281
|
-
value: safeStringify(sanitizedUser),
|
|
282
|
-
maxAge: refreshMaxAge,
|
|
283
|
-
},
|
|
284
|
-
account: {
|
|
285
|
-
value: safeStringify(sanitizedAccount),
|
|
286
|
-
maxAge: refreshMaxAge,
|
|
287
|
-
},
|
|
288
|
-
has_account_token: {
|
|
289
|
-
value: JSON.stringify({ value: 'true', expires: accessMaxAge }),
|
|
290
|
-
maxAge: accessMaxAge,
|
|
291
|
-
},
|
|
292
|
-
};
|
|
293
|
-
|
|
294
|
-
return {...httpOnlyCookies, ...regularCookies, ...commonAttributes}
|
|
295
|
-
};
|
|
296
|
-
|
|
297
204
|
/**
|
|
298
205
|
* Clears cookies from both web and extension contexts
|
|
299
206
|
*/
|
|
@@ -361,5 +268,4 @@ const clearAuthCookies = async (res, appUrl) => {
|
|
|
361
268
|
module.exports = {
|
|
362
269
|
setAuthCookies,
|
|
363
270
|
clearAuthCookies,
|
|
364
|
-
prepAuthCookies
|
|
365
271
|
};
|