Skip to content

Commit

Permalink
Merge pull request #422 from internxt/feat/add-lastPasswordChangeAt-t…
Browse files Browse the repository at this point in the history
…o-users-small-changes

[PB-267]: feat/small changes to add future iat to newTokens
  • Loading branch information
apsantiso authored Jan 23, 2024
2 parents cca3762 + b2da1ee commit 99c19a7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
20 changes: 14 additions & 6 deletions src/app/middleware/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ function Sign(data, secret, expires = false) {
return token;
}

function SignWithOlderIAT(data, secret) {
return jwt.sign({ email: data, iat: getOlderIAT() }, secret, { expiresIn: '14d' });
function SignWithFutureIAT(data, secret) {
return jwt.sign({ email: data, iat: getFutureIAT() }, secret, { expiresIn: '14d' });
}

function SignNewTokenWithFutureIAT(data, secret, expires = false) {
const futureIat = getFutureIAT();
return expires
? jwt.sign(getNewTokenPayload(data, futureIat), secret, { expiresIn: '14d' })
: jwt.sign(getNewTokenPayload(data, futureIat), secret);
}

function SignNewToken(data, secret, expires = false) {
Expand All @@ -21,7 +28,7 @@ function SignNewToken(data, secret, expires = false) {
return token;
}

function getNewTokenPayload(userData) {
function getNewTokenPayload(userData, customIat) {
return {
payload: {
uuid: userData.uuid,
Expand All @@ -34,22 +41,23 @@ function getNewTokenPayload(userData) {
user: userData.bridgeUser,
pass: userData.userId,
},
iat: getDefaultIAT(),
},
iat: customIat ?? getDefaultIAT(),
};
}

function getDefaultIAT() {
return Math.floor(Date.now() / 1000);
}

function getOlderIAT() {
function getFutureIAT() {
return Math.floor(Date.now() / 1000) + 60;
}

module.exports = {
passportAuth,
Sign,
SignNewToken,
SignWithOlderIAT,
SignWithFutureIAT,
SignNewTokenWithFutureIAT,
};
14 changes: 7 additions & 7 deletions src/app/routes/user.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const openpgp = require('openpgp');
const createHttpError = require('http-errors');
const { passportAuth, Sign, SignNewToken, SignWithOlderIAT } = require('../middleware/passport');
const { passportAuth, Sign, SignWithFutureIAT, SignNewTokenWithFutureIAT } = require('../middleware/passport');
const Logger = require('../../lib/logger').default;
const AnalyticsService = require('../../lib/analytics/AnalyticsService');
const { default: uploadAvatar } = require('../middleware/upload-avatar');
Expand All @@ -16,8 +16,8 @@ module.exports = (Router, Service, App) => {

Service.User.UpdatePasswordMnemonic(req.user, currentPassword, newPassword, newSalt, mnemonic, privateKey)
.then(() => {
const token = SignWithOlderIAT(req.user.email, App.config.get('secrets').JWT);
const newToken = SignNewToken(req.user, App.config.get('secrets').JWT);
const token = SignWithFutureIAT(req.user.email, App.config.get('secrets').JWT);
const newToken = SignNewTokenWithFutureIAT(req.user, App.config.get('secrets').JWT);
res.status(200).send({ token, newToken });
})
.catch((err) => {
Expand Down Expand Up @@ -158,10 +158,10 @@ module.exports = (Router, Service, App) => {
res.status(200).send({ token, user });
} catch (err) {
logger.error(
'Update user error %s: %s. STACK %s. BODY %s',
req.user.email,
err.message,
err.stack || 'NO STACK',
'Update user error %s: %s. STACK %s. BODY %s',
req.user.email,
err.message,
err.stack || 'NO STACK',
req.body
);
res.status(500).send({ error: 'Internal Server error' });
Expand Down

0 comments on commit 99c19a7

Please sign in to comment.