| 1 |
import time |
| 2 |
|
| 3 |
|
| 4 |
def verify_safetynet_timestamp(timestamp_ms: int) -> None: |
| 5 |
"""Handle time drift between an RP and the Google SafetyNet API servers with a window of |
| 6 |
time within which the response is valid |
| 7 |
""" |
| 8 |
# Buffer period in ms |
| 9 |
grace_ms = 10 * 1000 |
| 10 |
# Get "now" in ms |
| 11 |
now = int(time.time()) * 1000 |
| 12 |
|
| 13 |
# Make sure the response was generated in the past |
| 14 |
if timestamp_ms > (now + grace_ms): |
| 15 |
raise ValueError(f"Payload timestamp {timestamp_ms} was later than {now} + {grace_ms}") |
| 16 |
|
| 17 |
# Make sure the response arrived within the grace period |
| 18 |
if timestamp_ms < (now - grace_ms): |
| 19 |
raise ValueError("Payload has expired") |