From 6afd7a59c5e8a782fbeb9d85ef32b905d65b7e20 Mon Sep 17 00:00:00 2001 From: Justin Myers Date: Tue, 25 Feb 2025 17:29:04 -0800 Subject: [PATCH] Remove secrets --- examples/oauth2_simpletest.py | 26 +++++++++++--------------- examples/oauth2_simpletest_cpython.py | 18 ++++++------------ examples/oauth2_simpletest_esp32spi.py | 22 +++++++++------------- 3 files changed, 26 insertions(+), 40 deletions(-) diff --git a/examples/oauth2_simpletest.py b/examples/oauth2_simpletest.py index c1c6881..878b83e 100644 --- a/examples/oauth2_simpletest.py +++ b/examples/oauth2_simpletest.py @@ -1,25 +1,23 @@ # SPDX-FileCopyrightText: 2020 Brent Rubell, written for Adafruit Industries # # SPDX-License-Identifier: Unlicense + +from os import getenv import ssl import wifi import socketpool import adafruit_requests from adafruit_oauth2 import OAuth2 -# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and -# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other -# source control. -# pylint: disable=no-name-in-module,wrong-import-order -try: - from secrets import secrets -except ImportError: - print("Credentials and tokens are kept in secrets.py, please add them there!") - raise +# Get WiFi details and Google keys, ensure these are setup in settings.toml +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +google_client_id = getenv("google_client_id") +google_client_secret = getenv("google_client_secret") -print("Connecting to %s" % secrets["ssid"]) -wifi.radio.connect(secrets["ssid"], secrets["password"]) -print("Connected to %s!" % secrets["ssid"]) +print(f"Connecting to {ssid}") +wifi.radio.connect(ssid, password) +print(f"Connected to {ssid}") pool = socketpool.SocketPool(wifi.radio) requests = adafruit_requests.Session(pool, ssl.create_default_context()) @@ -29,9 +27,7 @@ scopes = ["email"] # Initialize an OAuth2 object -google_auth = OAuth2( - requests, secrets["google_client_id"], secrets["google_client_secret"], scopes -) +google_auth = OAuth2(requests, google_client_id, google_client_secret, scopes) # Request device and user codes # https://developers.google.com/identity/protocols/oauth2/limited-input-device#step-1:-request-device-and-user-codes diff --git a/examples/oauth2_simpletest_cpython.py b/examples/oauth2_simpletest_cpython.py index cd3a43b..3fa8025 100644 --- a/examples/oauth2_simpletest_cpython.py +++ b/examples/oauth2_simpletest_cpython.py @@ -1,20 +1,16 @@ # SPDX-FileCopyrightText: 2020 Brent Rubell, written for Adafruit Industries # # SPDX-License-Identifier: Unlicense + +from os import getenv import socket import ssl import adafruit_requests from adafruit_oauth2 import OAuth2 -# Add a secrets.py to your filesystem that has a dictionary called secrets with Google -# application tokens. DO NOT share that file or commit it into Git or other -# source control. -# pylint: disable=no-name-in-module,wrong-import-order -try: - from secrets import secrets -except ImportError: - print("Credentials and tokens are kept in secrets.py, please add them there!") - raise +# Get Google keys, ensure these are setup in settings.toml +google_client_id = getenv("google_client_id") +google_client_secret = getenv("google_client_secret") requests = adafruit_requests.Session(socket, ssl.create_default_context()) @@ -22,9 +18,7 @@ scopes = ["email"] # Initialize an OAuth2 object -google_auth = OAuth2( - requests, secrets["google_client_id"], secrets["google_client_secret"], scopes -) +google_auth = OAuth2(requests, google_client_id, google_client_secret, scopes) # Request device and user codes # https://developers.google.com/identity/protocols/oauth2/limited-input-device#step-1:-request-device-and-user-codes diff --git a/examples/oauth2_simpletest_esp32spi.py b/examples/oauth2_simpletest_esp32spi.py index 1172d5f..1428acc 100644 --- a/examples/oauth2_simpletest_esp32spi.py +++ b/examples/oauth2_simpletest_esp32spi.py @@ -1,6 +1,8 @@ # SPDX-FileCopyrightText: 2020 Brent Rubell, written for Adafruit Industries # # SPDX-License-Identifier: Unlicense + +from os import getenv import board import busio from digitalio import DigitalInOut @@ -10,15 +12,11 @@ import adafruit_requests from adafruit_oauth2 import OAuth2 -# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and -# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other -# source control. -# pylint: disable=no-name-in-module,wrong-import-order -try: - from secrets import secrets -except ImportError: - print("Credentials and tokens are kept in secrets.py, please add them there!") - raise +# Get WiFi details and Google keys, ensure these are setup in settings.toml +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +google_client_id = getenv("google_client_id") +google_client_secret = getenv("google_client_secret") esp32_cs = DigitalInOut(board.ESP_CS) esp32_ready = DigitalInOut(board.ESP_BUSY) @@ -30,7 +28,7 @@ print("Connecting to AP...") while not esp.is_connected: try: - esp.connect_AP(secrets["ssid"], secrets["password"]) + esp.connect_AP(ssid, password) except RuntimeError as e: print("could not connect to AP, retrying: ", e) continue @@ -45,9 +43,7 @@ scopes = ["email"] # Initialize an OAuth2 object -google_auth = OAuth2( - requests, secrets["google_client_id"], secrets["google_client_secret"], scopes -) +google_auth = OAuth2(requests, google_client_id, google_client_secret, scopes) # Request device and user codes # https://developers.google.com/identity/protocols/oauth2/limited-input-device#step-1:-request-device-and-user-codes