@sviy/authglue 0.0.0-stage → 1.0.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/README +3 -0
- package/index.js +83 -0
- package/package.json +22 -4
- package/README.md +0 -3
package/README
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import * as client from 'openid-client';
|
|
2
|
+
import session from 'express-session';
|
|
3
|
+
import FileStore from 'session-file-store';
|
|
4
|
+
|
|
5
|
+
export async function setupAuth(app, config) {
|
|
6
|
+
// ── Sessions ──────────────────────────
|
|
7
|
+
app.use(session({
|
|
8
|
+
secret: config.sessionSecret,
|
|
9
|
+
resave: false,
|
|
10
|
+
saveUninitialized: false,
|
|
11
|
+
store: new FileStore({ path: config.sessionPath }),
|
|
12
|
+
cookie: {
|
|
13
|
+
httpOnly: true,
|
|
14
|
+
sameSite: 'lax',
|
|
15
|
+
secure: config.secure ?? false,
|
|
16
|
+
},
|
|
17
|
+
}));
|
|
18
|
+
|
|
19
|
+
// ── Discover the IdP ───────────────────
|
|
20
|
+
const issuer = await client.discovery(
|
|
21
|
+
new URL(config.issuerUrl),
|
|
22
|
+
config.clientId,
|
|
23
|
+
config.clientSecret,
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
const oidcClient = new issuer.Client({
|
|
27
|
+
client_id: config.clientId,
|
|
28
|
+
client_secret: config.clientSecret,
|
|
29
|
+
redirect_uris: [config.callbackUrl],
|
|
30
|
+
response_types: ['code'],
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// ── Login ─────────────────────────────
|
|
34
|
+
app.get(config.loginPath || '/login', (req, res) => {
|
|
35
|
+
const verifier = client.randomPKCECodeVerifier();
|
|
36
|
+
const challenge = await client.calculatePKCECodeChallenge(verifier);
|
|
37
|
+
const state = client.randomState();
|
|
38
|
+
|
|
39
|
+
req.session.pkce = { verifier, state };
|
|
40
|
+
req.session.save();
|
|
41
|
+
|
|
42
|
+
res.redirect(client.buildAuthorizationUrl(oidcClient, {
|
|
43
|
+
scope: config.scopes || 'openid profile email offline_access',
|
|
44
|
+
state,
|
|
45
|
+
code_challenge: challenge,
|
|
46
|
+
code_challenge_method: 'S256',
|
|
47
|
+
redirect_uri: config.callbackUrl,
|
|
48
|
+
}));
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// ── Callback ───────────────────────────
|
|
52
|
+
app.get(config.callbackPath || '/callback', async (req, res) => {
|
|
53
|
+
const { pkce } = req.session;
|
|
54
|
+
if (!pkce) return res.status(400).send('Session expired. Try again.');
|
|
55
|
+
|
|
56
|
+
const tokens = await client.authorizationCodeGrant(
|
|
57
|
+
oidcClient,
|
|
58
|
+
new URL(`${req.protocol}://${req.get('host')}${req.originalUrl}`),
|
|
59
|
+
{ pkceCodeVerifier: pkce.verifier, expectedState: pkce.state },
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
req.session.user = tokens.claims();
|
|
63
|
+
req.session.tokens = tokens;
|
|
64
|
+
delete req.session.pkce;
|
|
65
|
+
|
|
66
|
+
res.redirect(config.successRedirect || '/');
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// ── Logout ───────────────────────────
|
|
70
|
+
app.get(config.logoutPath || '/logout', (req, res) => {
|
|
71
|
+
const logoutUrl = client.buildEndSessionUrl(oidcClient, {
|
|
72
|
+
id_token_hint: req.session.tokens?.id_token,
|
|
73
|
+
post_logout_redirect_uri: config.postLogoutUrl || config.successRedirect || '/',
|
|
74
|
+
});
|
|
75
|
+
req.session.destroy(() => res.redirect(logoutUrl));
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// ── Auth guard ───────────────────────
|
|
79
|
+
return function requireAuth(req, res, next) {
|
|
80
|
+
if (req.session?.user) return next();
|
|
81
|
+
res.redirect(config.loginPath || '/login');
|
|
82
|
+
};
|
|
83
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sviy/authglue",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "OIDC auth glue for Express apps",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"oidc",
|
|
7
|
+
"oauth",
|
|
8
|
+
"auth",
|
|
9
|
+
"express",
|
|
10
|
+
"sso"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"author": "sviy",
|
|
14
|
+
"type": "module",
|
|
15
|
+
"main": "index.js",
|
|
16
|
+
"scripts": {
|
|
17
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"openid-client": "^6.8.8",
|
|
21
|
+
"express-session": "^1.19.0",
|
|
22
|
+
"session-file-store": "^1.5.0"
|
|
23
|
+
}
|
|
24
|
+
}
|
package/README.md
DELETED