| 
									
										
										
										
											2023-08-12 00:57:00 +08:00
										 |  |  | import base64 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from libs import rsa | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def obfuscated_token(token: str): | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |     if not token: | 
					
						
							|  |  |  |         return token | 
					
						
							|  |  |  |     if len(token) <= 8: | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |         return "*" * 20 | 
					
						
							|  |  |  |     return token[:6] + "*" * 12 + token[-2:] | 
					
						
							| 
									
										
										
										
											2023-08-12 00:57:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def encrypt_token(tenant_id: str, token: str): | 
					
						
							| 
									
										
										
										
											2024-08-13 14:44:10 +08:00
										 |  |  |     from models.account import Tenant | 
					
						
							| 
									
										
										
										
											2024-12-20 14:12:29 +08:00
										 |  |  |     from models.engine import db | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |     if not (tenant := db.session.query(Tenant).filter(Tenant.id == tenant_id).first()): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |         raise ValueError(f"Tenant with id {tenant_id} not found") | 
					
						
							| 
									
										
										
										
											2023-08-12 00:57:00 +08:00
										 |  |  |     encrypted_token = rsa.encrypt(token, tenant.encrypt_public_key) | 
					
						
							|  |  |  |     return base64.b64encode(encrypted_token).decode() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def decrypt_token(tenant_id: str, token: str): | 
					
						
							|  |  |  |     return rsa.decrypt(base64.b64decode(token), tenant_id) | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def batch_decrypt_token(tenant_id: str, tokens: list[str]): | 
					
						
							|  |  |  |     rsa_key, cipher_rsa = rsa.get_decrypt_decoding(tenant_id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return [rsa.decrypt_token_with_decoding(base64.b64decode(token), rsa_key, cipher_rsa) for token in tokens] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def get_decrypt_decoding(tenant_id: str): | 
					
						
							|  |  |  |     return rsa.get_decrypt_decoding(tenant_id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def decrypt_token_with_decoding(token: str, rsa_key, cipher_rsa): | 
					
						
							|  |  |  |     return rsa.decrypt_token_with_decoding(base64.b64decode(token), rsa_key, cipher_rsa) |