@enbox/crypto 0.0.1

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.
Files changed (283) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +708 -0
  3. package/dist/browser.js +64 -0
  4. package/dist/browser.js.map +7 -0
  5. package/dist/browser.mjs +64 -0
  6. package/dist/browser.mjs.map +7 -0
  7. package/dist/cjs/algorithms/aes-ctr.js +188 -0
  8. package/dist/cjs/algorithms/aes-ctr.js.map +1 -0
  9. package/dist/cjs/algorithms/aes-gcm.js +196 -0
  10. package/dist/cjs/algorithms/aes-gcm.js.map +1 -0
  11. package/dist/cjs/algorithms/crypto-algorithm.js +13 -0
  12. package/dist/cjs/algorithms/crypto-algorithm.js.map +1 -0
  13. package/dist/cjs/algorithms/ecdsa.js +352 -0
  14. package/dist/cjs/algorithms/ecdsa.js.map +1 -0
  15. package/dist/cjs/algorithms/eddsa.js +325 -0
  16. package/dist/cjs/algorithms/eddsa.js.map +1 -0
  17. package/dist/cjs/algorithms/sha-2.js +119 -0
  18. package/dist/cjs/algorithms/sha-2.js.map +1 -0
  19. package/dist/cjs/index.js +41 -0
  20. package/dist/cjs/index.js.map +1 -0
  21. package/dist/cjs/jose/jwe.js +3 -0
  22. package/dist/cjs/jose/jwe.js.map +1 -0
  23. package/dist/cjs/jose/jwk.js +278 -0
  24. package/dist/cjs/jose/jwk.js.map +1 -0
  25. package/dist/cjs/jose/jws.js +3 -0
  26. package/dist/cjs/jose/jws.js.map +1 -0
  27. package/dist/cjs/jose/jwt.js +3 -0
  28. package/dist/cjs/jose/jwt.js.map +1 -0
  29. package/dist/cjs/jose/utils.js +60 -0
  30. package/dist/cjs/jose/utils.js.map +1 -0
  31. package/dist/cjs/local-key-manager.js +521 -0
  32. package/dist/cjs/local-key-manager.js.map +1 -0
  33. package/dist/cjs/package.json +1 -0
  34. package/dist/cjs/primitives/aes-ctr.js +398 -0
  35. package/dist/cjs/primitives/aes-ctr.js.map +1 -0
  36. package/dist/cjs/primitives/aes-gcm.js +425 -0
  37. package/dist/cjs/primitives/aes-gcm.js.map +1 -0
  38. package/dist/cjs/primitives/concat-kdf.js +215 -0
  39. package/dist/cjs/primitives/concat-kdf.js.map +1 -0
  40. package/dist/cjs/primitives/ed25519.js +651 -0
  41. package/dist/cjs/primitives/ed25519.js.map +1 -0
  42. package/dist/cjs/primitives/pbkdf2.js +120 -0
  43. package/dist/cjs/primitives/pbkdf2.js.map +1 -0
  44. package/dist/cjs/primitives/secp256k1.js +958 -0
  45. package/dist/cjs/primitives/secp256k1.js.map +1 -0
  46. package/dist/cjs/primitives/secp256r1.js +959 -0
  47. package/dist/cjs/primitives/secp256r1.js.map +1 -0
  48. package/dist/cjs/primitives/sha256.js +93 -0
  49. package/dist/cjs/primitives/sha256.js.map +1 -0
  50. package/dist/cjs/primitives/x25519.js +498 -0
  51. package/dist/cjs/primitives/x25519.js.map +1 -0
  52. package/dist/cjs/primitives/xchacha20-poly1305.js +340 -0
  53. package/dist/cjs/primitives/xchacha20-poly1305.js.map +1 -0
  54. package/dist/cjs/primitives/xchacha20.js +316 -0
  55. package/dist/cjs/primitives/xchacha20.js.map +1 -0
  56. package/dist/cjs/types/cipher.js +3 -0
  57. package/dist/cjs/types/cipher.js.map +1 -0
  58. package/dist/cjs/types/crypto-api.js +3 -0
  59. package/dist/cjs/types/crypto-api.js.map +1 -0
  60. package/dist/cjs/types/hasher.js +3 -0
  61. package/dist/cjs/types/hasher.js.map +1 -0
  62. package/dist/cjs/types/identifier.js +3 -0
  63. package/dist/cjs/types/identifier.js.map +1 -0
  64. package/dist/cjs/types/key-compressor.js +3 -0
  65. package/dist/cjs/types/key-compressor.js.map +1 -0
  66. package/dist/cjs/types/key-converter.js +3 -0
  67. package/dist/cjs/types/key-converter.js.map +1 -0
  68. package/dist/cjs/types/key-deriver.js +3 -0
  69. package/dist/cjs/types/key-deriver.js.map +1 -0
  70. package/dist/cjs/types/key-generator.js +3 -0
  71. package/dist/cjs/types/key-generator.js.map +1 -0
  72. package/dist/cjs/types/key-io.js +3 -0
  73. package/dist/cjs/types/key-io.js.map +1 -0
  74. package/dist/cjs/types/key-wrapper.js +3 -0
  75. package/dist/cjs/types/key-wrapper.js.map +1 -0
  76. package/dist/cjs/types/params-direct.js +3 -0
  77. package/dist/cjs/types/params-direct.js.map +1 -0
  78. package/dist/cjs/types/params-enclosed.js +3 -0
  79. package/dist/cjs/types/params-enclosed.js.map +1 -0
  80. package/dist/cjs/types/params-kms.js +3 -0
  81. package/dist/cjs/types/params-kms.js.map +1 -0
  82. package/dist/cjs/types/signer.js +3 -0
  83. package/dist/cjs/types/signer.js.map +1 -0
  84. package/dist/cjs/utils.js +173 -0
  85. package/dist/cjs/utils.js.map +1 -0
  86. package/dist/esm/algorithms/aes-ctr.js +124 -0
  87. package/dist/esm/algorithms/aes-ctr.js.map +1 -0
  88. package/dist/esm/algorithms/aes-gcm.js +132 -0
  89. package/dist/esm/algorithms/aes-gcm.js.map +1 -0
  90. package/dist/esm/algorithms/crypto-algorithm.js +6 -0
  91. package/dist/esm/algorithms/crypto-algorithm.js.map +1 -0
  92. package/dist/esm/algorithms/ecdsa.js +237 -0
  93. package/dist/esm/algorithms/ecdsa.js.map +1 -0
  94. package/dist/esm/algorithms/eddsa.js +213 -0
  95. package/dist/esm/algorithms/eddsa.js.map +1 -0
  96. package/dist/esm/algorithms/sha-2.js +57 -0
  97. package/dist/esm/algorithms/sha-2.js.map +1 -0
  98. package/dist/esm/index.js +25 -0
  99. package/dist/esm/index.js.map +1 -0
  100. package/dist/esm/jose/jwe.js +2 -0
  101. package/dist/esm/jose/jwe.js.map +1 -0
  102. package/dist/esm/jose/jwk.js +241 -0
  103. package/dist/esm/jose/jwk.js.map +1 -0
  104. package/dist/esm/jose/jws.js +2 -0
  105. package/dist/esm/jose/jws.js.map +1 -0
  106. package/dist/esm/jose/jwt.js +2 -0
  107. package/dist/esm/jose/jwt.js.map +1 -0
  108. package/dist/esm/jose/utils.js +34 -0
  109. package/dist/esm/jose/utils.js.map +1 -0
  110. package/dist/esm/local-key-manager.js +417 -0
  111. package/dist/esm/local-key-manager.js.map +1 -0
  112. package/dist/esm/primitives/aes-ctr.js +327 -0
  113. package/dist/esm/primitives/aes-ctr.js.map +1 -0
  114. package/dist/esm/primitives/aes-gcm.js +347 -0
  115. package/dist/esm/primitives/aes-gcm.js.map +1 -0
  116. package/dist/esm/primitives/concat-kdf.js +185 -0
  117. package/dist/esm/primitives/concat-kdf.js.map +1 -0
  118. package/dist/esm/primitives/ed25519.js +521 -0
  119. package/dist/esm/primitives/ed25519.js.map +1 -0
  120. package/dist/esm/primitives/pbkdf2.js +78 -0
  121. package/dist/esm/primitives/pbkdf2.js.map +1 -0
  122. package/dist/esm/primitives/secp256k1.js +805 -0
  123. package/dist/esm/primitives/secp256k1.js.map +1 -0
  124. package/dist/esm/primitives/secp256r1.js +806 -0
  125. package/dist/esm/primitives/secp256r1.js.map +1 -0
  126. package/dist/esm/primitives/sha256.js +55 -0
  127. package/dist/esm/primitives/sha256.js.map +1 -0
  128. package/dist/esm/primitives/x25519.js +392 -0
  129. package/dist/esm/primitives/x25519.js.map +1 -0
  130. package/dist/esm/primitives/xchacha20-poly1305.js +270 -0
  131. package/dist/esm/primitives/xchacha20-poly1305.js.map +1 -0
  132. package/dist/esm/primitives/xchacha20.js +246 -0
  133. package/dist/esm/primitives/xchacha20.js.map +1 -0
  134. package/dist/esm/types/cipher.js +2 -0
  135. package/dist/esm/types/cipher.js.map +1 -0
  136. package/dist/esm/types/crypto-api.js +2 -0
  137. package/dist/esm/types/crypto-api.js.map +1 -0
  138. package/dist/esm/types/hasher.js +2 -0
  139. package/dist/esm/types/hasher.js.map +1 -0
  140. package/dist/esm/types/identifier.js +2 -0
  141. package/dist/esm/types/identifier.js.map +1 -0
  142. package/dist/esm/types/key-compressor.js +2 -0
  143. package/dist/esm/types/key-compressor.js.map +1 -0
  144. package/dist/esm/types/key-converter.js +2 -0
  145. package/dist/esm/types/key-converter.js.map +1 -0
  146. package/dist/esm/types/key-deriver.js +2 -0
  147. package/dist/esm/types/key-deriver.js.map +1 -0
  148. package/dist/esm/types/key-generator.js +2 -0
  149. package/dist/esm/types/key-generator.js.map +1 -0
  150. package/dist/esm/types/key-io.js +2 -0
  151. package/dist/esm/types/key-io.js.map +1 -0
  152. package/dist/esm/types/key-wrapper.js +2 -0
  153. package/dist/esm/types/key-wrapper.js.map +1 -0
  154. package/dist/esm/types/params-direct.js +2 -0
  155. package/dist/esm/types/params-direct.js.map +1 -0
  156. package/dist/esm/types/params-enclosed.js +2 -0
  157. package/dist/esm/types/params-enclosed.js.map +1 -0
  158. package/dist/esm/types/params-kms.js +2 -0
  159. package/dist/esm/types/params-kms.js.map +1 -0
  160. package/dist/esm/types/signer.js +2 -0
  161. package/dist/esm/types/signer.js.map +1 -0
  162. package/dist/esm/utils.js +165 -0
  163. package/dist/esm/utils.js.map +1 -0
  164. package/dist/types/algorithms/aes-ctr.d.ts +121 -0
  165. package/dist/types/algorithms/aes-ctr.d.ts.map +1 -0
  166. package/dist/types/algorithms/aes-gcm.d.ts +152 -0
  167. package/dist/types/algorithms/aes-gcm.d.ts.map +1 -0
  168. package/dist/types/algorithms/crypto-algorithm.d.ts +6 -0
  169. package/dist/types/algorithms/crypto-algorithm.d.ts.map +1 -0
  170. package/dist/types/algorithms/ecdsa.d.ts +154 -0
  171. package/dist/types/algorithms/ecdsa.d.ts.map +1 -0
  172. package/dist/types/algorithms/eddsa.d.ts +151 -0
  173. package/dist/types/algorithms/eddsa.d.ts.map +1 -0
  174. package/dist/types/algorithms/sha-2.d.ts +51 -0
  175. package/dist/types/algorithms/sha-2.d.ts.map +1 -0
  176. package/dist/types/index.d.ts +39 -0
  177. package/dist/types/index.d.ts.map +1 -0
  178. package/dist/types/jose/jwe.d.ts +135 -0
  179. package/dist/types/jose/jwe.d.ts.map +1 -0
  180. package/dist/types/jose/jwk.d.ts +439 -0
  181. package/dist/types/jose/jwk.d.ts.map +1 -0
  182. package/dist/types/jose/jws.d.ts +67 -0
  183. package/dist/types/jose/jws.d.ts.map +1 -0
  184. package/dist/types/jose/jwt.d.ts +139 -0
  185. package/dist/types/jose/jwt.d.ts.map +1 -0
  186. package/dist/types/jose/utils.d.ts +14 -0
  187. package/dist/types/jose/utils.d.ts.map +1 -0
  188. package/dist/types/local-key-manager.d.ts +307 -0
  189. package/dist/types/local-key-manager.d.ts.map +1 -0
  190. package/dist/types/primitives/aes-ctr.d.ts +219 -0
  191. package/dist/types/primitives/aes-ctr.d.ts.map +1 -0
  192. package/dist/types/primitives/aes-gcm.d.ts +245 -0
  193. package/dist/types/primitives/aes-gcm.d.ts.map +1 -0
  194. package/dist/types/primitives/concat-kdf.d.ts +160 -0
  195. package/dist/types/primitives/concat-kdf.d.ts.map +1 -0
  196. package/dist/types/primitives/ed25519.d.ts +359 -0
  197. package/dist/types/primitives/ed25519.d.ts.map +1 -0
  198. package/dist/types/primitives/pbkdf2.d.ts +94 -0
  199. package/dist/types/primitives/pbkdf2.d.ts.map +1 -0
  200. package/dist/types/primitives/secp256k1.d.ts +598 -0
  201. package/dist/types/primitives/secp256k1.d.ts.map +1 -0
  202. package/dist/types/primitives/secp256r1.d.ts +599 -0
  203. package/dist/types/primitives/secp256r1.d.ts.map +1 -0
  204. package/dist/types/primitives/sha256.d.ts +42 -0
  205. package/dist/types/primitives/sha256.d.ts.map +1 -0
  206. package/dist/types/primitives/x25519.d.ts +283 -0
  207. package/dist/types/primitives/x25519.d.ts.map +1 -0
  208. package/dist/types/primitives/xchacha20-poly1305.d.ts +210 -0
  209. package/dist/types/primitives/xchacha20-poly1305.d.ts.map +1 -0
  210. package/dist/types/primitives/xchacha20.d.ts +186 -0
  211. package/dist/types/primitives/xchacha20.d.ts.map +1 -0
  212. package/dist/types/types/cipher.d.ts +49 -0
  213. package/dist/types/types/cipher.d.ts.map +1 -0
  214. package/dist/types/types/crypto-api.d.ts +40 -0
  215. package/dist/types/types/crypto-api.d.ts.map +1 -0
  216. package/dist/types/types/hasher.d.ts +33 -0
  217. package/dist/types/types/hasher.d.ts.map +1 -0
  218. package/dist/types/types/identifier.d.ts +16 -0
  219. package/dist/types/types/identifier.d.ts.map +1 -0
  220. package/dist/types/types/key-compressor.d.ts +28 -0
  221. package/dist/types/types/key-compressor.d.ts.map +1 -0
  222. package/dist/types/types/key-converter.d.ts +57 -0
  223. package/dist/types/types/key-converter.d.ts.map +1 -0
  224. package/dist/types/types/key-deriver.d.ts +39 -0
  225. package/dist/types/types/key-deriver.d.ts.map +1 -0
  226. package/dist/types/types/key-generator.d.ts +105 -0
  227. package/dist/types/types/key-generator.d.ts.map +1 -0
  228. package/dist/types/types/key-io.d.ts +37 -0
  229. package/dist/types/types/key-io.d.ts.map +1 -0
  230. package/dist/types/types/key-wrapper.d.ts +38 -0
  231. package/dist/types/types/key-wrapper.d.ts.map +1 -0
  232. package/dist/types/types/params-direct.d.ts +90 -0
  233. package/dist/types/types/params-direct.d.ts.map +1 -0
  234. package/dist/types/types/params-enclosed.d.ts +47 -0
  235. package/dist/types/types/params-enclosed.d.ts.map +1 -0
  236. package/dist/types/types/params-kms.d.ts +131 -0
  237. package/dist/types/types/params-kms.d.ts.map +1 -0
  238. package/dist/types/types/signer.d.ts +46 -0
  239. package/dist/types/types/signer.d.ts.map +1 -0
  240. package/dist/types/utils.d.ts +112 -0
  241. package/dist/types/utils.d.ts.map +1 -0
  242. package/dist/utils.js +7 -0
  243. package/dist/utils.js.map +7 -0
  244. package/package.json +103 -0
  245. package/src/algorithms/aes-ctr.ts +156 -0
  246. package/src/algorithms/aes-gcm.ts +187 -0
  247. package/src/algorithms/crypto-algorithm.ts +4 -0
  248. package/src/algorithms/ecdsa.ts +269 -0
  249. package/src/algorithms/eddsa.ts +243 -0
  250. package/src/algorithms/sha-2.ts +65 -0
  251. package/src/index.ts +42 -0
  252. package/src/jose/jwe.ts +196 -0
  253. package/src/jose/jwk.ts +632 -0
  254. package/src/jose/jws.ts +95 -0
  255. package/src/jose/jwt.ts +147 -0
  256. package/src/jose/utils.ts +34 -0
  257. package/src/local-key-manager.ts +540 -0
  258. package/src/primitives/aes-ctr.ts +352 -0
  259. package/src/primitives/aes-gcm.ts +378 -0
  260. package/src/primitives/concat-kdf.ts +240 -0
  261. package/src/primitives/ed25519.ts +548 -0
  262. package/src/primitives/pbkdf2.ts +122 -0
  263. package/src/primitives/secp256k1.ts +848 -0
  264. package/src/primitives/secp256r1.ts +850 -0
  265. package/src/primitives/sha256.ts +47 -0
  266. package/src/primitives/x25519.ts +403 -0
  267. package/src/primitives/xchacha20-poly1305.ts +274 -0
  268. package/src/primitives/xchacha20.ts +247 -0
  269. package/src/types/cipher.ts +53 -0
  270. package/src/types/crypto-api.ts +56 -0
  271. package/src/types/hasher.ts +32 -0
  272. package/src/types/identifier.ts +16 -0
  273. package/src/types/key-compressor.ts +25 -0
  274. package/src/types/key-converter.ts +53 -0
  275. package/src/types/key-deriver.ts +43 -0
  276. package/src/types/key-generator.ts +119 -0
  277. package/src/types/key-io.ts +42 -0
  278. package/src/types/key-wrapper.ts +42 -0
  279. package/src/types/params-direct.ts +106 -0
  280. package/src/types/params-enclosed.ts +50 -0
  281. package/src/types/params-kms.ts +156 -0
  282. package/src/types/signer.ts +50 -0
  283. package/src/utils.ts +181 -0
@@ -0,0 +1,95 @@
1
+ import type { Jwk } from './jwk.js';
2
+
3
+ /**
4
+ * JSON Object Signing and Encryption (JOSE) Header Parameters
5
+ *
6
+ * The Header Parameter names for use in both JWSs and JWEs are registered in the IANA "JSON Web
7
+ * Signature and Encryption Header Parameters" registry.
8
+ *
9
+ * As indicated by the common registry, JWSs and JWEs share a common Header Parameter space; when a
10
+ * parameter is used by both specifications, its usage must be compatible between the
11
+ * specifications.
12
+ *
13
+ * @see {@link https://datatracker.ietf.org/doc/html/rfc7515#section-4.1 | RFC 7515, Section 4.1}
14
+ */
15
+ export interface JoseHeaderParams {
16
+ /** Content Type Header Parameter */
17
+ cty?: string;
18
+
19
+ /** JWK Set URL Header Parameter */
20
+ jku?: string;
21
+
22
+ /** JSON Web Key Header Parameter */
23
+ jwk?: Jwk;
24
+
25
+ /** Key ID Header Parameter */
26
+ kid?: string;
27
+
28
+ /** Type Header Parameter */
29
+ typ?: string;
30
+
31
+ /** X.509 Certificate Chain Header Parameter */
32
+ x5c?: string[];
33
+
34
+ /** X.509 Certificate SHA-1 Thumbprint Header Parameter */
35
+ x5t?: string;
36
+
37
+ /** X.509 URL Header Parameter */
38
+ x5u?: string;
39
+ }
40
+
41
+ /**
42
+ * JSON Web Signature (JWS) Header Parameters
43
+ *
44
+ * The Header Parameter names for use in JWSs are registered in the IANA "JSON Web Signature and
45
+ * Encryption Header Parameters" registry.
46
+ *
47
+ * @see {@link https://datatracker.ietf.org/doc/html/rfc7515#section-4.1 | RFC 7515, Section 4.1}
48
+ */
49
+ export interface JwsHeaderParams extends JoseHeaderParams {
50
+ /**
51
+ * Algorithm Header Parameter
52
+ *
53
+ * Identifies the cryptographic algorithm used to secure the JWS. The JWS Signature value is not
54
+ * valid if the "alg" value does not represent a supported algorithm or if there is not a key for
55
+ * use with that algorithm associated with the party that digitally signed or MACed the content.
56
+ *
57
+ * "alg" values should either be registered in the IANA "JSON Web Signature and Encryption
58
+ * Algorithms" registry or be a value that contains a Collision-Resistant Name. The "alg" value is
59
+ * a case-sensitive ASCII string. This Header Parameter MUST be present and MUST be understood
60
+ * and processed by implementations.
61
+ *
62
+ * @see {@link https://datatracker.ietf.org/doc/html/rfc7515#section-4.1.1 | RFC 7515, Section 4.1.1}
63
+ */
64
+ alg:
65
+ // Edwards curve digital signature algorithm (e.g., Ed25519)
66
+ | 'EdDSA'
67
+ // ECDSA using P-256 and SHA-256
68
+ | 'ES256'
69
+ // ECDSA using secp256k1 curve and SHA-256
70
+ | 'ES256K'
71
+ // ECDSA using P-384 and SHA-384
72
+ | 'ES384'
73
+ // ECDSA using P-521 and SHA-512
74
+ | 'ES512'
75
+ // HMAC using SHA-256
76
+ | 'HS256'
77
+ // HMAC using SHA-384
78
+ | 'HS384'
79
+ // HMAC using SHA-512
80
+ | 'HS512'
81
+ // an unregistered, case-sensitive, collision-resistant string
82
+ | string;
83
+
84
+ /**
85
+ * Critical Header Parameter
86
+ *
87
+ * Indicates that extensions to JOSE RFCs are being used that MUST be understood and processed.
88
+ */
89
+ crit?: string[]
90
+
91
+ /**
92
+ * Additional Public or Private Header Parameter names.
93
+ */
94
+ [key: string]: unknown
95
+ }
@@ -0,0 +1,147 @@
1
+ import type { JweHeaderParams } from './jwe.js';
2
+ import type { JwsHeaderParams } from './jws.js';
3
+
4
+ /**
5
+ * JSON Web Token (JWT) Header
6
+ *
7
+ * For a JWT object, the members of the JSON object represented by the JOSE Header describe the
8
+ * cryptographic operations applied to the JWT and optionally, additional properties of the JWT.
9
+ * Depending upon whether the JWT is a JWS or JWE, the corresponding rules for the JOSE Header
10
+ * values apply.
11
+ *
12
+ * The {@link https://datatracker.ietf.org/doc/html/rfc7519#section-5 | RFC 7519} specification
13
+ * further specifies the use of the following Header Parameters in both the cases where the JWT is a
14
+ * JWS and where it is a JWE:
15
+ *
16
+ * - "typ" (type) Header Parameter: This Header Parameter is OPTIONAL. When used, this Header
17
+ * Parameter MUST be used to declare the MIME Media Type of this complete JWT. This parameter is
18
+ * ignored by JWT implementations; any processing of this parameter is performed by the JWT
19
+ * application. If present, it is RECOMMENDED that its value be "JWT" to indicate that this
20
+ * object is a JWT. While media type names are not case sensitive, it is RECOMMENDED that "JWT"
21
+ * always be spelled using uppercase characters for compatibility with legacy implementations.
22
+ *
23
+ * - "cty" (content type) Header Parameter: This Header Parameter is OPTIONAL. When used, this
24
+ * Header Parameter MUST be used to declare the MIME Media Type of the secured content (the
25
+ * payload). In the normal case in which nested signing or encryption operations are not employed,
26
+ * the use of this Header Parameter is NOT RECOMMENDED. In the case that nested signing or
27
+ * encryption is employed, this Header Parameter MUST be present; in this case, the value MUST be
28
+ * "JWT", to indicate that a Nested JWT is carried in this JWT. While media type names are not
29
+ * case sensitive, it is RECOMMENDED that "JWT" always be spelled using uppercase characters
30
+ * for compatibility with legacy implementations.
31
+ *
32
+ * @see {@link https://datatracker.ietf.org/doc/html/rfc7519#section-5 | RFC 7519, Section 5}
33
+ */
34
+ export type JwtHeaderParams = JwsHeaderParams | JweHeaderParams;
35
+
36
+ /**
37
+ * JSON Web Token Payload
38
+ *
39
+ * The JWT Claims Set represents a JSON object whose members are the claims conveyed by the JWT.
40
+ * The Claim Names within a JWT Claims Set MUST be unique; JWT parsers MUST either reject JWTs
41
+ * with duplicate Claim Names or use a JSON parser that returns only the lexically last duplicate
42
+ * member name.
43
+ *
44
+ * The set of claims that a JWT must contain to be considered valid is context dependent and is
45
+ * undefined by RFC 7519. Specific applications of JWTs will require implementations to understand
46
+ * and process some claims in particular ways.
47
+ *
48
+ * There are three classes of JWT Claim Names:
49
+ *
50
+ * - Registered Claim Names: Claim names registered in the IANA "JSON Web Token Claims" registry.
51
+ * None of the claims defined below are intended to be mandatory to use or implement in all cases,
52
+ * but rather they provide a starting point for a set of useful, interoperable claims
53
+ * Applications using JWTs should define which specific claims they use and when they are required
54
+ * or optional.
55
+ *
56
+ * - Public Claim Names: Claim Names can be defined at will by those using JWTs. However, in order
57
+ * prevent collisions, any new Claim Name should either be registered in the IANA "JSON Web Token
58
+ * Claims" registry or be a Public Name: a value that contains a Collision-Resistant Name. In each
59
+ * case, the definer of the name or value needs to take reasonable precautions to make sure they
60
+ * are in control of the part of the namespace they use to define the Claim Name.
61
+ *
62
+ * - Private Claim Names: A producer and consumer of a JWT MAY agree to use Claim Names that are
63
+ * Private Names: names that are not Registered Claim Names or Public Claim Names. Unlike Public
64
+ * Claim Names, Private Claim Names are subject to collision and should be used with caution.
65
+ *
66
+ * @see {@link https://datatracker.ietf.org/doc/html/rfc7519#section-4 | RFC 7519, Section 4}
67
+ */
68
+ export interface JwtPayload {
69
+ /**
70
+ * Issuer
71
+ * Identifies the principal that issued the JWT. The "iss" value is a case-sensitive string
72
+ * containing a string or URI value. Use of this claim is OPTIONAL.
73
+ * @see {@link https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.1 | RFC 7519, Section 4.1.1}
74
+ */
75
+ iss?: string;
76
+
77
+ /**
78
+ * Subject
79
+ * Identifies the principal that is the subject of the JWT. The claims in a JWT are normally
80
+ * statements about the subject. The subject value MUST either be scoped to be locally unique in
81
+ * the context of the issuer or be globally unique. The "sub" value is a case-sensitive string
82
+ * containing a string or URI value. Use of this claim is OPTIONAL.
83
+ * @see {@link https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.2 | RFC 7519, Section 4.1.2}
84
+ */
85
+ sub?: string;
86
+
87
+ /**
88
+ * Audience
89
+ * Identifies the recipients that the JWT is intended for. Each principal intended to process
90
+ * the JWT MUST identify itself with a value in the audience claim. If the principal processing
91
+ * the claim does not identify itself with a value in the "aud" claim when this claim is present,
92
+ * then the JWT MUST be rejected. In the general case, the "aud" value is an array of case-
93
+ * sensitive strings, each containing a string or URI value. In the special case when the JWT has
94
+ * one audience, the "aud" value MAY be a single case-sensitive string containing a string or URI
95
+ * value. Use of this claim is OPTIONAL.
96
+ * @see {@link https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.3 | RFC 7519, Section 4.1.3}
97
+ */
98
+ aud?: string | string[];
99
+
100
+ /**
101
+ * Expiration Time
102
+ * Identifies the expiration time on or after which the JWT MUST NOT be accepted for processing.
103
+ * The processing of the "exp" claim requires that the current date/time MUST be before the
104
+ * expiration date/time listed in the "exp" claim. Implementers MAY provide for some small leeway,
105
+ * usually no more than a few minutes, to account for clock skew. Its value MUST be a number
106
+ * containing a numeric date value. Use of this claim is OPTIONAL.
107
+ * @see {@link https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.4 | RFC 7519, Section 4.1.4}
108
+ */
109
+ exp?: number;
110
+
111
+ /**
112
+ * Not Before
113
+ * Identifies the time before which the JWT MUST NOT be accepted for processing. The processing
114
+ * of the "nbf" claim requires that the current date/time MUST be after or equal to the not-before
115
+ * date/time listed in the "nbf" claim. Implementers MAY provide for some small leeway, usually no
116
+ * more than a few minutes, to account for clock skew. Its value MUST be a number containing a
117
+ * numeric date value. Use of this claim is OPTIONAL.
118
+ * @see {@link https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.5 | RFC 7519, Section 4.1.5}
119
+ */
120
+ nbf?: number;
121
+
122
+ /**
123
+ * Issued At
124
+ * Identifies the time at which the JWT was issued. This claim can be used to determine the age
125
+ * of the JWT. Its value MUST be a number containing a numeric date value. Use of this claim is
126
+ * OPTIONAL.
127
+ * @see {@link https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.6 | RFC 7519, Section 4.1.6}
128
+ */
129
+ iat?: number;
130
+
131
+ /**
132
+ * JWT ID
133
+ * Provides a unique identifier for the JWT. The identifier value MUST be assigned in a manner
134
+ * that ensures that there is a negligible probability that the same value will be accidentally
135
+ * assigned to a different data object; if the application uses multiple issuers, collisions
136
+ * MUST be prevented among values produced by different issuers as well. The "jti" claim can be
137
+ * used to prevent the JWT from being replayed. The "jti" value is a case-sensitive string.
138
+ * Use of this claim is OPTIONAL.
139
+ * @see {@link https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.7 | RFC 7519, Section 4.1.7}
140
+ */
141
+ jti?: string;
142
+
143
+ /**
144
+ * Additional Public or Private Claim names.
145
+ */
146
+ [key: string]: unknown;
147
+ }
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Canonicalizes a given object according to RFC 8785 (https://tools.ietf.org/html/rfc8785),
3
+ * which describes JSON Canonicalization Scheme (JCS). This function sorts the keys of the
4
+ * object and its nested objects alphabetically and then returns a stringified version of it.
5
+ * This method handles nested objects, array values, and null values appropriately.
6
+ *
7
+ * @param obj - The object to canonicalize.
8
+ * @returns The stringified version of the input object with its keys sorted alphabetically
9
+ * per RFC 8785.
10
+ */
11
+ export function canonicalize(obj: { [key: string]: any }): string {
12
+ /**
13
+ * Recursively sorts the keys of an object.
14
+ *
15
+ * @param obj - The object whose keys are to be sorted.
16
+ * @returns A new object with sorted keys.
17
+ */
18
+ const sortObjKeys = (obj: { [key: string]: any }): { [key: string]: any } => {
19
+ if (obj !== null && typeof obj === 'object' && !Array.isArray(obj)) {
20
+ const sortedKeys = Object.keys(obj).sort();
21
+ const sortedObj: { [key: string]: any } = {};
22
+ for (const key of sortedKeys) {
23
+ // Recursively sort keys of nested objects.
24
+ sortedObj[key] = sortObjKeys(obj[key]);
25
+ }
26
+ return sortedObj;
27
+ }
28
+ return obj;
29
+ };
30
+
31
+ // Stringify and return the final sorted object.
32
+ const sortedObj = sortObjKeys(obj);
33
+ return JSON.stringify(sortedObj);
34
+ }