Skip to content

Commit 007cb19

Browse files
committed
replace hard-coded cookie-based auth w/ dynamic...
lookup based on env (eg, if there is a configured username:pw, use that)
1 parent 83e1a47 commit 007cb19

File tree

5 files changed

+47
-15
lines changed

5 files changed

+47
-15
lines changed

packages/db/src/impl/couch/CouchDBSyncStrategy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { SyncStrategy } from '../common/SyncStrategy';
88
import type { AccountCreationResult, AuthenticationResult } from '../common/types';
99
import { getLocalUserDB, hexEncode, updateGuestAccountExpirationDate } from '../common';
1010
import pouch from './pouchdb-setup';
11-
import { pouchDBincludeCredentialsConfig } from './index';
11+
import { createPouchDBConfig } from './index';
1212
import { getLoggedInUsername } from './auth';
1313

1414
const log = (s: any) => {
@@ -207,7 +207,7 @@ export class CouchDBSyncStrategy implements SyncStrategy {
207207
// see: https://github.com/pouchdb-community/pouchdb-authentication/issues/239
208208
const ret = new pouch(
209209
ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + dbName,
210-
pouchDBincludeCredentialsConfig
210+
createPouchDBConfig()
211211
);
212212

213213
if (guestAccount) {

packages/db/src/impl/couch/adminDB.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pouch from './pouchdb-setup';
22
import { ENV } from '@db/factory';
33
import {
4-
pouchDBincludeCredentialsConfig,
4+
createPouchDBConfig,
55
getStartAndEndKeys,
66
getCredentialledCourseConfig,
77
updateCredentialledCourseConfig,
@@ -21,7 +21,7 @@ export class AdminDB implements AdminDBInterface {
2121
// if the user is not an admin
2222
this.usersDB = new pouch(
2323
ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + '_users',
24-
pouchDBincludeCredentialsConfig
24+
createPouchDBConfig()
2525
);
2626
}
2727

packages/db/src/impl/couch/classroomDB.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import pouch from './pouchdb-setup';
1111
import {
1212
getCourseDB,
1313
getStartAndEndKeys,
14-
pouchDBincludeCredentialsConfig,
14+
createPouchDBConfig,
1515
REVIEW_TIME_FORMAT,
1616
} from '.';
1717
import { CourseDB, getTag } from './courseDB';
@@ -97,7 +97,7 @@ export class StudentClassroomDB
9797
const dbName = `classdb-student-${this._id}`;
9898
this._db = new pouch(
9999
ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + dbName,
100-
pouchDBincludeCredentialsConfig
100+
createPouchDBConfig()
101101
);
102102
try {
103103
const cfg = await this._db.get<ClassroomConfig>(CLASSROOM_CONFIG);
@@ -209,11 +209,11 @@ export class TeacherClassroomDB extends ClassroomDBBase implements TeacherClassr
209209
const stuDbName = `classdb-student-${this._id}`;
210210
this._db = new pouch(
211211
ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + dbName,
212-
pouchDBincludeCredentialsConfig
212+
createPouchDBConfig()
213213
);
214214
this._stuDb = new pouch(
215215
ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + stuDbName,
216-
pouchDBincludeCredentialsConfig
216+
createPouchDBConfig()
217217
);
218218
try {
219219
return this._db
@@ -297,7 +297,7 @@ export function getClassroomDB(classID: string, version: 'student' | 'teacher'):
297297

298298
return new pouch(
299299
ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + dbName,
300-
pouchDBincludeCredentialsConfig
300+
createPouchDBConfig()
301301
);
302302
}
303303

packages/db/src/impl/couch/courseAPI.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pouch from './pouchdb-setup';
2-
import { pouchDBincludeCredentialsConfig } from '.';
2+
import { createPouchDBConfig } from '.';
33
import { ENV } from '@db/factory';
44
// import { DataShape } from '../..base-course/Interfaces/DataShape';
55
import { NameSpacer, ShapeDescriptor } from '@vue-skuilder/common';
@@ -279,6 +279,6 @@ export function getCourseDB(courseID: string): PouchDB.Database {
279279
const dbName = `coursedb-${courseID}`;
280280
return new pouch(
281281
ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + dbName,
282-
pouchDBincludeCredentialsConfig
282+
createPouchDBConfig()
283283
);
284284
}

packages/db/src/impl/couch/index.ts

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,58 @@ export function hexEncode(str: string): string {
3838

3939
return returnStr;
4040
}
41-
export const pouchDBincludeCredentialsConfig: PouchDB.Configuration.RemoteDatabaseConfiguration = {
41+
const pouchDBincludeCredentialsConfig: PouchDB.Configuration.RemoteDatabaseConfiguration = {
4242
fetch(url: string | Request, opts: RequestInit): Promise<Response> {
4343
opts.credentials = 'include';
4444

4545
return (pouch as any).fetch(url, opts);
4646
},
4747
} as PouchDB.Configuration.RemoteDatabaseConfiguration;
4848

49+
/**
50+
* Creates PouchDB configuration with appropriate authentication method
51+
* - Uses HTTP Basic Auth when credentials are available (Node.js/MCP)
52+
* - Falls back to cookie auth for browser environments
53+
*/
54+
export function createPouchDBConfig(): PouchDB.Configuration.RemoteDatabaseConfiguration {
55+
// Check if running in Node.js with explicit credentials
56+
const hasExplicitCredentials = ENV.COUCHDB_USERNAME && ENV.COUCHDB_PASSWORD;
57+
const isNodeEnvironment = typeof window === 'undefined';
58+
59+
if (hasExplicitCredentials && isNodeEnvironment) {
60+
// Use HTTP Basic Auth for Node.js environments (MCP server)
61+
return {
62+
fetch(url: string | Request, opts: RequestInit = {}): Promise<Response> {
63+
const basicAuth = btoa(`${ENV.COUCHDB_USERNAME}:${ENV.COUCHDB_PASSWORD}`);
64+
const headers = new Headers(opts.headers || {});
65+
headers.set('Authorization', `Basic ${basicAuth}`);
66+
67+
const newOpts = {
68+
...opts,
69+
headers: headers
70+
};
71+
72+
return (pouch as any).fetch(url, newOpts);
73+
}
74+
} as PouchDB.Configuration.RemoteDatabaseConfiguration;
75+
}
76+
77+
// Use cookie-based auth for browser environments or when no explicit credentials
78+
return pouchDBincludeCredentialsConfig;
79+
}
80+
4981
function getCouchDB(dbName: string): PouchDB.Database {
5082
return new pouch(
5183
ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + dbName,
52-
pouchDBincludeCredentialsConfig
84+
createPouchDBConfig()
5385
);
5486
}
5587

5688
export function getCourseDB(courseID: string): PouchDB.Database {
5789
// todo: keep a cache of opened courseDBs? need to benchmark this somehow
5890
return new pouch(
5991
ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + 'coursedb-' + courseID,
60-
pouchDBincludeCredentialsConfig
92+
createPouchDBConfig()
6193
);
6294
}
6395

@@ -188,7 +220,7 @@ export function getCouchUserDB(username: string): PouchDB.Database {
188220
// see: https://github.com/pouchdb-community/pouchdb-authentication/issues/239
189221
const ret = new pouch(
190222
ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + dbName,
191-
pouchDBincludeCredentialsConfig
223+
createPouchDBConfig()
192224
);
193225
if (guestAccount) {
194226
updateGuestAccountExpirationDate(ret);

0 commit comments

Comments
 (0)