File: verify_safetynet_timestamp.py

package info (click to toggle)
odoo 18.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 878,716 kB
  • sloc: javascript: 927,937; python: 685,670; xml: 388,524; sh: 1,033; sql: 415; makefile: 26
file content (19 lines) | stat: -rw-r--r-- 667 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import time


def verify_safetynet_timestamp(timestamp_ms: int) -> None:
    """Handle time drift between an RP and the Google SafetyNet API servers with a window of
    time within which the response is valid
    """
    # Buffer period in ms
    grace_ms = 10 * 1000
    # Get "now" in ms
    now = int(time.time()) * 1000

    # Make sure the response was generated in the past
    if timestamp_ms > (now + grace_ms):
        raise ValueError(f"Payload timestamp {timestamp_ms} was later than {now} + {grace_ms}")

    # Make sure the response arrived within the grace period
    if timestamp_ms < (now - grace_ms):
        raise ValueError("Payload has expired")