feat: added disable auth mode
This commit is contained in:
@@ -3,6 +3,9 @@ BASE_URL=http://localhost:3001
|
||||
FRONTEND_ORIGIN=http://localhost:5173
|
||||
SESSION_SECRET=super-long-random-secret
|
||||
|
||||
# Если true/1/on/yes — сервер стартует без OIDC и не требует авторизации.
|
||||
DISABLE_AUTH=false
|
||||
|
||||
OIDC_ISSUER_BASE_URL=https://your-issuer.example.com
|
||||
OIDC_CLIENT_ID=your-client-id
|
||||
OIDC_CLIENT_SECRET=your-client-secret
|
||||
|
||||
@@ -9,6 +9,10 @@ const app = express();
|
||||
const port = Number(process.env.PORT ?? 3001);
|
||||
const frontendOrigin = process.env.FRONTEND_ORIGIN ?? 'http://localhost:5173';
|
||||
|
||||
const disableAuth = ['1', 'true', 'yes', 'on'].includes(
|
||||
String(process.env.DISABLE_AUTH ?? '').trim().toLowerCase(),
|
||||
);
|
||||
|
||||
const oidcConfig = {
|
||||
authRequired: false,
|
||||
auth0Logout: false,
|
||||
@@ -23,10 +27,10 @@ const oidcConfig = {
|
||||
},
|
||||
};
|
||||
|
||||
if (!oidcConfig.clientID || !oidcConfig.issuerBaseURL || !oidcConfig.clientSecret) {
|
||||
if (!disableAuth && (!oidcConfig.clientID || !oidcConfig.issuerBaseURL || !oidcConfig.clientSecret)) {
|
||||
// Keep a clear startup failure if OIDC is not configured.
|
||||
throw new Error(
|
||||
'OIDC config is missing. Set OIDC_ISSUER_BASE_URL, OIDC_CLIENT_ID, OIDC_CLIENT_SECRET in server/.env',
|
||||
'OIDC config is missing. Set OIDC_ISSUER_BASE_URL, OIDC_CLIENT_ID, OIDC_CLIENT_SECRET in server/.env (or set DISABLE_AUTH=true)',
|
||||
);
|
||||
}
|
||||
|
||||
@@ -38,13 +42,27 @@ app.use(
|
||||
);
|
||||
app.use(express.json());
|
||||
|
||||
app.use(auth(oidcConfig));
|
||||
if (!disableAuth) {
|
||||
app.use(auth(oidcConfig));
|
||||
}
|
||||
|
||||
app.get('/api/health', (_req, res) => {
|
||||
res.json({ ok: true });
|
||||
});
|
||||
|
||||
app.get('/api/me', (req, res) => {
|
||||
if (disableAuth) {
|
||||
res.json({
|
||||
authenticated: false,
|
||||
bypassAuth: true,
|
||||
user: {
|
||||
sub: 'noauth',
|
||||
name: 'No Auth',
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (!req.oidc.isAuthenticated()) {
|
||||
res.status(401).json({ authenticated: false });
|
||||
return;
|
||||
@@ -57,17 +75,29 @@ app.get('/api/me', (req, res) => {
|
||||
});
|
||||
|
||||
app.get('/api/login', (req, res) => {
|
||||
if (disableAuth) {
|
||||
res.status(204).end();
|
||||
return;
|
||||
}
|
||||
|
||||
res.oidc.login({
|
||||
returnTo: frontendOrigin,
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/api/logout', (req, res) => {
|
||||
if (disableAuth) {
|
||||
res.status(204).end();
|
||||
return;
|
||||
}
|
||||
|
||||
res.oidc.logout({
|
||||
returnTo: frontendOrigin,
|
||||
});
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`OIDC auth server listening on http://localhost:${port}`);
|
||||
console.log(
|
||||
`${disableAuth ? 'NO-AUTH' : 'OIDC auth'} server listening on http://localhost:${port}`,
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user