@sviy/authglue 0.0.0-stage → 1.0.2
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 +94 -0
- package/index.js +83 -0
- package/package.json +22 -4
- package/README.md +0 -3
package/README
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# @sviy/authglue
|
|
2
|
+
|
|
3
|
+
OIDC authentication glue for Express apps.
|
|
4
|
+
|
|
5
|
+
Wraps [`openid-client`](https://www.npmjs.com/package/openid-client) and [`express-session`](https://www.npmjs.com/package/express-session) into a single setup call. Your app redirects to any OIDC provider, handles the callback, creates a session, and gives you back `req.session.user`. No auth boilerplate per app.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @sviy/authglue
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import express from 'express';
|
|
17
|
+
import { setupAuth } from '@sviy/authglue';
|
|
18
|
+
|
|
19
|
+
const app = express();
|
|
20
|
+
|
|
21
|
+
const requireAuth = await setupAuth(app, {
|
|
22
|
+
issuerUrl: process.env.IDP_ISSUER,
|
|
23
|
+
clientId: process.env.IDP_CLIENT_ID,
|
|
24
|
+
clientSecret: process.env.IDP_CLIENT_SECRET,
|
|
25
|
+
callbackUrl: `${process.env.HOST}/callback`,
|
|
26
|
+
sessionSecret: process.env.SESSION_SECRET,
|
|
27
|
+
sessionPath: '/app/data/.session',
|
|
28
|
+
secure: true,
|
|
29
|
+
successRedirect: '/dashboard',
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
app.get('/dashboard', requireAuth, (req, res) => {
|
|
33
|
+
res.render('dashboard', { user: req.session.user });
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
app.listen(3000);
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Configuration
|
|
40
|
+
|
|
41
|
+
| Field | Required | Description |
|
|
42
|
+
|---|---|---|
|
|
43
|
+
| `issuerUrl` | Yes | OIDC provider issuer URL (e.g. `https://rauthy.example.com`) |
|
|
44
|
+
| `clientId` | Yes | Client ID registered with the OIDC provider |
|
|
45
|
+
| `clientSecret` | Yes | Client secret registered with the OIDC provider |
|
|
46
|
+
| `callbackUrl` | Yes | Redirect URI registered with the provider |
|
|
47
|
+
| `sessionSecret` | Yes | Secret used to sign session cookies |
|
|
48
|
+
| `sessionPath` | No | Where to persist session files. Default: `./sessions` |
|
|
49
|
+
| `scopes` | No | OIDC scopes to request. Default: `openid profile email offline_access` |
|
|
50
|
+
| `secure` | No | Set `true` to enable Secure cookies (requires HTTPS). Default: `false` |
|
|
51
|
+
| `loginPath` | No | Route that triggers login. Default: `/login` |
|
|
52
|
+
| `callbackPath` | No | Route the provider redirects to. Default: `/callback` |
|
|
53
|
+
| `logoutPath` | No | Route that triggers logout. Default: `/logout` |
|
|
54
|
+
| `successRedirect` | No | Where to send the user after successful login. Default: `/` |
|
|
55
|
+
|
|
56
|
+
## How It Works
|
|
57
|
+
|
|
58
|
+
1. User visits a protected route without a session
|
|
59
|
+
2. `requireAuth` redirects to `/login`
|
|
60
|
+
3. `authglue` generates PKCE verifier + state, redirects to the OIDC provider
|
|
61
|
+
4. User authenticates at the provider (password, passkey, 2FA — whatever the provider supports)
|
|
62
|
+
5. Provider redirects back to `/callback`
|
|
63
|
+
6. `authglue` exchanges the authorization code for tokens, creates an Express session
|
|
64
|
+
7. User is redirected to `successRedirect` with `req.session.user` populated
|
|
65
|
+
|
|
66
|
+
## Provider-Agnostic
|
|
67
|
+
|
|
68
|
+
Works with any OIDC-compatible provider:
|
|
69
|
+
|
|
70
|
+
- Rauthy
|
|
71
|
+
- Autentico
|
|
72
|
+
- Pocket ID
|
|
73
|
+
- Keycloak
|
|
74
|
+
- Zitadel
|
|
75
|
+
- Auth0
|
|
76
|
+
- Okta
|
|
77
|
+
- Google
|
|
78
|
+
- Any other OIDC provider
|
|
79
|
+
|
|
80
|
+
Switching providers means changing `issuerUrl`, `clientId`, and `clientSecret`. Nothing else.
|
|
81
|
+
|
|
82
|
+
## What This Package Does Not Do
|
|
83
|
+
|
|
84
|
+
- Does not store users or passwords — that's the IdP's job
|
|
85
|
+
- Does not provide an admin UI or login page — the IdP renders those
|
|
86
|
+
- Does not implement OIDC protocol details — [`openid-client`](https://www.npmjs.com/package/openid-client) does that
|
|
87
|
+
- Does not handle 2FA or passkeys — the IdP handles those
|
|
88
|
+
|
|
89
|
+
This package is the glue between your Express app and your OIDC provider. Nothing more.
|
|
90
|
+
|
|
91
|
+
## License
|
|
92
|
+
|
|
93
|
+
MIT
|
|
94
|
+
No responsibility, liability ro warranty. Use at your risk.
|
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.2",
|
|
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