-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd_credentials.py
More file actions
34 lines (29 loc) · 1.07 KB
/
d_credentials.py
File metadata and controls
34 lines (29 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import os
import cryptography
import json
from cryptography.fernet import Fernet
# Criar ou recuperar uma chave de criptografia
def get_key(page):
if not page.client_storage.get("key"):
key = Fernet.generate_key()
page.client_storage.set("key", key.decode())
else:
key = page.client_storage.get("key").encode()
return Fernet(key)
# Recuperar as credenciais do usuário
def get_credentials(page):
cipher_suite = get_key(page)
if not page.client_storage.get("credentials"):
credentials = None
else:
cipher_text = page.client_storage.get("credentials").encode()
decrypted = cipher_suite.decrypt(cipher_text)
credentials = json.loads(decrypted)
return credentials
# Gravar as credenciais do usuário
def save_credentials(page, usr_credentials):
cipher_suite = get_key(page)
cipher_json = json.dumps(usr_credentials)
cipher_bytes = cipher_json.encode('utf-8')
cipher_text = cipher_suite.encrypt(cipher_bytes) # Encrypt the credentials
page.client_storage.set("credentials", cipher_text.decode())