Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PB-267]: feat/small changes to add future iat to newTokens #422

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading