@chemmangat/msal-next 4.0.2 → 4.1.0
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/CHANGELOG.md +198 -0
- package/README.md +561 -723
- package/SECURITY.md +422 -110
- package/dist/index.d.mts +124 -5
- package/dist/index.d.ts +124 -5
- package/dist/index.js +2302 -43
- package/dist/index.mjs +2240 -43
- package/dist/server.js +89 -1
- package/dist/server.mjs +86 -1
- package/package.json +1 -1
package/dist/server.js
CHANGED
|
@@ -1 +1,89 @@
|
|
|
1
|
-
'use strict';
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var headers = require('next/headers');
|
|
4
|
+
|
|
5
|
+
// src/utils/getServerSession.ts
|
|
6
|
+
|
|
7
|
+
// src/utils/validation.ts
|
|
8
|
+
function safeJsonParse(jsonString, validator) {
|
|
9
|
+
try {
|
|
10
|
+
const parsed = JSON.parse(jsonString);
|
|
11
|
+
if (validator(parsed)) {
|
|
12
|
+
return parsed;
|
|
13
|
+
}
|
|
14
|
+
console.warn("[Validation] JSON validation failed");
|
|
15
|
+
return null;
|
|
16
|
+
} catch (error) {
|
|
17
|
+
console.error("[Validation] JSON parse error:", error);
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function isValidAccountData(data) {
|
|
22
|
+
return typeof data === "object" && data !== null && typeof data.homeAccountId === "string" && data.homeAccountId.length > 0 && typeof data.username === "string" && data.username.length > 0 && (data.name === void 0 || typeof data.name === "string");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// src/utils/getServerSession.ts
|
|
26
|
+
async function getServerSession() {
|
|
27
|
+
try {
|
|
28
|
+
const cookieStore = await headers.cookies();
|
|
29
|
+
const headersList = await headers.headers();
|
|
30
|
+
const msalAccount = cookieStore.get("msal.account");
|
|
31
|
+
const msalToken = cookieStore.get("msal.token");
|
|
32
|
+
if (msalAccount?.value) {
|
|
33
|
+
const accountData = safeJsonParse(
|
|
34
|
+
msalAccount.value,
|
|
35
|
+
isValidAccountData
|
|
36
|
+
);
|
|
37
|
+
if (accountData) {
|
|
38
|
+
return {
|
|
39
|
+
isAuthenticated: true,
|
|
40
|
+
accountId: accountData.homeAccountId,
|
|
41
|
+
username: accountData.username,
|
|
42
|
+
accessToken: msalToken?.value
|
|
43
|
+
};
|
|
44
|
+
} else {
|
|
45
|
+
console.warn("[ServerSession] Invalid account data in cookie");
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const authHeader = headersList.get("x-msal-authenticated");
|
|
49
|
+
if (authHeader === "true") {
|
|
50
|
+
const username = headersList.get("x-msal-username");
|
|
51
|
+
return {
|
|
52
|
+
isAuthenticated: true,
|
|
53
|
+
username: username || void 0
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
isAuthenticated: false
|
|
58
|
+
};
|
|
59
|
+
} catch (error) {
|
|
60
|
+
console.error("[ServerSession] Error reading session:", error);
|
|
61
|
+
return {
|
|
62
|
+
isAuthenticated: false
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
async function setServerSessionCookie(account, accessToken) {
|
|
67
|
+
try {
|
|
68
|
+
const accountData = {
|
|
69
|
+
homeAccountId: account.homeAccountId,
|
|
70
|
+
username: account.username,
|
|
71
|
+
name: account.name
|
|
72
|
+
};
|
|
73
|
+
await fetch("/api/auth/session", {
|
|
74
|
+
method: "POST",
|
|
75
|
+
headers: {
|
|
76
|
+
"Content-Type": "application/json"
|
|
77
|
+
},
|
|
78
|
+
body: JSON.stringify({
|
|
79
|
+
account: accountData,
|
|
80
|
+
token: accessToken
|
|
81
|
+
})
|
|
82
|
+
});
|
|
83
|
+
} catch (error) {
|
|
84
|
+
console.error("[ServerSession] Failed to set session cookie:", error);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
exports.getServerSession = getServerSession;
|
|
89
|
+
exports.setServerSessionCookie = setServerSessionCookie;
|
package/dist/server.mjs
CHANGED
|
@@ -1 +1,86 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { cookies, headers } from 'next/headers';
|
|
2
|
+
|
|
3
|
+
// src/utils/getServerSession.ts
|
|
4
|
+
|
|
5
|
+
// src/utils/validation.ts
|
|
6
|
+
function safeJsonParse(jsonString, validator) {
|
|
7
|
+
try {
|
|
8
|
+
const parsed = JSON.parse(jsonString);
|
|
9
|
+
if (validator(parsed)) {
|
|
10
|
+
return parsed;
|
|
11
|
+
}
|
|
12
|
+
console.warn("[Validation] JSON validation failed");
|
|
13
|
+
return null;
|
|
14
|
+
} catch (error) {
|
|
15
|
+
console.error("[Validation] JSON parse error:", error);
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function isValidAccountData(data) {
|
|
20
|
+
return typeof data === "object" && data !== null && typeof data.homeAccountId === "string" && data.homeAccountId.length > 0 && typeof data.username === "string" && data.username.length > 0 && (data.name === void 0 || typeof data.name === "string");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// src/utils/getServerSession.ts
|
|
24
|
+
async function getServerSession() {
|
|
25
|
+
try {
|
|
26
|
+
const cookieStore = await cookies();
|
|
27
|
+
const headersList = await headers();
|
|
28
|
+
const msalAccount = cookieStore.get("msal.account");
|
|
29
|
+
const msalToken = cookieStore.get("msal.token");
|
|
30
|
+
if (msalAccount?.value) {
|
|
31
|
+
const accountData = safeJsonParse(
|
|
32
|
+
msalAccount.value,
|
|
33
|
+
isValidAccountData
|
|
34
|
+
);
|
|
35
|
+
if (accountData) {
|
|
36
|
+
return {
|
|
37
|
+
isAuthenticated: true,
|
|
38
|
+
accountId: accountData.homeAccountId,
|
|
39
|
+
username: accountData.username,
|
|
40
|
+
accessToken: msalToken?.value
|
|
41
|
+
};
|
|
42
|
+
} else {
|
|
43
|
+
console.warn("[ServerSession] Invalid account data in cookie");
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const authHeader = headersList.get("x-msal-authenticated");
|
|
47
|
+
if (authHeader === "true") {
|
|
48
|
+
const username = headersList.get("x-msal-username");
|
|
49
|
+
return {
|
|
50
|
+
isAuthenticated: true,
|
|
51
|
+
username: username || void 0
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
isAuthenticated: false
|
|
56
|
+
};
|
|
57
|
+
} catch (error) {
|
|
58
|
+
console.error("[ServerSession] Error reading session:", error);
|
|
59
|
+
return {
|
|
60
|
+
isAuthenticated: false
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
async function setServerSessionCookie(account, accessToken) {
|
|
65
|
+
try {
|
|
66
|
+
const accountData = {
|
|
67
|
+
homeAccountId: account.homeAccountId,
|
|
68
|
+
username: account.username,
|
|
69
|
+
name: account.name
|
|
70
|
+
};
|
|
71
|
+
await fetch("/api/auth/session", {
|
|
72
|
+
method: "POST",
|
|
73
|
+
headers: {
|
|
74
|
+
"Content-Type": "application/json"
|
|
75
|
+
},
|
|
76
|
+
body: JSON.stringify({
|
|
77
|
+
account: accountData,
|
|
78
|
+
token: accessToken
|
|
79
|
+
})
|
|
80
|
+
});
|
|
81
|
+
} catch (error) {
|
|
82
|
+
console.error("[ServerSession] Failed to set session cookie:", error);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export { getServerSession, setServerSessionCookie };
|
package/package.json
CHANGED