|
| 1 | +# JWT Class |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +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. |
| 6 | + |
| 7 | +This class is typically used in three scenarios: |
| 8 | + |
| 9 | +* **Token generation**: Create a signed JWT when a user logs in. |
| 10 | +* **Token decoding**: Read and inspect a JWT received from an authentication provider. |
| 11 | +* **Token validation**: Verify the JWT’s signature and expiration before granting access to protected resources. |
| 12 | + |
| 13 | +This class can be instantiated using the `cs.NetKit.JWT.new()` function. |
| 14 | + |
| 15 | +**Note:** Shared objects are not supported by the 4D NetKit API. |
| 16 | + |
| 17 | +## Table of contents |
| 18 | + |
| 19 | +* [Initialization](#csnetkitjwtnew) |
| 20 | +* [JWT.encode()](#jwtencode) |
| 21 | +* [JWT.decode()](#jwtdecode) |
| 22 | +* [JWT.isValid()](#jwtisvalid) |
| 23 | + |
| 24 | + |
| 25 | +## **cs.NetKit.JWT.new()** |
| 26 | + |
| 27 | +**cs.NetKit.JWT.new()** : `cs.NetKit.JWT` |
| 28 | + |
| 29 | +Creates a new instance of the JWT class. |
| 30 | + |
| 31 | +### Example |
| 32 | + |
| 33 | +```4d |
| 34 | +var $jwt := cs.NetKit.JWT.new() |
| 35 | +``` |
| 36 | + |
| 37 | +## JWT.decode() |
| 38 | + |
| 39 | +**JWT.decode** ( *token* : Text ) : Object |
| 40 | + |
| 41 | +### Parameters |
| 42 | + |
| 43 | +| Parameter | Type | | Description | |
| 44 | +|-----------|----- |:---:|----------------- | |
| 45 | +| token | Text |->| JWT string to decode | |
| 46 | +| Result | Object |<-|The decoded content of the JWT | |
| 47 | + |
| 48 | +### Description |
| 49 | + |
| 50 | +Decodes a JWT string and returns its components (header, payload, signature). |
| 51 | + |
| 52 | +### Returned object |
| 53 | + |
| 54 | +The function returns an object containing the following properties: |
| 55 | + |
| 56 | +| Property | Type | Description | |
| 57 | +|---|---|---| |
| 58 | +|header| Object |Metadata about the token type and the signing algorithm | |
| 59 | +|payload| Object |The information (claims) of the token like the user's name, role, user ID, or expiration date.| |
| 60 | +|signature| Object |Ensures the integrity of the token and verifies the sender’s authenticity| |
| 61 | + |
| 62 | +### Example |
| 63 | + |
| 64 | +```4d |
| 65 | +
|
| 66 | +var $result := cs.NetKit.JWT.new().decode($token) |
| 67 | +
|
| 68 | +``` |
| 69 | + |
| 70 | +## JWT.generate() |
| 71 | + |
| 72 | +**JWT.generate** ( *params* : Object ; *privateKey* : Text ) : Text |
| 73 | + |
| 74 | +### Parameters |
| 75 | + |
| 76 | +| Parameter | Type | | Description | |
| 77 | +|------------|--------|:--:|--------------------------------------------------------------| |
| 78 | +| params | Object | ->| Options for the JWT content| |
| 79 | +| privateKey | Text | ->| Private key used to sign the JWT | |
| 80 | +| Result | Text | <-| The generated JWT token | |
| 81 | + |
| 82 | +### Description |
| 83 | + |
| 84 | +Generates a signed JWT based on the provided parameters and private key. |
| 85 | + |
| 86 | +In *params*, you can pass several properties: |
| 87 | + |
| 88 | +| Property | | Type | Description | |
| 89 | +|----------|--|------|-------------| |
| 90 | +| header | |Object | *(optional)* Metadata about the token | |
| 91 | +| | header.alg |Text |Signing algorithm. Defaults to `"RS256"` if not specified | |
| 92 | +| | header.typ |Text | Token type. Defaults to `"JWT"` if not specified| |
| 93 | +| payload | | Object | The claims/information you want to include in the token| |
| 94 | + |
| 95 | +### Example |
| 96 | + |
| 97 | +```4d |
| 98 | +
|
| 99 | +var $claims:={header: {alg: "HS256"; typ: "JWT"}} |
| 100 | +$claims.payload:={sub: "123456789"; name: "John"; exp : 50} |
| 101 | +
|
| 102 | +var $token := cs.NetKit.JWT.new().generate($claims; $privateKey) |
| 103 | +
|
| 104 | +``` |
| 105 | + |
| 106 | +## JWT.validate() |
| 107 | + |
| 108 | +**JWT.validate** ( *token* : Text ; *key* : Text ) : Boolean |
| 109 | + |
| 110 | +### Parameters |
| 111 | + |
| 112 | +| Parameter | Type | | Description | |
| 113 | +|-----------|------|--:|-------------------------------------------------------------| |
| 114 | +| token | Text | ->| JWT token to validate | |
| 115 | +| key | Text | ->| Public key or shared secret used to verify the signature | |
| 116 | +| Result | Boolean | <-| `True` if the token is valid, `False` otherwise | |
| 117 | + |
| 118 | +### Description |
| 119 | + |
| 120 | +Validates a JWT token using the provided public key or shared secret. |
| 121 | + |
| 122 | +### Example |
| 123 | + |
| 124 | +```4d |
| 125 | +
|
| 126 | +var $isValid:= cs.NetKit.JWT.new().validate($token; $key) |
| 127 | +
|
| 128 | +``` |
| 129 | +## See also |
| 130 | + |
| 131 | +* [OAuth2Provider Class](./OAuth2Provider.md) |
| 132 | +* [Google Class](./Google.md) |
| 133 | +* [Office365 Class](./Office365.md) |
| 134 | +* [Interactive JWT Debugger](https://jwt.io/) |
| 135 | + |
| 136 | + |
0 commit comments