Library for encoding with asymmetrically signed JSON web tokens for the Arduino and ESP8266 platforms. Only supports P-256 Elliptic Curve keys.
- Download the latest release from the releases page.
- Unzip the downloaded file.
- Rename the folder to
AsymJwt
. - Move the folder to your Arduino libraries folder.
- Add the library to your
platformio.ini
file:
lib_deps =
qube-ai/AsymJwt
- Install openssl
- Install fish shell (optional)
- Run
gen_keys.fish
orgen_keys.sh
script in thescripts
folder.
fish gen_keys.fish
output:
Generating P-256 Elliptic Curve key pair
Generating private key
Enter device name:
read> Test-2
read EC key
writing EC key
read EC key
writing EC key
Keys have been created.
- Upload the private key to the device in DER format.
- Keep the public key in a safe place.
- Open the Arduino IDE.
- Open the
Tools
menu. - Select
ESP8266 Sketch Data Upload
. - Select the private key file.
- Click
Upload
.
- Create a
data
folder in the root of your project. - Copy the private key file into the
data
folder. (The above script will create aprivate-key.der
file insidedata/
.) - Add the following to your
platformio.ini
file (Optional):
[env:your_env]
build_flags = -D PIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH
upload_flags = --upload-port $UPLOAD_PORT --upload-file data/private-key.der
or
- Go to PlatformIO Home > your_env > Platform > Build Filesystem Image > Upload Filesystem Image
#include <Arduino.h>
#include <AsymJwt.h>
void setup()
{
Serial.begin(9600);
Serial.println("Reading private ket....");
while (!AsymJWT::readPrivKey("/private-key.der"))
{
Serial.println("Failed to read private key. Retrying in 2 seconds");
delay(2000);
}
}
void loop()
{
time_t iat = time(nullptr);
String payload = "{\"iat\":" + String(iat) + ",\"exp\":" + String(iat + 300) + ",\"thing_id\":\"" + "THING_ID" + "\"}";
Serial.println(AsymJWT::generateJwt(payload));
delay(200000);
Serial.println("Refreshing JWT.");
}
This project is licensed under the UnLicense - see the LICENSE file for details
-
Enable debug mode by defining
ASYMJWT_DEBUG
before including the library.#define ASYMJWT_DEBUG 1 #include <AsymJwt.h>
- Add support for decoding JWTs (currently only encoding is supported)
- Add support for other key types
- Add support for other hashing algorithms