From f07a35f9846490afd60195ca01ccf18a04a333e5 Mon Sep 17 00:00:00 2001 From: Junxiang Huang Date: Wed, 4 Dec 2024 11:01:19 +0800 Subject: [PATCH] wip: update encrypy & decrypt --- dtable_events/automations/models.py | 50 +++++++---------------------- 1 file changed, 12 insertions(+), 38 deletions(-) diff --git a/dtable_events/automations/models.py b/dtable_events/automations/models.py index f1a0f311..79766b1a 100644 --- a/dtable_events/automations/models.py +++ b/dtable_events/automations/models.py @@ -10,61 +10,35 @@ logger = logging.getLogger(__name__) +ENCRYPT_KEYS = ['password', 'webhook_url', 'api_key', 'secret_key', 'repo_api_token', 'client_secret'] def _encrypt_detail(detail): detail_clone = deepcopy(detail) cryptor = AESPasswordHasher() try: - if 'password' in detail_clone.keys(): - password = detail_clone.get('password') - if password: - detail_clone.update({'password': cryptor.encode(password)}) - if 'webhook_url' in detail.keys(): - webhook_url = detail.get('webhook_url') - if webhook_url: - detail_clone.update({'webhook_url': cryptor.encode(webhook_url)}) - if 'api_key' in detail.keys(): - api_key = detail.get('api_key') - if api_key: - detail_clone.update({'api_key': cryptor.encode(api_key)}) - if 'secret_key' in detail.keys(): - secret_key = detail.get('secret_key') - if secret_key: - detail_clone.update({'secret_key': cryptor.encode(secret_key)}) - if 'repo_api_token' in detail.keys(): - repo_api_token = detail.get('repo_api_token') - if repo_api_token: - detail_clone.update({'repo_api_token': cryptor.encode(repo_api_token)}) + encrypted_details = { + key: cryptor.encode(detail_clone[key]) + for key in ENCRYPT_KEYS if key in detail_clone and detail_clone[key] + } + detail_clone.update(encrypted_details) return json.dumps(detail_clone) except Exception as e: logger.error(e) return None - + def _decrypt_detail(detail): detail_clone = deepcopy(detail) cryptor = AESPasswordHasher() try: - if 'password' in detail_clone.keys(): - password = detail_clone.get('password') - if password: - detail_clone.update({'password': cryptor.decode(password)}) - if 'webhook_url' in detail.keys(): - webhook_url = detail.get('webhook_url') - if webhook_url: - detail_clone.update({'webhook_url': cryptor.decode(webhook_url)}) - if 'api_key' in detail.keys(): - api_key = detail.get('api_key') - if api_key: - detail_clone.update({'api_key': cryptor.decode(api_key)}) - if 'secret_key' in detail.keys(): - secret_key = detail.get('secret_key') - if secret_key: - detail_clone.update({'secret_key': cryptor.decode(secret_key)}) + decrypted_details = { + key: cryptor.decode(detail_clone[key]) + for key in ENCRYPT_KEYS if key in detail_clone and detail_clone[key] + } + detail_clone.update(decrypted_details) return detail_clone except Exception as e: logger.error(e) return None - class BoundThirdPartyAccounts(Base): __tablename__ = 'bound_third_party_accounts'