Skip to content

Commit d778898

Browse files
committed
fix: distinguer le nom du cookie pour stocker le token par environnement
1 parent 4331a0d commit d778898

7 files changed

Lines changed: 20 additions & 8 deletions

File tree

back/config/settings/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,10 @@
632632
# et pour activer le SSL sur la connexion à la base de données.
633633
ENVIRONMENT = os.getenv("ENVIRONMENT", "local")
634634

635+
# Nom du cookie d'authentification : différencié par environnement pour éviter
636+
# les collisions de cookies entre prod et staging (même domaine parent).
637+
AUTH_COOKIE_NAME = "token" if ENVIRONMENT == "production" else f"token_{ENVIRONMENT}"
638+
635639
# Profiling (Silk) :
636640
# Doit être explicitement activé (via env var)
637641
PROFILE = False

back/dora/core/tests/test_utils.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,17 +226,19 @@ def test_address_to_one_line(address1, address2, postal_code, city, expected):
226226
assert address_to_one_line(address1, address2, postal_code, city) == expected
227227

228228

229-
@override_settings(FRONTEND_URL="https://subdomain.example.com")
229+
@override_settings(
230+
FRONTEND_URL="https://subdomain.example.com", AUTH_COOKIE_NAME="token_test"
231+
)
230232
def test_set_auth_token_cookie():
231233
"""set_auth_token_cookie définit le cookie avec les bons attributs."""
232234
response = HttpResponse()
233235
token_key = "test-token-key-12345"
234236

235237
set_auth_token_cookie(response, token_key)
236238

237-
cookie = response.cookies["token"]
239+
cookie = response.cookies["token_test"]
238240

239241
assert (
240242
cookie.OutputString()
241-
== f"token={token_key}; Domain=subdomain.example.com; Path=/; SameSite=Lax; Secure"
243+
== f"token_test={token_key}; Domain=subdomain.example.com; Path=/; SameSite=Lax; Secure"
242244
)

back/dora/core/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,4 @@ def set_auth_token_cookie(response: HttpResponse, token_key: str):
161161
parsed_frontend_url = urlparse(settings.FRONTEND_URL)
162162
cookie_kwargs["domain"] = parsed_frontend_url.hostname
163163

164-
response.set_cookie("token", token_key, **cookie_kwargs)
164+
response.set_cookie(settings.AUTH_COOKIE_NAME, token_key, **cookie_kwargs)

front/src/hooks.server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { ENVIRONMENT, SENTRY_DSN } from "$lib/env";
1010
import { handleInboundNexusAutoLogin } from "$lib/utils/nexus";
1111

1212
import { MAX_REQUESTS_PER_MINUTE } from "$env/static/private";
13+
import { TOKEN_KEY } from "$lib/utils/auth";
1314

1415
const rateLimiter = new RetryAfterRateLimiter({
1516
IPUA: [Number(MAX_REQUESTS_PER_MINUTE) || 24, "m"],
@@ -48,7 +49,7 @@ export const handle: Handle = sequence(
4849
);
4950
}
5051

51-
const token = event.cookies.get("token");
52+
const token = event.cookies.get(TOKEN_KEY);
5253
await handleInboundNexusAutoLogin(event.url, token);
5354

5455
const response = await resolve(event);

front/src/lib/utils/auth.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import { log, logException } from "./logger";
99
import { userPreferencesSet } from "./preferences";
1010
import { invalidateServicesOptionsCache } from "$lib/cache/services-options";
1111

12-
const TOKEN_KEY = "token";
12+
export const TOKEN_KEY =
13+
import.meta.env.VITE_ENVIRONMENT === "production"
14+
? "token"
15+
: `token_${import.meta.env.VITE_ENVIRONMENT}`;
1316

1417
export type UserMainActivity =
1518
| "accompagnateur"

front/src/routes/(modeles-services)/services/[slug]/+page.server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { redirect } from "@sveltejs/kit";
33
import type { PageServerLoad } from "./$types";
44
import { handleEmploisOrientation } from "$lib/requests/emplois-orientation";
55
import { ORIENTATION_JWT_QUERY_PARAM } from "$lib/consts";
6+
import { TOKEN_KEY } from "$lib/utils/auth";
67

78
export const load: PageServerLoad = async ({ url, params, cookies }) => {
89
const opJwt = url.searchParams.get(ORIENTATION_JWT_QUERY_PARAM);
@@ -12,7 +13,7 @@ export const load: PageServerLoad = async ({ url, params, cookies }) => {
1213
return;
1314
}
1415

15-
const token = cookies.get("token");
16+
const token = cookies.get(TOKEN_KEY);
1617

1718
const response = await handleEmploisOrientation({
1819
serviceSlug: params.slug,

front/src/routes/nexus/auto-login/+page.server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { getApiURL } from "$lib/utils/api";
22
import { error, redirect } from "@sveltejs/kit";
33
import type { RequestEvent } from "@sveltejs/kit";
4+
import { TOKEN_KEY } from "$lib/utils/auth";
45

56
export const load = async ({ cookies, url }: RequestEvent) => {
6-
const token = cookies.get("token") ?? null;
7+
const token = cookies.get(TOKEN_KEY) ?? null;
78

89
const nextUrl = url.searchParams.get("next");
910

0 commit comments

Comments
 (0)