@mcp-abap-adt/connection 9.4.2 → 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.
Files changed (58) hide show
  1. package/CHANGELOG.md +175 -1
  2. package/README.md +77 -36
  3. package/dist/connection/AbstractAbapConnection.d.ts +211 -56
  4. package/dist/connection/AbstractAbapConnection.d.ts.map +1 -1
  5. package/dist/connection/AbstractAbapConnection.js +622 -255
  6. package/dist/connection/CloudHttpTransport.d.ts.map +1 -1
  7. package/dist/connection/CloudHttpTransport.js +15 -5
  8. package/dist/connection/CredentialAbapConnection.d.ts +21 -20
  9. package/dist/connection/CredentialAbapConnection.d.ts.map +1 -1
  10. package/dist/connection/CredentialAbapConnection.js +39 -46
  11. package/dist/connection/HttpTransport.d.ts +30 -14
  12. package/dist/connection/HttpTransport.d.ts.map +1 -1
  13. package/dist/connection/HttpTransport.js +99 -19
  14. package/dist/connection/IAdtTransport.d.ts +19 -2
  15. package/dist/connection/IAdtTransport.d.ts.map +1 -1
  16. package/dist/connection/OnPremHttpTransport.d.ts.map +1 -1
  17. package/dist/connection/OnPremHttpTransport.js +4 -3
  18. package/dist/connection/RfcTransport.d.ts +23 -3
  19. package/dist/connection/RfcTransport.d.ts.map +1 -1
  20. package/dist/connection/RfcTransport.js +48 -15
  21. package/dist/connection/authErrors.d.ts +61 -0
  22. package/dist/connection/authErrors.d.ts.map +1 -0
  23. package/dist/connection/authErrors.js +78 -0
  24. package/dist/connection/requestTarget.d.ts +12 -0
  25. package/dist/connection/requestTarget.d.ts.map +1 -0
  26. package/dist/connection/requestTarget.js +23 -0
  27. package/dist/connection/rfcConversation.d.ts +10 -5
  28. package/dist/connection/rfcConversation.d.ts.map +1 -1
  29. package/dist/connection/rfcConversation.js +6 -8
  30. package/dist/index.d.ts +1 -2
  31. package/dist/index.d.ts.map +1 -1
  32. package/dist/index.js +4 -12
  33. package/dist/session/SessionLifecycle.d.ts +1 -1
  34. package/dist/session/SessionLifecycle.d.ts.map +1 -1
  35. package/docs/INDEX.md +4 -1
  36. package/docs/INSTALLATION.md +12 -5
  37. package/docs/JWT_AUTH_TOOLS.md +2 -2
  38. package/docs/MIGRATION-10.0.md +284 -0
  39. package/docs/SCOPE.md +2 -2
  40. package/docs/STATEFUL_SESSION_GUIDE.md +30 -10
  41. package/docs/USAGE.md +118 -35
  42. package/examples/README.md +5 -4
  43. package/examples/basic-connection.js +1 -1
  44. package/examples/jwt-with-token-refresh.js +14 -15
  45. package/examples/saml-connection.js +1 -1
  46. package/package.json +4 -3
  47. package/dist/auth/FileCertificateMaterialLoader.d.ts +0 -6
  48. package/dist/auth/FileCertificateMaterialLoader.d.ts.map +0 -1
  49. package/dist/auth/FileCertificateMaterialLoader.js +0 -28
  50. package/dist/auth/ntlm.d.ts +0 -26
  51. package/dist/auth/ntlm.d.ts.map +0 -1
  52. package/dist/auth/ntlm.js +0 -72
  53. package/dist/auth/providers.d.ts +0 -125
  54. package/dist/auth/providers.d.ts.map +0 -1
  55. package/dist/auth/providers.js +0 -183
  56. package/dist/utils/tokenRefresh.d.ts +0 -15
  57. package/dist/utils/tokenRefresh.d.ts.map +0 -1
  58. package/dist/utils/tokenRefresh.js +0 -55
@@ -0,0 +1,284 @@
1
+ # Migration to 10.0
2
+
3
+ The connection now speaks `IAuthProvider` 3.0 (`@mcp-abap-adt/interfaces-auth`
4
+ ^3.0.0), and the credential providers left this package for
5
+ `@mcp-abap-adt/auth-providers`.
6
+
7
+ What changed in behaviour is who decides. The connection asks the provider
8
+ before every attempt (`authorize`), at every logon (`establish`) and when the
9
+ system refuses (`rejected`); it never asks what kind of provider it holds. The
10
+ wire offers what it can carry, the provider writes the credential into it, and
11
+ the connection decides the lifecycle: exactly one more attempt after the provider
12
+ says Ok, never a loop.
13
+
14
+ **What you do**, in the order it usually bites:
15
+
16
+ 1. [Import the providers from `@mcp-abap-adt/auth-providers`](#1-the-providers-moved).
17
+ 2. [Build a token provider with `.fixed` or `.from`](#2-tokenauthprovider-has-two-constructors-now).
18
+ 3. [Drop the hand-wired TLS material](#3-tls-material-is-taken-at-logon).
19
+ 4. [If you take the RFC wire and wrote your own factory, pass the logon parameters through](#4-rfc-the-factory-takes-the-logon-parameters).
20
+ 5. [Catch `AuthRefusedError` where you matched a 401](#5-catch-authrefusederror).
21
+ 6. [If you wrote a wire, use `authorize` and `logon`](#6-a-custom-wire).
22
+
23
+ If you use none of these — you hold a provider you wrote yourself against
24
+ `IAuthProvider` 3.0 and take the HTTP wire — only step 5 can concern you.
25
+
26
+ ## 1. The providers moved
27
+
28
+ `BasicAuthProvider`, `TokenAuthProvider`, `SamlAuthProvider`,
29
+ `CertificateAuthProvider` and `FileCertificateMaterialLoader` are no longer
30
+ exported here. They are in `@mcp-abap-adt/auth-providers` ^5.0.1 with the same
31
+ names.
32
+
33
+ ```bash
34
+ npm install @mcp-abap-adt/auth-providers
35
+ ```
36
+
37
+ ```diff
38
+ -import {
39
+ - AdtOnPremConnector,
40
+ - BasicAuthProvider,
41
+ - OnPremHttpTransport,
42
+ -} from '@mcp-abap-adt/connection';
43
+ +import { AdtOnPremConnector, OnPremHttpTransport } from '@mcp-abap-adt/connection';
44
+ +import { BasicAuthProvider } from '@mcp-abap-adt/auth-providers';
45
+ ```
46
+
47
+ This package has no runtime dependency on `auth-providers`; you install it
48
+ because you use it. `IAuthProvider` itself is still not re-exported: import the
49
+ type from `@mcp-abap-adt/interfaces-auth`, which is where the ^3.0.0 contract
50
+ lives — an older `interfaces-auth` makes a provider of yours a type error.
51
+
52
+ ## 2. `TokenAuthProvider` has two constructors now
53
+
54
+ `new TokenAuthProvider(x)` is gone. A fixed token and a renewing one are different
55
+ objects, and the name says which.
56
+
57
+ ```diff
58
+ -new TokenAuthProvider('eyJhbGciOiJSUzI1NiIs...')
59
+ +TokenAuthProvider.fixed('eyJhbGciOiJSUzI1NiIs...')
60
+
61
+ -new TokenAuthProvider(refresher)
62
+ +TokenAuthProvider.from(refresher)
63
+ ```
64
+
65
+ `.from` renews when the system refuses the token — not by checking expiry before
66
+ each request, as the old class did. Many requests meeting one expired token each
67
+ call `rejected()`; the connection adds no single-flight of its own. Whether they
68
+ share one renewal in flight is the refresher's: `.from` asks it on each
69
+ rejection, so they share one only if your refresher shares one.
70
+
71
+ ## 3. TLS material is taken at logon
72
+
73
+ Before, a certificate credential's `cert` / `key` / `pfx` / `passphrase` reached
74
+ the HTTPS agent only because you passed them in `agentOptions`:
75
+
76
+ ```diff
77
+ -const material = await loader.load(config);
78
+ new OnPremHttpTransport(
79
+ - () => ({ cert: material.cert, key: material.key, ca: myCa }),
80
+ + () => ({ ca: myCa }),
81
+ logger,
82
+ { client: config.client, baseUrl: config.url },
83
+ )
84
+ ```
85
+
86
+ Now the HTTP wire offers a logon target to the provider at every logon; a
87
+ provider such as `CertificateAuthProvider` hands over its material there, and the
88
+ wire builds the agent from `{ rejectUnauthorized, ...agentOptions(), ...material }`
89
+ on the first request, where `material` is only the `cert`, `key`, `pfx` and
90
+ `passphrase` the provider offered with a value — any other key it passes is
91
+ ignored, and a field offered as `undefined` erases nothing. A later logon offering different material replaces the
92
+ client; the cookie jar and CSRF token are the session's and stay.
93
+
94
+ `agentOptions` stays for what is not the credential: `ca` and
95
+ `rejectUnauthorized`. Material you leave in it still works for a provider that
96
+ offers none, but nothing depends on that.
97
+
98
+ ## 4. RFC: the factory takes the logon parameters
99
+
100
+ `RfcTransport` opens a conversation for the session and one per non-stateful
101
+ call, and each open now takes the logon parameters the provider wrote for it —
102
+ a password, or SNC settings.
103
+
104
+ - `RfcTransport`'s first argument is
105
+ `(logon: Readonly<Record<string, string>>) => IRfcConversation`; it was
106
+ `() => IRfcConversation`.
107
+ - `rfcConversationFrom(config)` returns such a factory. If you only ever passed
108
+ its result to `RfcTransport`, nothing changes for you.
109
+ - `rfcParamsFrom(config)` returns the address only — `ashost`, `sysnr`,
110
+ `client` (default `'000'`), `lang` (`'EN'`). It no longer reads
111
+ `config.username` / `config.password`, and `RfcConnectionParams` lost `user`
112
+ and `passwd`. The credential comes from the provider, and only from it.
113
+
114
+ If you wrote your own factory:
115
+
116
+ ```diff
117
+ -const factory = () => new Client(rfcParamsFrom(config));
118
+ +const factory = (logon: Readonly<Record<string, string>>) =>
119
+ + new Client({ ...rfcParamsFrom(config), ...logon });
120
+ ```
121
+
122
+ A provider that writes no logon parameters — a bearer token — cannot log on over
123
+ RFC. The open fails, the provider is asked, and you get its refusal; RFC cannot
124
+ carry a bearer token.
125
+
126
+ A failed open is thrown as a `WireLogonError` whose `cause` is the raw
127
+ `sap-rfc-lite` error. The old `Failed to open RFC connection: …` wrapping is
128
+ gone, so a provider can read the SDK's `key` and message and, say, tell an SNC
129
+ failure from a network one.
130
+
131
+ **Every failed open, a network one included.** An open that fails because the
132
+ host cannot be reached (`RFC_COMMUNICATION_FAILURE`, say) is put to the provider
133
+ like a refused logon, and reaches you as an `AuthRefusedError` at `'logon'` — in
134
+ the provider's words, or the fixed "refused again" reason after its Ok — with the
135
+ SDK's error only in `cause`. A caller that matched the SDK error to tell an
136
+ unreachable system from a refused one reads `cause` now.
137
+
138
+ ## 5. Catch `AuthRefusedError`
139
+
140
+ Before, a refused credential arrived as whatever the wire threw — an axios error
141
+ with `response.status === 401`, an RFC logon failure — and a caller matching on
142
+ the status had to guess what it meant.
143
+
144
+ Now a 401 that survives the wire's own session recovery, and any refused logon, is put to
145
+ `provider.rejected(...)`. On Ok the request is authorized again and sent once
146
+ more; if the provider says no, or the retry is refused too, the caller gets an
147
+ `AuthRefusedError`:
148
+
149
+ ```diff
150
+ try {
151
+ await connection.connect();
152
+ } catch (error) {
153
+ - if ((error as any).response?.status === 401) {
154
+ - console.error('bad credentials');
155
+ - }
156
+ + if (error instanceof AuthRefusedError) {
157
+ + // The provider's words, not ours: it knows whether it was a password, a
158
+ + // token, or an SNC library that was missing.
159
+ + console.error(error.refusal.reason, error.refusal.hint);
160
+ + }
161
+ }
162
+ ```
163
+
164
+ ```text
165
+ class AuthRefusedError extends Error {
166
+ readonly refusal: { reason: string; hint?: string }; // the provider's words
167
+ readonly at: 'prepare' | 'logon' | 'request'; // when it refused
168
+ readonly cause?: unknown; // the provider's throw when it threw, else the wire's error
169
+ }
170
+ ```
171
+
172
+ - `at: 'prepare'` — the provider refused before the wire opened; nothing was sent.
173
+ - `at: 'logon'` — the system refused a logon, or the provider refused to offer one.
174
+ - `at: 'request'` — the provider refused to authorize a request, or the system
175
+ refused it again after a renewal.
176
+ - After an Ok, a second refusal is the verdict: the provider is not asked again
177
+ and `refusal` is `{ reason: 'the credential was refused again after the
178
+ provider renewed it' }`, with the second wire error as `cause`.
179
+ - A provider that throws is treated as refusing with the reason "the credential
180
+ provider failed" and the throw as `cause`.
181
+ - A `403` is never put to the provider and never becomes an `AuthRefusedError`; it
182
+ reaches you unchanged, as before.
183
+
184
+ Inside a critical section (`beginCriticalSection()`), a mutation refused with a
185
+ 401 while a CSRF token is cached is no longer moved to a new session: it goes to
186
+ the provider and is resent on the same one, so the lock survives — or it fails
187
+ with `SESSION_REPLACED` if SAP replaced the session. Outside a critical section
188
+ the wire's stale-session recovery still runs first.
189
+
190
+ `prepare()` now runs once at the start of `connect()`, before the wire opens; it
191
+ used to come after `transport.open()`.
192
+
193
+ ## 6. A custom wire
194
+
195
+ If you implement `IAdtTransport` yourself, `IAdtSessionContext.authHeaders` is
196
+ gone. Two methods replace it:
197
+
198
+ - `authorize(headers)` — fill the credential into a request the wire itself
199
+ originates (the CSRF fetch, a preflight, a logoff), per request, never held.
200
+ - `logon(target)` — call it at each of your logons, before any request. Pass a
201
+ `target` (an `ILogonTarget`) that says what your wire can carry: `tlsMaterial`
202
+ and `logonParameters` each answer Ok or `{ ok: false, refusal }`.
203
+
204
+ ```diff
205
+ async close(context: IAdtSessionContext): Promise<void> {
206
+ const headers: Record<string, string> = {};
207
+ - Object.assign(headers, await context.authHeaders());
208
+ - await this.client.post('/logoff', undefined, { headers });
209
+ + // A goodbye never throws: `authorize` throws AuthRefusedError when the
210
+ + // provider refuses, and the connection does not await every close.
211
+ + try {
212
+ + await context.authorize(headers);
213
+ + await this.client.post('/logoff', undefined, { headers });
214
+ + } catch {
215
+ + // Nothing to tell anyone: the session falls to its idle timeout.
216
+ + }
217
+ }
218
+ ```
219
+
220
+ `close()` must not throw. `context.authorize` throws `AuthRefusedError` when the
221
+ provider refuses, so a goodbye that authorizes catches it; the connection absorbs
222
+ a rejected close it dispatched, but a close that never throws is the contract.
223
+
224
+ ```ts
225
+ import type {
226
+ AuthOutcome,
227
+ ILogonTarget,
228
+ } from '@mcp-abap-adt/interfaces-auth';
229
+ import {
230
+ type IAdtSessionContext,
231
+ WireLogonError,
232
+ } from '@mcp-abap-adt/connection';
233
+
234
+ async function logOn(
235
+ context: IAdtSessionContext,
236
+ open: (parameters: Record<string, string>) => Promise<void>,
237
+ ): Promise<void> {
238
+ let parameters: Record<string, string> = {};
239
+ const target: ILogonTarget = {
240
+ tlsMaterial: (): AuthOutcome => ({
241
+ ok: false,
242
+ refusal: { reason: 'this wire carries no TLS material (example wire)' },
243
+ }),
244
+ logonParameters: (offered): AuthOutcome => {
245
+ parameters = { ...offered };
246
+ return { ok: true };
247
+ },
248
+ };
249
+ // Throws AuthRefusedError(at 'logon') if the provider refuses the logon.
250
+ await context.logon(target);
251
+ try {
252
+ await open(parameters);
253
+ } catch (cause) {
254
+ // Mark the failure as a refused logon; the connection asks the provider
255
+ // what to do about it. The wire decides nothing, and passes the raw error.
256
+ throw new WireLogonError(cause);
257
+ }
258
+ }
259
+ ```
260
+
261
+ `WireLogonError { cause, status? }` is the mark a wire puts on a failure that is
262
+ a refused logon. `cause` is the raw error, never re-wrapped; `status` is the HTTP
263
+ status when there is one (the HTTP wire marks a 401 while establishing its
264
+ session this way). A `WireLogonError` reaches `rejected()` with `at: 'logon'`; a
265
+ 401 on a request that survived the wire's own recovery reaches it with
266
+ `at: 'request'`.
267
+
268
+ ## Removed with no replacement
269
+
270
+ Nothing read them:
271
+
272
+ - `CredentialAbapConnection.getHttpsAgentOptions`, `buildAuthorizationHeader`, `isUnauthorized`
273
+ - `AbstractAbapConnection.recoverSession`, `discardSession`
274
+ - `src/utils/tokenRefresh.ts` and `src/auth/ntlm.ts` (neither was exported)
275
+
276
+ ## Why
277
+
278
+ Until 9.x the connection read the credential itself: which header it produced,
279
+ whether it could be renewed, whether it carried TLS material, and it wrapped or
280
+ dropped what the wire said about a refusal. Each new kind of credential — a
281
+ token, a certificate, SNC — was a change in the connection, and a refusal reached
282
+ the caller as a bare 401 nobody could word. With `IAuthProvider` 3.0 the provider
283
+ writes the credential and words its own refusal, the wire only carries it, and
284
+ the connection keeps to the lifecycle: one more attempt, never a loop.
package/docs/SCOPE.md CHANGED
@@ -7,14 +7,14 @@ This package is **one component of the `@mcp-abap-adt/*` family**. Each package
7
7
  - **HTTP transport to SAP ADT** — request dispatch, CSRF token fetch, cookie and session handling, stateful/stateless session control.
8
8
  - **The session lifecycle** — establishing a session and refusing to work without one, tearing it down without cutting in-flight requests, tracking which SAP session it is talking to, and telling the caller when that session is lost while a lock is held. See [USAGE.md — Session Lifecycle](./USAGE.md#session-lifecycle).
9
9
  - **RFC transport to on-premise SAP** — via `@mcp-abap-adt/sap-rfc-lite` + NW RFC SDK, calling `SADT_REST_RFC_ENDPOINT` (the same FM Eclipse ADT uses on on-prem).
10
- - **Applying credentials** handed to it by the caller (Basic user/password, Bearer JWT, SAML session cookies).
10
+ - **Carrying what an `IAuthProvider` hands it** (Basic user/password, Bearer JWT, SAML session cookies, TLS material, RFC logon parameters), and asking the provider again once when the system refuses it. The providers themselves are in `@mcp-abap-adt/auth-providers`.
11
11
 
12
12
  ## What this package does NOT do, and where it lives instead
13
13
 
14
14
  | Concern | Belongs in |
15
15
  |---|---|
16
16
  | Token acquisition (SAML/OAuth2 flows, browser login, PKCE) | `@mcp-abap-adt/auth-broker` |
17
- | Token validation, refresh, re-authentication, expiry tracking | consumer via `ITokenProvider` (from `@mcp-abap-adt/interfaces-auth`) |
17
+ | Token validation, refresh, re-authentication, expiry tracking | the `IAuthProvider` the consumer hands in (`@mcp-abap-adt/interfaces-auth`; ready-made ones in `@mcp-abap-adt/auth-providers`) |
18
18
  | Session state persistence across processes | `@mcp-abap-adt/auth-broker` |
19
19
  | Auth-type constants, provider error codes, auth lifecycle contracts | `@mcp-abap-adt/interfaces-auth`, `@mcp-abap-adt/interfaces-auth-sap` |
20
20
 
@@ -104,10 +104,10 @@ const logger = console;
104
104
  ```ts
105
105
  import {
106
106
  AdtOnPremConnector,
107
- BasicAuthProvider,
108
107
  OnPremHttpTransport,
109
108
  getTimeout,
110
109
  } from '@mcp-abap-adt/connection';
110
+ import { BasicAuthProvider } from '@mcp-abap-adt/auth-providers';
111
111
 
112
112
  const connection = new AdtOnPremConnector(
113
113
  config,
@@ -135,7 +135,8 @@ connection.setSessionType('stateless');
135
135
  ## Knowing Which Session You Are In
136
136
 
137
137
  ```ts
138
- import { AdtOnPremConnector, BasicAuthProvider, OnPremHttpTransport } from '@mcp-abap-adt/connection';
138
+ import { AdtOnPremConnector, OnPremHttpTransport } from '@mcp-abap-adt/connection';
139
+ import { BasicAuthProvider } from '@mcp-abap-adt/auth-providers';
139
140
 
140
141
  // getSessionIdentity() is on the HTTP connection classes, NOT on the
141
142
  // bare IAbapConnection type a caller may hand you.
@@ -170,13 +171,27 @@ Every ADT request issued through `makeAdtRequest` automatically:
170
171
 
171
172
  This logic is transparent to callers (Builders, handlers, CLI scripts).
172
173
 
173
- On a JWT connection a 401 is **not** handled here at all, and that is the point: since 6.0.0 the
174
- refusal surfaces and the session is left alone. Nothing replaces the credential behind you, so
175
- nothing replaces the SAP session behind you either — a lock window is not torn down by an
176
- authentication answer. A 403 never did this: it is an authorization answer, not a credential one.
177
-
178
- If you decide the refusal meant a stale token, `renew()` and reconnect are yours to call — and a
179
- reconnect is a NEW session, so do it outside a lock window rather than inside one.
174
+ A 401 that survives the wire's own CSRF recovery is put to the provider (`rejected`). On Ok the
175
+ request is sent once more, always under the same lease and generation as the attempt it repeats,
176
+ so stale-request fencing applies. After a rejection at `'request'` that resend goes out on the
177
+ same session; after a rejection at `'logon'` the wire logs on again before the resend.
178
+ **Inside a critical section a renewal keeps the session, and the lock in it:** a mutation
179
+ (POST / PUT / DELETE) refused with a 401 while a CSRF token is cached goes to the provider and is
180
+ resent on the same session with the same token, and if SAP did replace the session the request
181
+ fails with `SESSION_REPLACED` instead of carrying on in a new one. Outside a critical section
182
+ that mutation takes the wire's own stale-session recovery first, which starts a new session
183
+ before the provider is asked — so hold a lock inside `beginCriticalSection()` /
184
+ `endCriticalSection()`.
185
+ **The price of keeping the lock:** when the session is really dead but SAP answers the resend with
186
+ the SAME session cookie (no `Set-Cookie`), the connection cannot tell it from a refused
187
+ credential — the second 401 ends the request as `AuthRefusedError` with the refused-again
188
+ verdict (`at: 'request'`), not `SESSION_REPLACED`, and the connection stays connected. Inside a
189
+ critical section, treat that as a lost lock: `disconnect()`, `connect()`, and redo the work under
190
+ a new lock.
191
+ If the provider says no, or the retry is refused too, the caller gets an `AuthRefusedError`. A 403 is an authorization answer, not a credential one: it never goes to the
192
+ provider. See [When the credential is refused](./USAGE.md#when-the-credential-is-refused).
193
+
194
+ A reconnect is a NEW session, so do it outside a lock window rather than inside one.
180
195
 
181
196
  **Wait for the goodbye before opening the next one.** `disconnect()` dispatches the logoff and does
182
197
  not await it, so a reconnect otherwise opens the next session while the previous one's goodbye is
@@ -270,6 +285,10 @@ What it promises is narrow and worth stating exactly — inside a section the
270
285
  short: the ceiling is `SAP_TIMEOUT_CRITICAL`, ten minutes by default, and a
271
286
  socket ends a request whatever a contract says.
272
287
 
288
+ Since 10.0.0 a section also keeps its session through a credential renewal: a
289
+ mutation refused with a 401 inside it is renewed and resent on the same
290
+ session, never moved to a new one (see [Request Hooks](#request-hooks)).
291
+
273
292
  ### Two sessions, and they are not the same thing
274
293
 
275
294
  There are two sessions here, and they are not the same thing:
@@ -326,7 +345,8 @@ you get by asking — `getSessionIdentity()` for which session you are in, and
326
345
  through the `ISessionLifecycleAware` atom:
327
346
 
328
347
  ```ts
329
- import { AdtOnPremConnector, BasicAuthProvider, OnPremHttpTransport } from '@mcp-abap-adt/connection';
348
+ import { AdtOnPremConnector, OnPremHttpTransport } from '@mcp-abap-adt/connection';
349
+ import { BasicAuthProvider } from '@mcp-abap-adt/auth-providers';
330
350
 
331
351
  const connection = new AdtOnPremConnector(config, new BasicAuthProvider(user, pass), new OnPremHttpTransport(() => ({}), logger, { client: config.client, baseUrl: config.url }), logger);
332
352
  await connection.disconnect(); // ends the session on the server, then clears
package/docs/USAGE.md CHANGED
@@ -18,10 +18,10 @@
18
18
  ```typescript
19
19
  import {
20
20
  AdtOnPremConnector,
21
- BasicAuthProvider,
22
21
  OnPremHttpTransport,
23
22
  getTimeout,
24
23
  } from '@mcp-abap-adt/connection';
24
+ import { BasicAuthProvider } from '@mcp-abap-adt/auth-providers';
25
25
  import { SapConfig } from '@mcp-abap-adt/connection';
26
26
 
27
27
  const config: SapConfig = {
@@ -90,9 +90,9 @@ For on-premise SAP systems using basic authentication:
90
90
  ```typescript
91
91
  import {
92
92
  AdtOnPremConnector,
93
- BasicAuthProvider,
94
93
  OnPremHttpTransport,
95
94
  } from '@mcp-abap-adt/connection';
95
+ import { BasicAuthProvider } from '@mcp-abap-adt/auth-providers';
96
96
 
97
97
  const config = {
98
98
  url: 'https://sap-server.local:8000',
@@ -123,9 +123,9 @@ For SAP BTP ABAP Environment. Token refresh belongs to
123
123
  import {
124
124
  AdtCloudConnector,
125
125
  CloudHttpTransport,
126
- TokenAuthProvider,
127
126
  getTimeout,
128
127
  } from '@mcp-abap-adt/connection';
128
+ import { TokenAuthProvider } from '@mcp-abap-adt/auth-providers';
129
129
 
130
130
  const config = {
131
131
  url: 'https://tenant.abap.cloud',
@@ -133,12 +133,12 @@ const config = {
133
133
  client: '100', // Optional for cloud
134
134
  };
135
135
 
136
- // A bare token works and has no renewal behind it. Hand the provider an
137
- // ITokenRefresher instead — from @mcp-abap-adt/auth-broker or your own — and it
138
- // checks expiry and refreshes on its own.
136
+ // `.fixed` is a token with no renewal behind it. `TokenAuthProvider.from` takes
137
+ // an ITokenRefresher — from @mcp-abap-adt/auth-broker or your own — and renews
138
+ // when the system refuses the token.
139
139
  const connection = new AdtCloudConnector(
140
140
  config,
141
- new TokenAuthProvider('eyJhbGciOiJSUzI1NiIs...'),
141
+ TokenAuthProvider.fixed('eyJhbGciOiJSUzI1NiIs...'),
142
142
  new CloudHttpTransport(() => ({}), logger, {
143
143
  client: config.client,
144
144
  baseUrl: config.url,
@@ -234,7 +234,8 @@ await connection.makeAdtRequest({
234
234
  By default, connections are stateless - each request gets fresh cookies and CSRF tokens:
235
235
 
236
236
  ```typescript
237
- import { AdtOnPremConnector, BasicAuthProvider, OnPremHttpTransport, getTimeout } from '@mcp-abap-adt/connection';
237
+ import { AdtOnPremConnector, OnPremHttpTransport, getTimeout } from '@mcp-abap-adt/connection';
238
+ import { BasicAuthProvider } from '@mcp-abap-adt/auth-providers';
238
239
  const connection = new AdtOnPremConnector(
239
240
  config,
240
241
  new BasicAuthProvider(config.username!, config.password!),
@@ -259,7 +260,8 @@ writes from running in the lock context and failing with PAK/058 or an invalid
259
260
  lock handle. See [HTTP context isolation](./STATEFUL_SESSION_GUIDE.md#http-context-isolation-931).
260
261
 
261
262
  ```typescript
262
- import { AdtOnPremConnector, BasicAuthProvider, OnPremHttpTransport, getTimeout } from '@mcp-abap-adt/connection';
263
+ import { AdtOnPremConnector, OnPremHttpTransport, getTimeout } from '@mcp-abap-adt/connection';
264
+ import { BasicAuthProvider } from '@mcp-abap-adt/auth-providers';
263
265
  const connection = new AdtOnPremConnector(
264
266
  config,
265
267
  new BasicAuthProvider(config.username!, config.password!),
@@ -375,7 +377,8 @@ implied. Four things follow from it.
375
377
  ### connect() is required, and it tells the truth
376
378
 
377
379
  ```typescript
378
- import { AdtOnPremConnector, BasicAuthProvider, OnPremHttpTransport } from '@mcp-abap-adt/connection';
380
+ import { AdtOnPremConnector, OnPremHttpTransport } from '@mcp-abap-adt/connection';
381
+ import { BasicAuthProvider } from '@mcp-abap-adt/auth-providers';
379
382
 
380
383
  const connection = new AdtOnPremConnector(
381
384
  config,
@@ -490,8 +493,7 @@ precisely to avoid it.
490
493
 
491
494
  Two different things can cost you the session, and they do not behave alike.
492
495
 
493
- **The session was replaced** — a renewed credential, or a session cookie that
494
- changed underneath you. This is fatal **only while a lock window is open**:
496
+ **The session was replaced** — a session cookie that changed underneath you. This is fatal **only while a lock window is open**:
495
497
 
496
498
  ```typescript
497
499
  // window open → ADT_SESSION_REPLACED, the connection stops being usable
@@ -520,7 +522,8 @@ yours.
520
522
  Session IDs are auto-generated (UUID) when connection is created:
521
523
 
522
524
  ```typescript
523
- import { AdtOnPremConnector, BasicAuthProvider, OnPremHttpTransport } from '@mcp-abap-adt/connection';
525
+ import { AdtOnPremConnector, OnPremHttpTransport } from '@mcp-abap-adt/connection';
526
+ import { BasicAuthProvider } from '@mcp-abap-adt/auth-providers';
524
527
  const connection = new AdtOnPremConnector(
525
528
  config,
526
529
  new BasicAuthProvider(config.username!, config.password!),
@@ -550,7 +553,8 @@ console.log(connectionWithOwnId.getSessionId()); // 'custom-session-123'
550
553
  Dynamically switch between stateful and stateless modes:
551
554
 
552
555
  ```typescript
553
- import { AdtOnPremConnector, BasicAuthProvider, OnPremHttpTransport, getTimeout } from '@mcp-abap-adt/connection';
556
+ import { AdtOnPremConnector, OnPremHttpTransport, getTimeout } from '@mcp-abap-adt/connection';
557
+ import { BasicAuthProvider } from '@mcp-abap-adt/auth-providers';
554
558
  // Start in stateless mode (default)
555
559
  const connection = new AdtOnPremConnector(
556
560
  config,
@@ -592,7 +596,8 @@ in a `finally`. `disconnect()` never throws, so it is safe there.
592
596
  ### Using Custom Logger
593
597
 
594
598
  ```typescript
595
- import { AdtOnPremConnector, BasicAuthProvider, ILogger, OnPremHttpTransport } from '@mcp-abap-adt/connection';
599
+ import { AdtOnPremConnector, ILogger, OnPremHttpTransport } from '@mcp-abap-adt/connection';
600
+ import { BasicAuthProvider } from '@mcp-abap-adt/auth-providers';
596
601
 
597
602
  class CustomLogger implements ILogger {
598
603
  info(message: string, meta?: any) {
@@ -791,7 +796,11 @@ establishing, and whatever session state it keeps.
791
796
 
792
797
  ```text
793
798
  new HttpTransport(agentOptions?, logger?, { client?, baseUrl? })
794
- new RfcTransport(connect: () => IRfcConversation, logger?, { logWire?, maxLoggedBodyChars? })
799
+ new RfcTransport(
800
+ connect: (logon: Readonly<Record<string, string>>) => IRfcConversation,
801
+ logger?,
802
+ { logWire?, maxLoggedBodyChars? },
803
+ )
795
804
  ```
796
805
 
797
806
  `HttpTransport` is the ordinary wire, and you name it because the connector
@@ -801,6 +810,21 @@ what the connectors' type parameters admit. `RfcTransport`
801
810
  you build with `rfcConversationFrom(config)`, which derives `ashost` and `sysnr`
802
811
  and loads the SAP NW RFC SDK only when a conversation opens.
803
812
 
813
+ **What the wire takes from the credential.** The provider writes it, the wire
814
+ carries it; the wire never reads `config.username` or `config.password`.
815
+
816
+ - HTTP takes TLS material (`cert`, `key`, `pfx`, `passphrase`) at logon and builds
817
+ its `https.Agent` from it. `agentOptions` stays for what is not the credential
818
+ — `ca` and `rejectUnauthorized`. A later logon offering different material
819
+ replaces the client; the cookie jar and CSRF token are the session's and stay.
820
+ HTTP refuses logon parameters.
821
+ - RFC takes logon parameters (a password, or SNC settings) on every conversation
822
+ it opens, the session's own and each per-call one. RFC refuses TLS material,
823
+ and a provider that writes no logon parameters (a bearer token) cannot log on
824
+ over RFC: the open fails, the provider is asked, and you get its refusal.
825
+ - A failed RFC open is thrown as `WireLogonError` with the SDK's own error as
826
+ `cause`, so a provider can read the SDK's `key` and message.
827
+
804
828
  **`logWire` dumps the wire, and is off.** With it on, `RfcTransport` adds three
805
829
  debug lines per request — the header fields, the request body and the response
806
830
  body — which is what tells you a payload was mis-serialised before it reached
@@ -909,41 +933,100 @@ export class CloudSdkAbapConnection {
909
933
 
910
934
  ### The credential, and how a refusal is classified
911
935
 
912
- For SAP BTP cloud systems, hand `AdtCloudConnector` a `TokenAuthProvider`. A bare
913
- string is a token with nothing behind it; an `ITokenRefresher` is a provider that
914
- checks expiry and renews on its own, which is what you want in anything
915
- long-lived. Obtaining tokens in the first place is `@mcp-abap-adt/auth-broker`'s
916
- job, not this package's.
936
+ For SAP BTP cloud systems, hand `AdtCloudConnector` a token provider from
937
+ `@mcp-abap-adt/auth-providers`: `TokenAuthProvider.fixed(token)` is a token with
938
+ nothing behind it, `TokenAuthProvider.from(refresher)` takes an `ITokenRefresher`
939
+ and renews a token the system refused. Obtaining tokens in the first place is
940
+ `@mcp-abap-adt/auth-broker`'s job, not this package's.
917
941
 
918
942
  ```text
919
943
  new AdtCloudConnector(
920
944
  config,
921
- new TokenAuthProvider(refresher), // or a bare token string
945
+ TokenAuthProvider.from(refresher), // or TokenAuthProvider.fixed(token)
922
946
  new CloudHttpTransport(() => ({}), logger, { client: config.client, baseUrl: config.url }),
923
947
  logger,
924
948
  );
925
949
  ```
926
950
 
927
- **How failures are classified** (6.0.0 — see
928
- [MIGRATION-6.0.md](./MIGRATION-6.0.md)):
951
+ #### When the credential is refused
952
+
953
+ The connection speaks four calls to whatever `IAuthProvider` it holds, and
954
+ never asks what kind it is:
955
+
956
+ | call | when |
957
+ |---|---|
958
+ | `prepare()` | once, in `connect()`, before the wire opens |
959
+ | `establish(logon)` | at each logon the wire performs |
960
+ | `authorize(request)` | before every attempt of every request — including the CSRF fetch, the cloud preflight, the logoff, and each resend |
961
+ | `rejected(rejection)` | the system said no |
962
+
963
+ **Each request asks the provider.** Nothing is cached by the connection, so a
964
+ token the provider renewed is on the very next attempt.
965
+
966
+ **How failures are classified:**
929
967
 
930
968
  | answer | what happens |
931
969
  |---|---|
932
- | **401** | **surfaces.** Nothing here decides to get a new credential. An EXPIRED token is already replaced without anyone deciding — the provider is asked per request and checks expiry before answering — so a 401 is the other case: a credential the source still believes in and the server refuses. Whether that means "stale" is a judgement made with what you know, and `renew()` on an `IRenewableCredential` is the seam you make it with. The session is untouched: a refused credential is not a lost session |
933
- | **403** | propagates untouched. The server authenticated the caller and refused the action anyway, so a new token is the same caller — usually the body names the authorization object |
970
+ | **401** on a stale CSRF token or a login form | the wire's own session recovery runs first; a session fault is not a credential fault, and never reaches the provider. For a mutation refused while a CSRF token is cached, that recovery starts a new session — except inside a critical section, where the 401 goes straight to the provider (next row) so the session and its lock are kept |
971
+ | **401** that survives that recovery, or a logon the system refused | put to `provider.rejected({ at, status?, error })`. On Ok the request is authorized again and sent **once more**; on no, `AuthRefusedError` |
972
+ | the same refusal after an Ok | the verdict. The provider is not asked again — asking would invite a second renewal — and you get an `AuthRefusedError` saying the credential was refused again after the provider renewed it |
973
+ | **403** | propagates untouched, never put to the provider. The server authenticated the caller and refused the action anyway, so a new credential is the same caller — usually the body names the authorization object |
934
974
  | anything else | untouched |
935
975
 
936
- The connection never replaces the server's error with one of its own:
937
- `failure.response.status` and `failure.response.data` are always what SAP sent.
976
+ The one more attempt keeps the request's own lease and generation, so
977
+ stale-request fencing applies to it: a request from a superseded session asks
978
+ nobody and gets its error back. After a rejection `at: 'request'` it goes out on
979
+ the same session; after a rejection `at: 'logon'` the wire logs on again — token
980
+ dropped, established anew — before the resend. Inside a critical section a
981
+ renewal keeps the session: a mutation that meets a 401 with a CSRF token cached
982
+ is resent on it with the same token, and if SAP did replace the session the
983
+ request fails with `SESSION_REPLACED` rather than carrying on in a new one.
984
+ Outside a critical section that mutation takes the wire's stale-session recovery
985
+ first (above), which starts a new session.
986
+
987
+ **The price of keeping the lock.** Inside a critical section the connection
988
+ cannot tell a dead session from a refused credential when SAP answers the resend
989
+ with the same session cookie and no `Set-Cookie`: the 401 comes back a second
990
+ time, and the request ends as `AuthRefusedError` with the refused-again verdict
991
+ (`at: 'request'`), not `SESSION_REPLACED`. The connection stays connected on
992
+ that session. If you get this inside a critical section, treat the lock as lost:
993
+ `disconnect()`, `connect()`, and redo the work under a new lock. That is the
994
+ intended trade-off — a renewal must not discard a lock the session still holds.
995
+
996
+ One credential retry per request: the upfront token fetch before a mutation and
997
+ a later rejection draw on the same allowance.
938
998
 
939
- Concurrent requests that meet the same expired token share **one** renewal — a
940
- single token fetch and a single session re-establishment between them, not one
941
- each.
999
+ ```typescript
1000
+ import { AuthRefusedError } from '@mcp-abap-adt/connection';
1001
+
1002
+ try {
1003
+ await connection.connect();
1004
+ } catch (error) {
1005
+ if (error instanceof AuthRefusedError) {
1006
+ // error.refusal.reason / .hint are the provider's words;
1007
+ // error.at is 'prepare' | 'logon' | 'request'; error.cause is the provider's
1008
+ // throw when it threw, else the wire's error.
1009
+ console.error(error.message);
1010
+ }
1011
+ throw error;
1012
+ }
1013
+ ```
942
1014
 
943
- **A refusal during `connect()` surfaces.** Nothing is renewed behind you there:
944
- the provider already renews on expiry it can see, every time it is asked for a
945
- header, so a refusal at establishment means the credential needs attention that
946
- this library cannot give it.
1015
+ `refusal` is the provider's, `cause` is the error the wire raised as it arrived,
1016
+ so a network failure stays visible even when a provider words it as a credential
1017
+ problem. A provider that throws is treated as refusing with the reason "the
1018
+ credential provider failed" and the throw as the cause.
1019
+
1020
+ **Concurrency.** Many requests meeting the same expired token each call
1021
+ `rejected()`; the connection adds no single-flight of its own. They share one
1022
+ renewal in flight only if the provider shares one: `TokenAuthProvider.from` asks
1023
+ its refresher on each rejection, so they share one only if your refresher does.
1024
+
1025
+ **A refusal during `connect()`** is the same story: `prepare()` refused sends
1026
+ nothing; a logon the system refused goes to `rejected({ at: 'logon' })`, and on
1027
+ Ok the wire is closed and logged on once more. An `establish` refusal is the
1028
+ provider's own verdict on this logon (SNC with no library, a certificate the wire
1029
+ cannot take) and does not go to `rejected`.
947
1030
 
948
1031
  ### Configuration Types
949
1032