Skip to content

Commit

Permalink
fix: allow reset_otp_secret only if Two Factor Auth is enabled (#20506
Browse files Browse the repository at this point in the history
)

* fix: display `Reset OTP Secret` button only if Two factor Auth is enabled

* fix: added validations and fetched value from cached doc

* fix: linter changes
  • Loading branch information
DaizyModi committed Apr 3, 2023
1 parent fa32b61 commit 06580bd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
5 changes: 4 additions & 1 deletion frappe/core/doctype/user/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,10 @@ frappe.ui.form.on("User", {
});
}

if (frappe.session.user == doc.name || frappe.user.has_role("System Manager")) {
if (
cint(frappe.boot.sysdefaults.enable_two_factor_auth) &&
(frappe.session.user == doc.name || frappe.user.has_role("System Manager"))
) {
frm.add_custom_button(
__("Reset OTP Secret"),
function () {
Expand Down
18 changes: 13 additions & 5 deletions frappe/twofactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,23 +450,31 @@ def disable():


@frappe.whitelist()
def reset_otp_secret(user):
def reset_otp_secret(user: str):
if frappe.session.user != user:
frappe.only_for("System Manager", message=True)

otp_issuer = frappe.db.get_single_value("System Settings", "otp_issuer_name")
user_email = frappe.db.get_value("User", user, "email")
settings = frappe.get_cached_doc("System Settings")

if not settings.enable_two_factor_auth:
frappe.throw(
_("You have to enable Two Factor Auth from System Settings."),
title=_("Enable Two Factor Auth"),
)

otp_issuer = settings.otp_issuer_name or "Frappe Framework"
user_email = frappe.get_cached_value("User", user, "email")

clear_default(user + "_otplogin")
clear_default(user + "_otpsecret")

email_args = {
"recipients": user_email,
"sender": None,
"subject": _("OTP Secret Reset - {0}").format(otp_issuer or "Frappe Framework"),
"subject": _("OTP Secret Reset - {0}").format(otp_issuer),
"message": _(
"<p>Your OTP secret on {0} has been reset. If you did not perform this reset and did not request it, please contact your System Administrator immediately.</p>"
).format(otp_issuer or "Frappe Framework"),
).format(otp_issuer),
"delayed": False,
"retry": 3,
}
Expand Down

0 comments on commit 06580bd

Please sign in to comment.