Ohm-Management - Projektarbeit B-ME
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.

README.md 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. # connect-mongo
  2. MongoDB session store for [Connect](https://github.com/senchalabs/connect) and [Express](http://expressjs.com/)
  3. [![npm version](https://img.shields.io/npm/v/connect-mongo.svg)](https://www.npmjs.com/package/connect-mongo)
  4. [![downloads](https://img.shields.io/npm/dm/connect-mongo.svg)](https://www.npmjs.com/package/connect-mongo)
  5. [![Build Status](https://travis-ci.org/jdesboeufs/connect-mongo.svg?branch=master)](https://travis-ci.org/jdesboeufs/connect-mongo)
  6. [![Coverage Status](https://coveralls.io/repos/jdesboeufs/connect-mongo/badge.svg?branch=master&service=github)](https://coveralls.io/github/jdesboeufs/connect-mongo?branch=master)
  7. [![Dependency Status](https://david-dm.org/jdesboeufs/connect-mongo.svg?style=flat)](https://david-dm.org/jdesboeufs/connect-mongo)
  8. ## Compatibility
  9. * Support Express up to `5.0`
  10. * Support all Connect versions
  11. * Support [Mongoose](http://mongoosejs.com/index.html) `>= 5.0`
  12. * Support [native MongoDB driver](http://mongodb.github.io/node-mongodb-native/) `>= 3.0`
  13. * Support Node.js 8, 10 and 12
  14. * Support [MongoDB](https://www.mongodb.com/) `3.2 - 4.0`
  15. For extended compatibility, see previous versions [v2.0.3](https://github.com/jdesboeufs/connect-mongo/tree/v2.0.3).
  16. But please note that we are not maintaining v2.x.x anymore.
  17. ## Usage
  18. ### Express or Connect integration
  19. Express `4.x`, `5.0` and Connect `3.x`:
  20. ```js
  21. const session = require('express-session');
  22. const MongoStore = require('connect-mongo')(session);
  23. app.use(session({
  24. secret: 'foo',
  25. store: new MongoStore(options)
  26. }));
  27. ```
  28. ### Connection to MongoDB
  29. In many circumstances, `connect-mongo` will not be the only part of your application which need a connection to a MongoDB database. It could be interesting to re-use an existing connection.
  30. Alternatively, you can configure `connect-mongo` to establish a new connection.
  31. #### Re-use a Mongoose connection
  32. ```js
  33. const mongoose = require('mongoose');
  34. // Basic usage
  35. mongoose.connect(connectionOptions);
  36. app.use(session({
  37. store: new MongoStore({ mongooseConnection: mongoose.connection })
  38. }));
  39. // Advanced usage
  40. const connection = mongoose.createConnection(connectionOptions);
  41. app.use(session({
  42. store: new MongoStore({ mongooseConnection: connection })
  43. }));
  44. ```
  45. #### Re-use a native MongoDB driver client (or a promise)
  46. In this case, you just have to give your `MongoClient` instance to `connect-mongo`.
  47. ```js
  48. /*
  49. ** There are many ways to create MongoClient.
  50. ** You should refer to the driver documentation.
  51. */
  52. app.use(session({
  53. store: new MongoStore({ client: clientInstance })
  54. }));
  55. ```
  56. Or just give a promise...
  57. ```js
  58. app.use(session({
  59. store: new MongoStore({ clientPromise: clientInstancePromise })
  60. }));
  61. ```
  62. #### Create a new connection from a MongoDB connection string
  63. [MongoDB connection strings](http://docs.mongodb.org/manual/reference/connection-string/) are __the best way__ to configure a new connection. For advanced usage, [more options](http://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html#mongoclient-connect-options) can be configured with `mongoOptions` property.
  64. ```js
  65. // Basic usage
  66. app.use(session({
  67. store: new MongoStore({ url: 'mongodb://localhost/test-app' })
  68. }));
  69. // Advanced usage
  70. app.use(session({
  71. store: new MongoStore({
  72. url: 'mongodb://user12345:foobar@localhost/test-app?authSource=admins&w=1',
  73. mongoOptions: advancedOptions // See below for details
  74. })
  75. }));
  76. ```
  77. ## Events
  78. A `MongoStore` instance will emit the following events:
  79. | Event name | Description | Payload
  80. | ----- | ----- | ----- |
  81. | `create` | A session has been created | `sessionId` |
  82. | `touch` | A session has been touched (but not modified) | `sessionId` |
  83. | `update` | A session has been updated | `sessionId` |
  84. | `set` | A session has been created OR updated _(for compatibility purpose)_ | `sessionId` |
  85. | `destroy` | A session has been destroyed manually | `sessionId` |
  86. ## Session expiration
  87. When the session cookie has an expiration date, `connect-mongo` will use it.
  88. Otherwise, it will create a new one, using `ttl` option.
  89. ```js
  90. app.use(session({
  91. store: new MongoStore({
  92. url: 'mongodb://localhost/test-app',
  93. ttl: 14 * 24 * 60 * 60 // = 14 days. Default
  94. })
  95. }));
  96. ```
  97. __Note:__ Each time an user interacts with the server, its session expiration date is refreshed.
  98. ## Remove expired sessions
  99. By default, `connect-mongo` uses MongoDB's TTL collection feature (2.2+) to have mongod automatically remove expired sessions. But you can change this behavior.
  100. ### Set MongoDB to clean expired sessions (default mode)
  101. `connect-mongo` will create a TTL index for you at startup. You MUST have MongoDB 2.2+ and administration permissions.
  102. ```js
  103. app.use(session({
  104. store: new MongoStore({
  105. url: 'mongodb://localhost/test-app',
  106. autoRemove: 'native' // Default
  107. })
  108. }));
  109. ```
  110. __Note:__ If you use `connect-mongo` in a very concurrent environment, you should avoid this mode and prefer setting the index yourself, once!
  111. ### Set the compatibility mode
  112. You have an older MongoDB version (compatible with connect-mongo) or you can't or don't want to create a TTL index.
  113. `connect-mongo` will take care of removing expired sessions, using defined interval.
  114. ```js
  115. app.use(session({
  116. store: new MongoStore({
  117. url: 'mongodb://localhost/test-app',
  118. autoRemove: 'interval',
  119. autoRemoveInterval: 10 // In minutes. Default
  120. })
  121. }));
  122. ```
  123. ### Disable expired sessions cleaning
  124. You are in production environnement and/or you manage the TTL index elsewhere.
  125. ```js
  126. app.use(session({
  127. store: new MongoStore({
  128. url: 'mongodb://localhost/test-app',
  129. autoRemove: 'disabled'
  130. })
  131. }));
  132. ```
  133. ## Lazy session update
  134. If you are using [express-session](https://github.com/expressjs/session) >= [1.10.0](https://github.com/expressjs/session/releases/tag/v1.10.0) and don't want to resave all the session on database every single time that the user refresh the page, you can lazy update the session, by limiting a period of time.
  135. ```js
  136. app.use(express.session({
  137. secret: 'keyboard cat',
  138. saveUninitialized: false, // don't create session until something stored
  139. resave: false, //don't save session if unmodified
  140. store: new MongoStore({
  141. url: 'mongodb://localhost/test-app',
  142. touchAfter: 24 * 3600 // time period in seconds
  143. })
  144. }));
  145. ```
  146. by doing this, setting `touchAfter: 24 * 3600` you are saying to the session be updated only one time in a period of 24 hours, does not matter how many request's are made (with the exception of those that change something on the session data)
  147. ## Transparent encryption/decryption of session data
  148. When working with sensitive session data it is [recommended](https://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/Session_Management_Cheat_Sheet.md) to use encryption
  149. ```js
  150. const store = new MongoStore({
  151. url: 'mongodb://localhost/test-app',
  152. secret: 'squirrel'
  153. })
  154. ```
  155. ## More options
  156. - `collection` Collection (default: `sessions`)
  157. - `fallbackMemory` Fallback to `MemoryStore`. Useful if you want to use MemoryStore in some case, like in development environment.
  158. - `stringify` If true, connect-mongo will serialize sessions using `JSON.stringify` before
  159. setting them, and deserialize them with `JSON.parse` when getting them.
  160. (optional, default: true). This is useful if you are using types that
  161. MongoDB doesn't support.
  162. - `serialize` Custom hook for serializing sessions to MongoDB. This is helpful if you need
  163. to modify the session before writing it out.
  164. - `unserialize` Custom hook for unserializing sessions from MongoDB. This can be used in
  165. scenarios where you need to support different types of serializations
  166. (e.g., objects and JSON strings) or need to modify the session before using
  167. it in your app.
  168. - `writeOperationOptions` Options object to pass to every MongoDB write operation call that
  169. supports it (e.g. `update`, `remove`). Useful for adjusting the write concern.
  170. Only exception: If `autoRemove` is set to `'interval'`, the write concern
  171. from the `writeOperationOptions` object will get overwritten.
  172. - `transformId` (optional) Transform original sessionId in whatever you want to use as storage key.
  173. ## Crypto options
  174. - `secret` (optional) Enables transparent crypto in accordance with [OWASP session management recommendations](https://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/Session_Management_Cheat_Sheet.md).
  175. - `algorithm` (optional) Allows for changes to the default symmetric encryption cipher; default is `GCM`. See `crypto.getCiphers()` for supported algorithms.
  176. - `hashing` (optional) May be used to change the default hashing algorithm; default is `sha512`. See `crypto.getHashes()` for supported hashing algorithms.
  177. - `encodeas` (optional) Specify to change the session data cipher text encoding. Default is `hex`.
  178. - `key_size` (optional) When using varying algorithms the key size may be used. Default is `32` based on the `AES` blocksize.
  179. - `iv_size` (optional) This can be used to adjust the default [IV](https://csrc.nist.gov/glossary/term/IV) size if a different algorithm requires a different size. Default is `16`.
  180. - `at_size` (optional) When using newer `AES` modes such as the default `GCM` or `CCM` an authentication tag size can be defined; default is `16`.
  181. ## Tests
  182. ```
  183. docker run --rm -p 27017:27017 mongo:3.6
  184. yarn install
  185. yarn test
  186. ```
  187. The tests use a database called `connect-mongo-test`.
  188. ## Showcase
  189. Open source projects and production apps using `connect-mongo`. Feel free to add yours in a pull request.
  190. * [Builder Book](https://github.com/builderbook/builderbook): Open source web app to write and host documentation or sell books. Built with React, Material-UI, Next, Express, Mongoose, MongoDB.
  191. ## License
  192. The MIT License