You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 2 месеци
1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import base64
  2. from libs import rsa
  3. def obfuscated_token(token: str) -> str:
  4. if not token:
  5. return token
  6. if len(token) <= 8:
  7. return "*" * 20
  8. return token[:6] + "*" * 12 + token[-2:]
  9. def full_mask_token(token_length=20):
  10. return "*" * token_length
  11. def encrypt_token(tenant_id: str, token: str):
  12. from models.account import Tenant
  13. from models.engine import db
  14. if not (tenant := db.session.query(Tenant).where(Tenant.id == tenant_id).first()):
  15. raise ValueError(f"Tenant with id {tenant_id} not found")
  16. assert tenant.encrypt_public_key is not None
  17. encrypted_token = rsa.encrypt(token, tenant.encrypt_public_key)
  18. return base64.b64encode(encrypted_token).decode()
  19. def decrypt_token(tenant_id: str, token: str) -> str:
  20. return rsa.decrypt(base64.b64decode(token), tenant_id)
  21. def batch_decrypt_token(tenant_id: str, tokens: list[str]):
  22. rsa_key, cipher_rsa = rsa.get_decrypt_decoding(tenant_id)
  23. return [rsa.decrypt_token_with_decoding(base64.b64decode(token), rsa_key, cipher_rsa) for token in tokens]
  24. def get_decrypt_decoding(tenant_id: str):
  25. return rsa.get_decrypt_decoding(tenant_id)
  26. def decrypt_token_with_decoding(token: str, rsa_key, cipher_rsa):
  27. return rsa.decrypt_token_with_decoding(base64.b64decode(token), rsa_key, cipher_rsa)