@polyjose/node 0.0.0-stage → 0.1.2
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/LICENSE +21 -0
- package/README.md +98 -2
- package/THIRD-PARTY-NOTICES.txt +16006 -0
- package/addon-sha256.json +10 -0
- package/backend.d.ts +86 -0
- package/backend.js +253 -0
- package/facade/api.d.ts +123 -0
- package/facade/api.js +567 -0
- package/facade/backend.d.ts +159 -0
- package/facade/backend.js +19 -0
- package/facade/error-codes.d.ts +4 -0
- package/facade/error-codes.js +22 -0
- package/facade/normalize.d.ts +134 -0
- package/facade/normalize.js +380 -0
- package/facade/types.d.ts +206 -0
- package/facade/types.js +14 -0
- package/index.d.ts +11 -0
- package/index.js +12 -0
- package/package.json +60 -4
- package/polyjose.darwin-arm64.node +0 -0
- package/polyjose.darwin-x64.node +0 -0
- package/polyjose.linux-arm64-gnu.node +0 -0
- package/polyjose.linux-arm64-musl.node +0 -0
- package/polyjose.linux-x64-gnu.node +0 -0
- package/polyjose.linux-x64-musl.node +0 -0
- package/polyjose.win32-arm64-msvc.node +0 -0
- package/polyjose.win32-x64-msvc.node +0 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 The polyjose contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,99 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @polyjose/node
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
polyjose for Node.js: JOSE (JWS, JWE, JWK and JWT; RFC 7515 to 7519) implemented in pure Rust and
|
|
4
|
+
loaded as a Node-API addon. All cryptography comes from the RustCrypto and dalek crates, with no
|
|
5
|
+
OpenSSL. Verify and decrypt always take an explicit algorithm allow-list, and `none` is never
|
|
6
|
+
accepted (RFC 8725).
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
npm install @polyjose/node
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Prebuilt for macOS, Linux (glibc and musl) and Windows, see the docs for
|
|
15
|
+
[platform support](https://polyjose.dev/reference/node/). Needs Node.js 22.12 or later.
|
|
16
|
+
|
|
17
|
+
## Import
|
|
18
|
+
|
|
19
|
+
The package is ESM:
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
import { jwtSign, jwtVerify } from "@polyjose/node";
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
CommonJS code can load it too, through Node's `require(esm)`:
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
const { jwtSign, jwtVerify } = require("@polyjose/node");
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Every function returns a Promise, and the work runs on the libuv thread pool, so it doesn't block
|
|
32
|
+
the event loop. The API is the same as `@polyjose/wasm`'s.
|
|
33
|
+
|
|
34
|
+
## Quick start
|
|
35
|
+
|
|
36
|
+
Sign a JWT, then verify it with an explicit algorithm allow-list and the issuer and audience you
|
|
37
|
+
expect. `exp` is required and checked, with 60 seconds of leeway (RFC 8725 §3.8 to §3.10).
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
import { jwkGenerateForAlg, jwkToPublic, jwtSign, jwtVerify, PolyjoseError } from "@polyjose/node";
|
|
41
|
+
|
|
42
|
+
// An ES256 key pair. The verifier needs only the public half.
|
|
43
|
+
const privateKey = await jwkGenerateForAlg("ES256");
|
|
44
|
+
const publicKey = await jwkToPublic(privateKey);
|
|
45
|
+
|
|
46
|
+
const now = Math.floor(Date.now() / 1000);
|
|
47
|
+
const token = await jwtSign(
|
|
48
|
+
{ iss: "https://issuer.example", sub: "user-123", aud: "https://api.example", exp: now + 300 },
|
|
49
|
+
privateKey,
|
|
50
|
+
{ alg: "ES256" },
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
const verified = await jwtVerify(token, publicKey, {
|
|
55
|
+
allowedAlgs: ["ES256"], // never trust the token's own `alg` (RFC 8725 §3.1)
|
|
56
|
+
issuers: ["https://issuer.example"],
|
|
57
|
+
audiences: ["https://api.example"],
|
|
58
|
+
});
|
|
59
|
+
console.log(verified.claims.sub); // "user-123"
|
|
60
|
+
} catch (err) {
|
|
61
|
+
if (err instanceof PolyjoseError) console.error(err.code); // e.g. "TokenExpired"
|
|
62
|
+
else throw err;
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Encrypt to a recipient's public key, then decrypt, naming the algorithms you accept:
|
|
67
|
+
|
|
68
|
+
```js
|
|
69
|
+
import { jweDecryptCompact, jweEncryptCompact, jwkGenerateEc, jwkToPublic } from "@polyjose/node";
|
|
70
|
+
|
|
71
|
+
const recipientKey = await jwkGenerateEc("P-256");
|
|
72
|
+
const recipientPublicKey = await jwkToPublic(recipientKey);
|
|
73
|
+
|
|
74
|
+
const jwe = await jweEncryptCompact(new TextEncoder().encode("top secret"), {
|
|
75
|
+
enc: "A256GCM",
|
|
76
|
+
recipients: [{ alg: "ECDH-ES+A256KW", key: recipientPublicKey }],
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
const decrypted = await jweDecryptCompact(jwe, recipientKey, {
|
|
80
|
+
allowedAlgs: ["ECDH-ES+A256KW"],
|
|
81
|
+
allowedEnc: ["A256GCM"],
|
|
82
|
+
});
|
|
83
|
+
console.log(new TextDecoder().decode(decrypted.plaintext)); // "top secret"
|
|
84
|
+
decrypted.plaintext.fill(0); // results are plain data: wipe secret bytes, and never log them
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Documentation
|
|
88
|
+
|
|
89
|
+
- Getting started on Node.js: <https://polyjose.dev/start/node/>
|
|
90
|
+
- Platform support, how the addon is loaded, and handling secrets in results:
|
|
91
|
+
<https://polyjose.dev/reference/node/>
|
|
92
|
+
- Guides (sign and verify, encrypt and decrypt, validate a JWT, keys): <https://polyjose.dev>
|
|
93
|
+
- Error codes: <https://polyjose.dev/concepts/errors/>
|
|
94
|
+
- Source and issues: <https://github.com/bharathkontham/polyjose>
|
|
95
|
+
|
|
96
|
+
## License
|
|
97
|
+
|
|
98
|
+
MIT; see [LICENSE](https://github.com/bharathkontham/polyjose/blob/main/LICENSE).
|
|
99
|
+
`THIRD-PARTY-NOTICES.txt` in the package lists the third-party code it contains.
|