@truto/truto-jsonata 1.0.40 → 1.0.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1009,23 +1009,55 @@ expression2.evaluate({ text: text2, algorithm: algorithm2, secret: secret2, outp
1009
1009
  </details>
1010
1010
 
1011
1011
  <details>
1012
- <summary>signJwt(payload, secretOrPrivateKey, options)</summary>
1012
+ <summary>signJwt(payload, secretOrPrivateKey, headers = { alg: 'HS256', typ: 'JWT' })</summary>
1013
1013
 
1014
- Generates a signed JWT using the jsonwebtoken library. Supports various algorithms via options.
1014
+ Generates a signed JWT using the JOSE library. This function now accepts headers directly and extracts JWT claims (like `exp`, `nbf`, `iat`) directly from the payload. The `alg` in the header defaults to 'HS256' if not provided.
1015
1015
 
1016
1016
  **Example:**
1017
1017
 
1018
1018
  ```javascript
1019
1019
  import trutoJsonata from '@truto/truto-jsonata';
1020
1020
 
1021
- const payload = { sub: '1234567890', name: 'John Doe' };
1021
+ const payload = {
1022
+ sub: '1234567890',
1023
+ name: 'John Doe',
1024
+ exp: Math.floor(Date.now() / 1000) + 3600, // Expires in 1 hour
1025
+ iat: Math.floor(Date.now() / 1000) // Issued at current time
1026
+ };
1022
1027
  const secretOrPrivateKey = 'your-256-bit-secret';
1023
- const options = { expiresIn: '1h', algorithm: 'HS256' };
1028
+ const headers = { alg: 'HS256', kid: 'custom-key-id' }; // Optional custom headers
1029
+
1030
+ const expression = trutoJsonata('$signJwt(payload, secretOrPrivateKey, headers)');
1031
+ expression.evaluate({ payload, secretOrPrivateKey, headers }).then(result => {
1032
+ console.log(result);
1033
+ // Output: "eyJhbGciOiJIUzI1NiIsImtpZCI6ImN1c3RvbS1rZXktaWQiLCJ0eXAiOiJKV1QifQ.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiZXhwIjoxNzA3MjMzNjAwLCJpYXQiOjE3MDcyMzAwMDB9.<signature>"
1034
+ });
1035
+ ```
1036
+
1037
+ **Google Service Account Example:**
1038
+
1039
+ ```javascript
1040
+ import trutoJsonata from '@truto/truto-jsonata';
1041
+
1042
+ const serviceAccountPayload = {
1043
+ "iss": "service-account@my-project.iam.gserviceaccount.com",
1044
+ "sub": ,
1045
+ "scope": ,
1046
+ "aud": ,
1047
+ "iat": Math.floor(Date.now() / 1000),
1048
+ "exp": Math.floor(Date.now() / 1000) + 3600 /
1049
+ };
1050
+
1051
+ const serviceAccountHeaders = { alg: 'HS256' }; // Algorithm can be HS256 for testing, or RS256 with proper private key
1024
1052
 
1025
- const expression = trutoJsonata('$signJwt(payload, secretOrPrivateKey, options)');
1026
- expression.evaluate({ payload, secretOrPrivateKey, options }).then(result => {
1053
+ const serviceAccountExpression = trutoJsonata('$signJwt(serviceAccountPayload, serviceAccountSecret, serviceAccountHeaders)');
1054
+ serviceAccountExpression.evaluate({
1055
+ serviceAccountPayload,
1056
+ serviceAccountSecret,
1057
+ serviceAccountHeaders
1058
+ }).then(result => {
1027
1059
  console.log(result);
1028
- // Output: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIn0.<signature>"
1060
+ // Output: Signed JWT string for Google service account
1029
1061
  });
1030
1062
  ```
1031
1063