mirror of
https://gitlab.kube.huskypup.net/Scooby/Homelabv4.git
synced 2026-08-21 05:26:49 +00:00
Initial commit
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: n8n-hooks
|
||||
namespace: n8n
|
||||
data:
|
||||
hooks.js: |
|
||||
// n8n v2.0.3 compatible hooks for Authentik forward auth integration
|
||||
const { resolve, dirname } = require('path');
|
||||
|
||||
module.exports = {
|
||||
credentials: {
|
||||
create: [],
|
||||
delete: [],
|
||||
update: []
|
||||
},
|
||||
workflow: {
|
||||
create: [],
|
||||
delete: [],
|
||||
update: []
|
||||
},
|
||||
server: {
|
||||
started: [
|
||||
async function (app) {
|
||||
console.log('[n8n-hooks] Initializing forward auth middleware');
|
||||
|
||||
// Get Express app
|
||||
const expressApp = app?.app;
|
||||
if (!expressApp) {
|
||||
console.error('[n8n-hooks] Express app not available');
|
||||
return;
|
||||
}
|
||||
|
||||
let issueCookie, UserRepository, Container;
|
||||
try {
|
||||
const n8nPath = dirname(require.resolve('n8n'));
|
||||
issueCookie = require(resolve(n8nPath, 'dist/auth/jwt')).issueCookie;
|
||||
UserRepository = require(resolve(n8nPath, 'dist/databases/repositories/user.repository')).UserRepository;
|
||||
Container = require('typedi').Container;
|
||||
} catch (error) {
|
||||
console.error('[n8n-hooks] Failed to load dependencies:', error.message);
|
||||
return;
|
||||
}
|
||||
|
||||
const ignoreAuthRegexp = /^\/(assets|healthz|webhook|rest\/oauth2-credential|rest\/settings|static|icons|types)/;
|
||||
|
||||
// Add middleware for forward auth
|
||||
expressApp.use(async (req, res, next) => {
|
||||
try {
|
||||
if (ignoreAuthRegexp.test(req.url)) return next();
|
||||
if (req.cookies?.['n8n-auth']) return next();
|
||||
if (!process.env.N8N_FORWARD_AUTH_HEADER) return next();
|
||||
|
||||
const headerName = process.env.N8N_FORWARD_AUTH_HEADER.toLowerCase().replace(/_/g, '-');
|
||||
const email = req.headers[headerName];
|
||||
if (!email) return next();
|
||||
|
||||
const userRepo = Container.get(UserRepository);
|
||||
const user = await userRepo.findOne({ where: { email } });
|
||||
|
||||
if (!user) {
|
||||
console.warn(`[n8n-hooks] User not found: ${email}`);
|
||||
res.statusCode = 401;
|
||||
res.end(`User ${email} not found. Please contact an admin.`);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`[n8n-hooks] Auto-login: ${email}`);
|
||||
issueCookie(res, user);
|
||||
next();
|
||||
} catch (error) {
|
||||
console.error('[n8n-hooks] Middleware error:', error.message);
|
||||
next();
|
||||
}
|
||||
});
|
||||
|
||||
console.log('[n8n-hooks] Forward auth middleware active');
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user