| 51 | |
| 52 | |
| 53 | class ActionAuthentication(BaseModel): |
| 54 | encrypted: bool = Field(False) |
| 55 | type: ActionAuthenticationType = Field(...) |
| 56 | secret: Optional[str] = Field(None, min_length=1, max_length=1024) |
| 57 | content: Optional[Dict] = Field(None) |
| 58 | |
| 59 | def is_encrypted(self): |
| 60 | return self.encrypted or self.type == ActionAuthenticationType.none |
| 61 | |
| 62 | def encrypt(self): |
| 63 | # logger.debug("------------------- Encryption Start -------------------") |
| 64 | # logger.debug(f"Before encryption: {self.model_dump_json()}") |
| 65 | |
| 66 | if self.encrypted or self.type == ActionAuthenticationType.none: |
| 67 | return |
| 68 | if self.secret is not None: |
| 69 | self.secret = aes_encrypt(self.secret) |
| 70 | if self.content is not None: |
| 71 | for key in self.content: |
| 72 | self.content[key] = aes_encrypt(self.content[key]) |
| 73 | self.encrypted = True |
| 74 | |
| 75 | # logger.debug(f"After encryption: {self.model_dump_json()}") |
| 76 | # logger.debug("------------------- Encryption End -------------------") |
| 77 | |
| 78 | def decrypt(self): |
| 79 | # logger.debug("------------------- Decryption Start -------------------") |
| 80 | # logger.debug(f"Before decryption: {self.model_dump_json()}") |
| 81 | |
| 82 | if not self.encrypted or self.type == ActionAuthenticationType.none: |
| 83 | return |
| 84 | if self.secret is not None: |
| 85 | self.secret = aes_decrypt(self.secret) |
| 86 | if self.content is not None: |
| 87 | for key in self.content: |
| 88 | self.content[key] = aes_decrypt(self.content[key]) |
| 89 | self.encrypted = False |
| 90 | |
| 91 | # logger.debug(f"After decryption: {self.model_dump_json()}") |
| 92 | # logger.debug("------------------- Decryption End -------------------") |
| 93 | |
| 94 | def to_display_dict(self): |
| 95 | if self.encrypted: |
| 96 | raise ValueError("The authentication is not ready for display.") |
| 97 | |
| 98 | model_dict = self.model_dump() |
| 99 | # make secret and all content value in "xx****xx" format |
| 100 | if self.secret: |
| 101 | if len(self.secret) > 4: |
| 102 | model_dict["secret"] = f"{self.secret[:2]}****{self.secret[-2:]}" |
| 103 | |
| 104 | if self.content: |
| 105 | for key in self.content: |
| 106 | if self.content[key] and len(self.content[key]) > 4: |
| 107 | model_dict["content"][key] = f"{self.content[key][:2]}****{self.content[key][-2:]}" |
| 108 | |
| 109 | return model_dict |
no outgoing calls
no test coverage detected