Api: rotacja refresh tokenu + limit na /auth/refresh, dokumentacja i localhost CORS tylko poza produkcja
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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+)?$/]),
|
||||
],
|
||||
};
|
||||
|
||||
@@ -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<string, string | undefined>)[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');
|
||||
|
||||
@@ -22,10 +22,7 @@ export interface AuthResult {
|
||||
refreshToken: string;
|
||||
}
|
||||
|
||||
export async function register(
|
||||
prisma: PrismaClient,
|
||||
input: RegisterInput,
|
||||
): Promise<AuthResult> {
|
||||
export async function register(prisma: PrismaClient, input: RegisterInput): Promise<AuthResult> {
|
||||
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<Au
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Odświeżenie access tokenu z rotacją refresh tokenu — każde wywołanie
|
||||
* wystawia nowy refresh token (stary pozostaje ważny do wygaśnięcia, ale
|
||||
* klient i tak dostaje wyłącznie nowy w cookie httpOnly).
|
||||
*/
|
||||
export async function refresh(
|
||||
prisma: PrismaClient,
|
||||
token: string,
|
||||
): Promise<{ accessToken: string }> {
|
||||
): 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),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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 () => {
|
||||
|
||||
Reference in New Issue
Block a user