示例#1
0
    def generate_jwt(self, ttl=43200, algo="RS256"):
        """Generates a JSON Web Token (https://jwt.io/) using network time.
        :param int jwt_ttl: When the JWT token expires, defaults to 43200 minutes (or 12 hours).
        :param str algo: Algorithm used to create a JSON Web Token.

        Example usage of generating and setting a JSON-Web-Token:
        ..code-block:: python

            jwt = CloudCore.generate_jwt()
            print("Generated JWT: ", jwt)

        """
        if self.logger:
            self.logger.debug("Generating JWT...")
        ntp = NTP.NTP(self._esp)
        ntp.set_time()
        claims = {
            # The time that the token was issued at
            "iat": time.time(),
            # The time the token expires.
            "exp": time.time() + ttl,
            # The audience field should always be set to the GCP project id.
            "aud": self._proj_id,
        }
        jwt = JWT.generate(claims, self._private_key, algo)
        return jwt
# SPDX-FileCopyrightText: 2022 Scott Shawcroft for Adafruit Industries
# SPDX-License-Identifier: MIT

"""Print out time based on NTP."""

import time

import socketpool
import wifi

import adafruit_ntp

# Get wifi details and more from a secrets.py file
try:
    from secrets import secrets
except ImportError:
    print("WiFi secrets are kept in secrets.py, please add them there!")
    raise

wifi.radio.connect(secrets["ssid"], secrets["password"])

pool = socketpool.SocketPool(wifi.radio)
ntp = adafruit_ntp.NTP(pool, tz_offset=0)

while True:
    print(ntp.datetime)
    time.sleep(1)
示例#3
0
# SPDX-FileCopyrightText: 2022 Scott Shawcroft for Adafruit Industries
# SPDX-License-Identifier: MIT
"""Tests NTP with CPython socket"""

import socket
import time

import adafruit_ntp

# Don't use tz_offset kwarg with CPython because it will adjust automatically.
ntp = adafruit_ntp.NTP(socket)

while True:
    print(ntp.datetime)
    time.sleep(1)