Verifies if the given signature is valid
(
self,
body: Union[str, bytes],
timestamp: str,
signature: str,
)
| 38 | ) |
| 39 | |
| 40 | def is_valid( |
| 41 | self, |
| 42 | body: Union[str, bytes], |
| 43 | timestamp: str, |
| 44 | signature: str, |
| 45 | ) -> bool: |
| 46 | """Verifies if the given signature is valid""" |
| 47 | if timestamp is None or signature is None: |
| 48 | return False |
| 49 | |
| 50 | if abs(self.clock.now() - int(timestamp)) > 60 * 5: |
| 51 | return False |
| 52 | |
| 53 | calculated_signature = self.generate_signature(timestamp=timestamp, body=body) |
| 54 | if calculated_signature is None: |
| 55 | return False |
| 56 | return hmac.compare_digest(calculated_signature, signature) |
| 57 | |
| 58 | def generate_signature(self, *, timestamp: str, body: Union[str, bytes]) -> Optional[str]: |
| 59 | """Generates a signature""" |