@innertia-solutions/innertia-nuxt 0.1.66 → 0.1.68
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/composables/useLoginDestinations.js +3 -1
- package/composables/useTenantSelection.ts +13 -3
- package/package.json +1 -1
- package/server/routes/_auth/[provider]/callback.get.ts +3 -2
- package/server/routes/_auth/login.post.ts +3 -2
- package/server/routes/_auth/platform/login.post.ts +3 -2
- package/server/utils/__tests__/bff.test.ts +18 -0
- package/server/utils/bff.ts +19 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// useTenantSelection, useActiveTenantStore, useAppConfig auto-imported.
|
|
1
|
+
// useTenantSelection, useActiveTenantStore, useOrganizationStore, useAppConfig auto-imported.
|
|
2
2
|
//
|
|
3
3
|
// Modo 'open' — login personal unificado.
|
|
4
4
|
//
|
|
@@ -65,6 +65,8 @@ export function useLoginDestinations() {
|
|
|
65
65
|
|
|
66
66
|
/** Fija el tenant + contexto activos y navega a la home del contexto elegido. */
|
|
67
67
|
async function go(destination) {
|
|
68
|
+
// Cambio de tenant → limpiar la sede activa (la cookie de orgs no está keyed por tenant).
|
|
69
|
+
if (destination.tenantKey !== store.activeKey) useOrganizationStore().reset()
|
|
68
70
|
store.setActive(destination.tenantKey)
|
|
69
71
|
store.setActiveContext(destination.context)
|
|
70
72
|
await navigateTo(destination.home)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// useApi, useActiveTenantStore auto-imported.
|
|
1
|
+
// useApi, useActiveTenantStore, useOrganizationStore auto-imported.
|
|
2
2
|
// Selección de tenant en modo 'open': carga los tenants del usuario y resuelve
|
|
3
3
|
// el destino post-login según cuántos tenga (0 / 1 / 2+).
|
|
4
4
|
|
|
@@ -6,6 +6,16 @@ export function useTenantSelection() {
|
|
|
6
6
|
const api = useApi()
|
|
7
7
|
const store = useActiveTenantStore()
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Activa un tenant. Si cambia, resetea la sede (org) activa: la cookie de orgs
|
|
11
|
+
* está keyed por contexto, no por tenant, y una sede del tenant anterior
|
|
12
|
+
* viajaría en X-Organization (→ 401/403 en el backend).
|
|
13
|
+
*/
|
|
14
|
+
function activate(key) {
|
|
15
|
+
if (key !== store.activeKey) useOrganizationStore().reset()
|
|
16
|
+
store.setActive(key)
|
|
17
|
+
}
|
|
18
|
+
|
|
9
19
|
/** Carga los tenants del usuario autenticado y los guarda en el store. */
|
|
10
20
|
async function load() {
|
|
11
21
|
// El backend aún expone la ruta gym-flavored: { gyms: [{ key, name, status, contexts }] }
|
|
@@ -23,13 +33,13 @@ export function useTenantSelection() {
|
|
|
23
33
|
async function resolveAfterLogin() {
|
|
24
34
|
await load()
|
|
25
35
|
if (store.tenants.length === 0) return 'onboarding'
|
|
26
|
-
if (store.tenants.length === 1) {
|
|
36
|
+
if (store.tenants.length === 1) { activate(store.tenants[0].key); return 'ready' }
|
|
27
37
|
if (store.activeKey && store.tenants.some((t) => t.key === store.activeKey)) return 'ready'
|
|
28
38
|
return 'picker'
|
|
29
39
|
}
|
|
30
40
|
|
|
31
41
|
/** Cambia el tenant activo (switcher). */
|
|
32
|
-
function switchTo(key) {
|
|
42
|
+
function switchTo(key) { activate(key) }
|
|
33
43
|
|
|
34
44
|
return { load, resolveAfterLogin, switchTo }
|
|
35
45
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { defineEventHandler, getRouterParam, getQuery, sendRedirect, createError } from 'h3'
|
|
1
|
+
import { defineEventHandler, getRouterParam, getQuery, sendRedirect, createError, getRequestHeader } from 'h3'
|
|
2
2
|
import { writeSession } from '../../../utils/session'
|
|
3
3
|
import { normalizeLoginResponse } from '../../../utils/login'
|
|
4
|
+
import { forwardClientHeaders } from '../../../utils/bff'
|
|
4
5
|
|
|
5
6
|
// Recibe el callback de OAuth, intercambia contra Laravel, sella la sesión y
|
|
6
7
|
// redirige a la app. El token nunca llega al browser.
|
|
@@ -12,7 +13,7 @@ export default defineEventHandler(async (event) => {
|
|
|
12
13
|
let data: any
|
|
13
14
|
try {
|
|
14
15
|
data = await $fetch(`${config.apiInternalUrl}/auth/${provider}/callback`, {
|
|
15
|
-
method: 'GET', query, headers: { Accept: 'application/json' },
|
|
16
|
+
method: 'GET', query, headers: { Accept: 'application/json', ...forwardClientHeaders(n => getRequestHeader(event, n)) },
|
|
16
17
|
})
|
|
17
18
|
} catch (err: any) {
|
|
18
19
|
const msg = err?.data?.message ?? 'oauth_failed'
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { defineEventHandler, readBody, createError } from 'h3'
|
|
1
|
+
import { defineEventHandler, readBody, createError, getRequestHeader } from 'h3'
|
|
2
2
|
import { writeSession } from '../../utils/session'
|
|
3
|
+
import { forwardClientHeaders } from '../../utils/bff'
|
|
3
4
|
import { normalizeLoginResponse } from '../../utils/login'
|
|
4
5
|
|
|
5
6
|
// Llama a Laravel /auth/login server-to-server; en éxito sella el JWT en la
|
|
@@ -13,7 +14,7 @@ export default defineEventHandler(async (event) => {
|
|
|
13
14
|
data = await $fetch(`${config.apiInternalUrl}/auth/login`, {
|
|
14
15
|
method: 'POST',
|
|
15
16
|
body: { context, email, password, remember },
|
|
16
|
-
headers: { Accept: 'application/json' },
|
|
17
|
+
headers: { Accept: 'application/json', ...forwardClientHeaders(n => getRequestHeader(event, n)) },
|
|
17
18
|
})
|
|
18
19
|
} catch (err: any) {
|
|
19
20
|
const status = err?.response?.status ?? err?.statusCode ?? 500
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { defineEventHandler, readBody, createError } from 'h3'
|
|
1
|
+
import { defineEventHandler, readBody, createError, getRequestHeader } from 'h3'
|
|
2
2
|
import { writeSession } from '../../../utils/session'
|
|
3
|
+
import { forwardClientHeaders } from '../../../utils/bff'
|
|
3
4
|
import { normalizeLoginResponse } from '../../../utils/login'
|
|
4
5
|
|
|
5
6
|
// Login de plataforma (identidad separada, sin contexto de tenant). Llama a
|
|
@@ -15,7 +16,7 @@ export default defineEventHandler(async (event) => {
|
|
|
15
16
|
data = await $fetch(`${config.apiInternalUrl}/platform/auth/login`, {
|
|
16
17
|
method: 'POST',
|
|
17
18
|
body: { email, password },
|
|
18
|
-
headers: { Accept: 'application/json' },
|
|
19
|
+
headers: { Accept: 'application/json', ...forwardClientHeaders(n => getRequestHeader(event, n)) },
|
|
19
20
|
})
|
|
20
21
|
} catch (err: any) {
|
|
21
22
|
const status = err?.response?.status ?? err?.statusCode ?? 500
|
|
@@ -20,3 +20,21 @@ describe('buildAuthHeaders', () => {
|
|
|
20
20
|
expect(buildAuthHeaders(undefined)).toEqual({ cookie: '' })
|
|
21
21
|
})
|
|
22
22
|
})
|
|
23
|
+
|
|
24
|
+
describe('forwardClientHeaders', () => {
|
|
25
|
+
it('reenvía solo IP y user-agent del cliente, omitiendo los ausentes', async () => {
|
|
26
|
+
const { forwardClientHeaders } = await import('../bff')
|
|
27
|
+
const headers: Record<string, string> = {
|
|
28
|
+
'x-forwarded-for': '200.1.2.3, 172.68.0.1',
|
|
29
|
+
'cf-connecting-ip': '200.1.2.3',
|
|
30
|
+
'user-agent': 'Mozilla/5.0',
|
|
31
|
+
cookie: 'innertia_session=secret',
|
|
32
|
+
authorization: 'Bearer x',
|
|
33
|
+
}
|
|
34
|
+
expect(forwardClientHeaders(n => headers[n])).toEqual({
|
|
35
|
+
'x-forwarded-for': '200.1.2.3, 172.68.0.1',
|
|
36
|
+
'cf-connecting-ip': '200.1.2.3',
|
|
37
|
+
'user-agent': 'Mozilla/5.0',
|
|
38
|
+
})
|
|
39
|
+
})
|
|
40
|
+
})
|
package/server/utils/bff.ts
CHANGED
|
@@ -18,3 +18,22 @@ export function resolveApiTarget(base: string, path: string, search: string): st
|
|
|
18
18
|
export function buildAuthHeaders(token?: string): Record<string, string> {
|
|
19
19
|
return token ? { authorization: `Bearer ${token}`, cookie: '' } : { cookie: '' }
|
|
20
20
|
}
|
|
21
|
+
|
|
22
|
+
/** Headers del cliente que Laravel necesita para conocer su IP real y dispositivo. */
|
|
23
|
+
const CLIENT_HEADERS = ['x-forwarded-for', 'x-real-ip', 'cf-connecting-ip', 'user-agent'] as const
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Headers del cliente original a reenviar en las llamadas server-to-server
|
|
27
|
+
* (login, login de plataforma, callback OAuth, logout). Sin esto Laravel ve a
|
|
28
|
+
* TODOS los usuarios con la IP del contenedor Nuxt: el throttle de login queda
|
|
29
|
+
* compartido entre todos (un atacante bloquea el login de todos) y las
|
|
30
|
+
* sesiones quedan sin IP ni dispositivo. Recibe un getter para no depender de h3.
|
|
31
|
+
*/
|
|
32
|
+
export function forwardClientHeaders(get: (name: string) => string | undefined | null): Record<string, string> {
|
|
33
|
+
const out: Record<string, string> = {}
|
|
34
|
+
for (const name of CLIENT_HEADERS) {
|
|
35
|
+
const value = get(name)
|
|
36
|
+
if (value) out[name] = value
|
|
37
|
+
}
|
|
38
|
+
return out
|
|
39
|
+
}
|