From 43bcf4a647c4402e38ddedf225b1c5bb6a157b32 Mon Sep 17 00:00:00 2001 From: josedario87 Date: Mon, 13 Oct 2025 04:09:42 -0600 Subject: [PATCH] Add modular group verification system with frontend and backend checks This commit implements a comprehensive, reusable group verification system: Components: - GroupCheckButton: Base component for group verification - 7 specialized buttons: 3 real groups (authentik Admins, grupo-prueba, lvl0), 1 public access test, 2 system verification buttons - All buttons support both frontend and backend verification modes Backend: - New API endpoint /api/auth/check-group for server-side group validation - Reads Authentik headers and validates group membership Frontend: - Enhanced useAuthentik composable with hasGroup() and checkGroupBackend() methods - Toast notifications for all verification results - Smooth animations and color-coded visual feedback UI Improvements: - Organized layout with cards for different verification types - Grid layout for group buttons - Professional styling with hover effects and shadows - Clear visual distinction between frontend/backend checks --- nuxt4/app/app.vue | 49 ++++++++ .../auth/BackendVerificationButton.vue | 82 +++++++++++++ .../auth/CheckAuthentikAdminsButton.vue | 20 ++++ .../auth/CheckGrupoPruebaButton.vue | 20 ++++ nuxt4/app/components/auth/CheckLvl0Button.vue | 20 ++++ .../auth/CheckPublicAccessButton.vue | 20 ++++ .../auth/FrontendVerificationButton.vue | 59 +++++++++ .../app/components/auth/GroupCheckButton.vue | 112 ++++++++++++++++++ nuxt4/app/composables/useAuthentik.ts | 30 ++++- nuxt4/server/api/auth/check-group.post.ts | 40 +++++++ 10 files changed, 451 insertions(+), 1 deletion(-) create mode 100644 nuxt4/app/components/auth/BackendVerificationButton.vue create mode 100644 nuxt4/app/components/auth/CheckAuthentikAdminsButton.vue create mode 100644 nuxt4/app/components/auth/CheckGrupoPruebaButton.vue create mode 100644 nuxt4/app/components/auth/CheckLvl0Button.vue create mode 100644 nuxt4/app/components/auth/CheckPublicAccessButton.vue create mode 100644 nuxt4/app/components/auth/FrontendVerificationButton.vue create mode 100644 nuxt4/app/components/auth/GroupCheckButton.vue create mode 100644 nuxt4/server/api/auth/check-group.post.ts diff --git a/nuxt4/app/app.vue b/nuxt4/app/app.vue index 0d640f8..e37e66a 100644 --- a/nuxt4/app/app.vue +++ b/nuxt4/app/app.vue @@ -22,6 +22,9 @@ +
@@ -29,12 +32,58 @@
+ + + + +
+ + +
+
+ + + + +
+ + + + +
+
+ + + + +
+ + + + +
+
diff --git a/nuxt4/app/components/auth/BackendVerificationButton.vue b/nuxt4/app/components/auth/BackendVerificationButton.vue new file mode 100644 index 0000000..a3cf8c0 --- /dev/null +++ b/nuxt4/app/components/auth/BackendVerificationButton.vue @@ -0,0 +1,82 @@ + + + + + diff --git a/nuxt4/app/components/auth/CheckAuthentikAdminsButton.vue b/nuxt4/app/components/auth/CheckAuthentikAdminsButton.vue new file mode 100644 index 0000000..f5a016d --- /dev/null +++ b/nuxt4/app/components/auth/CheckAuthentikAdminsButton.vue @@ -0,0 +1,20 @@ + + + diff --git a/nuxt4/app/components/auth/CheckGrupoPruebaButton.vue b/nuxt4/app/components/auth/CheckGrupoPruebaButton.vue new file mode 100644 index 0000000..b431160 --- /dev/null +++ b/nuxt4/app/components/auth/CheckGrupoPruebaButton.vue @@ -0,0 +1,20 @@ + + + diff --git a/nuxt4/app/components/auth/CheckLvl0Button.vue b/nuxt4/app/components/auth/CheckLvl0Button.vue new file mode 100644 index 0000000..6591814 --- /dev/null +++ b/nuxt4/app/components/auth/CheckLvl0Button.vue @@ -0,0 +1,20 @@ + + + diff --git a/nuxt4/app/components/auth/CheckPublicAccessButton.vue b/nuxt4/app/components/auth/CheckPublicAccessButton.vue new file mode 100644 index 0000000..120f006 --- /dev/null +++ b/nuxt4/app/components/auth/CheckPublicAccessButton.vue @@ -0,0 +1,20 @@ + + + diff --git a/nuxt4/app/components/auth/FrontendVerificationButton.vue b/nuxt4/app/components/auth/FrontendVerificationButton.vue new file mode 100644 index 0000000..ed93d56 --- /dev/null +++ b/nuxt4/app/components/auth/FrontendVerificationButton.vue @@ -0,0 +1,59 @@ + + + + + diff --git a/nuxt4/app/components/auth/GroupCheckButton.vue b/nuxt4/app/components/auth/GroupCheckButton.vue new file mode 100644 index 0000000..bf44d05 --- /dev/null +++ b/nuxt4/app/components/auth/GroupCheckButton.vue @@ -0,0 +1,112 @@ + + + + + diff --git a/nuxt4/app/composables/useAuthentik.ts b/nuxt4/app/composables/useAuthentik.ts index 3a78b1f..00e079f 100644 --- a/nuxt4/app/composables/useAuthentik.ts +++ b/nuxt4/app/composables/useAuthentik.ts @@ -182,11 +182,39 @@ export const useAuthentik = () => { } } + /** + * Verifica si el usuario pertenece a un grupo específico (frontend) + * Lee los grupos desde el estado local (headers de Authentik) + */ + const hasGroup = (groupName: string): boolean => { + if (!user.value) return false + return user.value.groups.includes(groupName) + } + + /** + * Verifica si el usuario pertenece a un grupo específico (backend) + * Consulta al servidor para validar contra Authentik + */ + const checkGroupBackend = async (groupName: string): Promise => { + try { + const response = await $fetch<{ hasGroup: boolean }>(`/api/auth/check-group`, { + method: 'POST', + body: { groupName } + }) + return response.hasGroup + } catch (error) { + console.error('Error checking group membership:', error) + return false + } + } + return { user, isAuthenticated, logout, goToProfile, - checkSessionStatus + checkSessionStatus, + hasGroup, + checkGroupBackend } } diff --git a/nuxt4/server/api/auth/check-group.post.ts b/nuxt4/server/api/auth/check-group.post.ts new file mode 100644 index 0000000..5ac1ac4 --- /dev/null +++ b/nuxt4/server/api/auth/check-group.post.ts @@ -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 + } +})