From 8bfbbac7ff2492f5976fae037aeb2a63ef0dee9e Mon Sep 17 00:00:00 2001 From: paulpascal Date: Sun, 5 May 2024 00:12:25 +0000 Subject: [PATCH] feat: session token authentication --- src/lib/api.js | 6 ++---- src/lib/db.js | 4 ++-- test/api-stub.js | 1 + 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/lib/api.js b/src/lib/api.js index 302fff628..424c8f9f6 100644 --- a/src/lib/api.js +++ b/src/lib/api.js @@ -5,17 +5,15 @@ const archivingApi = require('./archiving-api'); const environment = require('./environment'); const log = require('./log'); const url = require('url'); -const { sessionTokenHeader } = require('./nools-utils'); +const nools = require('./nools-utils'); const cache = new Map(); -// const _request = (method) => (...args) => retry(() => rpn[method](...args), { retries: 5, randomize: false, factor: 1.5 }); - // Helper function to create request headers with session token (if available) const withCookieSession = (options) => { return Object.assign({}, options, { headers: Object.assign({}, options.headers || {}, { - Cookie: sessionTokenHeader(environment) || undefined + Cookie: nools.sessionTokenHeader(environment) || undefined }) }); }; diff --git a/src/lib/db.js b/src/lib/db.js index ec3a5183a..0a8caae04 100644 --- a/src/lib/db.js +++ b/src/lib/db.js @@ -4,13 +4,13 @@ PouchDB.plugin(require('pouchdb-mapreduce')); const ArchivingDB = require('./archiving-db'); const environment = require('./environment'); -const { sessionTokenHeader } = require('./nools-utils'); +const nools = require('./nools-utils'); module.exports = () => { if (environment.isArchiveMode) { return new ArchivingDB(environment.archiveDestination); } - const headers = sessionTokenHeader(environment); + const headers = nools.sessionTokenHeader(environment); return new PouchDB(environment.apiUrl, { ajax: { timeout: 60000, headers } }); }; diff --git a/test/api-stub.js b/test/api-stub.js index 6b0d1e5b6..912fa21ee 100644 --- a/test/api-stub.js +++ b/test/api-stub.js @@ -39,6 +39,7 @@ module.exports = { const port = server.address().port; const couchUrl = `http://admin:pass@localhost:${port}/medic`; sinon.stub(environment, 'apiUrl').get(() => couchUrl); + sinon.stub(environment, 'sessionToken').get(() => undefined); module.exports.couchUrl = couchUrl; module.exports.gatewayRequests = []; },