propro-utils 1.5.23 → 1.5.25
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 +1 -1
- package/src/server/middleware/cookieUtils.js +118 -30
package/package.json
CHANGED
|
@@ -1,3 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Authentication Cookies Module
|
|
3
|
+
* This module provides functions for setting and clearing authentication cookies.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const { URL } = require('url');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Safely stringify an object, handling circular references
|
|
10
|
+
* @param {Object} obj - The object to stringify
|
|
11
|
+
* @return {string} A JSON string representation of the object
|
|
12
|
+
*/
|
|
13
|
+
const safeStringify = obj => {
|
|
14
|
+
const seen = new WeakSet();
|
|
15
|
+
return JSON.stringify(obj, (key, value) => {
|
|
16
|
+
if (typeof value === 'object' && value !== null) {
|
|
17
|
+
if (seen.has(value)) {
|
|
18
|
+
return '[Circular]';
|
|
19
|
+
}
|
|
20
|
+
seen.add(value);
|
|
21
|
+
}
|
|
22
|
+
return value;
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Sanitize user object by removing sensitive and unnecessary information
|
|
28
|
+
* @param {Object} user - The user object to sanitize
|
|
29
|
+
* @return {Object} A sanitized version of the user object
|
|
30
|
+
*/
|
|
31
|
+
const sanitizeUser = user => {
|
|
32
|
+
const sanitized = { ...user };
|
|
33
|
+
|
|
34
|
+
delete sanitized.password;
|
|
35
|
+
delete sanitized.passwordHistory;
|
|
36
|
+
delete sanitized.loginAttempts;
|
|
37
|
+
|
|
38
|
+
// Remove Mongoose-specific fields
|
|
39
|
+
delete sanitized.$__;
|
|
40
|
+
delete sanitized.$isNew;
|
|
41
|
+
|
|
42
|
+
if (sanitized._doc) {
|
|
43
|
+
sanitized.id = sanitized._doc.id;
|
|
44
|
+
sanitized.accountId = sanitized._doc.accountId;
|
|
45
|
+
sanitized.createdAt = sanitized._doc.createdAt;
|
|
46
|
+
sanitized.updatedAt = sanitized._doc.updatedAt;
|
|
47
|
+
|
|
48
|
+
delete sanitized._doc.mapList;
|
|
49
|
+
delete sanitized._doc.folderList;
|
|
50
|
+
|
|
51
|
+
delete sanitized._doc;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return sanitized;
|
|
55
|
+
};
|
|
56
|
+
|
|
1
57
|
/**
|
|
2
58
|
* Sets the authentication cookies in the response object.
|
|
3
59
|
*
|
|
@@ -11,90 +67,122 @@ const setAuthCookies = (res, tokens, account, user, appUrl) => {
|
|
|
11
67
|
if (!tokens || !tokens.refresh || !tokens.access) {
|
|
12
68
|
throw new Error('Invalid tokens object');
|
|
13
69
|
}
|
|
14
|
-
|
|
15
70
|
if (!account) {
|
|
16
71
|
throw new Error('Invalid account object');
|
|
17
72
|
}
|
|
18
|
-
|
|
19
73
|
if (!user) {
|
|
20
74
|
throw new Error('Invalid user object');
|
|
21
75
|
}
|
|
22
76
|
|
|
23
|
-
console.log('Setting auth cookies');
|
|
24
|
-
console.log('tokens', tokens);
|
|
25
|
-
console.log('account', account);
|
|
26
|
-
console.log('user', user);
|
|
27
77
|
const currentDateTime = new Date();
|
|
28
|
-
|
|
29
78
|
const refreshMaxAge =
|
|
30
79
|
new Date(tokens.refresh.expires).getTime() - currentDateTime.getTime();
|
|
31
80
|
const accessMaxAge =
|
|
32
81
|
new Date(tokens.access.expires).getTime() - currentDateTime.getTime();
|
|
33
82
|
|
|
34
|
-
let domain
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
domain
|
|
83
|
+
let domain;
|
|
84
|
+
try {
|
|
85
|
+
domain = appUrl ? new URL(appUrl).hostname : undefined;
|
|
86
|
+
if (domain && domain.includes('mapmap.app')) {
|
|
87
|
+
domain = '.mapmap.app';
|
|
88
|
+
}
|
|
89
|
+
} catch (error) {
|
|
90
|
+
console.error('Invalid appUrl:', error);
|
|
91
|
+
domain = undefined;
|
|
38
92
|
}
|
|
39
93
|
|
|
40
94
|
const commonAttributes = {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
sameSite: 'None',
|
|
44
|
-
// path: '/',
|
|
95
|
+
secure: process.env.NODE_ENV === 'production',
|
|
96
|
+
sameSite: process.env.NODE_ENV === 'production' ? 'None' : 'Lax',
|
|
45
97
|
domain: domain,
|
|
46
98
|
};
|
|
47
99
|
|
|
100
|
+
if (commonAttributes.sameSite === 'None') {
|
|
101
|
+
commonAttributes.secure = true;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Set httpOnly cookies
|
|
48
105
|
res.cookie('x-refresh-token', tokens.refresh.token, {
|
|
106
|
+
...commonAttributes,
|
|
49
107
|
httpOnly: true,
|
|
50
108
|
maxAge: refreshMaxAge,
|
|
51
|
-
...commonAttributes,
|
|
52
109
|
});
|
|
53
110
|
|
|
54
111
|
res.cookie('x-access-token', tokens.access.token, {
|
|
112
|
+
...commonAttributes,
|
|
55
113
|
httpOnly: true,
|
|
56
114
|
maxAge: accessMaxAge,
|
|
57
|
-
...commonAttributes,
|
|
58
115
|
});
|
|
59
116
|
|
|
60
|
-
|
|
61
|
-
|
|
117
|
+
// Set non-httpOnly cookies
|
|
118
|
+
const sanitizedUser = sanitizeUser(user);
|
|
119
|
+
const sanitizedAccount = { ...account };
|
|
120
|
+
delete sanitizedAccount.passwordHistory;
|
|
121
|
+
|
|
122
|
+
res.cookie('user', safeStringify(sanitizedUser), {
|
|
62
123
|
...commonAttributes,
|
|
124
|
+
maxAge: refreshMaxAge,
|
|
63
125
|
});
|
|
64
126
|
|
|
65
|
-
res.cookie('account',
|
|
66
|
-
maxAge: refreshMaxAge,
|
|
127
|
+
res.cookie('account', safeStringify(sanitizedAccount), {
|
|
67
128
|
...commonAttributes,
|
|
129
|
+
maxAge: refreshMaxAge,
|
|
68
130
|
});
|
|
69
131
|
|
|
70
132
|
res.cookie(
|
|
71
133
|
'has_account_token',
|
|
72
134
|
JSON.stringify({ value: 'true', expires: accessMaxAge }),
|
|
73
135
|
{
|
|
74
|
-
maxAge: accessMaxAge,
|
|
75
136
|
...commonAttributes,
|
|
137
|
+
maxAge: accessMaxAge,
|
|
76
138
|
}
|
|
77
139
|
);
|
|
140
|
+
|
|
141
|
+
console.log('Setting auth cookies');
|
|
142
|
+
console.log('sanitizedUser', sanitizedUser);
|
|
78
143
|
};
|
|
79
144
|
|
|
80
|
-
|
|
145
|
+
/**
|
|
146
|
+
* Clears all authentication cookies from the response object.
|
|
147
|
+
*
|
|
148
|
+
* @param {Object} res - The response object.
|
|
149
|
+
* @param {string} appUrl - The URL of the client application.
|
|
150
|
+
*/
|
|
151
|
+
const clearAuthCookies = (res, appUrl) => {
|
|
152
|
+
let domain;
|
|
153
|
+
try {
|
|
154
|
+
domain = appUrl ? new URL(appUrl).hostname : undefined;
|
|
155
|
+
if (domain && domain.includes('mapmap.app')) {
|
|
156
|
+
domain = '.mapmap.app';
|
|
157
|
+
}
|
|
158
|
+
} catch (error) {
|
|
159
|
+
console.error('Invalid appUrl:', error);
|
|
160
|
+
domain = undefined;
|
|
161
|
+
}
|
|
162
|
+
|
|
81
163
|
const commonAttributes = {
|
|
82
|
-
secure:
|
|
83
|
-
sameSite: 'None',
|
|
84
|
-
domain:
|
|
85
|
-
? new URL(process.env.APP_URL).hostname
|
|
86
|
-
: undefined,
|
|
164
|
+
secure: process.env.NODE_ENV === 'production',
|
|
165
|
+
sameSite: process.env.NODE_ENV === 'production' ? 'None' : 'Lax',
|
|
166
|
+
domain: domain,
|
|
87
167
|
};
|
|
88
168
|
|
|
89
|
-
|
|
169
|
+
if (commonAttributes.sameSite === 'None') {
|
|
170
|
+
commonAttributes.secure = true;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const cookieNames = [
|
|
90
174
|
'x-refresh-token',
|
|
91
175
|
'x-access-token',
|
|
92
176
|
'user',
|
|
93
177
|
'account',
|
|
94
178
|
'has_account_token',
|
|
95
|
-
]
|
|
179
|
+
];
|
|
180
|
+
|
|
181
|
+
cookieNames.forEach(cookieName => {
|
|
96
182
|
res.clearCookie(cookieName, commonAttributes);
|
|
97
183
|
});
|
|
184
|
+
|
|
185
|
+
console.log('Cleared auth cookies');
|
|
98
186
|
};
|
|
99
187
|
|
|
100
188
|
module.exports = {
|