Bläddra i källkod

feat: enable use of custom encryption_key in encrypt()/decrypt()

version-14
Abhishek Balam 4 år sedan
förälder
incheckning
f15edd21d1
2 ändrade filer med 18 tillägg och 8 borttagningar
  1. +11
    -1
      frappe/tests/test_password.py
  2. +7
    -7
      frappe/utils/password.py

+ 11
- 1
frappe/tests/test_password.py Visa fil

@@ -3,7 +3,7 @@
from __future__ import unicode_literals
import frappe
import unittest
from frappe.utils.password import update_password, check_password, passlibctx
from frappe.utils.password import update_password, check_password, passlibctx, encrypt, decrypt

class TestPassword(unittest.TestCase):
def setUp(self):
@@ -105,6 +105,16 @@ class TestPassword(unittest.TestCase):
doc.save()
self.assertEqual(doc.get_password(raise_exception=False), None)

def test_custom_encryption_key(self):
text = 'Frappe Framework'
custom_encryption_key = 'DFTBA'

encrypted_text = encrypt(text, encryption_key=custom_encryption_key)
decrypted_text = decrypt(encrypted_text, encryption_key=custom_encryption_key)
self.assertEqual(text, decrypted_text)

pass

def get_password_list(doc):
return frappe.db.sql("""SELECT `password`


+ 7
- 7
frappe/utils/password.py Visa fil

@@ -157,20 +157,20 @@ def create_auth_table():
frappe.db.create_auth_table()


def encrypt(pwd):
cipher_suite = Fernet(encode(get_encryption_key()))
cipher_text = cstr(cipher_suite.encrypt(encode(pwd)))
def encrypt(txt, encryption_key=None):
cipher_suite = Fernet(encode(encryption_key or get_encryption_key()))
cipher_text = cstr(cipher_suite.encrypt(encode(txt)))
return cipher_text


def decrypt(pwd):
def decrypt(txt, encryption_key=None):
try:
cipher_suite = Fernet(encode(get_encryption_key()))
plain_text = cstr(cipher_suite.decrypt(encode(pwd)))
cipher_suite = Fernet(encode(encryption_key or get_encryption_key()))
plain_text = cstr(cipher_suite.decrypt(encode(txt)))
return plain_text
except InvalidToken:
# encryption_key in site_config is changed and not valid
frappe.throw(_('Encryption key is invalid, Please check site_config.json'))
frappe.throw(_('Encryption key is invalid' + '!' if encryption_key else ', please check site_config.json.'))


def get_encryption_key():


Laddar…
Avbryt
Spara