jwtDecodeSym (Function)

Top  Previous  Next

Architecture:

NX32L

Firmware version:

1.70.00


jwtDecodeSym decodes a token into a JWT object.

jwtDecodeSym is used for symmetrical encryption and unencrypted tokens. For asymmetrical encryption, see jwtDecodeAsym.

 

When the JWT object is no longer needed, it must be released using jwtFree.

 

Note: To be sure that the signature was valid, it is important to check that it used the expected encryption algorithm with jwtAlgGet.

 

Input:

token : STRING

The token to decode.

 

key : PTR

The key to use for decoding the token. If not provided, no validation of the signature will be done.

 

key_len : INT

The size of the key.

 

Output:

jwt : SYSHANDLE

A handle to the decoded JWT object.

 

Returns: INT

1

- Success

0

- Function is not supported.

-1

- Invalid token.

-2

- Could not allocate JWT, there may be too many in use.

-99

- Failed to decode JWT.

 

 

Declaration:

FUNCTION jwtDecodeSym : INT;
VAR_INPUT
   jwt     : ACCESS SYSHANDLE;
   token   : STRING;
   key     : PTR;
   key_len : INT;
END_VAR;

 

 

Example:

INCLUDE rtcu.inc
 
PROGRAM ex;
// These are the local variables of the program block
VAR
   rc       : INT;
   str      : STRING;
   jwt      : SYSHANDLE;
   key      : ARRAY[1..32OF BYTE;
   key_len  : INT;
   token    : STRING;
   
END_VAR;

 
   // The next code will only be executed once after the program starts
 
   // Set up key
   key_len := strLen(str:="your-256-bit-secret");
   strToMemory(dst:=ADDR(key), str:="your-256-bit-secret", len := key_len);
 
   token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c";
 
   // Create JWT object from token
   rc := jwtDecodeSym(jwt:=jwttoken:=tokenkey := ADDR(key), key_len := key_len);
   DebugFmt(message:="jwtDecodeSym(): \1, ", v1:=rc);
 
   IF rc = 1 THEN
      // Show the used algorithm
      rc := jwtAlgGet(jwt:=jwt);
      DebugFmt(message:="jwtGetAlg: \1", v1:=rc);
 
      // Print header
      rc := jwtHeaderGetJSON(jwt:=jwtvalue:=str);
      DebugFmt(message:="jwtHeaderGetJSON: \1, "+strv1:=rc);
 
      // Print claim
      rc := jwtClaimGetJSON(jwt:=jwtvalue:=str);
      DebugFmt(message:="jwtClaimGetJSON: \1, "+strv1:=rc);
 
      // clean up
      rc := jwtFree(jwt:=jwt);
      DebugFmt(message:="jwtFree: \1", v1:=rc);
   END_IF;
 
BEGIN
// Code from this point until END will be executed repeatedly
 
 
END;
END_PROGRAM;