You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
7.0 KiB

4 years ago
  1. # node-jws [![Build Status](https://secure.travis-ci.org/brianloveswords/node-jws.png)](http://travis-ci.org/brianloveswords/node-jws)
  2. An implementation of [JSON Web Signatures](http://self-issued.info/docs/draft-ietf-jose-json-web-signature.html).
  3. This was developed against `draft-ietf-jose-json-web-signature-08` and
  4. implements the entire spec **except** X.509 Certificate Chain
  5. signing/verifying (patches welcome).
  6. There are both synchronous (`jws.sign`, `jws.verify`) and streaming
  7. (`jws.createSign`, `jws.createVerify`) APIs.
  8. # Install
  9. ```bash
  10. $ npm install jws
  11. ```
  12. # Usage
  13. ## jws.ALGORITHMS
  14. Array of supported algorithms. The following algorithms are currently supported.
  15. alg Parameter Value | Digital Signature or MAC Algorithm
  16. ----------------|----------------------------
  17. HS256 | HMAC using SHA-256 hash algorithm
  18. HS384 | HMAC using SHA-384 hash algorithm
  19. HS512 | HMAC using SHA-512 hash algorithm
  20. RS256 | RSASSA using SHA-256 hash algorithm
  21. RS384 | RSASSA using SHA-384 hash algorithm
  22. RS512 | RSASSA using SHA-512 hash algorithm
  23. PS256 | RSASSA-PSS using SHA-256 hash algorithm
  24. PS384 | RSASSA-PSS using SHA-384 hash algorithm
  25. PS512 | RSASSA-PSS using SHA-512 hash algorithm
  26. ES256 | ECDSA using P-256 curve and SHA-256 hash algorithm
  27. ES384 | ECDSA using P-384 curve and SHA-384 hash algorithm
  28. ES512 | ECDSA using P-521 curve and SHA-512 hash algorithm
  29. none | No digital signature or MAC value included
  30. ## jws.sign(options)
  31. (Synchronous) Return a JSON Web Signature for a header and a payload.
  32. Options:
  33. * `header`
  34. * `payload`
  35. * `secret` or `privateKey`
  36. * `encoding` (Optional, defaults to 'utf8')
  37. `header` must be an object with an `alg` property. `header.alg` must be
  38. one a value found in `jws.ALGORITHMS`. See above for a table of
  39. supported algorithms.
  40. If `payload` is not a buffer or a string, it will be coerced into a string
  41. using `JSON.stringify`.
  42. Example
  43. ```js
  44. const signature = jws.sign({
  45. header: { alg: 'HS256' },
  46. payload: 'h. jon benjamin',
  47. secret: 'has a van',
  48. });
  49. ```
  50. ## jws.verify(signature, algorithm, secretOrKey)
  51. (Synchronous) Returns `true` or `false` for whether a signature matches a
  52. secret or key.
  53. `signature` is a JWS Signature. `header.alg` must be a value found in `jws.ALGORITHMS`.
  54. See above for a table of supported algorithms. `secretOrKey` is a string or
  55. buffer containing either the secret for HMAC algorithms, or the PEM
  56. encoded public key for RSA and ECDSA.
  57. Note that the `"alg"` value from the signature header is ignored.
  58. ## jws.decode(signature)
  59. (Synchronous) Returns the decoded header, decoded payload, and signature
  60. parts of the JWS Signature.
  61. Returns an object with three properties, e.g.
  62. ```js
  63. { header: { alg: 'HS256' },
  64. payload: 'h. jon benjamin',
  65. signature: 'YOWPewyGHKu4Y_0M_vtlEnNlqmFOclqp4Hy6hVHfFT4'
  66. }
  67. ```
  68. ## jws.createSign(options)
  69. Returns a new SignStream object.
  70. Options:
  71. * `header` (required)
  72. * `payload`
  73. * `key` || `privateKey` || `secret`
  74. * `encoding` (Optional, defaults to 'utf8')
  75. Other than `header`, all options expect a string or a buffer when the
  76. value is known ahead of time, or a stream for convenience.
  77. `key`/`privateKey`/`secret` may also be an object when using an encrypted
  78. private key, see the [crypto documentation][encrypted-key-docs].
  79. Example:
  80. ```js
  81. // This...
  82. jws.createSign({
  83. header: { alg: 'RS256' },
  84. privateKey: privateKeyStream,
  85. payload: payloadStream,
  86. }).on('done', function(signature) {
  87. // ...
  88. });
  89. // is equivalent to this:
  90. const signer = jws.createSign({
  91. header: { alg: 'RS256' },
  92. });
  93. privateKeyStream.pipe(signer.privateKey);
  94. payloadStream.pipe(signer.payload);
  95. signer.on('done', function(signature) {
  96. // ...
  97. });
  98. ```
  99. ## jws.createVerify(options)
  100. Returns a new VerifyStream object.
  101. Options:
  102. * `signature`
  103. * `algorithm`
  104. * `key` || `publicKey` || `secret`
  105. * `encoding` (Optional, defaults to 'utf8')
  106. All options expect a string or a buffer when the value is known ahead of
  107. time, or a stream for convenience.
  108. Example:
  109. ```js
  110. // This...
  111. jws.createVerify({
  112. publicKey: pubKeyStream,
  113. signature: sigStream,
  114. }).on('done', function(verified, obj) {
  115. // ...
  116. });
  117. // is equivilant to this:
  118. const verifier = jws.createVerify();
  119. pubKeyStream.pipe(verifier.publicKey);
  120. sigStream.pipe(verifier.signature);
  121. verifier.on('done', function(verified, obj) {
  122. // ...
  123. });
  124. ```
  125. ## Class: SignStream
  126. A `Readable Stream` that emits a single data event (the calculated
  127. signature) when done.
  128. ### Event: 'done'
  129. `function (signature) { }`
  130. ### signer.payload
  131. A `Writable Stream` that expects the JWS payload. Do *not* use if you
  132. passed a `payload` option to the constructor.
  133. Example:
  134. ```js
  135. payloadStream.pipe(signer.payload);
  136. ```
  137. ### signer.secret<br>signer.key<br>signer.privateKey
  138. A `Writable Stream`. Expects the JWS secret for HMAC, or the privateKey
  139. for ECDSA and RSA. Do *not* use if you passed a `secret` or `key` option
  140. to the constructor.
  141. Example:
  142. ```js
  143. privateKeyStream.pipe(signer.privateKey);
  144. ```
  145. ## Class: VerifyStream
  146. This is a `Readable Stream` that emits a single data event, the result
  147. of whether or not that signature was valid.
  148. ### Event: 'done'
  149. `function (valid, obj) { }`
  150. `valid` is a boolean for whether or not the signature is valid.
  151. ### verifier.signature
  152. A `Writable Stream` that expects a JWS Signature. Do *not* use if you
  153. passed a `signature` option to the constructor.
  154. ### verifier.secret<br>verifier.key<br>verifier.publicKey
  155. A `Writable Stream` that expects a public key or secret. Do *not* use if you
  156. passed a `key` or `secret` option to the constructor.
  157. # TODO
  158. * It feels like there should be some convenience options/APIs for
  159. defining the algorithm rather than having to define a header object
  160. with `{ alg: 'ES512' }` or whatever every time.
  161. * X.509 support, ugh
  162. # License
  163. MIT
  164. ```
  165. Copyright (c) 2013-2015 Brian J. Brennan
  166. Permission is hereby granted, free of charge, to any person obtaining a
  167. copy of this software and associated documentation files (the
  168. "Software"), to deal in the Software without restriction, including
  169. without limitation the rights to use, copy, modify, merge, publish,
  170. distribute, sublicense, and/or sell copies of the Software, and to
  171. permit persons to whom the Software is furnished to do so, subject to
  172. the following conditions:
  173. The above copyright notice and this permission notice shall be included
  174. in all copies or substantial portions of the Software.
  175. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  176. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  177. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  178. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  179. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  180. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  181. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  182. ```
  183. [encrypted-key-docs]: https://nodejs.org/api/crypto.html#crypto_sign_sign_private_key_output_format