Skip to content

Commit

Permalink
Fix passsword expiration check for google login accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
hbapte committed Jul 16, 2024
1 parent 1796c66 commit cbed37b
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 29 deletions.
11 changes: 6 additions & 5 deletions src/helpers/passwordExpiryNotifications.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// src/helpers/passwordExpiryNotifications.ts
import { Op } from "sequelize";
import Users from "../databases/models/users";
import { eventEmitter } from "./notifications";

const PASSWORD_EXPIRATION_MINUTES = Number(process.env.PASSWORD_EXPIRATION_MINUTES) || 90;
const EXPIRATION_GRACE_PERIOD_MINUTES = 3;
const EXPIRATION_GRACE_PERIOD_MINUTES = 1;

const WARNING_INTERVALS = [6,4,2,1];
const WARNING_INTERVALS = [4,3,2,1];

const subtractMinutes = (date: Date, minutes: number) => {
const result = new Date(date);
Expand Down Expand Up @@ -35,7 +34,8 @@ export const checkPasswordExpirations = async () => {
]
},
isVerified: true,
status: "enabled"
status: "enabled",
isGoogleAccount: false
}
});

Expand All @@ -57,7 +57,8 @@ export const checkPasswordExpirations = async () => {
]
},
isVerified: true,
status: "enabled"
status: "enabled",
isGoogleAccount: false
}
});

Expand Down
20 changes: 0 additions & 20 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,26 +367,6 @@ describe("checkPasswordExpiration middleware", () => {
expect(next).to.not.have.been.called;
});

it("should set header if the password is expiring soon", async () => {
const minutesToExpire = 9;
sinon.stub(Users, "findByPk").resolves({
passwordUpdatedAt: new Date(
Date.now() - 1000 * 60 * (PASSWORD_EXPIRATION_MINUTES - minutesToExpire)
),
email: "[email protected]",
});

await checkPasswordExpiration(req, res, next);

expect(res.setHeader).to.have.been.calledWith(
"Password-Expiry-Notification",
sinon.match(
/Your password will expire in \d+ minutes. Please update your password./
)
);
expect(next).to.have.been.calledOnce;
});

it("should call next if the password is valid", async () => {
sinon.stub(Users, "findByPk").resolves({
passwordUpdatedAt: new Date(Date.now() - 1000 * 60 * 5),
Expand Down
3 changes: 0 additions & 3 deletions src/middlewares/passwordExpiryCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ const checkPasswordExpiration = async (req: ExtendedRequest, res: Response, next
status: httpStatus.FORBIDDEN,
message: "Password expired, please check your email to reset your password."
});
} else if (minutesRemaining <= 10) {
res.setHeader("Password-Expiry-Notification", `Your password will expire in ${minutesRemaining} minutes. Please update your password.`);
}

next();
} catch (error: any) {
res.status(httpStatus.INTERNAL_SERVER_ERROR).json({
Expand Down
2 changes: 1 addition & 1 deletion src/services/cronJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ cron.schedule(
);

cron.schedule(
"*/2 * * * *",
"0 0 * * SUN",
async () => {
try {
console.log("Cron Job Started..");
Expand Down

0 comments on commit cbed37b

Please sign in to comment.