Skip to content

Commit

Permalink
feat: add secure and httpOnly attributes on cookie, set cookie on ser…
Browse files Browse the repository at this point in the history
…ver side
  • Loading branch information
HoreKk committed Apr 30, 2024
1 parent 8b84560 commit 27ff301
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 24 deletions.
14 changes: 7 additions & 7 deletions webapp-next/components/landing/NavbarLanding.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ import {
Flex,
Image,
Box,
useBreakpointValue,
Text,
useDisclosure,
Wrap,
WrapItem,
CloseButton,
Link,
} from "@chakra-ui/react";

import NextLink from "next/link";
import { useRouter } from "next/router";
import cookie from "js-cookie";
import { ELASTIC_API_KEY_NAME } from "@/utils/tools";
import useSWR from "swr";

export default function NavbarLanding() {
const { isOpen, onOpen, onClose } = useDisclosure();

const router = useRouter();

const hasApiKey = !!cookie.get(ELASTIC_API_KEY_NAME);
const { data: user, isLoading: isLoadingUser } = useSWR(
"/api/auth/user",
(...args) => fetch(...args).then((res) => res.json())
);

const links = [
{ label: "Accueil", path: "/" },
Expand Down Expand Up @@ -72,7 +72,7 @@ export default function NavbarLanding() {
<HamburgerIcon onClick={onOpen} display={["inline", "none"]} />
)}
</Flex>
{hasApiKey ? (
{!isLoadingUser && !!user ? (
<Flex as="nav" bg="white">
<Button
as={NextLink}
Expand Down
1 change: 0 additions & 1 deletion webapp-next/components/layouts/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ export function Menu() {
label: 'Déconnexion',
icon: '/icons/log-out.svg',
onClick: () => {
cookie.remove(ELASTIC_API_KEY_NAME);
triggerInvalidateApiKey({
username: context.user.username as string
}).then(() => {
Expand Down
17 changes: 2 additions & 15 deletions webapp-next/components/login/FormLogin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,10 @@ export const FormLogin = () => {
const handleModalTermsAccept = async () => {
if (code) {
await triggerCreateUser({ username, versionCGU: "1" });
const res = (await triggerVerify({
(await triggerVerify({
username: username,
code: code.toString(),
})) as any;
const result = await res.json();
cookie.set(ELASTIC_API_KEY_NAME, result.apiKey.encoded, {
expires: 1,
});
onCloseTerms();
router.push("/bo");
}
Expand All @@ -137,9 +133,6 @@ export const FormLogin = () => {
if (result.firstLogin) {
onOpenTerms();
} else {
cookie.set(ELASTIC_API_KEY_NAME, result.apiKey.encoded, {
expires: 1,
});
router.push("/bo");
}
setIsLoading(false);
Expand All @@ -162,13 +155,7 @@ export const FormLogin = () => {
setIsLoading(true);
const res = (await triggerLogin({ username, password })) as any;
if (res.ok) {
const result = await res.json();
if (process.env.NODE_ENV === "development") {
cookie.set(ELASTIC_API_KEY_NAME, result.encoded, {
expires: 1,
});
router.push("/bo");
}
if (process.env.NODE_ENV === "development") router.push("/bo");
startTimer();
setShowCodeForm(true);
setIsLoading(false);
Expand Down
4 changes: 3 additions & 1 deletion webapp-next/pages/api/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { sendMail } from '@/utils/mailter';
import {
generateCode,
getCodeEmailHtml,
ELASTIC_API_KEY_NAME
ELASTIC_API_KEY_NAME,
setCookieServerSide
} from '@/utils/tools';
import { Client } from '@elastic/elasticsearch';
import fs from 'fs';
Expand Down Expand Up @@ -74,6 +75,7 @@ export default async function handler(
});

if (process.env.NODE_ENV === 'development') {
setCookieServerSide(res, securityToken.encoded);
res.status(200).json(securityToken);
} else {
tmpCodes[username] = { code: generateCode(), apiKey: securityToken };
Expand Down
7 changes: 7 additions & 0 deletions webapp-next/pages/api/auth/invalidate-api-key.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ELASTIC_API_KEY_NAME } from "@/utils/tools";
import { Client } from "@elastic/elasticsearch";
import fs from "fs";
import type { NextApiRequest, NextApiResponse } from "next";
Expand Down Expand Up @@ -27,6 +28,12 @@ export default async function handler(
username,
});

res.setHeader("Set-Cookie", [
`${ELASTIC_API_KEY_NAME}=; Path=/; HttpOnly; Max-Age=-1; ${
process.env.NODE_ENV !== "development" ? "Secure" : ""
}`,
]);

res.status(200).json(invalidatedApiKey);
} catch (error: any) {
res.status(500).end();
Expand Down
5 changes: 5 additions & 0 deletions webapp-next/pages/api/auth/verify-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { NextApiRequest, NextApiResponse } from "next";
import fs from "fs";
import path from "path";
import rateLimit from "@/utils/rate-limit";
import { setCookieServerSide } from "@/utils/tools";
const tmpCodes = require("../../../utils/codes");

const limiter = rateLimit({
Expand Down Expand Up @@ -54,6 +55,10 @@ export default async function handler(
firstLogin = true;
}

if (!firstLogin) {
setCookieServerSide(res, codeObj.apiKey.encoded)
}

res.status(200).json({
apiKey:
firstLogin && process.env.NODE_ENV !== "development"
Expand Down
14 changes: 14 additions & 0 deletions webapp-next/utils/tools.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { format } from 'date-fns';
import moment from 'moment';
import { Filters, SearchCategory, View } from './cm2d-provider';
import { NextApiResponse } from 'next';

export const viewRefs: { label: string; value: View }[] = [
{ label: 'Vue courbe', value: 'line' },
Expand Down Expand Up @@ -754,3 +755,16 @@ export function removeAccents(str: string) {

export const ELASTIC_API_KEY_NAME =
(process.env.NEXT_PUBLIC_ELASTIC_API_KEY_NAME as string) || 'cm2d_api_key';


export const setCookieServerSide = (
res: NextApiResponse,
securityTokenEncoded: string
) => {
res.setHeader(
"Set-Cookie",
`${ELASTIC_API_KEY_NAME}=${securityTokenEncoded}; path=/; HttpOnly; ${
process.env.NODE_ENV !== "development" ? "Secure;" : ""
}`
);
};

0 comments on commit 27ff301

Please sign in to comment.