@eaccess/auth 0.1.19 → 0.1.21
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/dist/index.cjs +63 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +11 -3
- package/dist/index.d.ts +11 -3
- package/dist/index.js +62 -7
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -67,6 +67,7 @@ __export(index_exports, {
|
|
|
67
67
|
addRoleForUserBy: () => addRoleForUserBy,
|
|
68
68
|
addRoleToUser: () => addRoleToUser,
|
|
69
69
|
authFunctions: () => auth_functions_exports,
|
|
70
|
+
authenticateRequest: () => authenticateRequest,
|
|
70
71
|
changePasswordForUserBy: () => changePasswordForUserBy,
|
|
71
72
|
cleanupExpiredTokens: () => cleanupExpiredTokens,
|
|
72
73
|
confirmResetPassword: () => confirmResetPassword,
|
|
@@ -785,11 +786,12 @@ var BaseOAuthProvider = class {
|
|
|
785
786
|
}
|
|
786
787
|
return data.access_token;
|
|
787
788
|
}
|
|
788
|
-
async fetchUserFromAPI(accessToken, apiUrl) {
|
|
789
|
+
async fetchUserFromAPI(accessToken, apiUrl, headers = {}) {
|
|
789
790
|
const response = await fetch(apiUrl, {
|
|
790
791
|
headers: {
|
|
791
792
|
Authorization: `Bearer ${accessToken}`,
|
|
792
|
-
Accept: "application/json"
|
|
793
|
+
Accept: "application/json",
|
|
794
|
+
...headers
|
|
793
795
|
}
|
|
794
796
|
});
|
|
795
797
|
if (!response.ok) {
|
|
@@ -820,14 +822,24 @@ var GitHubProvider = class extends BaseOAuthProvider {
|
|
|
820
822
|
throw new Error("No authorization code provided");
|
|
821
823
|
}
|
|
822
824
|
const accessToken = await this.exchangeCodeForToken(code, "https://github.com/login/oauth/access_token");
|
|
823
|
-
const
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
825
|
+
const apiHeaders = {
|
|
826
|
+
Accept: "application/vnd.github+json",
|
|
827
|
+
"User-Agent": this.authConfig.githubUserAgent || "EasyAccess",
|
|
828
|
+
"X-GitHub-Api-Version": "2022-11-28"
|
|
829
|
+
};
|
|
830
|
+
const [user, emails] = await Promise.all([
|
|
831
|
+
this.fetchUserFromAPI(accessToken, "https://api.github.com/user", apiHeaders),
|
|
832
|
+
this.fetchUserFromAPI(accessToken, "https://api.github.com/user/emails", apiHeaders)
|
|
833
|
+
]);
|
|
834
|
+
const verifiedEmails = Array.isArray(emails) ? emails.filter((email) => email.verified) : [];
|
|
835
|
+
const primaryEmail = verifiedEmails.find((email) => email.primary)?.email;
|
|
836
|
+
const fallbackEmail = primaryEmail || verifiedEmails[0]?.email;
|
|
837
|
+
if (!fallbackEmail) {
|
|
838
|
+
throw new Error("No verified email found in GitHub account");
|
|
827
839
|
}
|
|
828
840
|
return {
|
|
829
841
|
id: user.id.toString(),
|
|
830
|
-
email:
|
|
842
|
+
email: fallbackEmail,
|
|
831
843
|
username: user.login,
|
|
832
844
|
name: user.name || user.login,
|
|
833
845
|
avatar: user.avatar_url
|
|
@@ -1460,6 +1472,7 @@ var TwoFactorManager = class {
|
|
|
1460
1472
|
var auth_functions_exports = {};
|
|
1461
1473
|
__export(auth_functions_exports, {
|
|
1462
1474
|
addRoleForUserBy: () => addRoleForUserBy,
|
|
1475
|
+
authenticateRequest: () => authenticateRequest,
|
|
1463
1476
|
changePasswordForUserBy: () => changePasswordForUserBy,
|
|
1464
1477
|
confirmResetPassword: () => confirmResetPassword,
|
|
1465
1478
|
createUser: () => createUser,
|
|
@@ -1475,6 +1488,48 @@ __export(auth_functions_exports, {
|
|
|
1475
1488
|
});
|
|
1476
1489
|
var import_hash3 = require("@prsm/hash");
|
|
1477
1490
|
var import_ms2 = __toESM(require("@prsm/ms"), 1);
|
|
1491
|
+
function parseCookies(cookieHeader) {
|
|
1492
|
+
const cookies = {};
|
|
1493
|
+
if (!cookieHeader) return cookies;
|
|
1494
|
+
for (const pair of cookieHeader.split(";")) {
|
|
1495
|
+
const idx = pair.indexOf("=");
|
|
1496
|
+
if (idx === -1) continue;
|
|
1497
|
+
const key = pair.slice(0, idx).trim();
|
|
1498
|
+
const value = pair.slice(idx + 1).trim();
|
|
1499
|
+
if (key) cookies[key] = decodeURIComponent(value);
|
|
1500
|
+
}
|
|
1501
|
+
return cookies;
|
|
1502
|
+
}
|
|
1503
|
+
async function authenticateRequest(config, req, sessionMiddleware) {
|
|
1504
|
+
const queries = new AuthQueries(config);
|
|
1505
|
+
if (sessionMiddleware) {
|
|
1506
|
+
await new Promise((resolve) => {
|
|
1507
|
+
sessionMiddleware(req, {}, resolve);
|
|
1508
|
+
});
|
|
1509
|
+
}
|
|
1510
|
+
const session = req.session;
|
|
1511
|
+
if (session?.auth?.loggedIn && session.auth.accountId) {
|
|
1512
|
+
const account2 = await queries.findAccountById(session.auth.accountId);
|
|
1513
|
+
if (account2 && account2.status === AuthStatus.Normal) {
|
|
1514
|
+
return { account: account2, source: "session" };
|
|
1515
|
+
}
|
|
1516
|
+
}
|
|
1517
|
+
const cookies = parseCookies(req.headers.cookie || "");
|
|
1518
|
+
const cookieName = config.rememberCookieName || "remember_token";
|
|
1519
|
+
const token = cookies[cookieName];
|
|
1520
|
+
if (!token) {
|
|
1521
|
+
return { account: null, source: null };
|
|
1522
|
+
}
|
|
1523
|
+
const remember = await queries.findRememberToken(token);
|
|
1524
|
+
if (!remember || /* @__PURE__ */ new Date() > remember.expires) {
|
|
1525
|
+
return { account: null, source: null };
|
|
1526
|
+
}
|
|
1527
|
+
const account = await queries.findAccountById(remember.account_id);
|
|
1528
|
+
if (!account || account.status !== AuthStatus.Normal) {
|
|
1529
|
+
return { account: null, source: null };
|
|
1530
|
+
}
|
|
1531
|
+
return { account, source: "remember" };
|
|
1532
|
+
}
|
|
1478
1533
|
function validatePassword(password, config) {
|
|
1479
1534
|
const minLength = config.minPasswordLength || 8;
|
|
1480
1535
|
const maxLength = config.maxPasswordLength || 64;
|
|
@@ -2794,6 +2849,7 @@ async function getUserRoles(config, identifier) {
|
|
|
2794
2849
|
addRoleForUserBy,
|
|
2795
2850
|
addRoleToUser,
|
|
2796
2851
|
authFunctions,
|
|
2852
|
+
authenticateRequest,
|
|
2797
2853
|
changePasswordForUserBy,
|
|
2798
2854
|
cleanupExpiredTokens,
|
|
2799
2855
|
confirmResetPassword,
|