Skip to content

Commit

Permalink
feat: rotate DKIM keys and access token
Browse files Browse the repository at this point in the history
  • Loading branch information
s-aga-r committed Nov 15, 2024
1 parent b8299cd commit 485d9b0
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
63 changes: 61 additions & 2 deletions mail_client/mail_client/doctype/mail_domain/mail_domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,37 @@ frappe.ui.form.on("Mail Domain", {
frm.add_custom_button(
__("Refresh DNS Records"),
() => {
frappe.confirm(__("Are you certain you wish to proceed?"), () =>
frm.trigger("refresh_dns_records")
frappe.confirm(
__(
"Are you sure you want to refresh the DNS records? If there are any changes, you'll need to update the DNS settings with your DNS provider accordingly."
),
() => frm.trigger("refresh_dns_records")
);
},
__("Actions")
);

frm.add_custom_button(
__("Rotate DKIM Keys"),
() => {
frappe.confirm(
__(
"Are you sure you want to rotate the DKIM keys? This will generate new keys for email signing and may take up to 10 minutes to propagate across DNS servers. Emails sent during this period may be blocked due to a DKIM signature mismatch."
),
() => frm.trigger("rotate_dkim_keys")
);
},
__("Actions")
);

frm.add_custom_button(
__("Rotate Access Token"),
() => {
frappe.confirm(
__(
"Are you sure you want to rotate the access token? This will replace the current token with a new one, potentially interrupting any active sessions using the old token."
),
() => frm.trigger("rotate_access_token")
);
},
__("Actions")
Expand Down Expand Up @@ -57,4 +86,34 @@ frappe.ui.form.on("Mail Domain", {
},
});
},

rotate_dkim_keys(frm) {
frappe.call({
doc: frm.doc,
method: "rotate_dkim_keys",
args: {},
freeze: true,
freeze_message: __("Rotating DKIM Keys..."),
callback: (r) => {
if (!r.exc) {
frm.refresh();
}
},
});
},

rotate_access_token(frm) {
frappe.call({
doc: frm.doc,
method: "rotate_access_token",
args: {},
freeze: true,
freeze_message: __("Rotating Access Token..."),
callback: (r) => {
if (!r.exc) {
frm.refresh();
}
},
});
},
});
20 changes: 20 additions & 0 deletions mail_client/mail_client/doctype/mail_domain/mail_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,26 @@ def verify_dns_records(self, do_not_save: bool = False) -> None:
if not do_not_save:
self.save()

@frappe.whitelist()
def rotate_dkim_keys(self) -> None:
"""Rotates the DKIM Keys."""

frappe.only_for(["System Manager", "Administrator"])
self.dkim_private_key, self.dkim_public_key = generate_dkim_keys()
self.add_or_update_domain_in_mail_server()
self.save()
frappe.msgprint(_("DKIM Keys rotated successfully."), indicator="green", alert=True)

@frappe.whitelist()
def rotate_access_token(self) -> None:
"""Rotates the Access Token."""

frappe.only_for(["System Manager", "Administrator"])
self.access_token = generate_access_token()
self.add_or_update_domain_in_mail_server()
self.save()
frappe.msgprint(_("Access Token rotated successfully."), indicator="green", alert=True)


def generate_access_token() -> str:
"""Generates and returns the Access Token."""
Expand Down

0 comments on commit 485d9b0

Please sign in to comment.