@jacebenson/jsn 5.5.7-dev.215.1 → 5.5.7-dev.216.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/sdk.js +23 -15
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jacebenson/jsn",
3
- "version": "5.5.7-dev.215.1",
3
+ "version": "5.5.7-dev.216.1",
4
4
  "description": "A command-line interface for ServiceNow that follows the Unix philosophy: simple, composable, and scriptable.",
5
5
  "type": "module",
6
6
  "bin": {
package/src/sdk.js CHANGED
@@ -39,15 +39,13 @@ function serializeCookies(cookieJar) {
39
39
  return [...cookieJar].map(([name, value]) => `${name}=${value}`).join('; ');
40
40
  }
41
41
 
42
- // REST resources that authenticate from the request's own headers. Every CLI
43
- // invocation is a fresh Node process, so `_warmSession()` always pays 2-3
44
- // sequential requests to mint UI cookies these paths never read — measured on a
45
- // live instance, `jsn records list` spent 4 round-trips on one list. Anything
46
- // else (script processors, GraphQL, the concourse picker, xmlhttp.do, update-set
47
- // export/import, binary attachment uploads) keeps the session bootstrap it
48
- // needs, and JSN_ALWAYS_WARM_SESSION=1 restores it for every path.
49
- // Basic is the exception: normal requests must first detect any TOTP challenge
50
- // before a pending operation can be sent. See docs/AUTH_LIFECYCLE.md.
42
+ // Endpoint capability, not authentication policy: these REST surfaces can use
43
+ // header-only auth. OAuth now prefers the existing UI session even here because
44
+ // tables with ws_access=false reject Bearer while allowing the same user's UI
45
+ // session. JSN_ALWAYS_WARM_SESSION=0 opts OAuth out only on these paths;
46
+ // stateful:true, required UI endpoints and value 1 retain session precedence.
47
+ // Basic/TOTP, supplied token and GCK policies remain unchanged. Internal OAuth
48
+ // bootstrap must bypass warming to avoid recursion. See site/docs/guides/setup.md.
51
49
  const STATELESS_REST_PREFIXES = ['/api/now/table/', '/api/now/stats/', '/api/now/v1/batch'];
52
50
 
53
51
  export function needsUiSession(url, { stateful = false, forceSession = false } = {}) {
@@ -106,7 +104,7 @@ export class SDKClient {
106
104
  this._recoveryPromise = null;
107
105
  }
108
106
 
109
- async _setAuth(req, { skipOAuthSession = false, basicSession = false } = {}) {
107
+ async _setAuth(req, { skipOAuthSession = false, basicSession = false, preferOAuthSession = false, oauthHeaderOnly = false } = {}) {
110
108
  if (!this.authProvider) {
111
109
  throw errAuth('No authentication configured');
112
110
  }
@@ -141,14 +139,19 @@ export class SDKClient {
141
139
  break;
142
140
  case 'token':
143
141
  case 'oauth':
144
- if (!skipOAuthSession) {
142
+ if (!skipOAuthSession || creds.auth_method === 'oauth' && preferOAuthSession) {
145
143
  const cookies = await this._warmSession();
146
144
  if (cookies && this.sessionUserToken) {
145
+ if (creds.auth_method === 'oauth') req.headers.delete('Authorization');
147
146
  req.headers.set('Cookie', cookies);
148
147
  req.headers.set('X-UserToken', this.sessionUserToken);
149
148
  break;
150
149
  }
151
150
  }
151
+ if (creds.auth_method === 'oauth' && oauthHeaderOnly) {
152
+ req.headers.delete('Cookie');
153
+ req.headers.delete('X-UserToken');
154
+ }
152
155
  req.headers.set('Authorization', `Bearer ${creds.access_token}`);
153
156
  break;
154
157
  case 'gck':
@@ -181,14 +184,19 @@ export class SDKClient {
181
184
  try {
182
185
  const req = new Request(endpoint, { ...opts, redirect: 'error', signal: controller.signal });
183
186
  if (prepare) prepare(req);
184
- // Header-authenticated REST skips UI warming for OAuth/token/GCK.
185
- // Basic must still check for TOTP before dispatch; internal bootstrap
186
- // requests explicitly bypass that check to avoid recursion.
187
+ // Endpoint requirements and Basic challenge gates are unchanged. OAuth
188
+ // additionally prefers the UI session on otherwise stateless REST unless
189
+ // explicitly opted out. Internal bootstrap bypasses both to avoid recursion.
187
190
  const skipSession = skipOAuthSession || !needsUiSession(endpoint, {
188
191
  stateful,
189
192
  forceSession: process.env.JSN_ALWAYS_WARM_SESSION === '1',
190
193
  });
191
- await this._setAuth(req, { skipOAuthSession: skipSession, basicSession: !skipOAuthSession });
194
+ await this._setAuth(req, {
195
+ skipOAuthSession: skipSession,
196
+ basicSession: !skipOAuthSession,
197
+ preferOAuthSession: !skipOAuthSession && process.env.JSN_ALWAYS_WARM_SESSION !== '0',
198
+ oauthHeaderOnly: !skipOAuthSession && skipSession && process.env.JSN_ALWAYS_WARM_SESSION === '0',
199
+ });
192
200
  timer = setTimeout(() => controller.abort(), timeout);
193
201
  const response = await fetch(req);
194
202
  if (!skipOAuthSession && this.recovery) {