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;
VAR
rc : INT;
str : STRING;
jwt : SYSHANDLE;
key : ARRAY[1..32] OF BYTE;
key_len : INT;
END_VAR;
key_len := strLen(str:="your-256-bit-secret");
strToMemory(dst:=ADDR(key), str:="your-256-bit-secret", len := key_len);
rc := jwtCreate(jwt:=jwt);
DebugFmt(message:="jwtCreate: \1", v1:=rc);
rc := jwtClaimAddDint(jwt:=jwt, name:="iat", value:=1516239022);
DebugFmt(message:="jwtClaimAddDint: \1", v1:=rc);
rc := jwtClaimAdd(jwt:=jwt, name:="name", value:="John Doe");
DebugFmt(message:="jwtClaimAdd: \1", v1:=rc);
rc := jwtClaimAddDint(jwt:=jwt, name:="sub", value:=1234567890);
DebugFmt(message:="jwtClaimAddDint: \1", v1:=rc);
rc := jwtAlgSetSym(jwt:=jwt, type := 1, key := ADDR(key), key_len := key_len);
DebugFmt(message:="jwtSetAlgSym: \1", v1:=rc);
rc := jwtEncode(jwt:=jwt, token:=str);
DebugFmt(message:="jwtEncode: \1, ", v1:=rc);
DebugMsg(message:="--- Token ---");
DebugMsg(message:=str);
rc := jwtFree(jwt:=jwt);
BEGIN
END;
END_PROGRAM;
|