Skip to content

Commit 9f2ae36

Browse files
glynnbirdrobman87Robert MichalskiGlynn Bird
authored
Fix cookie/session support in Nano 9 (#245)
* Save authentication cookie after calling nano.auth nano.auth has no effect if session cookie is not saved correctly so it can be reused in next request. * Add closing ) * Comply with coding standard * Comply with coding standard, added missing space after function * Check that cookie is set after calling nano.auth in test case. * Wrap in promise * alternative to pr #242 using toughcookie library for axios Co-authored-by: Robert Michalski <robmi87@gmail.com> Co-authored-by: Robert Michalski <robert@surveylegend.com> Co-authored-by: Glynn Bird <glynnbird@apache.org>
1 parent 88d90ec commit 9f2ae36

File tree

4 files changed

+88
-37
lines changed

4 files changed

+88
-37
lines changed

lib/nano.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ const { URL } = require('url')
1414
const assert = require('assert')
1515
const querystring = require('qs')
1616
const axios = require('axios').default
17+
const axiosCookieJarSupport = require('axios-cookiejar-support').default
18+
const tough = require('tough-cookie')
19+
axiosCookieJarSupport(axios)
20+
const cookieJar = new tough.CookieJar()
1721
const stream = require('stream')
1822
const http = require('http')
1923
const https = require('https')
@@ -223,7 +227,8 @@ module.exports = exports = function dbScope (cfg) {
223227
const isJar = opts.jar || cfg.jar
224228

225229
if (isJar) {
226-
req.jar = isJar
230+
req.jar = cookieJar
231+
req.withCredentials = true
227232
}
228233

229234
// http://wiki.apache.org/couchdb/HTTP_database_API#Naming_and_Addressing
@@ -312,6 +317,7 @@ module.exports = exports = function dbScope (cfg) {
312317
req.qsStringifyOptions = { arrayFormat: 'repeat' }
313318

314319
log(req)
320+
cfg.cookies = cookieJar.getCookiesSync(cfg.url)
315321

316322
// This where the HTTP request is made.
317323
// Nano used to use the now-deprecated "request" library but now we're going to

package-lock.json

Lines changed: 69 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
],
1919
"dependencies": {
2020
"axios": "^0.21.0",
21-
"qs": "^6.9.4"
21+
"axios-cookiejar-support": "^1.0.1",
22+
"qs": "^6.9.4",
23+
"tough-cookie": "^4.0.0"
2224
},
2325
"devDependencies": {
2426
"@types/node": "^14.14.6",

test/nano.auth.test.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
const Nano = require('..')
1414
const COUCH_URL = 'http://localhost:5984'
15-
const nano = Nano(COUCH_URL)
15+
const nano = Nano({ url: COUCH_URL, jar: true })
1616
const nock = require('nock')
1717

1818
afterEach(() => {
@@ -24,12 +24,19 @@ test('should be able to authenticate - POST /_session - nano.auth', async () =>
2424
const username = 'u'
2525
const password = 'p'
2626
const response = { ok: true, name: 'admin', roles: ['_admin', 'admin'] }
27+
const authsession = 'AuthSession=YWRtaW46NUU0MTFBMDE6stHsxYnlDy4mYxwZEcnXHn4fm5w;'
28+
const cookie = authsession + ' Version=1; Expires=Mon, 10-Feb-2050 09:03:21 GMT; Max-Age=600; Path=/; HttpOnly'
2729
const scope = nock(COUCH_URL)
2830
.post('/_session', 'name=u&password=p', { 'content-type': 'application/x-www-form-urlencoded; charset=utf-8' })
29-
.reply(200, response, { 'Set-Cookie': 'AuthSession=YWRtaW46NUU0MTFBMDE6stHsxYnlDy4mYxwZEcnXHn4fm5w; Version=1; Expires=Mon, 10-Feb-2050 09:03:21 GMT; Max-Age=600; Path=/; HttpOnly' })
31+
.reply(200, response, { 'Set-Cookie': cookie })
32+
.get('/_all_dbs')
33+
.reply(200, ['a'])
3034

3135
// test POST /_session
3236
const p = await nano.auth(username, password)
3337
expect(p).toStrictEqual(response)
38+
await nano.db.list()
39+
expect(nano.config.cookies.length).toBe(1)
40+
expect(nano.config.cookies[0].toString().startsWith(authsession)).toBe(true)
3441
expect(scope.isDone()).toBe(true)
3542
})

0 commit comments

Comments
 (0)