diff --git a/api/src/app.ts b/api/src/app.ts index ce33235..d1b19dd 100644 --- a/api/src/app.ts +++ b/api/src/app.ts @@ -66,7 +66,10 @@ export function createApp(deps: AppDeps): express.Express { app.use('/api/v1/admin/equipment', createEquipmentRoutes(deps)); app.use('/api/v1/admin/exercises', createExerciseRoutes(deps)); - app.use('/api/docs', swaggerUi.serve, swaggerUi.setup(openapi)); + // Dokumentacja API tylko poza produkcją (w produkcji ukryta przed skanerami) + if (config.nodeEnv !== 'production') { + app.use('/api/docs', swaggerUi.serve, swaggerUi.setup(openapi)); + } app.use(notFoundHandler); app.use(errorHandler); diff --git a/api/src/config.ts b/api/src/config.ts index fc416bc..cd7e228 100644 --- a/api/src/config.ts +++ b/api/src/config.ts @@ -21,7 +21,9 @@ export const config = { /^https?:\/\/(www\.)?domena\.pl(:\d+)?$/, /^https?:\/\/admin\.domena\.pl(:\d+)?$/, /^https?:\/\/(www\.)?tisachi\.ddns\.net(:\d+)?$/, - /^https?:\/\/localhost(:\d+)?$/, - /^https?:\/\/127\.0\.0\.1(:\d+)?$/, + // localhost tylko poza produkcją (dev/testy e2e); w produkcji originy wyłącznie znad domeny + ...(process.env.NODE_ENV === 'production' + ? [] + : [/^https?:\/\/localhost(:\d+)?$/, /^https?:\/\/127\.0\.0\.1(:\d+)?$/]), ], }; diff --git a/api/src/routes/auth.ts b/api/src/routes/auth.ts index 61d14ec..c5372e8 100644 --- a/api/src/routes/auth.ts +++ b/api/src/routes/auth.ts @@ -103,20 +103,22 @@ export function createAuthRoutes({ prisma }: AppDeps) { * @openapi * /auth/refresh: * post: - * summary: Refresh access token (refresh token from httpOnly cookie) + * summary: Refresh access token (refresh token from httpOnly cookie, rotated on success) * tags: [Auth] * responses: - * 200: { description: OK, returns new access token } + * 200: { description: OK, returns new access token (new refresh token in cookie) } * 401: { description: Missing/invalid/revoked refresh token } */ router.post( '/refresh', + authLimiter, asyncHandler(async (req, res) => { const token = (req.cookies as Record)[REFRESH_COOKIE]; try { if (!token) throw unauthorized('Missing refresh token'); const result = await refresh(prisma, token); - res.json(result); + setRefreshCookie(res, result.refreshToken); + res.json({ accessToken: result.accessToken }); } catch (e) { res.clearCookie(REFRESH_COOKIE, { path: '/' }); throw e instanceof AppError ? e : unauthorized('Invalid refresh token'); diff --git a/api/src/services/auth.ts b/api/src/services/auth.ts index 9c6eda0..4fb8504 100644 --- a/api/src/services/auth.ts +++ b/api/src/services/auth.ts @@ -22,10 +22,7 @@ export interface AuthResult { refreshToken: string; } -export async function register( - prisma: PrismaClient, - input: RegisterInput, -): Promise { +export async function register(prisma: PrismaClient, input: RegisterInput): Promise { const email = input.email.toLowerCase(); const existing = await prisma.user.findUnique({ where: { email } }); if (existing) throw conflict('Email already registered'); @@ -52,12 +49,20 @@ export async function login(prisma: PrismaClient, input: LoginInput): Promise { +): Promise<{ accessToken: string; refreshToken: string }> { const payload = verifyRefreshToken(token); const user = await prisma.user.findUnique({ where: { id: payload.sub } }); if (!user || user.tokenVersion !== payload.tv) throw unauthorized('Token revoked'); - return { accessToken: signAccessToken(user.id, user.tokenVersion) }; + return { + accessToken: signAccessToken(user.id, user.tokenVersion), + refreshToken: signRefreshToken(user.id, user.tokenVersion), + }; } diff --git a/api/test/integration/auth.test.ts b/api/test/integration/auth.test.ts index 84f23d3..eebbdfc 100644 --- a/api/test/integration/auth.test.ts +++ b/api/test/integration/auth.test.ts @@ -101,6 +101,10 @@ describe('auth routes', () => { const refresh = await agent.post('/api/v1/auth/refresh'); expect(refresh.status).toBe(200); expect(typeof refresh.body.accessToken).toBe('string'); + // rotacja: każde odświeżenie wystawia nowy refresh token w cookie httpOnly + const setCookie = (refresh.headers['set-cookie'] ?? []).join('; '); + expect(setCookie).toContain('fitos_refresh='); + expect(setCookie).toContain('HttpOnly'); }); it('refresh: rejects a missing cookie with 401', async () => {