@oneaccount/express 0.1.0 → 0.1.1
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/middleware/auth.js +21 -2
- package/dist/types/index.d.ts +1 -0
- package/package.json +1 -1
package/dist/middleware/auth.js
CHANGED
|
@@ -69,16 +69,35 @@ function rsaPublicKeyFromJWK(jwk) {
|
|
|
69
69
|
const pem = `-----BEGIN PUBLIC KEY-----\n${der.toString("base64").match(/.{1,64}/g)?.join("\n")}\n-----END PUBLIC KEY-----`;
|
|
70
70
|
return pem;
|
|
71
71
|
}
|
|
72
|
+
function extractTokenFromCookies(cookieHeader, cookieName) {
|
|
73
|
+
if (!cookieHeader)
|
|
74
|
+
return null;
|
|
75
|
+
const cookies = cookieHeader.split(';').map(c => c.trim());
|
|
76
|
+
for (const cookie of cookies) {
|
|
77
|
+
const [name, ...valueParts] = cookie.split('=');
|
|
78
|
+
if (name === cookieName) {
|
|
79
|
+
return valueParts.join('=');
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
72
84
|
function createAuthMiddleware(config) {
|
|
73
85
|
const jwksUrl = config.jwksUrl ||
|
|
74
86
|
`${config.accountProUrl || "https://myaccount.one"}/.well-known/jwks.json`;
|
|
87
|
+
const cookieName = config.cookieName || "auth_token";
|
|
75
88
|
return async function authMiddleware(req, res, next) {
|
|
76
89
|
req.oneAccount = { user: null };
|
|
90
|
+
let token = null;
|
|
77
91
|
const authHeader = req.headers.authorization;
|
|
78
|
-
if (
|
|
92
|
+
if (authHeader?.startsWith("Bearer ")) {
|
|
93
|
+
token = authHeader.substring(7);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
token = extractTokenFromCookies(req.headers.cookie, cookieName);
|
|
97
|
+
}
|
|
98
|
+
if (!token) {
|
|
79
99
|
return next();
|
|
80
100
|
}
|
|
81
|
-
const token = authHeader.substring(7);
|
|
82
101
|
try {
|
|
83
102
|
const decoded = jsonwebtoken_1.default.decode(token, { complete: true });
|
|
84
103
|
if (!decoded) {
|
package/dist/types/index.d.ts
CHANGED