propro-utils 1.4.40 → 1.4.42
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
CHANGED
package/src/server/index.js
CHANGED
|
@@ -3,6 +3,7 @@ const {
|
|
|
3
3
|
exchangeToken,
|
|
4
4
|
formatRedirectUrl,
|
|
5
5
|
} = require('./middleware/verifyToken');
|
|
6
|
+
const { setAuthCookies } = require('./middleware/setAuthCookies');
|
|
6
7
|
const { checkIfUserExists } = require('../../middlewares/account_info');
|
|
7
8
|
const { post } = require('axios');
|
|
8
9
|
|
|
@@ -33,43 +34,6 @@ function proproAuthMiddleware(options = {}, userSchema) {
|
|
|
33
34
|
|
|
34
35
|
let refreshToken;
|
|
35
36
|
|
|
36
|
-
const setAuthCookies = (res, tokens, account, user) => {
|
|
37
|
-
const currentDateTime = new Date();
|
|
38
|
-
|
|
39
|
-
const refreshMaxAge =
|
|
40
|
-
new Date(tokens.refresh.expires).getTime() - currentDateTime.getTime();
|
|
41
|
-
|
|
42
|
-
res.cookie('x-refresh-token', tokens.refresh.token, {
|
|
43
|
-
httpOnly: true,
|
|
44
|
-
secure: process.env.NODE_ENV === 'production',
|
|
45
|
-
maxAge: refreshMaxAge,
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
const accessMaxAge =
|
|
49
|
-
new Date(tokens.access.expires).getTime() - currentDateTime.getTime();
|
|
50
|
-
|
|
51
|
-
res.cookie('x-access-token', tokens.access.token, {
|
|
52
|
-
httpOnly: true,
|
|
53
|
-
secure: process.env.NODE_ENV === 'production',
|
|
54
|
-
maxAge: accessMaxAge,
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
res.cookie('user', JSON.stringify(user), {
|
|
58
|
-
maxAge: refreshMaxAge,
|
|
59
|
-
secure: process.env.NODE_ENV === 'production',
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
res.cookie('account', JSON.stringify(account), {
|
|
63
|
-
maxAge: refreshMaxAge,
|
|
64
|
-
secure: process.env.NODE_ENV === 'production',
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
res.cookie('has_account_token', 'true', {
|
|
68
|
-
maxAge: accessMaxAge,
|
|
69
|
-
secure: process.env.NODE_ENV === 'production',
|
|
70
|
-
});
|
|
71
|
-
};
|
|
72
|
-
|
|
73
37
|
return async (req, res, next) => {
|
|
74
38
|
try {
|
|
75
39
|
if (
|
|
@@ -106,8 +70,6 @@ function proproAuthMiddleware(options = {}, userSchema) {
|
|
|
106
70
|
|
|
107
71
|
const formatedAuthUrl = formatRedirectUrl(authUrl);
|
|
108
72
|
|
|
109
|
-
console.log('making a refreshToken Request: ', refreshToken);
|
|
110
|
-
|
|
111
73
|
let response;
|
|
112
74
|
|
|
113
75
|
try {
|
|
@@ -134,16 +96,10 @@ function proproAuthMiddleware(options = {}, userSchema) {
|
|
|
134
96
|
.json({ error: 'Invalid or expired refresh token' });
|
|
135
97
|
}
|
|
136
98
|
|
|
137
|
-
console.log('Checking for existing user: ', account);
|
|
138
|
-
|
|
139
99
|
const user = await checkIfUserExists(userSchema, account.accountId);
|
|
140
100
|
|
|
141
|
-
console.log('Setting Cookies: ', user, account);
|
|
142
|
-
|
|
143
101
|
setAuthCookies(res, { access, refresh }, account, user);
|
|
144
102
|
|
|
145
|
-
console.log('Cookies Set: ');
|
|
146
|
-
|
|
147
103
|
return res
|
|
148
104
|
.status(200)
|
|
149
105
|
.json({ message: 'Token refreshed successfully' });
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sets the authentication cookies in the response object.
|
|
3
|
+
*
|
|
4
|
+
* @param {Object} res - The response object.
|
|
5
|
+
* @param {Object} tokens - The authentication tokens.
|
|
6
|
+
* @param {Object} account - The user's account information.
|
|
7
|
+
* @param {Object} user - The user's information.
|
|
8
|
+
*/
|
|
9
|
+
const setAuthCookies = (res, tokens, account, user) => {
|
|
10
|
+
const currentDateTime = new Date();
|
|
11
|
+
|
|
12
|
+
const refreshMaxAge =
|
|
13
|
+
new Date(tokens.refresh.expires).getTime() - currentDateTime.getTime();
|
|
14
|
+
|
|
15
|
+
res.cookie('x-refresh-token', tokens.refresh.token, {
|
|
16
|
+
httpOnly: true,
|
|
17
|
+
secure: process.env.NODE_ENV === 'production',
|
|
18
|
+
maxAge: refreshMaxAge,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const accessMaxAge =
|
|
22
|
+
new Date(tokens.access.expires).getTime() - currentDateTime.getTime();
|
|
23
|
+
|
|
24
|
+
res.cookie('x-access-token', tokens.access.token, {
|
|
25
|
+
httpOnly: true,
|
|
26
|
+
secure: process.env.NODE_ENV === 'production',
|
|
27
|
+
maxAge: accessMaxAge,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
res.cookie('user', JSON.stringify(user), {
|
|
31
|
+
maxAge: refreshMaxAge,
|
|
32
|
+
secure: process.env.NODE_ENV === 'production',
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
res.cookie('account', JSON.stringify(account), {
|
|
36
|
+
maxAge: refreshMaxAge,
|
|
37
|
+
secure: process.env.NODE_ENV === 'production',
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
res.cookie('has_account_token', 'true', {
|
|
41
|
+
maxAge: accessMaxAge,
|
|
42
|
+
secure: process.env.NODE_ENV === 'production',
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
module.exports = {
|
|
47
|
+
setAuthCookies,
|
|
48
|
+
};
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
const { setAuthCookies } = require('./setAuthCookies');
|
|
2
|
+
|
|
3
|
+
describe('setAuthCookies', () => {
|
|
4
|
+
let res;
|
|
5
|
+
let tokens;
|
|
6
|
+
let account;
|
|
7
|
+
let user;
|
|
8
|
+
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
res = {
|
|
11
|
+
cookie: jest.fn(),
|
|
12
|
+
};
|
|
13
|
+
tokens = {
|
|
14
|
+
refresh: {
|
|
15
|
+
token: 'refresh-token',
|
|
16
|
+
expires: '2024-03-23T00:00:00.000Z',
|
|
17
|
+
},
|
|
18
|
+
access: {
|
|
19
|
+
token: 'access-token',
|
|
20
|
+
expires: '2024-03-23T01:00:00.000Z',
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
account = {
|
|
24
|
+
id: 1,
|
|
25
|
+
name: 'Test Account',
|
|
26
|
+
};
|
|
27
|
+
user = {
|
|
28
|
+
id: 1,
|
|
29
|
+
name: 'Test User',
|
|
30
|
+
};
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('should set the x-refresh-token cookie with the correct options', () => {
|
|
34
|
+
setAuthCookies(res, tokens, account, user);
|
|
35
|
+
expect(res.cookie).toHaveBeenCalledWith(
|
|
36
|
+
'x-refresh-token',
|
|
37
|
+
tokens.refresh.token,
|
|
38
|
+
{
|
|
39
|
+
httpOnly: true,
|
|
40
|
+
secure: process.env.NODE_ENV === 'production',
|
|
41
|
+
maxAge: expect.any(Number),
|
|
42
|
+
}
|
|
43
|
+
);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('should set the x-access-token cookie with the correct options', () => {
|
|
47
|
+
setAuthCookies(res, tokens, account, user);
|
|
48
|
+
expect(res.cookie).toHaveBeenCalledWith(
|
|
49
|
+
'x-access-token',
|
|
50
|
+
tokens.access.token,
|
|
51
|
+
{
|
|
52
|
+
httpOnly: true,
|
|
53
|
+
secure: process.env.NODE_ENV === 'production',
|
|
54
|
+
maxAge: expect.any(Number),
|
|
55
|
+
}
|
|
56
|
+
);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('should set the user cookie with the correct options', () => {
|
|
60
|
+
setAuthCookies(res, tokens, account, user);
|
|
61
|
+
expect(res.cookie).toHaveBeenCalledWith('user', JSON.stringify(user), {
|
|
62
|
+
maxAge: expect.any(Number),
|
|
63
|
+
secure: process.env.NODE_ENV === 'production',
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('should set the account cookie with the correct options', () => {
|
|
68
|
+
setAuthCookies(res, tokens, account, user);
|
|
69
|
+
expect(res.cookie).toHaveBeenCalledWith(
|
|
70
|
+
'account',
|
|
71
|
+
JSON.stringify(account),
|
|
72
|
+
{
|
|
73
|
+
maxAge: expect.any(Number),
|
|
74
|
+
secure: process.env.NODE_ENV === 'production',
|
|
75
|
+
}
|
|
76
|
+
);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('should set the has_account_token cookie with the correct options', () => {
|
|
80
|
+
setAuthCookies(res, tokens, account, user);
|
|
81
|
+
expect(res.cookie).toHaveBeenCalledWith('has_account_token', 'true', {
|
|
82
|
+
maxAge: expect.any(Number),
|
|
83
|
+
secure: process.env.NODE_ENV === 'production',
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('should set the maxAge of refresh-related cookies to the correct value', () => {
|
|
88
|
+
const currentDateTime = new Date();
|
|
89
|
+
const refreshMaxAge =
|
|
90
|
+
new Date(tokens.refresh.expires).getTime() - currentDateTime.getTime();
|
|
91
|
+
setAuthCookies(res, tokens, account, user);
|
|
92
|
+
expect(res.cookie).toHaveBeenCalledWith(
|
|
93
|
+
'x-refresh-token',
|
|
94
|
+
expect.any(String),
|
|
95
|
+
expect.objectContaining({ maxAge: refreshMaxAge })
|
|
96
|
+
);
|
|
97
|
+
expect(res.cookie).toHaveBeenCalledWith(
|
|
98
|
+
'user',
|
|
99
|
+
expect.any(String),
|
|
100
|
+
expect.objectContaining({ maxAge: refreshMaxAge })
|
|
101
|
+
);
|
|
102
|
+
expect(res.cookie).toHaveBeenCalledWith(
|
|
103
|
+
'account',
|
|
104
|
+
expect.any(String),
|
|
105
|
+
expect.objectContaining({ maxAge: refreshMaxAge })
|
|
106
|
+
);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('should set the maxAge of access-related cookies to the correct value', () => {
|
|
110
|
+
const currentDateTime = new Date();
|
|
111
|
+
const accessMaxAge =
|
|
112
|
+
new Date(tokens.access.expires).getTime() - currentDateTime.getTime();
|
|
113
|
+
setAuthCookies(res, tokens, account, user);
|
|
114
|
+
expect(res.cookie).toHaveBeenCalledWith(
|
|
115
|
+
'x-access-token',
|
|
116
|
+
expect.any(String),
|
|
117
|
+
expect.objectContaining({ maxAge: accessMaxAge })
|
|
118
|
+
);
|
|
119
|
+
expect(res.cookie).toHaveBeenCalledWith(
|
|
120
|
+
'has_account_token',
|
|
121
|
+
expect.any(String),
|
|
122
|
+
expect.objectContaining({ maxAge: accessMaxAge })
|
|
123
|
+
);
|
|
124
|
+
});
|
|
125
|
+
});
|