22.2 ENCODE Function

This function encodes and optionally encrypts payload.

Syntax

function encode (
     p_iss           in varchar2                 default null,
     p_sub           in varchar2                 default null,
     p_aud           in varchar2                 default null,
     p_nbf_ts        in timestamp with time zone default null,
     p_iat_ts        in timestamp with time zone default systimestamp,
     p_exp_sec       in pls_integer              default null,
     p_jti           in varchar2                 default null,
     p_other_claims  in varchar2                 default null,
     p_signature_key in raw                      default null )
     return varchar2

Parameters

Table 22-2 ENCODE Function Parameters

Parameter Description

p_iss

Optional "iss" (Issuer) claim.

p_sub

Optional "sub" (Subject) claim.

p_aud

Optional "aud" (Audience) claim.

p_nbf_ts

Optional "nbf" (Not Before) claim.

p_iat_ts

Optional "iat" (Issued At) claim (default systimestamp).

p_exp_sec

Optional "exp" (Expiration Time) claim, in seconds. The start time is taken from "nbf", "iat" or current time.

p_jti

Optional "jti" (JWT ID) Claim.

p_other_claims

Optional raw JSON with additional claims.

p_signature_key

Optional MAC key for the signature. If not null, a 'HS256' signature is added. This requires 12c or higher.

Returns

A varchar2, the encoded token value.

Example

This example creates and prints a JWT value for Example User, intended to be used by Example JWT Recipient. The token is valid for 5 minutes.

declare
    l_jwt_value varchar2(32767);
begin
    l_jwt_value := apex_jwt.encode (
                       p_iss => 'Example Issuer',
                       p_sub => 'Example User',
                       p_aud => 'Example JWT Recipient',
                       p_exp_sec => 60*5,
                       p_other_claims => '"name1": '||apex_json.stringify('value1')||
                                         ',"name2": '||apex_json.stringify('value2'),
                       p_signature_key => ... encryption key ... );
    sys.dbms_output.put_line(l_jwt_value);
end;