jwtEncode (Function)

Top  Previous  Next

Architecture:

NX32L

Firmware version:

1.70.00


jwtEncode encodes a JWT object into a token.

It uses the encryption algorithm specified in jwtAlgSetAsym and jwtAlgSetSym.

 

Input:

jwt : SYSHANDLE

A handle to the JWT object to encode.´

 

Output:

token : STRING

The generated token.

 

Returns: INT

1

- Success

0

- Function is not supported.

-2

- Could not find JWT.

-99

- Failed to encode JWT.

 

 

Declaration:

FUNCTION jwtEncode : INT;
VAR_INPUT
   jwt     : SYSHANDLE;
   token   : ACCESS STRING;
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;
   
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);
 
   // Create empty JWT object with default header
   rc := jwtCreate(jwt:=jwt);
   DebugFmt(message:="jwtCreate: \1", v1:=rc);
 
   // Add claim content
   rc := jwtClaimAddDint(jwt:=jwtname:="iat", value:=1516239022);
   DebugFmt(message:="jwtClaimAddDint: \1", v1:=rc);
 
   rc := jwtClaimAdd(jwt:=jwtname:="name", value:="John Doe");
 
   DebugFmt(message:="jwtClaimAdd: \1", v1:=rc);
 
   rc := jwtClaimAddDint(jwt:=jwtname:="sub", value:=1234567890);
 
   DebugFmt(message:="jwtClaimAddDint: \1", v1:=rc);
 
   // set the encryption type
   rc := jwtAlgSetSym(jwt:=jwttype := 1key := ADDR(key), key_len := key_len);
   DebugFmt(message:="jwtSetAlgSym: \1", v1:=rc);
 
   // Generate token from the JWT object
   rc := jwtEncode(jwt:=jwttoken:=str);
   DebugFmt(message:="jwtEncode: \1, ", v1:=rc);
 
   DebugMsg(message:="--- Token ---");
   DebugMsg(message:=str);
 
   // clean up
   rc := jwtFree(jwt:=jwt);
 
BEGIN
// Code from this point until END will be executed repeatedly
 
 
END;
END_PROGRAM;