Skip to content

Commit ef9f2cc

Browse files
authored
Merge pull request adafruit#3001 from justmobilize/secrets-cleanup-l-o
Secrets Cleanup: L, M and O
2 parents 923b6f2 + 592b33b commit ef9f2cc

File tree

27 files changed

+341
-191
lines changed

27 files changed

+341
-191
lines changed

MacroPad_Scramble_Lock/code.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
# CONFIGURABLES ------------------------
2323

2424
# Password information
25-
# For higher security, place password in a separate file like secrets.py
25+
# For higher security, place password in a separate file like settings.toml
2626
PASSWORD = "2468"
2727
PASSWORD_LENGTH = len(PASSWORD)
2828

Macropad_2FA_TOTP/.circuitpython.skip

Lines changed: 0 additions & 6 deletions
This file was deleted.

Macropad_2FA_TOTP/code.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,11 @@
3131
DISPLAY_RATE = 1 # screen refresh rate
3232
#-------------------------------------------------------------------------
3333

34-
# Get secrets from a secrets.py file
34+
# Get totp_keys from a totp_keys.py file
3535
try:
36-
from secrets import secrets
37-
totp_keys = secrets["totp_keys"]
36+
from totp_keys import totp_keys
3837
except ImportError:
39-
print("Secrets are kept in secrets.py, please add them there!")
40-
raise
41-
except KeyError:
42-
print("TOTP info not found in secrets.py.")
38+
print("TOTP info not found in totp_keys.py, please add them there!")
4339
raise
4440

4541
# set board to use PCF8523 as its RTC
@@ -80,7 +76,9 @@
8076
rtc_time.anchor_point = (0.0, 0.5)
8177
rtc_time.anchored_position = (0, 59)
8278

83-
progress_bar = HorizontalProgressBar((68, 46), (55, 17), bar_color=0xFFFFFF, min_value=0, max_value=30)
79+
progress_bar = HorizontalProgressBar(
80+
(68, 46), (55, 17), bar_color=0xFFFFFF, min_value=0, max_value=30
81+
)
8482

8583
splash = displayio.Group()
8684
splash.append(name)
@@ -172,15 +170,15 @@ def generate_otp(int_input, secret_key, digits=6):
172170
int_to_bytestring(int_input)).digest()
173171
)
174172
offset = hmac_hash[-1] & 0xf
175-
code = ((hmac_hash[offset] & 0x7f) << 24 |
176-
(hmac_hash[offset + 1] & 0xff) << 16 |
177-
(hmac_hash[offset + 2] & 0xff) << 8 |
178-
(hmac_hash[offset + 3] & 0xff))
179-
str_code = str(code % 10 ** digits)
180-
while len(str_code) < digits:
181-
str_code = '0' + str_code
173+
otp_code = ((hmac_hash[offset] & 0x7f) << 24 |
174+
(hmac_hash[offset + 1] & 0xff) << 16 |
175+
(hmac_hash[offset + 2] & 0xff) << 8 |
176+
(hmac_hash[offset + 3] & 0xff))
177+
str_otp_code = str(otp_code % 10 ** digits)
178+
while len(str_otp_code) < digits:
179+
str_otp_code = '0' + str_otp_code
182180

183-
return str_code
181+
return str_otp_code
184182

185183
#-------------------------------------------------------------------------
186184
# M A C R O P A D S E T U P

Macropad_2FA_TOTP/secrets.py

Lines changed: 0 additions & 23 deletions
This file was deleted.

Macropad_2FA_TOTP/totp_keys.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# SPDX-FileCopyrightText: 2021 Carter Nelson for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
# This file contains totp codes!
6+
# If you put them in the code you risk committing that info or sharing it
7+
8+
# tuples of name, key, color
9+
totp_keys = [
10+
("Github", "JBSWY3DPEHPK3PXP", 0x8732A8),
11+
("Discord", "JBSWY3DPEHPK3PXQ", 0x32A89E),
12+
("Slack", "JBSWY5DZEHPK3PXR", 0xFC861E),
13+
("Basecamp", "JBSWY6DZEHPK3PXS", 0x55C24C),
14+
("Gmail", "JBSWY7DZEHPK3PXT", 0x3029FF),
15+
None,
16+
None, # must have 12 entires
17+
None, # set None for unused keys
18+
None,
19+
("Hello Kitty", "JBSWY7DZEHPK3PXU", 0xED164F),
20+
None,
21+
None,
22+
]

MagTag/MagTag_Google_Calendar/authenticator.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
# SPDX-FileCopyrightText: 2021 Brent Rubell, written for Adafruit Industries
22
#
33
# SPDX-License-Identifier: Unlicense
4+
5+
from os import getenv
46
from adafruit_oauth2 import OAuth2
57
from adafruit_display_text.label import Label
68
from adafruit_bitmap_font import bitmap_font
79
from adafruit_magtag.magtag import Graphics, Network
810
from adafruit_display_shapes.rect import Rect
911

10-
# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
11-
# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other
12-
# source control.
13-
# pylint: disable=no-name-in-module,wrong-import-order
14-
try:
15-
from secrets import secrets
16-
except ImportError:
17-
print("Credentials and tokens are kept in secrets.py, please add them there!")
18-
raise
12+
# Get WiFi details, ensure these are setup in settings.toml
13+
ssid = getenv("CIRCUITPY_WIFI_SSID")
14+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
15+
16+
if None in [ssid, password]:
17+
raise RuntimeError(
18+
"WiFi settings are kept in settings.toml, "
19+
"please add them there. The settings file must contain "
20+
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
21+
"at a minimum."
22+
)
1923

2024
network = Network()
2125
network.connect()
@@ -57,8 +61,8 @@
5761
# Initialize an OAuth2 object
5862
google_auth = OAuth2(
5963
network.requests,
60-
secrets["google_client_id"],
61-
secrets["google_client_secret"],
64+
getenv("google_client_id"),
65+
getenv("google_client_secret"),
6266
scopes,
6367
)
6468

@@ -90,16 +94,16 @@
9094
raise RuntimeError("Timed out waiting for browser response!")
9195

9296
print("Successfully Authenticated with Google!")
93-
print("Add the following lines to your secrets.py file:")
94-
print("\t'google_access_token' " + ":" + " '%s'," % google_auth.access_token)
95-
print("\t'google_refresh_token' " + ":" + " '%s'" % google_auth.refresh_token)
97+
print("Add the following lines to your settings.toml file:")
98+
print(f'google_access_token="{google_auth.access_token}"')
99+
print(f'google_refresh_token="{google_auth.refresh_token}"')
96100

97101
graphics.splash.pop()
98102
graphics.splash.pop()
99103
graphics.splash.pop()
100104

101105
label_overview_text.text = "Successfully Authenticated!"
102106
label_verification_url.text = (
103-
"Check the REPL for tokens to add\n\tto your secrets.py file"
107+
"Check the REPL for tokens to add\n\tto your settings.toml file"
104108
)
105109
display.refresh()

MagTag/MagTag_Google_Calendar/code.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
# SPDX-FileCopyrightText: 2021 Brent Rubell, written for Adafruit Industries
22
#
33
# SPDX-License-Identifier: Unlicense
4+
5+
from os import getenv
46
import time
57
import rtc
68
from adafruit_oauth2 import OAuth2
79
from adafruit_display_shapes.line import Line
810
from adafruit_magtag.magtag import MagTag
911

12+
# Get WiFi details, ensure these are setup in settings.toml
13+
ssid = getenv("CIRCUITPY_WIFI_SSID")
14+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
15+
16+
if None in [ssid, password]:
17+
raise RuntimeError(
18+
"WiFi settings are kept in settings.toml, "
19+
"please add them there. The settings file must contain "
20+
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
21+
"at a minimum."
22+
)
23+
1024
# Calendar ID
1125
CALENDAR_ID = "YOUR_CALENDAR_ID"
1226

@@ -42,15 +56,6 @@
4256
6: "Sunday",
4357
}
4458

45-
# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
46-
# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other
47-
# source control.
48-
# pylint: disable=no-name-in-module,wrong-import-order
49-
try:
50-
from secrets import secrets
51-
except ImportError:
52-
print("Credentials and tokens are kept in secrets.py, please add them there!")
53-
raise
5459

5560
# Create a new MagTag object
5661
magtag = MagTag()
@@ -62,18 +67,18 @@
6267
scopes = ["https://www.googleapis.com/auth/calendar.readonly"]
6368
google_auth = OAuth2(
6469
magtag.network.requests,
65-
secrets["google_client_id"],
66-
secrets["google_client_secret"],
70+
getenv("google_client_id"),
71+
getenv("google_client_secret"),
6772
scopes,
68-
secrets["google_access_token"],
69-
secrets["google_refresh_token"],
73+
getenv("google_access_token"),
74+
getenv("google_refresh_token"),
7075
)
7176

7277

7378
def get_current_time(time_max=False):
7479
"""Gets local time from Adafruit IO and converts to RFC3339 timestamp."""
7580
# Get local time from Adafruit IO
76-
magtag.get_local_time(secrets["timezone"])
81+
magtag.get_local_time(getenv("timezone"))
7782
# Format as RFC339 timestamp
7883
cur_time = r.datetime
7984
if time_max: # maximum time to fetch events is midnight (4:59:59UTC)

MagTag/MagTag_NextBus/code.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
"""
1212

1313
# pylint: disable=import-error
14+
15+
from os import getenv
1416
import gc
1517
import time
16-
from secrets import secrets
1718
import displayio
1819
from rtc import RTC
1920
from adafruit_magtag.magtag import Graphics
@@ -23,6 +24,17 @@
2324
from adafruit_display_text.label import Label
2425
from nextbus import NextBus
2526

27+
# Get WiFi details, ensure these are setup in settings.toml
28+
ssid = getenv("CIRCUITPY_WIFI_SSID")
29+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
30+
31+
if None in [ssid, password]:
32+
raise RuntimeError(
33+
"WiFi settings are kept in settings.toml, "
34+
"please add them there. The settings file must contain "
35+
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
36+
"at a minimum."
37+
)
2638

2739
# CONFIGURABLE SETTINGS ----------------------------------------------------
2840

@@ -54,15 +66,12 @@
5466
# not a big problem if this drifts a bit due to infrequent synchronizations.
5567
# 6 hour default.
5668
CLOCK_SYNC_INTERVAL = 6 * 60 * 60
57-
# Load time zone string from secrets.py, else IP geolocation is used
69+
# Load time zone string from settings.toml, else IP geolocation is used
5870
# (http://worldtimeapi.org/api/timezone for list). Again, this is only
5971
# used for the 'Last checked' display, not predictions, so it's not
6072
# especially disruptive if missing.
6173
# pylint: disable=bare-except
62-
try:
63-
TIME_ZONE = secrets['timezone'] # e.g. 'America/New_York'
64-
except:
65-
TIME_ZONE = None # Use IP geolocation
74+
TIME_ZONE = getenv('timezone') # e.g. 'America/New_York'
6675

6776

6877
# SOME UTILITY FUNCTIONS ---------------------------------------------------

MagTag/MagTag_Project_Selector/secrets.py

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# SPDX-FileCopyrightText: 2020 Eva Herrada for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
# This file is where you keep private settings, passwords, and tokens!
6+
# If you put them in the code you risk committing that info or sharing it
7+
8+
CIRCUITPY_WIFI_SSID="your-wifi-ssid"
9+
CIRCUITPY_WIFI_PASSWORD="your-wifi-password"
10+
ADAFRUIT_AIO_USERNAME="my_username"
11+
ADAFRUIT_AIO_KEY="my_key"
12+
timezone="America/New_York" # http://worldtimeapi.org/timezones
13+
openweather_token="my_openweather_token"
14+
openweather_location="New York City, US"

0 commit comments

Comments
 (0)