Skip to content

hamid-rostami/ucjwt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JWT implemention for Microcontroller

Encode and decode JWT tokens. Lightweight and no need to external library, suitable for using in microcontrollers.

At this moment, only HS256 algorithm supported.

Sample usage

Encode

char *data = "{\"uname\":\"user\"}";
char *key = "secretkey";
char *token;

token = jwt_encode(data, strlen(data), key, strlen(key));
/* your codes ... */
free(token)

Compile example

For compile and run example:

cd example
cmake .
make
./example

Decode

char data[100];
JWTDecode decode_result;
char *key = "secretkey";

decode_result = jwt_decode(token, strlen(token),
                           key, strlen(key),
                           data, sizeof(data));

decode_result is a typedef in jwt.h file:

typedef enum {
  JWTDecode_Verified = 0,      // Everything is fine
  JWTDecode_NotVerified = -1,  // Signture verification failed
  JWTDecode_BadToken = -2,     // Bad JWT Token
  JWTDecode_NoBufSpace = -3    // No input buffer space to copy decoded payload
} JWTDecode;

jwt_decode output explaination:

  • JWTDecode_Verified: Token signture verified successfully and data copied to given buffer.

  • JWTDecode_NotVerified: Token signture is wrong, but data copied to given buffer.

  • JWTDecode_BadToken: Given token is not in JWT format.

  • JWTDecode_NoBufSpace: No enough space to copy token's payload data