2025-06-15 16:13:22 +02:00
|
|
|
/**
|
|
|
|
|
* Check if the request is authenticated via either:
|
|
|
|
|
* 1. x-tag header (for webhooks/external calls)
|
2025-06-15 17:03:42 +02:00
|
|
|
* 2. Directus token (for Directus authenticated users)
|
|
|
|
|
* 3. OIDC session (for Keycloak authenticated users)
|
2025-06-15 16:13:22 +02:00
|
|
|
*/
|
|
|
|
|
export const isAuthenticated = async (event: any): Promise<boolean> => {
|
|
|
|
|
// Check x-tag header authentication (existing method)
|
|
|
|
|
const xTagHeader = getRequestHeader(event, "x-tag");
|
|
|
|
|
if (xTagHeader && (xTagHeader === "094ut234" || xTagHeader === "pjnvü1230")) {
|
|
|
|
|
console.log('[auth] Authenticated via x-tag header');
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-15 17:03:42 +02:00
|
|
|
// Check Directus token authentication
|
|
|
|
|
try {
|
|
|
|
|
const directusToken = getCookie(event, 'directus_token');
|
|
|
|
|
if (directusToken) {
|
|
|
|
|
// Validate Directus token is not expired
|
|
|
|
|
const directusExpiry = getCookie(event, 'directus_token_expired_at');
|
|
|
|
|
if (directusExpiry) {
|
|
|
|
|
const expiryTime = parseInt(directusExpiry);
|
|
|
|
|
if (Date.now() < expiryTime) {
|
|
|
|
|
console.log('[auth] Authenticated via Directus token');
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
console.log('[auth] Directus token expired');
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// If no expiry cookie, assume token is valid
|
|
|
|
|
console.log('[auth] Authenticated via Directus token (no expiry check)');
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log('[auth] Directus token check failed:', error);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-15 16:58:45 +02:00
|
|
|
// Check OIDC session authentication
|
2025-06-15 16:13:22 +02:00
|
|
|
try {
|
2025-06-15 16:58:45 +02:00
|
|
|
const oidcSession = getCookie(event, 'nuxt-oidc-auth');
|
|
|
|
|
if (oidcSession) {
|
2025-06-15 17:03:42 +02:00
|
|
|
// Note: OIDC session might be encrypted, we'll validate it properly in session endpoint
|
2025-06-15 16:58:45 +02:00
|
|
|
console.log('[auth] Authenticated via OIDC session');
|
2025-06-15 16:13:22 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
2025-06-15 16:58:45 +02:00
|
|
|
console.log('[auth] OIDC session check failed:', error);
|
2025-06-15 16:13:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('[auth] No valid authentication found');
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const requireAuth = async (event: any) => {
|
|
|
|
|
const authenticated = await isAuthenticated(event);
|
|
|
|
|
if (!authenticated) {
|
2025-06-15 16:53:20 +02:00
|
|
|
console.log('[requireAuth] Authentication failed for:', event.node.req.url);
|
|
|
|
|
console.log('[requireAuth] Available headers:', Object.keys(event.node.req.headers));
|
|
|
|
|
console.log('[requireAuth] Available cookies:', Object.keys(event.node.req.headers.cookie ? parseCookies(event.node.req.headers.cookie) : {}));
|
2025-06-15 16:13:22 +02:00
|
|
|
throw createError({
|
|
|
|
|
statusCode: 401,
|
|
|
|
|
statusMessage: "Authentication required. Please provide x-tag header or valid session."
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-15 16:53:20 +02:00
|
|
|
|
|
|
|
|
function parseCookies(cookieString: string): Record<string, string> {
|
|
|
|
|
return cookieString.split(';').reduce((cookies: Record<string, string>, cookie) => {
|
|
|
|
|
const [name, value] = cookie.trim().split('=');
|
|
|
|
|
if (name && value) {
|
|
|
|
|
cookies[name] = value;
|
|
|
|
|
}
|
|
|
|
|
return cookies;
|
|
|
|
|
}, {});
|
|
|
|
|
}
|