Skip to content

Commit

Permalink
wip: update encrypy & decrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
wacmkxiaoyi committed Dec 4, 2024
1 parent 7318dac commit f07a35f
Showing 1 changed file with 12 additions and 38 deletions.
50 changes: 12 additions & 38 deletions dtable_events/automations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down

0 comments on commit f07a35f

Please sign in to comment.