Skip to content

Commit 24279aa

Browse files
committed
Supports multiple instances using different credentials
1 parent f5a731c commit 24279aa

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

lib/nano.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ const axios = require('axios').default
1717
const axiosCookieJarSupport = require('axios-cookiejar-support').default
1818
const tough = require('tough-cookie')
1919
axiosCookieJarSupport(axios)
20-
const cookieJar = new tough.CookieJar()
2120
const stream = require('stream')
2221
const http = require('http')
2322
const https = require('https')
@@ -73,6 +72,8 @@ module.exports = exports = function dbScope (cfg) {
7372

7473
cfg = Object.assign({}, cfg)
7574

75+
cfg.cookieJar = new tough.CookieJar()
76+
7677
serverScope.config = cfg
7778
cfg.requestDefaults = cfg.requestDefaults || {}
7879

@@ -273,7 +274,7 @@ module.exports = exports = function dbScope (cfg) {
273274
const isJar = opts.jar || cfg.jar || (cfg.requestDefaults && cfg.requestDefaults.jar)
274275

275276
if (isJar) {
276-
req.jar = cookieJar
277+
req.jar = cfg.cookieJar
277278
req.withCredentials = true
278279
}
279280

@@ -366,7 +367,7 @@ module.exports = exports = function dbScope (cfg) {
366367
// ?drilldown=["author","Dickens"]&drilldown=["publisher","Penguin"]
367368
req.qsStringifyOptions = { arrayFormat: 'repeat' }
368369

369-
cfg.cookies = cookieJar.getCookiesSync(cfg.url)
370+
cfg.cookies = cfg.cookieJar.getCookiesSync(cfg.url)
370371

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

test/nano.auth.test.js

+25-1
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,54 @@
1313
const Nano = require('..')
1414
const COUCH_URL = 'http://localhost:5984'
1515
const nano = Nano({ url: COUCH_URL, jar: true })
16+
const nano2 = Nano({ url: COUCH_URL, jar: true })
1617
const nock = require('nock')
1718

1819
afterEach(() => {
1920
nock.cleanAll()
2021
})
2122

22-
test('should be able to authenticate - POST /_session - nano.auth', async () => {
23+
test('should be able to authenticate multiple separate sessions - POST /_session - nano.auth', async () => {
2324
// mocks
2425
const username = 'u'
26+
const username2 = 'u2'
2527
const password = 'p'
28+
const password2 = 'p2'
2629
const response = { ok: true, name: 'admin', roles: ['_admin', 'admin'] }
30+
const response2 = { ok: true, name: 'operator', roles: ['_admin'] }
2731
const authsession = 'AuthSession=YWRtaW46NUU0MTFBMDE6stHsxYnlDy4mYxwZEcnXHn4fm5w;'
32+
const authsession2 = 'AuthSession=XYZtaW46NUU0MTFBMDE6stHsxYnlDy4mYxwZEcnXHn4123;'
2833
const cookie = authsession + ' Version=1; Expires=Mon, 10-Feb-2050 09:03:21 GMT; Max-Age=600; Path=/; HttpOnly'
34+
const cookie2 = authsession2 + ' Version=1; Expires=Fri Jun 10 2022 20:13:00 GMT; Max-Age=300; Path=/; HttpOnly'
2935
const scope = nock(COUCH_URL)
3036
.post('/_session', 'name=u&password=p', { 'content-type': 'application/x-www-form-urlencoded; charset=utf-8' })
3137
.reply(200, response, { 'Set-Cookie': cookie })
3238
.get('/_all_dbs')
3339
.reply(200, ['a'])
40+
.post('/_session', 'name=u2&password=p2', { 'content-type': 'application/x-www-form-urlencoded; charset=utf-8' })
41+
.reply(200, response2, { 'Set-Cookie': cookie2 })
42+
.get('/_all_dbs')
43+
.reply(200, ['a'])
44+
.get('/_all_dbs')
45+
.reply(200, ['a'])
3446

3547
// test POST /_session
3648
const p = await nano.auth(username, password)
3749
expect(p).toStrictEqual(response)
3850
await nano.db.list()
3951
expect(nano.config.cookies.length).toBe(1)
4052
expect(nano.config.cookies[0].toString().startsWith(authsession)).toBe(true)
53+
54+
// test POST /_session
55+
const p2 = await nano2.auth(username2, password2)
56+
expect(p2).toStrictEqual(response2)
57+
await nano2.db.list()
58+
expect(nano2.config.cookies.length).toBe(1)
59+
expect(nano2.config.cookies[0].toString()).toMatch(new RegExp('^' + authsession2))
60+
61+
await nano.db.list()
62+
expect(nano.config.cookies.length).toBe(1)
63+
expect(nano.config.cookies[0].toString()).toMatch(new RegExp('^' + authsession))
64+
4165
expect(scope.isDone()).toBe(true)
4266
})

0 commit comments

Comments
 (0)