diff --git a/packages/hoppscotch-backend/src/app.module.ts b/packages/hoppscotch-backend/src/app.module.ts index c0e541604..d77e995db 100644 --- a/packages/hoppscotch-backend/src/app.module.ts +++ b/packages/hoppscotch-backend/src/app.module.ts @@ -7,6 +7,7 @@ import { AuthModule } from './auth/auth.module'; import { UserSettingsModule } from './user-settings/user-settings.module'; import { UserEnvironmentsModule } from './user-environment/user-environments.module'; import { UserHistoryModule } from './user-history/user-history.module'; +import { subscriptionContextCookieParser } from './auth/helper'; @Module({ imports: [ @@ -22,14 +23,12 @@ import { UserHistoryModule } from './user-history/user-history.module'; subscriptions: { 'subscriptions-transport-ws': { path: '/graphql', - onConnect: (connectionParams: any) => { + onConnect: (_, websocket) => { + const cookies = subscriptionContextCookieParser( + websocket.upgradeReq.headers.cookie, + ); return { - reqHeaders: Object.fromEntries( - Object.entries(connectionParams).map(([k, v]) => [ - k.toLowerCase(), - v, - ]), - ), + headers: { ...websocket?.upgradeReq?.headers, cookies }, }; }, }, diff --git a/packages/hoppscotch-backend/src/auth/helper.ts b/packages/hoppscotch-backend/src/auth/helper.ts index 39ebb8f2b..50231a5a2 100644 --- a/packages/hoppscotch-backend/src/auth/helper.ts +++ b/packages/hoppscotch-backend/src/auth/helper.ts @@ -52,3 +52,17 @@ export const authCookieHandler = ( res.status(HttpStatus.OK).redirect(process.env.REDIRECT_URL); } else res.status(HttpStatus.OK).send(); }; + +/** + * Sets and returns the cookies in the response object on successful authentication + * @param rawCookies cookies from the websocket connection + * @returns AuthTokens for JWT strategy to use + */ +export const subscriptionContextCookieParser = (rawCookies: string) => { + const access_tokenString = rawCookies.split(';')[0].split('=')[1]; + const refresh_tokenString = rawCookies.split(';')[1].split('=')[1]; + return { + access_token: access_tokenString, + refresh_token: refresh_tokenString, + }; +}; diff --git a/packages/hoppscotch-backend/src/auth/strategies/jwt.strategy.ts b/packages/hoppscotch-backend/src/auth/strategies/jwt.strategy.ts index 81ff70b8c..d589e82c5 100644 --- a/packages/hoppscotch-backend/src/auth/strategies/jwt.strategy.ts +++ b/packages/hoppscotch-backend/src/auth/strategies/jwt.strategy.ts @@ -37,8 +37,6 @@ export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') { if (!payload) throw new ForbiddenException(INVALID_ACCESS_TOKEN); const user = await this.usersService.findUserById(payload.sub); - console.log('user', user); - if (O.isNone(user)) { throw new UnauthorizedException(USER_NOT_FOUND); }