The JWT class allows you to generate, decode, and validate JSON Web Tokens (JWTs) to authenticate users and secure API calls. JWTs are widely used in modern web authentication systems, including OAuth2 and OpenID Connect.
This class is typically used in three scenarios:
This class is instantiated using the cs.NetKit.JWT.new() function.
Note: Shared objects are not supported by the 4D NetKit API.
cs.NetKit.JWT.new ( key : Text or Object ) : cs.NetKit.JWT
Creates a new instance of the JWT class.
| Parameter | Type | Description |
|---|---|---|
| key | Text/Object | Optional. If text → Key in PEM format. - If object → Must be an object returned by 4D.CryptoKey.If it’s a private key, the public key will be inferred. |
var $jwt := cs.NetKit.JWT.new($key)
JWT.decode ( token : Text ) : Object
| Parameter | Type | Description | |
|---|---|---|---|
| token | Text | -> | JWT string to decode |
| Result | Object | <- | The decoded content of the JWT |
Decodes a JWT string and returns its components (header, payload, signature).
The function returns an object containing the following properties:
| Property | Type | Description |
|---|---|---|
| header | Object | Metadata about the token type and the signing algorithm |
| payload | Object | The information (claims) of the token like the user’s name, role, user ID, or expiration date. |
| signature | Object | Ensures the integrity of the token and verifies the sender’s authenticity |
var $result := cs.NetKit.JWT.new().decode($token)
JWT.generate ( params : Object { ; privateKey : Text or Object } ) : Text
| Parameter | Type | Description | |
|---|---|---|---|
| params | Object | -> | Options for the JWT content |
| privateKey | Text/Object | -> | Optional. If text → Private key in PEM format. - If object → Must be returned by 4D.CryptoKey.If omitted, the key passed to JWT.new() will be used. |
| Result | Text | <- | The generated JWT token |
Generates a signed JWT based on the provided parameters and optional private key.
In params, you can pass several properties:
| Property | Type | Description | |
|---|---|---|---|
| header | Object | (optional) Metadata about the token | |
| header.alg | Text | Signing algorithm. Defaults to "RS256" if not specified |
|
| header.typ | Text | Token type. Defaults to "JWT" if not specified |
|
| payload | Object | The claims/information you want to include in the token |
var $params:={header: {alg: "HS256"; typ: "JWT"}}
$params.payload:={sub: "123456789"; name: "John"; exp : 50}
var $token := cs.NetKit.JWT.new().generate($params; $privateKey)
JWT.validate ( token : Text { ; key : Text or Object } ) : Boolean
| Parameter | Type | Description | |
|---|---|---|---|
| token | Text | -> | JWT token to validate |
| key | Text | -> | Optional. If text → Private or public key in PEM format. - If object → Must be returned by 4D.CryptoKey.If omitted, the key passed to JWT.new() will be used. |
| Result | Boolean | <- | True if the token is valid, False otherwise |
Validates a JWT token using the provided public key or the key passed to the constructor.
var $isValid:= cs.NetKit.JWT.new().validate($token; $key)