@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 +39 -7
- package/dist/browser/index.js +1057 -15407
- package/dist/browser/index.js.map +28 -81
- package/dist/cjs/index.cjs +1057 -15407
- package/dist/cjs/index.cjs.map +28 -81
- package/dist/esm/index.js +11 -6
- package/dist/esm/index.js.map +3 -3
- package/dist/functions/signJwt.d.ts +1 -1
- package/package.json +2 -3
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,
|
|
1012
|
+
<summary>signJwt(payload, secretOrPrivateKey, headers = { alg: 'HS256', typ: 'JWT' })</summary>
|
|
1013
1013
|
|
|
1014
|
-
Generates a signed JWT using the
|
|
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 = {
|
|
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
|
|
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
|
|
1026
|
-
|
|
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:
|
|
1060
|
+
// Output: Signed JWT string for Google service account
|
|
1029
1061
|
});
|
|
1030
1062
|
```
|
|
1031
1063
|
|