Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

HBE 145 - fixes cookie parse issue (#17)

Mir Arif Hasanb60d45ba76c5
diff --git a/packages/hoppscotch-backend/src/auth/helper.ts b/packages/hoppscotch-backend/src/auth/helper.ts
index a2a9371de..12dd56535 100644
--- a/packages/hoppscotch-backend/src/auth/helper.ts
+++ b/packages/hoppscotch-backend/src/auth/helper.ts
@@ -4,6 +4,11 @@ import { AuthError } from 'src/types/AuthError';
import { AuthTokens } from 'src/types/AuthTokens';
import { Response } from 'express';
+enum AuthTokenType {
+ ACCESS_TOKEN = 'access_token',
+ REFRESH_TOKEN = 'refresh_token',
+}
+
/**
* This function allows throw to be used as an expression
* @param errMessage Message present in the error message
@@ -36,13 +41,13 @@ export const authCookieHandler = (
})
.toMillis();
- res.cookie('access_token', authTokens.access_token, {
+ res.cookie(AuthTokenType.ACCESS_TOKEN, authTokens.access_token, {
httpOnly: true,
secure: true,
sameSite: 'lax',
maxAge: accessTokenValidity,
});
- res.cookie('refresh_token', authTokens.refresh_token, {
+ res.cookie(AuthTokenType.REFRESH_TOKEN, authTokens.refresh_token, {
httpOnly: true,
secure: true,
sameSite: 'lax',
@@ -59,10 +64,14 @@ export const authCookieHandler = (
* @returns AuthTokens for JWT strategy to use
*/
export const subscriptionContextCookieParser = (rawCookies: string) => {
- const access_token = rawCookies.split(';')[0].split('=')[1];
- const refresh_token = rawCookies.split(';')[1].split('=')[1];
+ const cookieMap = new Map<string, string>();
+ rawCookies.split(';').forEach((cookie) => {
+ const [key, value] = cookie.split('=');
+ cookieMap.set(key, value);
+ });
+
return <AuthTokens>{
- access_token,
- refresh_token,
+ access_token: cookieMap.get(AuthTokenType.ACCESS_TOKEN),
+ refresh_token: cookieMap.get(AuthTokenType.REFRESH_TOKEN),
};
};
diff --git a/packages/hoppscotch-backend/src/utils.ts b/packages/hoppscotch-backend/src/utils.ts
index 8ef4df427..8a649363a 100644
--- a/packages/hoppscotch-backend/src/utils.ts
+++ b/packages/hoppscotch-backend/src/utils.ts
@@ -8,7 +8,6 @@ import * as T from 'fp-ts/Task';
import * as E from 'fp-ts/Either';
import * as A from 'fp-ts/Array';
import { TeamMemberRole } from './team/team.model';
-import { User } from './user/user.model';
import { JSON_INVALID } from './errors';
/**