Skip to content

Commit

Permalink
🔒 fix: update refresh token handling to use plain token instead of ha…
Browse files Browse the repository at this point in the history
…shed token (#5088)

* 🔒 fix: update refresh token handling to use plain token instead of hashed token

* 🔒 fix: simplify logoutUser by using plain refresh token for session lookup
  • Loading branch information
berry-13 authored Dec 23, 2024
1 parent 04923dd commit d6f1ecf
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 11 deletions.
7 changes: 2 additions & 5 deletions api/server/controllers/AuthController.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const {
requestPasswordReset,
} = require('~/server/services/AuthService');
const { findSession, getUserById, deleteAllUserSessions } = require('~/models');
const { hashToken } = require('~/server/utils/crypto');
const { logger } = require('~/config');

const registrationController = async (req, res) => {
Expand Down Expand Up @@ -74,11 +73,9 @@ const refreshController = async (req, res) => {
return res.status(200).send({ token, user });
}

// Hash the refresh token
const hashedToken = await hashToken(refreshToken);

// Find the session with the hashed refresh token
const session = await findSession({ userId: userId, refreshToken: hashedToken });
const session = await findSession({ userId: userId, refreshToken: refreshToken });

if (session && session.expiration > new Date()) {
const token = await setAuthTokens(userId, res, session._id);
res.status(200).send({ token, user });
Expand Down
8 changes: 2 additions & 6 deletions api/server/services/AuthService.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const {
const { isEnabled, checkEmailConfig, sendEmail } = require('~/server/utils');
const { isEmailDomainAllowed } = require('~/server/services/domains');
const { registerSchema } = require('~/strategies/validators');
const { hashToken } = require('~/server/utils/crypto');
const { logger } = require('~/config');

const domains = {
Expand All @@ -42,10 +41,7 @@ const genericVerificationMessage = 'Please check your email to verify your email
*/
const logoutUser = async (userId, refreshToken) => {
try {
const hash = await hashToken(refreshToken);

// Find the session with the matching user and refreshTokenHash
const session = await findSession({ userId: userId, refreshToken: hash });
const session = await findSession({ userId: userId, refreshToken: refreshToken });

if (session) {
try {
Expand Down Expand Up @@ -343,7 +339,7 @@ const setAuthTokens = async (userId, res, sessionId = null) => {
let refreshTokenExpires;

if (sessionId) {
session = await findSession({ sessionId: sessionId });
session = await findSession({ sessionId: sessionId }, { lean: false });
refreshTokenExpires = session.expiration.getTime();
refreshToken = await generateRefreshToken(session);
} else {
Expand Down

0 comments on commit d6f1ecf

Please sign in to comment.