# CircuitPython 2FA TOTP Authentication Friend

## Introduction

![](https://cdn-learn.adafruit.com/assets/assets/000/049/760/medium800/hacks_pyotp.jpg?1514695004)

# What is TOTP?

Having 2 Factor Authentication on all your accounts is a good way to keep your data more secure. With 2FA logins, not only is a username and password needed, but also a one-time-use code. There's a few different ways to get that code, such as by email, phone or SMS. But my favorite way is to do it is via a 'Google Authenticator' time-based OTP ( **o** ne **t** ime **p** assword), also known as a **TOTP**.

Using an app on your phone like Authy or Authenticator, you set up a secret given to you by the service, then every 30 seconds, a new code is generated for you. What's extra nice is that the Google Authenticator protocol is supported by just about _every_ service and phone/tablet

# So What's The Problem?

**I don't own a phone!** So I have to ask Mr. Ladyada for an authenticator code. Or I can use my tablet, but it's not always at my desk. And I don't want to buy a phone just for using 2FA!

# A Solution!

Luckily for us, the Google Authenticator protocol is really simple - You just need to be able to know the current time, and run a SHA1 hash.

I decided to build a simple device that all it does is generate TOTP's for me, using CircuitPython - my favorite programming language! It uses a **Feather ESP8266** which has WiFi so it can connect to NTP to get the current time on startup, and a **Feather OLED** to display text nice and clearly.

Every time I need a new code, I just click the reset button and within 2 seconds I've got my 3 most common TOTP's on hand (yes its that fast!)

# FAQ
### 

This project is probably not for you

# CircuitPython 2FA TOTP Authentication Friend

## Hardware

# Parts Required

Easy! You only need two parts - a **Feather Huzzah** and an **OLED FeatherWing**

_Your purchases from the Adafruit shop help support us writing up these awesome guides, libraries and CircuitPython development and are appreciated!!!_

### Adafruit Feather HUZZAH with ESP8266 - Loose Headers

[Adafruit Feather HUZZAH with ESP8266 - Loose Headers](https://www.adafruit.com/product/2821)
Feather is the new development board from Adafruit, and like its namesake, it is thin, light, and lets you fly! We designed Feather to be a new standard for portable microcontroller cores.

This is the&nbsp; **Adafruit Feather HUZZAH ESP8266** &nbsp;- our take on an...

In Stock
[Buy Now](https://www.adafruit.com/product/2821)
[Related Guides to the Product](https://learn.adafruit.com/products/2821/guides)
![Angled shot of black, rectangular WiFi development board.](https://cdn-shop.adafruit.com/640x480/2821-07.jpg)

### Adafruit FeatherWing OLED - 128x32 OLED Add-on For Feather

[Adafruit FeatherWing OLED - 128x32 OLED Add-on For Feather](https://www.adafruit.com/product/2900)
A Feather board without ambition is a Feather board without FeatherWings! This is the **FeatherWing OLED** : it adds a 128x32 monochrome OLED plus 3 user buttons to _any_ Feather main board. Using our [Feather Stacking...](https://www.adafruit.com/products/2830)

In Stock
[Buy Now](https://www.adafruit.com/product/2900)
[Related Guides to the Product](https://learn.adafruit.com/products/2900/guides)
![Angled shot of a Adafruit FeatherWing OLED - 128x32 OLED Add-on For Feather connected to a white breadboard and a lithium battery. ](https://cdn-shop.adafruit.com/640x480/2900-10.jpg)

If you happen to be an [AdaBox subscriber (what? you should be](https://www.adafruit.com/adabox)!) You can find these parts in your **Adabox 003** kit!

# Assembly
While I started on a breadboard, I ended up making a cute little sandwich without socket headers at all by connecting the OLED directly to the Feather HUZZAH

If you press the OLED headers against a table you can use a little solder to wick into each hole and have a perfectly flat bottom side. That will keep it from scratching your desk!

![hacks_top.jpg](https://cdn-learn.adafruit.com/assets/assets/000/049/763/medium640/hacks_top.jpg?1514695650)

![hacks_bottom.jpg](https://cdn-learn.adafruit.com/assets/assets/000/049/764/medium640/hacks_bottom.jpg?1514695656)

![hacks_soldered.jpg](https://cdn-learn.adafruit.com/assets/assets/000/049/765/medium640/hacks_soldered.jpg?1514695683)

# CircuitPython 2FA TOTP Authentication Friend

## Software

Follow our handy getting-started guide on CircuitPython and especially the [ESP8266 installation page/guide to learn how to install CircuitPython on your ESP8266 Feather](../../../../welcome-to-circuitpython/circuitpython-for-esp8266)

[Introduction To CircuitPython](https://learn.adafruit.com/welcome-to-circuitpython)
Flash the latest version of CircuitPython (you'll need v 2.2 or higher) and continue to the next step!

![](https://cdn-learn.adafruit.com/assets/assets/000/049/751/medium800/hacks_flash.png?1514688114)

# Installing and using ampy

We're using the ESP8266 Feather which means it has lots of memory and Internet capability. We use the Internet part to get the current time. However, this Feather is not as easy to use as the SAMD series, as it _does not show up as a disk drive!_

**You'll need to use ampy to install the circuitpython scripts!**

[Install and learn how to use ampy](https://learn.adafruit.com/micropython-basics-load-files-and-run-code/)
# Install boot.py

Once you've gotten ampy working save the following to your computer as **boot.py** and upload it so that you don't have to turn off the os debug output via REPL anymore

```
import esp
esp.osdebug(None)
```

![](https://cdn-learn.adafruit.com/assets/assets/000/049/752/medium800/hacks_bootpy.png?1514691741)

# Install libraries

You'll need a bunch of libraries to get the OLED working. Use ampy to create a directory called **lib**

Then [download the latest library bundle](../../../../welcome-to-circuitpython/circuitpython-libraries)

You'll need to upload **adafruit\_ssd1306.mpy** , and the&nbsp; **adafruit\_bus\_device** and&nbsp; **adafruit\_register** folders to the&nbsp; **lib** folder. Then check with ampy's `ls` command to verify all&nbsp; your files are in place!

![](https://cdn-learn.adafruit.com/assets/assets/000/049/753/medium800/hacks_libs.png?1514692155)

# Main Sketch

Now you can download the following script to your computer and save it as **code.py**

_Don't upload it via ampy yet! The current file has fake tokens in it that need to be set!_

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPy_OTP/code.py

# Set Up Networking

Before uploading, change these two lines to your network SSID and password

```
ssid     = 'my_wifi_ssid'
password = 'my_wifi_password'
```

# Set Up Tokens

You'll also need to get 2 factor "authenticator tokens/secrets". Each site is a little different about how it does this.

For example, when you set up GMail for 2FA it will show you a QR code like this:

![](https://cdn-learn.adafruit.com/assets/assets/000/049/754/medium800/hacks_qr.png?1514692358 This is not the real token from my gmail)

Which is great for phones. For us, we need the base32-encoded token. Click the **Can't Scan It?** link or otherwise request the text token. You'll get a page like this

![](https://cdn-learn.adafruit.com/assets/assets/000/049/755/medium800/hacks_barcode.png?1514692434 This is not the real token from my gmail)

That string of letters and numbers may be uppercase or lower case, it may also be 16 digits or 24 or 32 or some other qty. It doesn't matter! Grab that string, and remove the spaces so its one long string like&nbsp;`"ra4ndd2utltotseol564z3jijj5jo677"` Note that the number 0 and number 1 never appear so anything that looks like an `O`, `l` or an `I` is a letter.

Now edit this section of the code, you can display up to 3 accounts on a Feather OLED. If you pad the name with spaces the numbers will be right-justified but its not important, I'm just picky

```
totp = [("Discord ", 'JBSWY3DPEHPK3PXP'), # https://github.com/pyotp/pyotp exmple
        ("Gmail   ", 'abcdefghijklmnopqrstuvwxyz234567'),
("Accounts", 'asfdkwefoaiwejfa323nfjkl')]
```

If you want to test the setup first, you can keep the Discord entry which is the "PyOTP" example token. Then scan this with your phone in Authy or Google Authenticator

![](https://cdn-learn.adafruit.com/assets/assets/000/049/756/medium800/hacks_pyotpqr.png?1514692753)

# Test It Out!

OK once you've set everything up lets test!

Run the program directly on the Feather with OLED attached `using ampy --port portname run main.py`

You'll see it connect to your local network, get the time via NTP, then calculate and display OTP codes both on the OLED and on the serial port (you'll need to wait till the program is done to see the serial output)

![](https://cdn-learn.adafruit.com/assets/assets/000/049/759/medium800/hacks_breadboard.jpg?1514693495)

![](https://cdn-learn.adafruit.com/assets/assets/000/049/757/medium800/hacks_runmain.png?1514693188)

Check against your phone to make sure the codes are correct. Once you're satisfied, tweak the two lines to change the behavior

```
ALWAYS_ON = False  # Set to true if you never want to go to sleep!
ON_SECONDS = 60 # how long to stay on if not in always_on mode
```

Then finalize by uploading main.py with ampy's `put` command

![](https://cdn-learn.adafruit.com/assets/assets/000/049/758/medium800/hacks_putmain.png?1514693318)


## Featured Products

### Adafruit Feather HUZZAH with ESP8266 - Loose Headers

[Adafruit Feather HUZZAH with ESP8266 - Loose Headers](https://www.adafruit.com/product/2821)
Feather is the new development board from Adafruit, and like its namesake, it is thin, light, and lets you fly! We designed Feather to be a new standard for portable microcontroller cores.

This is the&nbsp; **Adafruit Feather HUZZAH ESP8266** &nbsp;- our take on an...

In Stock
[Buy Now](https://www.adafruit.com/product/2821)
[Related Guides to the Product](https://learn.adafruit.com/products/2821/guides)
### Adafruit FeatherWing OLED - 128x32 OLED Add-on For Feather

[Adafruit FeatherWing OLED - 128x32 OLED Add-on For Feather](https://www.adafruit.com/product/2900)
A Feather board without ambition is a Feather board without FeatherWings! This is the **FeatherWing OLED** : it adds a 128x32 monochrome OLED plus 3 user buttons to _any_ Feather main board. Using our [Feather Stacking...](https://www.adafruit.com/products/2830)

In Stock
[Buy Now](https://www.adafruit.com/product/2900)
[Related Guides to the Product](https://learn.adafruit.com/products/2900/guides)
### Assembled Adafruit Feather HUZZAH with ESP8266 With Headers

[Assembled Adafruit Feather HUZZAH with ESP8266 With Headers](https://www.adafruit.com/product/3046)
Feather is the flagship development board from Adafruit, and like its namesake, it is thin, light, and lets you fly! We designed Feather to be a new standard for portable microcontroller cores.

This is the **Assembled Adafruit Feather HUZZAH ESP8266**  **with...**

In Stock
[Buy Now](https://www.adafruit.com/product/3046)
[Related Guides to the Product](https://learn.adafruit.com/products/3046/guides)
### Assembled Feather HUZZAH w/ ESP8266 WiFi With Stacking Headers

[Assembled Feather HUZZAH w/ ESP8266 WiFi With Stacking Headers](https://www.adafruit.com/product/3213)
Feather is the new development board from Adafruit, and like its namesake, it is thin, light, and lets you fly! We designed Feather to be a new standard for portable microcontroller cores.

This is the&nbsp; **Adafruit Feather HUZZAH ESP8266** &nbsp;- our take on an...

In Stock
[Buy Now](https://www.adafruit.com/product/3213)
[Related Guides to the Product](https://learn.adafruit.com/products/3213/guides)
### AdaBox003 – The World of IoT – Curated by Digi-Key

[AdaBox003 – The World of IoT – Curated by Digi-Key](https://www.adafruit.com/product/3268)
**AdaBox003 –&nbsp;The World of IoT (Curated by Digi-Key)** is the perfect gift for folks who are just getting started in the world of DIY electronics. It's an excellent&nbsp;introduction to our wide world of the [Internet...](https://www.adafruit.com/category/342)

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/3268)
[Related Guides to the Product](https://learn.adafruit.com/products/3268/guides)
### AdaBox Subscription

[AdaBox Subscription](https://www.adafruit.com/adabox_get_started)
Subscription
[Buy Now](https://www.adafruit.com/adabox_get_started)
[Related Guides to the Product](https://learn.adafruit.com/products/3067/guides)
### Assembled Adafruit FeatherWing OLED

[Assembled Adafruit FeatherWing OLED](https://www.adafruit.com/product/3045)
A Feather board without ambition is a Feather board without FeatherWings! This is the **Assembled FeatherWing OLED** : it adds a 128x32 monochrome OLED plus 3 user buttons to _any_ Feather main board. Comes fully assembled so you can connect a FeatherWing on top of your...

In Stock
[Buy Now](https://www.adafruit.com/product/3045)
[Related Guides to the Product](https://learn.adafruit.com/products/3045/guides)

## Related Guides

- [Adafruit Feather HUZZAH ESP8266](https://learn.adafruit.com/adafruit-feather-huzzah-esp8266.md)
- [Adafruit OLED FeatherWing](https://learn.adafruit.com/adafruit-oled-featherwing.md)
- [Integrating Home Assistant with Adafruit IO](https://learn.adafruit.com/integrating-adafruit-io-with-home-assistant.md)
- [All the Internet of Things - Episode Three: Services](https://learn.adafruit.com/all-the-internet-of-things-episode-three-services.md)
- [Adafruit IO Home: Lights and Temperature ](https://learn.adafruit.com/adafruit-io-house-lights-and-temperature.md)
- [Feather Weather Lamp](https://learn.adafruit.com/feather-weather-lamp.md)
- [Personalized NextBus ESP8266 Transit Clock](https://learn.adafruit.com/personalized-esp8266-transit-clock.md)
- [MicroPython Hardware: I2C Devices](https://learn.adafruit.com/micropython-hardware-i2c-devices.md)
- [DIY ESP8266 Home Security with Lua and MQTT](https://learn.adafruit.com/diy-esp8266-home-security-with-lua-and-mqtt.md)
- [MicroPython Hardware: SPI Devices](https://learn.adafruit.com/micropython-hardware-spi-devices.md)
- [Build a Cloud-Connected ESP8266 Power Meter](https://learn.adafruit.com/build-a-cloud-connected-esp8266-power-meter.md)
- [USB MIDI Host Messenger](https://learn.adafruit.com/usb-midi-host-messenger.md)
- [3D Printed Case for Adafruit Feather](https://learn.adafruit.com/3d-printed-case-for-adafruit-feather.md)
- [MAC Address Finder](https://learn.adafruit.com/mac-address-finder.md)
- [Schluff - The Sleep Monitor](https://learn.adafruit.com/schluff-the-oshw-sleep-monitor.md)
- [Using Crickit and Adafruit IO together](https://learn.adafruit.com/crickit-and-adafruitio.md)
- [Smart Toilet Light](https://learn.adafruit.com/smart-toilet-light.md)
