🪄 Formats ArduinoJsonJWT
This commit is contained in:
@@ -14,19 +14,11 @@
|
|||||||
|
|
||||||
#include "ArduinoJsonJWT.h"
|
#include "ArduinoJsonJWT.h"
|
||||||
|
|
||||||
ArduinoJsonJWT::ArduinoJsonJWT(String secret) : _secret(secret)
|
ArduinoJsonJWT::ArduinoJsonJWT(String secret) : _secret(secret) {}
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void ArduinoJsonJWT::setSecret(String secret)
|
void ArduinoJsonJWT::setSecret(String secret) { _secret = secret; }
|
||||||
{
|
|
||||||
_secret = secret;
|
|
||||||
}
|
|
||||||
|
|
||||||
String ArduinoJsonJWT::getSecret()
|
String ArduinoJsonJWT::getSecret() { return _secret; }
|
||||||
{
|
|
||||||
return _secret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* ESP32 uses mbedtls,
|
* ESP32 uses mbedtls,
|
||||||
@@ -35,8 +27,7 @@ String ArduinoJsonJWT::getSecret()
|
|||||||
*
|
*
|
||||||
* No need to pull in additional crypto libraries - lets use what we already have.
|
* No need to pull in additional crypto libraries - lets use what we already have.
|
||||||
*/
|
*/
|
||||||
String ArduinoJsonJWT::sign(String &payload)
|
String ArduinoJsonJWT::sign(String &payload) {
|
||||||
{
|
|
||||||
unsigned char hmacResult[32];
|
unsigned char hmacResult[32];
|
||||||
{
|
{
|
||||||
mbedtls_md_context_t ctx;
|
mbedtls_md_context_t ctx;
|
||||||
@@ -51,8 +42,7 @@ String ArduinoJsonJWT::sign(String &payload)
|
|||||||
return encode((char *)hmacResult, 32);
|
return encode((char *)hmacResult, 32);
|
||||||
}
|
}
|
||||||
|
|
||||||
String ArduinoJsonJWT::buildJWT(JsonObject &payload)
|
String ArduinoJsonJWT::buildJWT(JsonObject &payload) {
|
||||||
{
|
|
||||||
// serialize, then encode payload
|
// serialize, then encode payload
|
||||||
String jwt;
|
String jwt;
|
||||||
serializeJson(payload, jwt);
|
serializeJson(payload, jwt);
|
||||||
@@ -67,29 +57,25 @@ String ArduinoJsonJWT::buildJWT(JsonObject &payload)
|
|||||||
return jwt;
|
return jwt;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ArduinoJsonJWT::parseJWT(String jwt, JsonDocument &jsonDocument)
|
void ArduinoJsonJWT::parseJWT(String jwt, JsonDocument &jsonDocument) {
|
||||||
{
|
|
||||||
// clear json document before we begin, jsonDocument wil be null on failure
|
// clear json document before we begin, jsonDocument wil be null on failure
|
||||||
jsonDocument.clear();
|
jsonDocument.clear();
|
||||||
|
|
||||||
// must have the correct header and delimiter
|
// must have the correct header and delimiter
|
||||||
if (!jwt.startsWith(JWT_HEADER) || jwt.indexOf('.') != JWT_HEADER_SIZE)
|
if (!jwt.startsWith(JWT_HEADER) || jwt.indexOf('.') != JWT_HEADER_SIZE) {
|
||||||
{
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check there is a signature delimieter
|
// check there is a signature delimieter
|
||||||
int signatureDelimiterIndex = jwt.lastIndexOf('.');
|
int signatureDelimiterIndex = jwt.lastIndexOf('.');
|
||||||
if (signatureDelimiterIndex == JWT_HEADER_SIZE)
|
if (signatureDelimiterIndex == JWT_HEADER_SIZE) {
|
||||||
{
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check the signature is valid
|
// check the signature is valid
|
||||||
String signature = jwt.substring(signatureDelimiterIndex + 1);
|
String signature = jwt.substring(signatureDelimiterIndex + 1);
|
||||||
jwt = jwt.substring(0, signatureDelimiterIndex);
|
jwt = jwt.substring(0, signatureDelimiterIndex);
|
||||||
if (sign(jwt) != signature)
|
if (sign(jwt) != signature) {
|
||||||
{
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,22 +85,19 @@ void ArduinoJsonJWT::parseJWT(String jwt, JsonDocument &jsonDocument)
|
|||||||
|
|
||||||
// parse payload, clearing json document after failure
|
// parse payload, clearing json document after failure
|
||||||
DeserializationError error = deserializeJson(jsonDocument, jwt);
|
DeserializationError error = deserializeJson(jsonDocument, jwt);
|
||||||
if (error != DeserializationError::Ok || !jsonDocument.is<JsonObject>())
|
if (error != DeserializationError::Ok || !jsonDocument.is<JsonObject>()) {
|
||||||
{
|
|
||||||
jsonDocument.clear();
|
jsonDocument.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String ArduinoJsonJWT::encode(const char *cstr, int inputLen)
|
String ArduinoJsonJWT::encode(const char *cstr, int inputLen) {
|
||||||
{
|
|
||||||
// prepare encoder
|
// prepare encoder
|
||||||
base64_encodestate _state;
|
base64_encodestate _state;
|
||||||
base64_init_encodestate(&_state);
|
base64_init_encodestate(&_state);
|
||||||
size_t encodedLength = base64_encode_expected_len(inputLen) + 1;
|
size_t encodedLength = base64_encode_expected_len(inputLen) + 1;
|
||||||
// prepare buffer of correct length, returning an empty string on failure
|
// prepare buffer of correct length, returning an empty string on failure
|
||||||
char *buffer = (char *)malloc(encodedLength * sizeof(char));
|
char *buffer = (char *)malloc(encodedLength * sizeof(char));
|
||||||
if (buffer == nullptr)
|
if (buffer == nullptr) {
|
||||||
{
|
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,8 +112,7 @@ String ArduinoJsonJWT::encode(const char *cstr, int inputLen)
|
|||||||
buffer = nullptr;
|
buffer = nullptr;
|
||||||
|
|
||||||
// remove padding and convert to URL safe form
|
// remove padding and convert to URL safe form
|
||||||
while (value.length() > 0 && value.charAt(value.length() - 1) == '=')
|
while (value.length() > 0 && value.charAt(value.length() - 1) == '=') {
|
||||||
{
|
|
||||||
value.remove(value.length() - 1);
|
value.remove(value.length() - 1);
|
||||||
}
|
}
|
||||||
value.replace('+', '-');
|
value.replace('+', '-');
|
||||||
@@ -140,8 +122,7 @@ String ArduinoJsonJWT::encode(const char *cstr, int inputLen)
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
String ArduinoJsonJWT::decode(String value)
|
String ArduinoJsonJWT::decode(String value) {
|
||||||
{
|
|
||||||
// convert to standard base64
|
// convert to standard base64
|
||||||
value.replace('-', '+');
|
value.replace('-', '+');
|
||||||
value.replace('_', '/');
|
value.replace('_', '/');
|
||||||
|
|||||||
@@ -21,9 +21,8 @@
|
|||||||
#include <libb64/cencode.h>
|
#include <libb64/cencode.h>
|
||||||
#include <mbedtls/md.h>
|
#include <mbedtls/md.h>
|
||||||
|
|
||||||
class ArduinoJsonJWT
|
class ArduinoJsonJWT {
|
||||||
{
|
private:
|
||||||
private:
|
|
||||||
String _secret;
|
String _secret;
|
||||||
|
|
||||||
const String JWT_HEADER = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9";
|
const String JWT_HEADER = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9";
|
||||||
@@ -34,7 +33,7 @@ private:
|
|||||||
static String encode(const char *cstr, int len);
|
static String encode(const char *cstr, int len);
|
||||||
static String decode(String value);
|
static String decode(String value);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ArduinoJsonJWT(String secret);
|
ArduinoJsonJWT(String secret);
|
||||||
|
|
||||||
void setSecret(String secret);
|
void setSecret(String secret);
|
||||||
|
|||||||
Reference in New Issue
Block a user