fix: Cleaner error message on invalid encryption_key

This commit is contained in:
Gavin D'souza 2022-06-09 16:43:58 +05:30
parent c67ec6c87a
commit 7e346933c7

View file

@ -213,21 +213,16 @@ def decrypt(txt, encryption_key=None):
try:
cipher_suite = Fernet(encode(encryption_key or get_encryption_key()))
plain_text = cstr(cipher_suite.decrypt(encode(txt)))
return plain_text
return cstr(cipher_suite.decrypt(encode(txt)))
except InvalidToken:
# encryption_key in site_config is changed and not valid
frappe.throw(
_("Encryption key is invalid") + "!"
if encryption_key
else _(", please check site_config.json.")
)
frappe.throw(_("Encryption key is invalid! Please check site_config.json"))
def get_encryption_key():
from frappe.installer import update_site_config
if "encryption_key" not in frappe.local.conf:
from frappe.installer import update_site_config
encryption_key = Fernet.generate_key().decode()
update_site_config("encryption_key", encryption_key)
frappe.local.conf.encryption_key = encryption_key