JWT JwtAuthenticator for embedded applications. More...
#include <JwtAuthenticator.h>
Public Member Functions | |
JwtAuthenticator () | |
Get an instance of the JwtAuthenticator. | |
void | init (const std::string &secret, int expirySeconds) |
Initialize the JwtAuthenticator with a secret key and expiry time. | |
std::string | generateJWT (const std::string &userId, const std::string &userName) const |
Generate a JWT for a given user. | |
bool | validateJWT (const std::string &token, bool validateExpiry=false) const |
Validate a JWT's signature and optionally its expiration. | |
bool | decodeJWT (const std::string &token, std::string &header, std::string &payload, std::string &signature) const |
Decode a JWT into its components. | |
bool | isJWTExpired (const std::string &token) const |
Check if a JWT is expired based on the exp claim. | |
bool | isJWTPayloadExpired (const std::string &payload) const |
Check if a decoded JWT payload is expired. | |
bool | verifyJWTSignature (const std::string &encoded_header, const std::string &encoded_payload, const std::string &signature) const |
Verify a JWT's signature using HMAC-SHA256. | |
Private Member Functions | |
std::string | base64urlEncode (const std::string &input) const |
Encode a string using base64url encoding. | |
bool | base64urlDecode (const std::string &input, std::string &output) const |
Decode a base64url-encoded string. | |
bool | isBase64urlEncoded (const std::string &str) const |
Check if a string is valid base64url. | |
std::string | bytesToBase64url (const unsigned char *data, size_t length) const |
Convert a byte buffer to a base64url string. | |
std::string | hmacSHA256 (const std::string &message) const |
Calculate HMAC-SHA256 for a given message. | |
Private Attributes | |
std::string | secretKey |
Construct a new JwtAuthenticator object. | |
std::string | expiryTime |
This class provides helper methods to generate and validate JWT tokens using HMAC-SHA256. It uses a singleton design pattern and is intended to be stateless aside from a secret key.
Definition at line 26 of file JwtAuthenticator.h.
JwtAuthenticator::JwtAuthenticator | ( | ) |
Get an instance of the JwtAuthenticator.
Definition at line 39 of file JwtAuthenticator.cpp.
References secretKey.
|
private |
Decode a base64url-encoded string.
input | Encoded input. |
output | Decoded output. |
input | Encoded input. |
output | Decoded output. |
Definition at line 72 of file JwtAuthenticator.cpp.
Referenced by decodeJWT(), isJWTExpired(), and validateJWT().
|
private |
Encode a string using base64url encoding.
input | Raw input string. |
input | Raw input string. |
Definition at line 45 of file JwtAuthenticator.cpp.
Referenced by generateJWT().
|
private |
Convert a byte buffer to a base64url string.
data | Byte array. |
length | Length of array. |
data | Byte array. |
length | Length of array. |
Definition at line 201 of file JwtAuthenticator.cpp.
Referenced by verifyJWTSignature().
bool JwtAuthenticator::decodeJWT | ( | const std::string & | token, |
std::string & | header, | ||
std::string & | payload, | ||
std::string & | signature | ||
) | const |
Decode a JWT into its components.
token | JWT string. |
header | Output decoded header JSON string. |
payload | Output decoded payload JSON string. |
signature | Output base64url-encoded signature. |
token | JWT string. |
header | Output decoded header JSON string. |
payload | Output decoded payload JSON string. |
signature | Output base64url-encoded signature. |
Definition at line 168 of file JwtAuthenticator.cpp.
References base64urlDecode(), and isBase64urlEncoded().
std::string JwtAuthenticator::generateJWT | ( | const std::string & | userId, |
const std::string & | userName | ||
) | const |
Generate a JWT for a given user.
userId | User ID (used as the sub claim). |
userName | User name (stored in name claim). |
userId | User ID (used as the sub claim). |
userName | User name (stored in name claim). |
Definition at line 150 of file JwtAuthenticator.cpp.
References base64urlEncode(), and hmacSHA256().
|
private |
Calculate HMAC-SHA256 for a given message.
message | Input string. |
message | Input string. |
Definition at line 133 of file JwtAuthenticator.cpp.
References MBEDTLS_SHA256_DIGEST_LENGTH, and secretKey.
Referenced by generateJWT().
void JwtAuthenticator::init | ( | const std::string & | secret, |
int | expirySeconds | ||
) |
secret | Secret key for HMAC-SHA256 signing. |
expirySeconds | Expiry time in seconds. |
Definition at line 299 of file JwtAuthenticator.cpp.
References expiryTime, and secretKey.
|
private |
Check if a string is valid base64url.
str | String to check. |
str | String to check. |
Definition at line 112 of file JwtAuthenticator.cpp.
Referenced by decodeJWT().
bool JwtAuthenticator::isJWTExpired | ( | const std::string & | token | ) | const |
Check if a JWT is expired based on the exp
claim.
token | JWT string. |
token | JWT string. |
Definition at line 257 of file JwtAuthenticator.cpp.
References base64urlDecode(), and isJWTPayloadExpired().
bool JwtAuthenticator::isJWTPayloadExpired | ( | const std::string & | payload | ) | const |
Check if a decoded JWT payload is expired.
payload | Decoded payload string (JSON). |
payload | Decoded payload string (JSON). |
Definition at line 239 of file JwtAuthenticator.cpp.
Referenced by isJWTExpired(), and validateJWT().
bool JwtAuthenticator::validateJWT | ( | const std::string & | token, |
bool | validateExpiry = false |
||
) | const |
Validate a JWT's signature and optionally its expiration.
token | JWT string. |
validateExpiry | Whether to check the exp claim. |
token | JWT string. |
validateExpiry | Whether to check the exp claim. |
Definition at line 273 of file JwtAuthenticator.cpp.
References base64urlDecode(), isJWTPayloadExpired(), and verifyJWTSignature().
bool JwtAuthenticator::verifyJWTSignature | ( | const std::string & | encoded_header, |
const std::string & | encoded_payload, | ||
const std::string & | signature | ||
) | const |
Verify a JWT's signature using HMAC-SHA256.
encoded_header | Base64url-encoded JWT header. |
encoded_payload | Base64url-encoded JWT payload. |
signature | Base64url-encoded signature to verify against. |
encoded_header | Base64url-encoded JWT header. |
encoded_payload | Base64url-encoded JWT payload. |
signature | Base64url-encoded signature to verify against. |
Definition at line 219 of file JwtAuthenticator.cpp.
References bytesToBase64url(), MBEDTLS_SHA256_DIGEST_LENGTH, and secretKey.
Referenced by validateJWT().
|
private |
Definition at line 99 of file JwtAuthenticator.h.
Referenced by init().
|
private |
Secret key is loaded from build config.
Definition at line 98 of file JwtAuthenticator.h.
Referenced by hmacSHA256(), init(), JwtAuthenticator(), and verifyJWTSignature().