Add some boilerplate to the password reset.

This commit is contained in:
MarkBryanMilligan 2021-08-07 17:01:11 -05:00
parent 8d09ac39f2
commit 5a1f97abca
3 changed files with 9 additions and 3 deletions

View File

@ -174,6 +174,8 @@ public class MongoCurrentMonitorDao implements CurrentMonitorDao {
Date start = DateUtils.getMidnightBefore(_start, tz); Date start = DateUtils.getMidnightBefore(_start, tz);
Date monthStart = DateUtils.getStartOfMonth(_start, tz); Date monthStart = DateUtils.getStartOfMonth(_start, tz);
BreakerGroup root = CollectionUtils.getFirst(config.getBreakerGroups()); BreakerGroup root = CollectionUtils.getFirst(config.getBreakerGroups());
if (root == null)
return;
proxy.delete(BreakerGroupSummary.class, new DaoQuery("_id", BreakerGroupEnergy.toId(_accountId, root.getId(), EnergyBlockViewMode.MONTH, monthStart))); proxy.delete(BreakerGroupSummary.class, new DaoQuery("_id", BreakerGroupEnergy.toId(_accountId, root.getId(), EnergyBlockViewMode.MONTH, monthStart)));
while (start.before(_end)) { while (start.before(_end)) {
Date dayEnd = DateUtils.getMidnightAfter(start, tz); Date dayEnd = DateUtils.getMidnightAfter(start, tz);
@ -335,7 +337,7 @@ public class MongoCurrentMonitorDao implements CurrentMonitorDao {
@Override @Override
public String addPasswordResetKey(String _email) { public String addPasswordResetKey(String _email) {
String key = aes.encryptToBase64(_email); String key = aes.encryptToUrlSafeBase64(_email);
proxy.saveEntity("password_reset", new DaoEntity("_id", key)); proxy.saveEntity("password_reset", new DaoEntity("_id", key));
return key; return key;
} }
@ -356,6 +358,7 @@ public class MongoCurrentMonitorDao implements CurrentMonitorDao {
Account acct = getAccountByUsername(aes.decryptFromBase64ToString(_key)); Account acct = getAccountByUsername(aes.decryptFromBase64ToString(_key));
acct.setPassword(_password); acct.setPassword(_password);
putAccount(acct); putAccount(acct);
proxy.delete("password_reset", new DaoQuery("_id", _key));
return true; return true;
} }

View File

@ -412,6 +412,7 @@ public class BreakerGroupEnergy {
_energyBlocks.put(block.getStart().getTime(), b); _energyBlocks.put(block.getStart().getTime(), b);
} else } else
b.addJoules(block.getJoules()); b.addJoules(block.getJoules());
b.addCharge(block.getCharge());
} }
} }
} }

View File

@ -62,7 +62,7 @@ public class ResetPasswordServlet extends FreemarkerServlet {
} else { } else {
DaoEntity payload = getRequestZipBson(_req); DaoEntity payload = getRequestZipBson(_req);
String email = DaoSerializer.getString(payload, "email"); String email = DaoSerializer.getString(payload, "email");
if (NullUtils.isNotEmpty(email)) { if (EmailValidator.getInstance().isValid(email)) {
String key = Globals.dao.addPasswordResetKey(email); String key = Globals.dao.addPasswordResetKey(email);
Email from = new Email("info@lanternsoftware.com"); Email from = new Email("info@lanternsoftware.com");
String subject = "Password Reset - Lantern Power Monitor"; String subject = "Password Reset - Lantern Power Monitor";
@ -79,9 +79,11 @@ public class ResetPasswordServlet extends FreemarkerServlet {
zipBsonResponse(_resp, new DaoEntity("success", response.getStatusCode() == 200)); zipBsonResponse(_resp, new DaoEntity("success", response.getStatusCode() == 200));
} catch (IOException ex) { } catch (IOException ex) {
LOG.error("Failed to send password reset email", ex); LOG.error("Failed to send password reset email", ex);
zipBsonResponse(_resp, new DaoEntity("success", false)); _resp.setStatus(500);
} }
} }
else
_resp.setStatus(400);
} }
} }
} }