@mcp-abap-adt/connection 9.4.1 → 10.0.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.
- package/CHANGELOG.md +187 -1
- package/README.md +80 -38
- package/dist/connection/AbstractAbapConnection.d.ts +211 -56
- package/dist/connection/AbstractAbapConnection.d.ts.map +1 -1
- package/dist/connection/AbstractAbapConnection.js +622 -255
- package/dist/connection/CloudHttpTransport.d.ts.map +1 -1
- package/dist/connection/CloudHttpTransport.js +15 -5
- package/dist/connection/CredentialAbapConnection.d.ts +21 -20
- package/dist/connection/CredentialAbapConnection.d.ts.map +1 -1
- package/dist/connection/CredentialAbapConnection.js +39 -46
- package/dist/connection/HttpTransport.d.ts +30 -14
- package/dist/connection/HttpTransport.d.ts.map +1 -1
- package/dist/connection/HttpTransport.js +99 -19
- package/dist/connection/IAdtTransport.d.ts +19 -2
- package/dist/connection/IAdtTransport.d.ts.map +1 -1
- package/dist/connection/OnPremHttpTransport.d.ts.map +1 -1
- package/dist/connection/OnPremHttpTransport.js +4 -3
- package/dist/connection/RfcTransport.d.ts +23 -3
- package/dist/connection/RfcTransport.d.ts.map +1 -1
- package/dist/connection/RfcTransport.js +48 -15
- package/dist/connection/authErrors.d.ts +61 -0
- package/dist/connection/authErrors.d.ts.map +1 -0
- package/dist/connection/authErrors.js +78 -0
- package/dist/connection/requestTarget.d.ts +12 -0
- package/dist/connection/requestTarget.d.ts.map +1 -0
- package/dist/connection/requestTarget.js +23 -0
- package/dist/connection/rfcConversation.d.ts +10 -5
- package/dist/connection/rfcConversation.d.ts.map +1 -1
- package/dist/connection/rfcConversation.js +6 -8
- package/dist/index.d.ts +1 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -12
- package/dist/session/SessionLifecycle.d.ts +1 -1
- package/dist/session/SessionLifecycle.d.ts.map +1 -1
- package/docs/INDEX.md +4 -1
- package/docs/INSTALLATION.md +12 -5
- package/docs/JWT_AUTH_TOOLS.md +2 -2
- package/docs/MIGRATION-10.0.md +284 -0
- package/docs/SCOPE.md +2 -2
- package/docs/STATEFUL_SESSION_GUIDE.md +34 -13
- package/docs/USAGE.md +118 -35
- package/examples/README.md +5 -4
- package/examples/basic-connection.js +1 -1
- package/examples/jwt-with-token-refresh.js +14 -15
- package/examples/saml-connection.js +1 -1
- package/package.json +5 -4
- package/dist/auth/FileCertificateMaterialLoader.d.ts +0 -6
- package/dist/auth/FileCertificateMaterialLoader.d.ts.map +0 -1
- package/dist/auth/FileCertificateMaterialLoader.js +0 -28
- package/dist/auth/ntlm.d.ts +0 -26
- package/dist/auth/ntlm.d.ts.map +0 -1
- package/dist/auth/ntlm.js +0 -72
- package/dist/auth/providers.d.ts +0 -125
- package/dist/auth/providers.d.ts.map +0 -1
- package/dist/auth/providers.js +0 -183
- package/dist/utils/tokenRefresh.d.ts +0 -15
- package/dist/utils/tokenRefresh.d.ts.map +0 -1
- package/dist/utils/tokenRefresh.js +0 -55
|
@@ -6,6 +6,7 @@ const interfaces_adt_connection_1 = require("@mcp-abap-adt/interfaces-adt-connec
|
|
|
6
6
|
const SessionLifecycle_js_1 = require("../session/SessionLifecycle.js");
|
|
7
7
|
const networkErrors_js_1 = require("../utils/networkErrors.js");
|
|
8
8
|
const timeouts_js_1 = require("../utils/timeouts.js");
|
|
9
|
+
const authErrors_js_1 = require("./authErrors.js");
|
|
9
10
|
const csrfConfig_js_1 = require("./csrfConfig.js");
|
|
10
11
|
const IAdtTransport_js_1 = require("./IAdtTransport.js");
|
|
11
12
|
/**
|
|
@@ -42,6 +43,11 @@ function hasHeader(headers, name) {
|
|
|
42
43
|
const wanted = name.toLowerCase();
|
|
43
44
|
return Object.keys(headers).some((key) => key.toLowerCase() === wanted);
|
|
44
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* The request's own headers a credential may not replace, lowercased: the
|
|
48
|
+
* wire's session token and the request's content negotiation.
|
|
49
|
+
*/
|
|
50
|
+
const OUTRANK_THE_CREDENTIAL = ['x-csrf-token', 'content-type', 'accept'];
|
|
45
51
|
class AbstractAbapConnection {
|
|
46
52
|
config;
|
|
47
53
|
transport;
|
|
@@ -150,7 +156,9 @@ class AbstractAbapConnection {
|
|
|
150
156
|
* config: the wire is a fact about the deployment, and a library that
|
|
151
157
|
* picked one would be guessing at the thing this design exists to stop
|
|
152
158
|
* guessing at. It also carries what the caller alone can wire — the
|
|
153
|
-
*
|
|
159
|
+
* client, the address, agent options such as `ca` — which is why it
|
|
160
|
+
* arrives built. The credential's TLS material is not among them: the
|
|
161
|
+
* provider offers it to the wire at logon.
|
|
154
162
|
*/
|
|
155
163
|
transport, logger, sessionId) {
|
|
156
164
|
this.config = config;
|
|
@@ -265,17 +273,10 @@ class AbstractAbapConnection {
|
|
|
265
273
|
/**
|
|
266
274
|
* Gets the credential ready before anything is sent.
|
|
267
275
|
*
|
|
268
|
-
*
|
|
269
|
-
*
|
|
270
|
-
*
|
|
271
|
-
*
|
|
272
|
-
* go out with: a certificate connection reads its material there, and
|
|
273
|
-
* without this the preflight throws `certificate material not loaded` while
|
|
274
|
-
* assembling the transport — before a single request is made, on every
|
|
275
|
-
* system, cloud or on-prem.
|
|
276
|
-
*
|
|
277
|
-
* Must be idempotent: `establishSession()` may prepare the same credential
|
|
278
|
-
* again, and does.
|
|
276
|
+
* `connect()` calls it once, before the wire opens, so that a credential
|
|
277
|
+
* which cannot get ready sends nothing and the wire's logon has something to
|
|
278
|
+
* bring. Nothing prepares again afterwards: not the preflight, not
|
|
279
|
+
* `establishSession()`, not the one more logon a refused one is given.
|
|
279
280
|
*/
|
|
280
281
|
async prepareCredential() { }
|
|
281
282
|
/**
|
|
@@ -297,12 +298,34 @@ class AbstractAbapConnection {
|
|
|
297
298
|
await this.lifecycle.transition('connect', async () => {
|
|
298
299
|
if (this.lifecycle.connected)
|
|
299
300
|
return;
|
|
300
|
-
//
|
|
301
|
-
//
|
|
302
|
-
|
|
303
|
-
//
|
|
304
|
-
|
|
305
|
-
|
|
301
|
+
// Before the wire opens, so that a credential which cannot get ready
|
|
302
|
+
// sends nothing and the wire's logon has something to bring.
|
|
303
|
+
await this.prepareCredential();
|
|
304
|
+
// The wire logs on inside, through the context, and then the session is
|
|
305
|
+
// established: one step, which a refused logon repeats once.
|
|
306
|
+
try {
|
|
307
|
+
await this.establishAndCommit(baselineEpoch);
|
|
308
|
+
}
|
|
309
|
+
catch (error) {
|
|
310
|
+
const rejection = this.credentialRejection(error);
|
|
311
|
+
if (!(error instanceof authErrors_js_1.WireLogonError) ||
|
|
312
|
+
!rejection ||
|
|
313
|
+
this.lifecycle.teardownEpoch !== baselineEpoch) {
|
|
314
|
+
throw error;
|
|
315
|
+
}
|
|
316
|
+
// The step's own failure path has already said goodbye to whatever
|
|
317
|
+
// the wire opened and dropped what it held, so the second attempt
|
|
318
|
+
// starts from nothing.
|
|
319
|
+
await this.renewCredential(rejection, { spent: false });
|
|
320
|
+
try {
|
|
321
|
+
await this.establishAndCommit(baselineEpoch);
|
|
322
|
+
}
|
|
323
|
+
catch (again) {
|
|
324
|
+
if (!(again instanceof authErrors_js_1.WireLogonError))
|
|
325
|
+
throw again;
|
|
326
|
+
throw new authErrors_js_1.AuthRefusedError(authErrors_js_1.REFUSED_AGAIN, 'logon', again.cause);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
306
329
|
});
|
|
307
330
|
}
|
|
308
331
|
/**
|
|
@@ -367,7 +390,7 @@ class AbstractAbapConnection {
|
|
|
367
390
|
// reconnect waits for it. Rejections are absorbed here so that holding
|
|
368
391
|
// the promise never turns a failed goodbye into an unhandled rejection —
|
|
369
392
|
// the transport already logs what went wrong.
|
|
370
|
-
this.goodbye =
|
|
393
|
+
this.goodbye = this.dispatchGoodbye(context);
|
|
371
394
|
this.clearSessionState();
|
|
372
395
|
this.lifecycle.markDisconnected();
|
|
373
396
|
});
|
|
@@ -404,6 +427,33 @@ class AbstractAbapConnection {
|
|
|
404
427
|
clearTimeout(timer);
|
|
405
428
|
}
|
|
406
429
|
}
|
|
430
|
+
/**
|
|
431
|
+
* Say goodbye to the session `context` describes, without waiting for it.
|
|
432
|
+
*
|
|
433
|
+
* The one way every goodbye is sent, so that each behaves the same: a
|
|
434
|
+
* `close()` that rejects and one that throws synchronously — a custom wire
|
|
435
|
+
* may write it as a plain function — are both logged and absorbed, and the
|
|
436
|
+
* promise returned never rejects. Unguarded, a synchronous throw escaped
|
|
437
|
+
* before any `.catch` was attached, replaced the error the caller was owed
|
|
438
|
+
* and skipped the clearing that follows every call site.
|
|
439
|
+
*
|
|
440
|
+
* `close()` is CALLED here, synchronously, not deferred to a later turn: the
|
|
441
|
+
* wires read the cookie jar and the affinity headers on their first line,
|
|
442
|
+
* and every caller clears them right after this returns. A deferred call
|
|
443
|
+
* would send the goodbye without the session it is ending.
|
|
444
|
+
*/
|
|
445
|
+
dispatchGoodbye(context) {
|
|
446
|
+
let closing;
|
|
447
|
+
try {
|
|
448
|
+
closing = Promise.resolve(this.transport.close(context));
|
|
449
|
+
}
|
|
450
|
+
catch (error) {
|
|
451
|
+
closing = Promise.reject(error);
|
|
452
|
+
}
|
|
453
|
+
return closing.catch((error) => {
|
|
454
|
+
this.logger?.debug(`Could not tell the server the session is finished: ${error instanceof Error ? error.message : String(error)}`);
|
|
455
|
+
});
|
|
456
|
+
}
|
|
407
457
|
isConnected() {
|
|
408
458
|
return this.lifecycle.connected;
|
|
409
459
|
}
|
|
@@ -420,37 +470,6 @@ class AbstractAbapConnection {
|
|
|
420
470
|
getSessionIdentity() {
|
|
421
471
|
return this.lifecycle.identity;
|
|
422
472
|
}
|
|
423
|
-
/**
|
|
424
|
-
* Re-establishes the session for a request that is recovering from a
|
|
425
|
-
* credential renewal, then lets that request retry.
|
|
426
|
-
*
|
|
427
|
-
* Runs as its own `recover` transition, which never joins another: each
|
|
428
|
-
* recovery carries the baseline of its own request. It yields to a caller's
|
|
429
|
-
* teardown — if the epoch moved since `baselineEpoch`, someone asked to stop
|
|
430
|
-
* while this was being prepared, and a retry must not resurrect a session
|
|
431
|
-
* they discarded.
|
|
432
|
-
*
|
|
433
|
-
* The transition queues behind the cleanup that the renewal itself raised, so
|
|
434
|
-
* it never re-establishes on top of stale transport state.
|
|
435
|
-
*/
|
|
436
|
-
async recoverSession(baselineEpoch) {
|
|
437
|
-
await this.lifecycle.transition('recover', async () => {
|
|
438
|
-
await this.establishAndCommit(baselineEpoch);
|
|
439
|
-
});
|
|
440
|
-
}
|
|
441
|
-
/**
|
|
442
|
-
* Establishes a session and publishes it — but only if nobody asked to stop
|
|
443
|
-
* meanwhile.
|
|
444
|
-
*
|
|
445
|
-
* The epoch is checked BEFORE, so a teardown already requested costs no round
|
|
446
|
-
* trip, and AFTER, because establishment takes time and a caller can ask to
|
|
447
|
-
* stop during it. Checking only before is the defect this exists to prevent:
|
|
448
|
-
* markConnected() would then clear the teardown state and hand back a session
|
|
449
|
-
* the caller had already discarded.
|
|
450
|
-
*
|
|
451
|
-
* Shared by connect() and recoverSession() rather than written twice —
|
|
452
|
-
* the two drifted apart once already, and a third caller would drift again.
|
|
453
|
-
*/
|
|
454
473
|
/**
|
|
455
474
|
* What a session strategy is given: enough to make one request and to prove
|
|
456
475
|
* the session is ours, and nothing else. It cannot reach session state, so a
|
|
@@ -476,11 +495,25 @@ class AbstractAbapConnection {
|
|
|
476
495
|
sessionContext() {
|
|
477
496
|
return {
|
|
478
497
|
baseUrl: this.baseUrl,
|
|
479
|
-
|
|
498
|
+
authorize: (headers) => this.credentialHeaders(headers),
|
|
499
|
+
logon: (target) => this.logon(target),
|
|
480
500
|
extraHeaders: { 'sap-adt-connection-id': this.getSessionId() ?? '' },
|
|
481
501
|
observe: (headers) => this.observeResponse(headers),
|
|
482
502
|
};
|
|
483
503
|
}
|
|
504
|
+
/**
|
|
505
|
+
* Establishes a session and publishes it — but only if nobody asked to stop
|
|
506
|
+
* meanwhile.
|
|
507
|
+
*
|
|
508
|
+
* The epoch is checked BEFORE, so a teardown already requested costs no round
|
|
509
|
+
* trip, and AFTER, because establishment takes time and a caller can ask to
|
|
510
|
+
* stop during it. Checking only before is the defect this exists to prevent:
|
|
511
|
+
* markConnected() would then clear the teardown state and hand back a session
|
|
512
|
+
* the caller had already discarded.
|
|
513
|
+
*
|
|
514
|
+
* Shared by connect() and its one more attempt after a refused logon rather
|
|
515
|
+
* than written twice — the two would drift apart, as they did once before.
|
|
516
|
+
*/
|
|
484
517
|
async establishAndCommit(baselineEpoch) {
|
|
485
518
|
if (this.lifecycle.teardownEpoch !== baselineEpoch) {
|
|
486
519
|
throw (0, SessionLifecycle_js_1.sessionError)(interfaces_adt_connection_1.ADT_SESSION_ERROR.NOT_CONNECTED, 'Establishment abandoned: a teardown was requested for this connection');
|
|
@@ -492,13 +525,11 @@ class AbstractAbapConnection {
|
|
|
492
525
|
// makes the new fingerprint `established`, which is what it is.
|
|
493
526
|
this.lifecycle.forgetIdentity();
|
|
494
527
|
try {
|
|
495
|
-
// First, because the preflight below has to be able to authenticate: the
|
|
496
|
-
// credential of a certificate connection is not in hand until
|
|
497
|
-
// it is loaded, and assembling a request without it throws.
|
|
498
|
-
await this.prepareCredential();
|
|
499
528
|
// Before the establishing call, because on a system that has one this is
|
|
500
529
|
// what creates the session the rest of the connection runs in — and the
|
|
501
|
-
// cookies it sets are the ones the establishing call must carry.
|
|
530
|
+
// cookies it sets are the ones the establishing call must carry. The wire
|
|
531
|
+
// logs on first, through the context, before anything is sent.
|
|
532
|
+
await this.transport.open(this.sessionContext());
|
|
502
533
|
// Forgotten again, because the open was OURS. Establishment is now two
|
|
503
534
|
// requests where it used to be one, and the identity policy answers "did
|
|
504
535
|
// the server move us to a different session while we were working" — a
|
|
@@ -512,7 +543,7 @@ class AbstractAbapConnection {
|
|
|
512
543
|
// A failed establishment leaves debris that poisons the next attempt: the
|
|
513
544
|
// 401 that rejected us may still have carried a Set-Cookie, and every
|
|
514
545
|
// subclass treats a cookie as proof that auth is already settled —
|
|
515
|
-
//
|
|
546
|
+
// a credential that sees a cookie may skip its own header. So the next
|
|
516
547
|
// connect() would go out with NO credentials at all, and be rejected for
|
|
517
548
|
// a reason that has nothing to do with why the first one failed.
|
|
518
549
|
//
|
|
@@ -538,7 +569,11 @@ class AbstractAbapConnection {
|
|
|
538
569
|
// session has an address or it does not, and an on-prem one was
|
|
539
570
|
// established or the cookie is debris from the refusal. The connection
|
|
540
571
|
// asks, and each wire answers by doing nothing when it has nothing.
|
|
541
|
-
|
|
572
|
+
// Its failure is absorbed, a synchronous throw included: a wire's
|
|
573
|
+
// close() may reject — its context.authorize throws when the provider
|
|
574
|
+
// refuses — and nobody awaits this one. A throw that escaped here would
|
|
575
|
+
// replace the establishment error and skip the clearing below.
|
|
576
|
+
const goodbye = this.dispatchGoodbye(this.sessionContext());
|
|
542
577
|
this.invalidateSession();
|
|
543
578
|
// And the identity with it. The rejecting response was still observed, so
|
|
544
579
|
// its cookie was recorded as a session that had just been established —
|
|
@@ -548,7 +583,7 @@ class AbstractAbapConnection {
|
|
|
548
583
|
this.lifecycle.markDisconnected();
|
|
549
584
|
// Not awaited, for the same reason a teardown does not wait: the caller
|
|
550
585
|
// is owed the establishment error now, not after a round trip nobody is
|
|
551
|
-
// waiting on.
|
|
586
|
+
// waiting on. Its rejection was absorbed above, so nothing goes unhandled.
|
|
552
587
|
void goodbye;
|
|
553
588
|
throw error;
|
|
554
589
|
}
|
|
@@ -600,12 +635,9 @@ class AbstractAbapConnection {
|
|
|
600
635
|
// preflight may have opened a session — on cloud it does — and this path
|
|
601
636
|
// is about to drop the cookies that are the only permission to close it.
|
|
602
637
|
// Refusing to connect must not leak the session the refusal is about.
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
catch (error) {
|
|
607
|
-
this.logger?.debug(`Could not tell the server the session is finished: ${error instanceof Error ? error.message : String(error)}`);
|
|
608
|
-
}
|
|
638
|
+
// Dispatched, not awaited; a close that throws or rejects is logged and
|
|
639
|
+
// absorbed, never left unhandled.
|
|
640
|
+
void this.dispatchGoodbye(this.sessionContext());
|
|
609
641
|
this.invalidateSession();
|
|
610
642
|
this.lifecycle.forgetIdentity();
|
|
611
643
|
this.lifecycle.markDisconnected();
|
|
@@ -633,14 +665,16 @@ class AbstractAbapConnection {
|
|
|
633
665
|
/**
|
|
634
666
|
* Raises a session-lost teardown from inside request handling.
|
|
635
667
|
*
|
|
636
|
-
* There are exactly
|
|
668
|
+
* There are exactly two things that can cost us the ABAP session, and they
|
|
637
669
|
* were found one at a time precisely because they were written apart. They go
|
|
638
|
-
* through here so a
|
|
670
|
+
* through here so a third joins the list instead of inventing its own
|
|
639
671
|
* sequence:
|
|
640
672
|
*
|
|
641
|
-
* - the credential was renewed (the injected auth says so);
|
|
642
673
|
* - the server says the session is gone (a dead-session response);
|
|
643
|
-
* - the tracked cookie changed under us
|
|
674
|
+
* - the tracked cookie changed under us.
|
|
675
|
+
*
|
|
676
|
+
* A renewed credential is not one: the session survives a renewal, and a
|
|
677
|
+
* resend that finds it replaced is the second case.
|
|
644
678
|
*
|
|
645
679
|
* `internal` origin, so it does not cancel the recovery that raised it, and
|
|
646
680
|
* `sessionLost`, so admission shuts at once and the identity is dropped
|
|
@@ -658,10 +692,6 @@ class AbstractAbapConnection {
|
|
|
658
692
|
this.lifecycle.markDisconnected();
|
|
659
693
|
});
|
|
660
694
|
}
|
|
661
|
-
/** The credential-renewal raiser; see raiseSessionLost(). */
|
|
662
|
-
discardSession() {
|
|
663
|
-
this.raiseSessionLost('the credential backing it was renewed');
|
|
664
|
-
}
|
|
665
695
|
/**
|
|
666
696
|
* Whether an error is this connection's own verdict about the session rather
|
|
667
697
|
* than something the server said about a request.
|
|
@@ -777,14 +807,38 @@ class AbstractAbapConnection {
|
|
|
777
807
|
}
|
|
778
808
|
async getAuthHeaders() {
|
|
779
809
|
const headers = {};
|
|
810
|
+
await this.credentialHeaders(headers);
|
|
811
|
+
return headers;
|
|
812
|
+
}
|
|
813
|
+
/**
|
|
814
|
+
* What every request carries: the client, then whatever the credential
|
|
815
|
+
* writes. On every path, the establishing call included.
|
|
816
|
+
*/
|
|
817
|
+
async credentialHeaders(headers) {
|
|
780
818
|
if (this.config.client) {
|
|
781
819
|
headers['X-SAP-Client'] = this.config.client;
|
|
782
820
|
}
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
821
|
+
await this.authorizeRequest(headers);
|
|
822
|
+
}
|
|
823
|
+
/**
|
|
824
|
+
* The credential writes what this request carries into `headers`. Asked per
|
|
825
|
+
* attempt, never held. Throws when the credential refuses. The base has no
|
|
826
|
+
* credential and writes nothing.
|
|
827
|
+
*/
|
|
828
|
+
async authorizeRequest(_headers) { }
|
|
829
|
+
/**
|
|
830
|
+
* A logon is being made; the credential says what it brings. Throws when the
|
|
831
|
+
* credential refuses. The base has no credential and brings nothing.
|
|
832
|
+
*/
|
|
833
|
+
async logon(_target) { }
|
|
834
|
+
/**
|
|
835
|
+
* The system refused the credential. The answer is the credential's: Ok
|
|
836
|
+
* means it renewed and one more attempt is worth making; `thrown`, when
|
|
837
|
+
* present, is what the provider threw instead of answering. The base has
|
|
838
|
+
* nothing to renew.
|
|
839
|
+
*/
|
|
840
|
+
async credentialRejected(_rejection) {
|
|
841
|
+
return { outcome: { ok: false, refusal: authErrors_js_1.NO_CREDENTIAL_TO_RENEW } };
|
|
788
842
|
}
|
|
789
843
|
async makeAdtRequest(options) {
|
|
790
844
|
// Admission first, synchronously, before any await: the check and the
|
|
@@ -806,23 +860,21 @@ class AbstractAbapConnection {
|
|
|
806
860
|
// The PATH, not an address. Which server it belongs in front of — or
|
|
807
861
|
// whether it belongs in front of one at all — is the wire's to say.
|
|
808
862
|
const requestUrl = endpoint;
|
|
809
|
-
|
|
810
|
-
// The retry logic will handle CSRF token errors automatically
|
|
811
|
-
if (normalizedMethod === 'POST' ||
|
|
863
|
+
const mutation = normalizedMethod === 'POST' ||
|
|
812
864
|
normalizedMethod === 'PUT' ||
|
|
813
|
-
normalizedMethod === 'DELETE'
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
}
|
|
865
|
+
normalizedMethod === 'DELETE';
|
|
866
|
+
// One per request (H5): whichever of the upfront token fetch or a later
|
|
867
|
+
// rejection meets a refused credential first spends it, and a second
|
|
868
|
+
// refusal anywhere in the same request is the verdict.
|
|
869
|
+
const renewal = { spent: false };
|
|
870
|
+
// Get the wire ready for a mutation before sending it. A failure of the
|
|
871
|
+
// exchange itself does not stop the request — the CSRF recovery below
|
|
872
|
+
// handles a token that is missing — but a refused credential does.
|
|
873
|
+
if (mutation && !this.transport.csrfToken()) {
|
|
874
|
+
await this.readyWireUpfront(renewal, lease);
|
|
824
875
|
}
|
|
825
|
-
//
|
|
876
|
+
// The request's own headers: everything but the credential's, which is
|
|
877
|
+
// written fresh onto a copy of these for every attempt (`authorizedFrom`).
|
|
826
878
|
const requestHeaders = {};
|
|
827
879
|
if (!customHeaders || !customHeaders.Accept) {
|
|
828
880
|
requestHeaders.Accept =
|
|
@@ -864,15 +916,10 @@ class AbstractAbapConnection {
|
|
|
864
916
|
// `x-sap-adt-sessiontype: stateful` and the context cookie, RFC with
|
|
865
917
|
// neither. It travels as `stateful` on the request below, and each
|
|
866
918
|
// transport expresses it its own way.
|
|
867
|
-
// Add auth headers (these MUST NOT be overridden)
|
|
868
|
-
Object.assign(requestHeaders, await this.getAuthHeaders());
|
|
869
919
|
// Read once: the wire is asked what it holds, and the same value is what
|
|
870
920
|
// goes on the header.
|
|
871
921
|
const presented = this.transport.csrfToken();
|
|
872
|
-
if (
|
|
873
|
-
normalizedMethod === 'PUT' ||
|
|
874
|
-
normalizedMethod === 'DELETE') &&
|
|
875
|
-
presented) {
|
|
922
|
+
if (mutation && presented) {
|
|
876
923
|
requestHeaders['x-csrf-token'] = presented;
|
|
877
924
|
}
|
|
878
925
|
// No cookies and no affinity headers here. Both are the wire's own state,
|
|
@@ -906,7 +953,14 @@ class AbstractAbapConnection {
|
|
|
906
953
|
const requestConfig = {
|
|
907
954
|
method: normalizedMethod,
|
|
908
955
|
url: requestUrl,
|
|
909
|
-
headers
|
|
956
|
+
// What the credential writes outranks the caller's custom headers, and
|
|
957
|
+
// is written anew for every resend below; the wire's session token and
|
|
958
|
+
// the request's content negotiation outrank it (`authorizedFrom`).
|
|
959
|
+
// A provider that answers after the session went away answers for a
|
|
960
|
+
// request that no longer has one.
|
|
961
|
+
headers: await this.authorizedFrom(requestHeaders).catch((error) => {
|
|
962
|
+
throw this.staleOr(error, lease);
|
|
963
|
+
}),
|
|
910
964
|
timeout: effectiveTimeout,
|
|
911
965
|
// `unknown` on the caller's options, a record on the seam: the two
|
|
912
966
|
// transports serialise a query differently and both need the pairs.
|
|
@@ -922,8 +976,7 @@ class AbstractAbapConnection {
|
|
|
922
976
|
method: normalizedMethod,
|
|
923
977
|
});
|
|
924
978
|
try {
|
|
925
|
-
const response = await this.
|
|
926
|
-
this.observeResponse(response.headers, lease.generation);
|
|
979
|
+
const response = await this.sendObserved(requestConfig, lease);
|
|
927
980
|
this.logger?.debug(`Request succeeded with status ${response.status}`, {
|
|
928
981
|
type: 'REQUEST_SUCCESS',
|
|
929
982
|
status: response.status,
|
|
@@ -933,155 +986,425 @@ class AbstractAbapConnection {
|
|
|
933
986
|
return response;
|
|
934
987
|
}
|
|
935
988
|
catch (error) {
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
989
|
+
let survived;
|
|
990
|
+
try {
|
|
991
|
+
return await this.recoverOnWire(error, requestConfig, requestHeaders, lease);
|
|
992
|
+
}
|
|
993
|
+
catch (failure) {
|
|
994
|
+
survived = failure;
|
|
995
|
+
}
|
|
996
|
+
return await this.answerCredentialFailure(survived, requestConfig, requestHeaders, lease, renewal);
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
/**
|
|
1000
|
+
* One attempt: sent, and its response folded into the session.
|
|
1001
|
+
*
|
|
1002
|
+
* Only while the session the request was admitted to is still the current
|
|
1003
|
+
* one. Every attempt passes here — the first and each resend — and a resend
|
|
1004
|
+
* can follow an await long enough for the caller to disconnect and connect
|
|
1005
|
+
* again; sent then, it would carry the new session's cookie and token and
|
|
1006
|
+
* act inside a session the request was never admitted to.
|
|
1007
|
+
*/
|
|
1008
|
+
async sendObserved(requestConfig, lease) {
|
|
1009
|
+
if (!this.lifecycle.isCurrent(lease)) {
|
|
1010
|
+
throw (0, SessionLifecycle_js_1.sessionError)(interfaces_adt_connection_1.ADT_SESSION_ERROR.NOT_CONNECTED, 'The session this request was admitted to is gone; nothing was sent');
|
|
1011
|
+
}
|
|
1012
|
+
const response = await this.transport.send(requestConfig);
|
|
1013
|
+
this.observeResponse(response.headers, lease.generation);
|
|
1014
|
+
return response;
|
|
1015
|
+
}
|
|
1016
|
+
/**
|
|
1017
|
+
* One attempt's headers: the request's own, with what the credential writes
|
|
1018
|
+
* for THIS attempt on top.
|
|
1019
|
+
*
|
|
1020
|
+
* A fresh record every time, never the previous attempt's: a provider that
|
|
1021
|
+
* wrote `Authorization` and, once renewed, writes only cookies — or renames
|
|
1022
|
+
* its cookie — would otherwise send the stale value beside the new one. The
|
|
1023
|
+
* credential's cookies merge into the request's `Cookie` on the copy, so the
|
|
1024
|
+
* wire's session cookies in `base` survive and a stale credential cookie
|
|
1025
|
+
* does not.
|
|
1026
|
+
*
|
|
1027
|
+
* The credential owns authentication only. It outranks the caller's custom
|
|
1028
|
+
* headers, but not the wire's CSRF token nor the request's `Content-Type`
|
|
1029
|
+
* and `Accept`: those are put back from `base` whichever way the provider
|
|
1030
|
+
* spelled them — as the wire's own token fetch already does.
|
|
1031
|
+
*/
|
|
1032
|
+
async authorizedFrom(base) {
|
|
1033
|
+
const headers = { ...base };
|
|
1034
|
+
await this.credentialHeaders(headers);
|
|
1035
|
+
for (const name of OUTRANK_THE_CREDENTIAL) {
|
|
1036
|
+
const own = Object.keys(base).filter((key) => key.toLowerCase() === name);
|
|
1037
|
+
if (own.length === 0)
|
|
1038
|
+
continue;
|
|
1039
|
+
for (const key of Object.keys(headers)) {
|
|
1040
|
+
if (key.toLowerCase() === name)
|
|
1041
|
+
delete headers[key];
|
|
949
1042
|
}
|
|
950
|
-
const
|
|
951
|
-
|
|
952
|
-
|
|
1043
|
+
for (const key of own)
|
|
1044
|
+
headers[key] = base[key];
|
|
1045
|
+
}
|
|
1046
|
+
return headers;
|
|
1047
|
+
}
|
|
1048
|
+
/**
|
|
1049
|
+
* The wire's own recovery from a failed attempt: the session faults it can
|
|
1050
|
+
* cure — a stale CSRF token, a login-form 401 on a mutation, a GET 401 that
|
|
1051
|
+
* cookies answer — and the verdicts it must not hide.
|
|
1052
|
+
*
|
|
1053
|
+
* Runs before anything asks the credential, because it cures session
|
|
1054
|
+
* faults, not credential ones: a stale CSRF token under Basic must not
|
|
1055
|
+
* become "the user or password was refused". What it cannot cure it throws,
|
|
1056
|
+
* and a credential failure among that goes on to the provider. One
|
|
1057
|
+
* exception: inside a critical section the login-form 401 is not cured
|
|
1058
|
+
* here, because curing it discards the session and the lock with it.
|
|
1059
|
+
*
|
|
1060
|
+
* `requestHeaders` are the request's own, without the credential's: what
|
|
1061
|
+
* the recovery adds — a new token, the jar's cookies — goes there, and each
|
|
1062
|
+
* resend is authorized anew on top (`authorizedFrom`).
|
|
1063
|
+
*/
|
|
1064
|
+
async recoverOnWire(error, requestConfig, requestHeaders, lease) {
|
|
1065
|
+
const requestUrl = requestConfig.url;
|
|
1066
|
+
const normalizedMethod = requestConfig.method;
|
|
1067
|
+
// FENCE FIRST, before anything reads or writes shared state.
|
|
1068
|
+
//
|
|
1069
|
+
// Fencing observeResponse() alone was not enough, and the gap was wide:
|
|
1070
|
+
// everything below acts on this connection, not on the request. A late
|
|
1071
|
+
// 400 "session not found" would call raiseSessionLost() and tear down the
|
|
1072
|
+
// healthy session established since; a late 401/403 would call
|
|
1073
|
+
// invalidateSession(), write a fresh CSRF token, and RETRY — replaying a
|
|
1074
|
+
// mutation from a dead session inside the live one.
|
|
1075
|
+
//
|
|
1076
|
+
// A stale request gets its error back and nothing else happens.
|
|
1077
|
+
if (!this.lifecycle.isCurrent(lease)) {
|
|
1078
|
+
this.logger?.debug('A request from a previous session failed; its recovery is fenced');
|
|
1079
|
+
throw error;
|
|
1080
|
+
}
|
|
1081
|
+
const errorDetails = {
|
|
1082
|
+
type: 'REQUEST_ERROR',
|
|
1083
|
+
message: error instanceof Error ? error.message : String(error),
|
|
1084
|
+
url: requestUrl,
|
|
1085
|
+
method: normalizedMethod,
|
|
1086
|
+
status: (0, IAdtTransport_js_1.refusalOf)(error)?.status,
|
|
1087
|
+
data: undefined,
|
|
1088
|
+
};
|
|
1089
|
+
const refusal = (0, IAdtTransport_js_1.refusalOf)(error);
|
|
1090
|
+
if (refusal) {
|
|
1091
|
+
errorDetails.data =
|
|
1092
|
+
typeof refusal.data === 'string'
|
|
1093
|
+
? refusal.data.slice(0, 200)
|
|
1094
|
+
: JSON.stringify(refusal.data).slice(0, 200);
|
|
1095
|
+
// Every wire's refusal, not only axios's. A response the connection
|
|
1096
|
+
// never observed is a session replacement it never noticed.
|
|
1097
|
+
this.observeResponse(refusal.headers, lease.generation);
|
|
1098
|
+
}
|
|
1099
|
+
this.throwIfSessionDead(error, lease);
|
|
1100
|
+
// Check if this is a network error (connection refused, timeout, DNS, etc.)
|
|
1101
|
+
// Don't retry for network errors - these indicate infrastructure/VPN issues
|
|
1102
|
+
const networkError = (0, networkErrors_js_1.isNetworkError)(error);
|
|
1103
|
+
if (networkError) {
|
|
1104
|
+
this.logger?.error(`Network error - cannot connect to SAP system: ${errorDetails.message}`, errorDetails);
|
|
1105
|
+
throw error;
|
|
1106
|
+
}
|
|
1107
|
+
// Log 404 as debug (common for existence checks), other errors as error
|
|
1108
|
+
if (errorDetails.status === 404) {
|
|
1109
|
+
this.logger?.debug(errorDetails.message, errorDetails);
|
|
1110
|
+
}
|
|
1111
|
+
else {
|
|
1112
|
+
this.logger?.error(errorDetails.message, errorDetails);
|
|
1113
|
+
}
|
|
1114
|
+
// The "login-form 401": SAP refused a mutation while we hold a cached CSRF
|
|
1115
|
+
// token, so the token and the session it is bound to are dead and must be
|
|
1116
|
+
// discarded before the retry.
|
|
1117
|
+
//
|
|
1118
|
+
// These retries cure the session. A 401 that survives them is the
|
|
1119
|
+
// credential's, not the session's.
|
|
1120
|
+
const isCachedTokenStale = (normalizedMethod === 'POST' ||
|
|
1121
|
+
normalizedMethod === 'PUT' ||
|
|
1122
|
+
normalizedMethod === 'DELETE') &&
|
|
1123
|
+
(0, IAdtTransport_js_1.refusalOf)(error)?.status === 401 &&
|
|
1124
|
+
this.getCsrfToken() !== null;
|
|
1125
|
+
// Not inside a critical section. Curing the login-form 401 discards the
|
|
1126
|
+
// session, and with it the lock the section exists to hold; a renewal
|
|
1127
|
+
// must keep it (goal decision 2). There the 401 goes to the provider,
|
|
1128
|
+
// whose Ok resends on the same session — and a session SAP really
|
|
1129
|
+
// replaced is reported as SESSION_REPLACED from that resend's refusal.
|
|
1130
|
+
// Keyed on the section, not the session mode: the write under a lock is
|
|
1131
|
+
// itself stateless.
|
|
1132
|
+
if (isCachedTokenStale && this.inCriticalSection)
|
|
1133
|
+
throw error;
|
|
1134
|
+
// Retry logic for CSRF token errors (403 with CSRF message) and the
|
|
1135
|
+
// login-form 401 pattern.
|
|
1136
|
+
if (this.shouldRetryCsrf(error, normalizedMethod) || isCachedTokenStale) {
|
|
1137
|
+
this.logger?.debug(isCachedTokenStale
|
|
1138
|
+
? 'Stale CSRF token / SAP session — invalidating and retrying'
|
|
1139
|
+
: 'CSRF token validation failed, fetching new token and retrying request', {
|
|
953
1140
|
url: requestUrl,
|
|
954
1141
|
method: normalizedMethod,
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
errorDetails.data =
|
|
961
|
-
typeof refusal.data === 'string'
|
|
962
|
-
? refusal.data.slice(0, 200)
|
|
963
|
-
: JSON.stringify(refusal.data).slice(0, 200);
|
|
964
|
-
// Every wire's refusal, not only axios's. A response the connection
|
|
965
|
-
// never observed is a session replacement it never noticed.
|
|
966
|
-
this.observeResponse(refusal.headers, lease.generation);
|
|
1142
|
+
});
|
|
1143
|
+
if (isCachedTokenStale) {
|
|
1144
|
+
this.invalidateSession();
|
|
1145
|
+
delete requestHeaders.Cookie;
|
|
1146
|
+
delete requestHeaders.cookie;
|
|
967
1147
|
}
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
1148
|
+
try {
|
|
1149
|
+
this.setCsrfToken(await this.fetchCsrfToken(requestUrl, 5, 2000, lease));
|
|
1150
|
+
const refreshedToken = this.getCsrfToken();
|
|
1151
|
+
if (refreshedToken) {
|
|
1152
|
+
requestHeaders['x-csrf-token'] = refreshedToken;
|
|
1153
|
+
}
|
|
1154
|
+
const refreshedCookies = this.getCookies();
|
|
1155
|
+
if (refreshedCookies) {
|
|
1156
|
+
requestHeaders.Cookie = refreshedCookies;
|
|
1157
|
+
}
|
|
1158
|
+
// Re-authorized: a provider may have renewed since the first attempt.
|
|
1159
|
+
requestConfig.headers = await this.authorizedFrom(requestHeaders);
|
|
1160
|
+
return await this.sendObserved(requestConfig, lease);
|
|
977
1161
|
}
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
1162
|
+
catch (retryError) {
|
|
1163
|
+
// The session went away while the retry was out: nothing it met —
|
|
1164
|
+
// the provider's refusal included — is about this request's session.
|
|
1165
|
+
if (!this.lifecycle.isCurrent(lease)) {
|
|
1166
|
+
throw this.staleVerdict(retryError);
|
|
1167
|
+
}
|
|
1168
|
+
this.throwIfSessionDead(retryError, lease);
|
|
1169
|
+
// A session verdict outranks the error that started the retry: the
|
|
1170
|
+
// caller can retry a 403 itself, but it cannot discover that its lock
|
|
1171
|
+
// handle is dead from a 403. So does the credential's failure, which
|
|
1172
|
+
// the 403 would hide from the provider.
|
|
1173
|
+
if (this.outranksOriginal(retryError)) {
|
|
1174
|
+
throw retryError;
|
|
1175
|
+
}
|
|
1176
|
+
this.logger?.debug(`CSRF retry failed; rethrowing original error: ${retryError instanceof Error
|
|
1177
|
+
? retryError.message
|
|
1178
|
+
: String(retryError)}`);
|
|
983
1179
|
throw error;
|
|
984
1180
|
}
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
1181
|
+
}
|
|
1182
|
+
// A 401 on a GET where cookies have since arrived: the first request had
|
|
1183
|
+
// none, and the session they name is what the retry needs. Guarded below
|
|
1184
|
+
// on actually holding some, so a wire that issues no cookies — RFC —
|
|
1185
|
+
// never takes it.
|
|
1186
|
+
if ((0, IAdtTransport_js_1.refusalOf)(error)?.status === 401 && normalizedMethod === 'GET') {
|
|
1187
|
+
// If we already have cookies from error response, retry immediately
|
|
1188
|
+
const afterError = this.transport.cookies();
|
|
1189
|
+
if (afterError) {
|
|
1190
|
+
this.logger?.debug(`401 on GET request, retrying with cookies from error response`);
|
|
1191
|
+
requestHeaders.Cookie = afterError;
|
|
1192
|
+
try {
|
|
1193
|
+
requestConfig.headers = await this.authorizedFrom(requestHeaders);
|
|
1194
|
+
return await this.sendObserved(requestConfig, lease);
|
|
1195
|
+
}
|
|
1196
|
+
catch (resendError) {
|
|
1197
|
+
throw this.staleOr(resendError, lease);
|
|
1198
|
+
}
|
|
991
1199
|
}
|
|
992
|
-
//
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
normalizedMethod === 'PUT' ||
|
|
1004
|
-
normalizedMethod === 'DELETE') &&
|
|
1005
|
-
(0, IAdtTransport_js_1.refusalOf)(error)?.status === 401 &&
|
|
1006
|
-
this.getCsrfToken() !== null;
|
|
1007
|
-
// Retry logic for CSRF token errors (403 with CSRF message) and the
|
|
1008
|
-
// login-form 401 pattern.
|
|
1009
|
-
if (this.shouldRetryCsrf(error, normalizedMethod) || isCachedTokenStale) {
|
|
1010
|
-
this.logger?.debug(isCachedTokenStale
|
|
1011
|
-
? 'Stale CSRF token / SAP session — invalidating and retrying'
|
|
1012
|
-
: 'CSRF token validation failed, fetching new token and retrying request', {
|
|
1013
|
-
url: requestUrl,
|
|
1014
|
-
method: normalizedMethod,
|
|
1015
|
-
});
|
|
1016
|
-
if (isCachedTokenStale) {
|
|
1017
|
-
this.invalidateSession();
|
|
1018
|
-
delete requestHeaders.Cookie;
|
|
1019
|
-
delete requestHeaders.cookie;
|
|
1200
|
+
// If no cookies, try to get them via CSRF token fetch
|
|
1201
|
+
this.logger?.debug(`401 on GET request, attempting to get cookies via CSRF token fetch`);
|
|
1202
|
+
try {
|
|
1203
|
+
// Try to get CSRF token (this will also get cookies)
|
|
1204
|
+
this.setCsrfToken(await this.fetchCsrfToken(requestUrl, 3, 1000, lease));
|
|
1205
|
+
const afterCsrf = this.transport.cookies();
|
|
1206
|
+
if (afterCsrf) {
|
|
1207
|
+
requestHeaders.Cookie = afterCsrf;
|
|
1208
|
+
this.logger?.debug(`Retrying GET request with cookies from CSRF fetch`);
|
|
1209
|
+
requestConfig.headers = await this.authorizedFrom(requestHeaders);
|
|
1210
|
+
return await this.sendObserved(requestConfig, lease);
|
|
1020
1211
|
}
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
}
|
|
1027
|
-
const refreshedCookies = this.getCookies();
|
|
1028
|
-
if (refreshedCookies) {
|
|
1029
|
-
requestHeaders.Cookie = refreshedCookies;
|
|
1030
|
-
}
|
|
1031
|
-
const retryResponse = await this.transport.send(requestConfig);
|
|
1032
|
-
this.observeResponse(retryResponse.headers, lease.generation);
|
|
1033
|
-
return retryResponse;
|
|
1212
|
+
}
|
|
1213
|
+
catch (csrfError) {
|
|
1214
|
+
// As in path (a): a stale request gets the session verdict.
|
|
1215
|
+
if (!this.lifecycle.isCurrent(lease)) {
|
|
1216
|
+
throw this.staleVerdict(csrfError);
|
|
1034
1217
|
}
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
throw retryError;
|
|
1041
|
-
}
|
|
1042
|
-
this.logger?.debug(`CSRF retry failed; rethrowing original error: ${retryError instanceof Error
|
|
1043
|
-
? retryError.message
|
|
1044
|
-
: String(retryError)}`);
|
|
1045
|
-
throw error;
|
|
1218
|
+
this.throwIfSessionDead(csrfError, lease);
|
|
1219
|
+
// Swallowed, unless it says more than the 401 that started this —
|
|
1220
|
+
// a session verdict, or the credential's failure.
|
|
1221
|
+
if (this.outranksOriginal(csrfError)) {
|
|
1222
|
+
throw csrfError;
|
|
1046
1223
|
}
|
|
1224
|
+
this.logger?.debug(`Failed to get CSRF token for 401 retry: ${csrfError instanceof Error ? csrfError.message : String(csrfError)}`);
|
|
1225
|
+
// Fall through to throw original error
|
|
1047
1226
|
}
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1227
|
+
}
|
|
1228
|
+
throw error;
|
|
1229
|
+
}
|
|
1230
|
+
/**
|
|
1231
|
+
* What reaches the provider: a failure the wire's recovery could not cure
|
|
1232
|
+
* and that is the credential's — a refused logon, or a 401 on the request.
|
|
1233
|
+
* Everything else is thrown as it is.
|
|
1234
|
+
*
|
|
1235
|
+
* Ok buys one more attempt on the same lease, re-authorized. After a refused
|
|
1236
|
+
* logon the wire has no token, so it logs on anew first — its token dropped
|
|
1237
|
+
* and earned again, nothing on RFC — and the new token and cookies go onto
|
|
1238
|
+
* the request. The logon and the resend share the request's one retry: a
|
|
1239
|
+
* credential failure in either is the verdict. As in `recoverOnWire`,
|
|
1240
|
+
* `requestHeaders` are the request's own, and the resend is authorized anew.
|
|
1241
|
+
*/
|
|
1242
|
+
async answerCredentialFailure(failure, requestConfig, requestHeaders, lease, renewal) {
|
|
1243
|
+
// A resend inside the wire's recovery that met a dead session.
|
|
1244
|
+
this.throwIfSessionDead(failure, lease);
|
|
1245
|
+
const rejection = this.credentialRejection(failure);
|
|
1246
|
+
// A request from a previous session asks nobody anything: its recovery is
|
|
1247
|
+
// fenced, and so is the credential's.
|
|
1248
|
+
if (!rejection)
|
|
1249
|
+
throw failure;
|
|
1250
|
+
if (!this.lifecycle.isCurrent(lease))
|
|
1251
|
+
throw this.staleVerdict(failure);
|
|
1252
|
+
await this.renewCredential(rejection, renewal).catch((error) => {
|
|
1253
|
+
throw this.staleOr(error, lease);
|
|
1254
|
+
});
|
|
1255
|
+
if (!this.lifecycle.isCurrent(lease))
|
|
1256
|
+
throw this.staleVerdict(failure);
|
|
1257
|
+
try {
|
|
1258
|
+
if (rejection.at === 'logon') {
|
|
1259
|
+
this.transport.adoptCsrfToken(null);
|
|
1260
|
+
await this.ensureWireReady(lease);
|
|
1261
|
+
const token = this.transport.csrfToken();
|
|
1262
|
+
if (token && ['POST', 'PUT', 'DELETE'].includes(requestConfig.method)) {
|
|
1263
|
+
requestHeaders['x-csrf-token'] = token;
|
|
1061
1264
|
}
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1265
|
+
const cookies = this.transport.cookies();
|
|
1266
|
+
if (cookies)
|
|
1267
|
+
requestHeaders.Cookie = cookies;
|
|
1268
|
+
}
|
|
1269
|
+
requestConfig.headers = await this.authorizedFrom(requestHeaders);
|
|
1270
|
+
return await this.sendObserved(requestConfig, lease);
|
|
1271
|
+
}
|
|
1272
|
+
catch (again) {
|
|
1273
|
+
// A verdict about the session outranks everything: it is already the
|
|
1274
|
+
// caller's answer.
|
|
1275
|
+
if (this.isSessionVerdict(again))
|
|
1276
|
+
throw again;
|
|
1277
|
+
// The session the request was admitted to went away while the resend
|
|
1278
|
+
// was out: whatever came back is about a session this request no longer
|
|
1279
|
+
// holds, so it is not a credential verdict.
|
|
1280
|
+
if (!this.lifecycle.isCurrent(lease))
|
|
1281
|
+
throw this.staleVerdict(again);
|
|
1282
|
+
// The resend's refusal is observed like the first attempt's: a session
|
|
1283
|
+
// SAP replaced in between is SESSION_REPLACED, not a bare status.
|
|
1284
|
+
const refusal = (0, IAdtTransport_js_1.refusalOf)(again);
|
|
1285
|
+
if (refusal) {
|
|
1286
|
+
this.observeResponse(refusal.headers, lease.generation);
|
|
1287
|
+
}
|
|
1288
|
+
this.throwIfSessionDead(again, lease);
|
|
1289
|
+
const refusedAgain = this.credentialRejection(again);
|
|
1290
|
+
if (!refusedAgain)
|
|
1291
|
+
throw again;
|
|
1292
|
+
throw new authErrors_js_1.AuthRefusedError(authErrors_js_1.REFUSED_AGAIN, refusedAgain.at, refusedAgain.error);
|
|
1293
|
+
}
|
|
1294
|
+
}
|
|
1295
|
+
/**
|
|
1296
|
+
* The server telling us the session is gone, on any attempt — the first or
|
|
1297
|
+
* a resend — is the same verdict: SESSION_REPLACED, and the session is lost.
|
|
1298
|
+
*
|
|
1299
|
+
* Invisible to the identity comparison: the cookie, and therefore the
|
|
1300
|
+
* fingerprint, is unchanged. Only the state can see it, and it must say so
|
|
1301
|
+
* at once — otherwise a later unlockAll() finds a match and unlocks over a
|
|
1302
|
+
* dead session. A request from a previous session says nothing about the
|
|
1303
|
+
* current one, so its answer is not acted on.
|
|
1304
|
+
*/
|
|
1305
|
+
throwIfSessionDead(error, lease) {
|
|
1306
|
+
if (!this.isDeadSessionResponse(error))
|
|
1307
|
+
return;
|
|
1308
|
+
if (!this.lifecycle.isCurrent(lease))
|
|
1309
|
+
return;
|
|
1310
|
+
this.raiseSessionLost('the server reports the session no longer exists');
|
|
1311
|
+
// No internal retry: a blind retry here is what produced further locks
|
|
1312
|
+
// in the field. The caller decides.
|
|
1313
|
+
throw (0, SessionLifecycle_js_1.sessionError)(interfaces_adt_connection_1.ADT_SESSION_ERROR.SESSION_REPLACED, 'The SAP session no longer exists; any lock handle from it is dead');
|
|
1314
|
+
}
|
|
1315
|
+
/**
|
|
1316
|
+
* What a request whose lease went stale gets: a session verdict as it is,
|
|
1317
|
+
* anything else — the wire's error, the provider's refusal — replaced by
|
|
1318
|
+
* NOT_CONNECTED, because it is about a session the request no longer holds.
|
|
1319
|
+
*/
|
|
1320
|
+
staleVerdict(error) {
|
|
1321
|
+
if (this.isSessionVerdict(error))
|
|
1322
|
+
return error;
|
|
1323
|
+
return (0, SessionLifecycle_js_1.sessionError)(interfaces_adt_connection_1.ADT_SESSION_ERROR.NOT_CONNECTED, 'The session this request was admitted to is gone');
|
|
1324
|
+
}
|
|
1325
|
+
/** `error` as it is while the lease is current; its stale verdict once not. */
|
|
1326
|
+
staleOr(error, lease) {
|
|
1327
|
+
return this.lifecycle.isCurrent(lease) ? error : this.staleVerdict(error);
|
|
1328
|
+
}
|
|
1329
|
+
/**
|
|
1330
|
+
* The provider's answer to a refusal, within the request's one retry.
|
|
1331
|
+
*
|
|
1332
|
+
* Returns when it renewed. Its Oops is thrown in its own words, the wire's
|
|
1333
|
+
* error as the cause — or, when the provider threw instead of answering,
|
|
1334
|
+
* that throw. A refusal after the retry was spent is the verdict, and the
|
|
1335
|
+
* provider is not asked again — asking would invite a second renewal.
|
|
1336
|
+
*/
|
|
1337
|
+
async renewCredential(rejection, renewal) {
|
|
1338
|
+
if (renewal.spent) {
|
|
1339
|
+
throw new authErrors_js_1.AuthRefusedError(authErrors_js_1.REFUSED_AGAIN, rejection.at, rejection.error);
|
|
1340
|
+
}
|
|
1341
|
+
renewal.spent = true;
|
|
1342
|
+
const answer = await this.credentialRejected(rejection);
|
|
1343
|
+
if (!answer.outcome.ok) {
|
|
1344
|
+
throw new authErrors_js_1.AuthRefusedError(answer.outcome.refusal, rejection.at, 'thrown' in answer ? answer.thrown : rejection.error);
|
|
1345
|
+
}
|
|
1346
|
+
}
|
|
1347
|
+
/**
|
|
1348
|
+
* Whether a failure is the credential's to answer, and how to tell the
|
|
1349
|
+
* provider: a wire's refused logon, with its raw error, or a 401 on the
|
|
1350
|
+
* request. A 403 and every other status are not — the credential got in,
|
|
1351
|
+
* and what it may do is not something renewing it changes.
|
|
1352
|
+
*/
|
|
1353
|
+
credentialRejection(error) {
|
|
1354
|
+
if (error instanceof authErrors_js_1.WireLogonError) {
|
|
1355
|
+
return { at: 'logon', status: error.status, error: error.cause };
|
|
1356
|
+
}
|
|
1357
|
+
if ((0, IAdtTransport_js_1.refusalOf)(error)?.status === 401) {
|
|
1358
|
+
return { at: 'request', status: 401, error };
|
|
1359
|
+
}
|
|
1360
|
+
return null;
|
|
1361
|
+
}
|
|
1362
|
+
/**
|
|
1363
|
+
* Whether a failure of the wire's retry says more than the error that
|
|
1364
|
+
* started it. A session verdict does, and so does the credential's: a
|
|
1365
|
+
* provider's refusal, a refused logon, a 401 — the original 403 would hide
|
|
1366
|
+
* exactly what the caller or the provider has to answer.
|
|
1367
|
+
*/
|
|
1368
|
+
outranksOriginal(retryError) {
|
|
1369
|
+
return (this.isSessionVerdict(retryError) ||
|
|
1370
|
+
retryError instanceof authErrors_js_1.AuthRefusedError ||
|
|
1371
|
+
this.credentialRejection(retryError) !== null);
|
|
1372
|
+
}
|
|
1373
|
+
/**
|
|
1374
|
+
* The token fetch before a mutation. Its failures are swallowed as they
|
|
1375
|
+
* always were — the CSRF recovery handles a missing token — except the
|
|
1376
|
+
* credential's: the provider's refusal ends the request before anything is
|
|
1377
|
+
* sent, and a refused logon goes to the provider, whose Ok buys one more
|
|
1378
|
+
* fetch within the request's one retry — unless the session the request
|
|
1379
|
+
* was admitted to is gone by then, when nothing more is sent.
|
|
1380
|
+
*/
|
|
1381
|
+
async readyWireUpfront(renewal, lease) {
|
|
1382
|
+
for (;;) {
|
|
1383
|
+
try {
|
|
1384
|
+
await this.ensureWireReady(lease);
|
|
1385
|
+
return;
|
|
1386
|
+
}
|
|
1387
|
+
catch (error) {
|
|
1388
|
+
// The session the request was admitted to is gone: nothing more of it
|
|
1389
|
+
// is sent, the provider is not asked, and what the caller gets is the
|
|
1390
|
+
// session verdict — never the wire's error, nor the provider's, about
|
|
1391
|
+
// a session it no longer holds.
|
|
1392
|
+
if (!this.lifecycle.isCurrent(lease))
|
|
1393
|
+
throw this.staleVerdict(error);
|
|
1394
|
+
if (error instanceof authErrors_js_1.AuthRefusedError)
|
|
1395
|
+
throw error;
|
|
1396
|
+
if (!(error instanceof authErrors_js_1.WireLogonError)) {
|
|
1397
|
+
this.logger?.debug(`Could not fetch CSRF token upfront, will retry on error: ${error instanceof Error ? error.message : String(error)}`);
|
|
1398
|
+
return;
|
|
1075
1399
|
}
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1400
|
+
const rejection = this.credentialRejection(error);
|
|
1401
|
+
await this.renewCredential(rejection, renewal).catch((refused) => {
|
|
1402
|
+
throw this.staleOr(refused, lease);
|
|
1403
|
+
});
|
|
1404
|
+
if (!this.lifecycle.isCurrent(lease)) {
|
|
1405
|
+
throw (0, SessionLifecycle_js_1.sessionError)(interfaces_adt_connection_1.ADT_SESSION_ERROR.NOT_CONNECTED, 'The session this request was admitted to is gone; nothing was sent');
|
|
1082
1406
|
}
|
|
1083
1407
|
}
|
|
1084
|
-
throw error;
|
|
1085
1408
|
}
|
|
1086
1409
|
}
|
|
1087
1410
|
/**
|
|
@@ -1094,21 +1417,27 @@ class AbstractAbapConnection {
|
|
|
1094
1417
|
* read what it means.
|
|
1095
1418
|
*/
|
|
1096
1419
|
async fetchCsrfToken(_url, retryCount = csrfConfig_js_1.CSRF_CONFIG.RETRY_COUNT, retryDelay = csrfConfig_js_1.CSRF_CONFIG.RETRY_DELAY,
|
|
1097
|
-
/**
|
|
1098
|
-
|
|
1420
|
+
/**
|
|
1421
|
+
* The request's lease: fences what is sent and the response effects.
|
|
1422
|
+
* A plain session generation — what this parameter took before 10.0.1,
|
|
1423
|
+
* and what a subclass may still pass — keeps its 10.0.0 meaning: it fences
|
|
1424
|
+
* what the connection does with the answer (the identity policy), not the
|
|
1425
|
+
* wire's own ingest of it, and never stops the request being sent.
|
|
1426
|
+
* Omitted during connect(), which has no lease.
|
|
1427
|
+
*/
|
|
1428
|
+
fence) {
|
|
1099
1429
|
// Dropped first, because this is only ever reached to REPLACE one: the
|
|
1100
1430
|
// establishment is idempotent and would hand back the very token the
|
|
1101
1431
|
// caller has just been told is stale.
|
|
1102
1432
|
this.transport.adoptCsrfToken(null);
|
|
1103
1433
|
await this.transport.establish({
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
observe: (headers) => this.observeResponse(headers, generation),
|
|
1434
|
+
...(typeof fence === 'number'
|
|
1435
|
+
? this.leasedContext(undefined, fence)
|
|
1436
|
+
: this.leasedContext(fence)),
|
|
1108
1437
|
retries: retryCount,
|
|
1109
1438
|
retryDelayMs: retryDelay,
|
|
1110
1439
|
timeoutMs: (0, timeouts_js_1.getTimeout)('csrf'),
|
|
1111
|
-
isFatal: (error) => this.
|
|
1440
|
+
isFatal: (error) => this.endsTheExchange(error),
|
|
1112
1441
|
});
|
|
1113
1442
|
const token = this.transport.csrfToken();
|
|
1114
1443
|
if (!token)
|
|
@@ -1144,14 +1473,6 @@ class AbstractAbapConnection {
|
|
|
1144
1473
|
'set-cookie': cookies.split(';').map((entry) => entry.trim()),
|
|
1145
1474
|
});
|
|
1146
1475
|
}
|
|
1147
|
-
/**
|
|
1148
|
-
* Subclasses override to inject extra https.Agent options (e.g. mTLS
|
|
1149
|
-
* cert/key/pfx). The returned options are merged with the base options
|
|
1150
|
-
* (rejectUnauthorized).
|
|
1151
|
-
*/
|
|
1152
|
-
getHttpsAgentOptions() {
|
|
1153
|
-
return {};
|
|
1154
|
-
}
|
|
1155
1476
|
/**
|
|
1156
1477
|
* Make sure the wire is ready to carry a mutation.
|
|
1157
1478
|
*
|
|
@@ -1161,14 +1482,60 @@ class AbstractAbapConnection {
|
|
|
1161
1482
|
* raised `No CSRF token in response headers` before every write — swallowed
|
|
1162
1483
|
* by the caller, but logged as an error and repeated on the next one.
|
|
1163
1484
|
*/
|
|
1164
|
-
async ensureWireReady(
|
|
1485
|
+
async ensureWireReady(
|
|
1486
|
+
/** The request's lease: fences what is sent and the response effects. */
|
|
1487
|
+
lease) {
|
|
1165
1488
|
await this.transport.establish({
|
|
1489
|
+
...this.leasedContext(lease),
|
|
1490
|
+
isFatal: (error) => this.endsTheExchange(error),
|
|
1491
|
+
});
|
|
1492
|
+
}
|
|
1493
|
+
/**
|
|
1494
|
+
* The context for a token exchange a request makes on its own behalf — its
|
|
1495
|
+
* wire's upfront fetch, a CSRF recovery, the fetch after a refused logon.
|
|
1496
|
+
*
|
|
1497
|
+
* With a lease, nothing is sent once the session the request was admitted
|
|
1498
|
+
* to is gone. The check is made in `authorize`, AFTER the provider has
|
|
1499
|
+
* written: that is the last await before the wire sends, and a provider may
|
|
1500
|
+
* take long enough for the caller to disconnect and connect again — sent
|
|
1501
|
+
* then, the request would carry the new session's cookies. The throw is a
|
|
1502
|
+
* session verdict, so the exchange stops at once, past its retries.
|
|
1503
|
+
*
|
|
1504
|
+
* Without one — connect()'s own establishment, which is making the session
|
|
1505
|
+
* rather than working in it — nothing is fenced. `generation` alone fences
|
|
1506
|
+
* what the connection does with an answer, and nothing else: the request is
|
|
1507
|
+
* sent, and the wire still folds the answer into its jar.
|
|
1508
|
+
*/
|
|
1509
|
+
leasedContext(lease, generation = lease?.generation) {
|
|
1510
|
+
return {
|
|
1166
1511
|
baseUrl: this.baseUrl,
|
|
1167
|
-
|
|
1512
|
+
authorize: async (headers) => {
|
|
1513
|
+
await this.credentialHeaders(headers);
|
|
1514
|
+
if (lease && !this.lifecycle.isCurrent(lease)) {
|
|
1515
|
+
throw (0, SessionLifecycle_js_1.sessionError)(interfaces_adt_connection_1.ADT_SESSION_ERROR.NOT_CONNECTED, 'The session this request was admitted to is gone; nothing was sent');
|
|
1516
|
+
}
|
|
1517
|
+
},
|
|
1518
|
+
logon: (target) => this.logon(target),
|
|
1168
1519
|
extraHeaders: { 'sap-adt-connection-id': this.sessionId ?? '' },
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1520
|
+
// An answer that arrives after the lease went stale belongs to a session
|
|
1521
|
+
// that is gone. Refused with the session verdict, which every wire hands
|
|
1522
|
+
// up BEFORE folding an answer in — so its cookies and token never reach
|
|
1523
|
+
// the jar of the session that replaced it — and which ends the exchange.
|
|
1524
|
+
observe: (headers) => {
|
|
1525
|
+
if (lease && !this.lifecycle.isCurrent(lease)) {
|
|
1526
|
+
throw (0, SessionLifecycle_js_1.sessionError)(interfaces_adt_connection_1.ADT_SESSION_ERROR.NOT_CONNECTED, 'The session this request was admitted to is gone; its answer was not taken');
|
|
1527
|
+
}
|
|
1528
|
+
this.observeResponse(headers, generation);
|
|
1529
|
+
},
|
|
1530
|
+
};
|
|
1531
|
+
}
|
|
1532
|
+
/**
|
|
1533
|
+
* What stops a token exchange at once, past its retries: a session verdict,
|
|
1534
|
+
* or the provider's refusal to authorize it — asking again would only ask
|
|
1535
|
+
* the provider again.
|
|
1536
|
+
*/
|
|
1537
|
+
endsTheExchange(error) {
|
|
1538
|
+
return this.isSessionVerdict(error) || error instanceof authErrors_js_1.AuthRefusedError;
|
|
1172
1539
|
}
|
|
1173
1540
|
/**
|
|
1174
1541
|
* Clear SAP-side session state when SAP rejects the cached CSRF token + session
|