Add missing auth API endpoints
- Create /api/auth/status endpoint for session verification - Create /api/auth/check-group endpoint for group membership validation - These endpoints read Authentik proxy headers to provide auth state - Required for SessionStatusButton and backend group verification
This commit is contained in:
40
nuxt4-app/server/api/auth/check-group.post.ts
Normal file
40
nuxt4-app/server/api/auth/check-group.post.ts
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
/**
|
||||||
|
* Endpoint para verificar membresía de grupo desde el backend
|
||||||
|
* Valida contra los headers de Authentik en el servidor
|
||||||
|
*/
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
// Leer el body de la petición
|
||||||
|
const body = await readBody(event)
|
||||||
|
const { groupName } = body
|
||||||
|
|
||||||
|
if (!groupName || typeof groupName !== 'string') {
|
||||||
|
throw createError({
|
||||||
|
statusCode: 400,
|
||||||
|
statusMessage: 'Group name is required'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Leer headers de Authentik
|
||||||
|
const headers = getHeaders(event)
|
||||||
|
const authentikGroups = headers['x-authentik-groups']
|
||||||
|
|
||||||
|
// Si no hay header de grupos, el usuario no está autenticado o no tiene grupos
|
||||||
|
if (!authentikGroups) {
|
||||||
|
return {
|
||||||
|
hasGroup: false,
|
||||||
|
groups: []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parsear los grupos (separados por |)
|
||||||
|
const userGroups = authentikGroups.split('|').filter(g => g.trim())
|
||||||
|
|
||||||
|
// Verificar si el usuario tiene el grupo solicitado
|
||||||
|
const hasGroup = userGroups.includes(groupName)
|
||||||
|
|
||||||
|
return {
|
||||||
|
hasGroup,
|
||||||
|
groups: userGroups,
|
||||||
|
checkedGroup: groupName
|
||||||
|
}
|
||||||
|
})
|
||||||
43
nuxt4-app/server/api/auth/status.get.ts
Normal file
43
nuxt4-app/server/api/auth/status.get.ts
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
/**
|
||||||
|
* API endpoint para verificar el estado de autenticación en tiempo real
|
||||||
|
* Consulta los headers inyectados por Authentik Proxy Outpost
|
||||||
|
*/
|
||||||
|
export default defineEventHandler((event) => {
|
||||||
|
// Establecer headers para prevenir caching
|
||||||
|
setResponseHeaders(event, {
|
||||||
|
'Cache-Control': 'no-store, no-cache, must-revalidate, proxy-revalidate',
|
||||||
|
'Pragma': 'no-cache',
|
||||||
|
'Expires': '0'
|
||||||
|
})
|
||||||
|
|
||||||
|
// Leer headers de Authentik en tiempo real
|
||||||
|
const headers = getHeaders(event)
|
||||||
|
|
||||||
|
const username = headers['x-authentik-username']
|
||||||
|
const email = headers['x-authentik-email']
|
||||||
|
const name = headers['x-authentik-name']
|
||||||
|
const groups = headers['x-authentik-groups']
|
||||||
|
const uid = headers['x-authentik-uid']
|
||||||
|
|
||||||
|
// Si no hay username, no hay sesión activa en Authentik
|
||||||
|
if (!username) {
|
||||||
|
return {
|
||||||
|
authenticated: false,
|
||||||
|
user: null,
|
||||||
|
timestamp: new Date().toISOString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sesión activa
|
||||||
|
return {
|
||||||
|
authenticated: true,
|
||||||
|
user: {
|
||||||
|
username,
|
||||||
|
email,
|
||||||
|
name,
|
||||||
|
groups: groups ? groups.split('|') : [],
|
||||||
|
uid
|
||||||
|
},
|
||||||
|
timestamp: new Date().toISOString()
|
||||||
|
}
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user