Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 0 additions & 57 deletions auth/couchdbSessionAuthenticator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,11 @@
*/

import { Authenticator, UserOptions, validateInput } from 'ibm-cloud-sdk-core';
import { Cookie, CookieJar, Store } from 'tough-cookie';
import {
SessionTokenManager,
SessionTokenManagerOptions,
} from './sessionTokenManager';

interface SessionCookieJar extends CookieJar {
cloudantPatch: boolean;
store: Store;
}

interface SessionCookie extends Cookie {
cloudantPatchUpdateTime: Date;
}

/** Configuration options for CouchDB session authentication. */
export type CouchdbSessionAuthenticatorOptions = {
/** The username to be used in CouchDB session authentication. */
Expand Down Expand Up @@ -93,53 +83,6 @@ export class CouchdbSessionAuthenticator extends Authenticator {
// Merge the options
Object.assign(this.tokenOptions, userOptions);

// START monkey patch for https://github.com/salesforce/tough-cookie/issues/154
// Check if we've already patched the jar
const cookieJar = this.tokenOptions.jar as SessionCookieJar;
if (cookieJar && !cookieJar.cloudantPatch) {
// Set the patching flag
cookieJar.cloudantPatch = true;
// Replace the store's updateCookie function with one that applies a patch to newCookie
const originalUpdateCookieFn = cookieJar.store.updateCookie;
cookieJar.store.updateCookie = function updateCookie(
oldCookie,
newCookie: SessionCookie,
cb
) {
// Add current time as an update timestamp to the newCookie
newCookie.cloudantPatchUpdateTime = new Date();
// Replace the cookie's expiryTime function with one that uses cloudantPatchUpdateTime
// in place of creation time to check the expiry.
const originalExpiryTimeFn = newCookie.expiryTime;
newCookie.expiryTime = function expiryTime(now) {
// The original expiryTime check is relative to a time in this order:
// 1. supplied now argument
// 2. this.creation (original cookie creation time)
// 3. current time
// This patch replaces 2 with an expiry check relative to the cloudantPatchUpdateTime if set instead of
// the creation time by passing it as the now argument.
return originalExpiryTimeFn.call(
newCookie,
newCookie.cloudantPatchUpdateTime || now
);
};
// Finally, delegate back to the original update function or the fallback put (which is set by Cookie
// when an update function is not present on the store). Since we always set an update function for our
// patch we need to also provide that fallback.
if (originalUpdateCookieFn) {
originalUpdateCookieFn.call(
cookieJar.store,
oldCookie,
newCookie,
cb
);
} else {
cookieJar.store.putCookie.call(cookieJar.store, newCookie, cb);
}
};
}
// END cookie jar monkey patch

// Set the token manager
this.tokenManager = new SessionTokenManager(this.tokenOptions);
}
Expand Down
83 changes: 26 additions & 57 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
},
"dependencies": {
"@types/node": "22.20.5",
"ibm-cloud-sdk-core": "5.6.3"
"ibm-cloud-sdk-core": "5.8.0"
},
"devDependencies": {
"@eslint/js": "9.39.5",
Expand Down Expand Up @@ -69,9 +69,8 @@
"typescript": "6.0.3"
},
"peerDependencies": {
"@types/tough-cookie": "^4.0.0",
"extend": "^3.0.2",
"tough-cookie": "^4.0.0"
"tough-cookie": "^6.0.0"
},
"jest": {
"collectCoverage": true,
Expand Down
7 changes: 5 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
/* Basic Options */
"target": "es2022", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */
"module": "node16", /* Specify module code generation: 'none', commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"module": "node20", /* Specify module code generation: 'none', commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"lib": [
"es2022",
"dom"
Expand All @@ -23,7 +23,10 @@
"moduleResolution": "node16", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"types": ["node"], /* Type declaration files to be included in compilation. */
"resolveJsonModule": true, /* Allows importing modules with a .json extension. */
"esModuleInterop": true
"esModuleInterop": true,
"paths": {
"tough-cookie": ["./node_modules/tough-cookie"]
}
},
"exclude": [
"node_modules",
Expand Down
Loading