28.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 28-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 Oracle Database 12c or higher. Other signature algorithms are not supported.

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;