@ondewo/sip-client-nodejs 5.2.0 → 5.4.0
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/LICENSE +0 -0
- package/README.md +3 -0
- package/api/ondewo/sip/sip_grpc_pb.js +15 -16
- package/auth/offlineTokenProvider.d.ts +158 -0
- package/auth/offlineTokenProvider.js +320 -0
- package/auth/offlineTokenProvider.spec.ts +595 -0
- package/auth/offlineTokenProvider.ts +433 -0
- package/package.json +7 -4
- package/public-api.js +0 -0
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
// Copyright 2021-2026 ONDEWO GmbH
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
//
|
|
15
|
+
|
|
16
|
+
// D18 headless-SDK auth helper (keycloak-migration-plan §7.8 + D18).
|
|
17
|
+
//
|
|
18
|
+
// One-time ROPC login (grant_type=password, scope=offline_access) against the PUBLIC SDK client
|
|
19
|
+
// `ondewo-sip-cai-sdk-public` (no client_secret -- Q1), then a bounded background loop that refreshes
|
|
20
|
+
// the short-lived access token from the offline refresh token before it expires. The current access
|
|
21
|
+
// token is exposed for an `Authorization: Bearer <token>` gRPC metadata header. The refresh loop stops
|
|
22
|
+
// after `tokenExpirationInS` (if given) has elapsed since login.
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Seconds of head-room subtracted from a token's `expires_in` so the refresh fires before the access
|
|
26
|
+
* token actually lapses (covers clock skew + the round-trip to Keycloak).
|
|
27
|
+
*/
|
|
28
|
+
const REFRESH_SKEW_IN_S: number = 30;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Lower bound (in seconds) for the scheduled refresh delay so a tiny/zero `expires_in` cannot spin a
|
|
32
|
+
* hot loop.
|
|
33
|
+
*/
|
|
34
|
+
const MIN_REFRESH_DELAY_IN_S: number = 1;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Minimal structural type of the fetch Response fields this helper reads. Keeps the module
|
|
38
|
+
* self-contained (no DOM lib dependency) while still typing the injectable `fetchImpl`.
|
|
39
|
+
*/
|
|
40
|
+
export interface TokenFetchResponse {
|
|
41
|
+
/** Whether the HTTP status is in the 2xx success range. */
|
|
42
|
+
ok: boolean;
|
|
43
|
+
/** The numeric HTTP status code of the response. */
|
|
44
|
+
status: number;
|
|
45
|
+
/**
|
|
46
|
+
* Read the full response body as text.
|
|
47
|
+
*
|
|
48
|
+
* @returns A promise resolving to the raw response body string.
|
|
49
|
+
*/
|
|
50
|
+
text(): Promise<string>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Init object passed to the injectable fetch. */
|
|
54
|
+
export interface TokenFetchInit {
|
|
55
|
+
/** The HTTP method, always `"POST"` for the token endpoint. */
|
|
56
|
+
method: string;
|
|
57
|
+
/** The request headers (content type + accept) sent to the token endpoint. */
|
|
58
|
+
headers: Record<string, string>;
|
|
59
|
+
/** The form-encoded request body. */
|
|
60
|
+
body: string;
|
|
61
|
+
/**
|
|
62
|
+
* Optional undici dispatcher (Node's non-standard `fetch` extension). Set only on the
|
|
63
|
+
* default transport when `keycloakVerifySsl` is `false`; carries an
|
|
64
|
+
* `Agent({ connect: { rejectUnauthorized: false } })` so the token request skips TLS
|
|
65
|
+
* certificate verification. Left `undefined` on the secure default path.
|
|
66
|
+
*/
|
|
67
|
+
dispatcher?: unknown;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Injectable fetch signature (a subset of the global `fetch`) used by the token endpoint call.
|
|
72
|
+
*
|
|
73
|
+
* @param url - The absolute token-endpoint URL to POST to.
|
|
74
|
+
* @param init - The request method, headers, and form-encoded body.
|
|
75
|
+
* @returns A promise resolving to the minimal {@link TokenFetchResponse} the helper reads.
|
|
76
|
+
*/
|
|
77
|
+
export type TokenFetch = (url: string, init: TokenFetchInit) => Promise<TokenFetchResponse>;
|
|
78
|
+
|
|
79
|
+
/** Parsed Keycloak token-endpoint response (only the fields this helper consumes). */
|
|
80
|
+
interface KeycloakTokenResponse {
|
|
81
|
+
/** The short-lived bearer access token used for the gRPC `Authorization` header. */
|
|
82
|
+
access_token: string;
|
|
83
|
+
/** The offline refresh token (present only when `offline_access` was granted). */
|
|
84
|
+
refresh_token?: string;
|
|
85
|
+
/** The access token lifetime in seconds, used to schedule the next refresh. */
|
|
86
|
+
expires_in?: number;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Options for the D18 headless-SDK offline-token login. */
|
|
90
|
+
export interface OfflineTokenLoginOptions {
|
|
91
|
+
/** Base Keycloak URL, e.g. "https://auth.example.com/auth" (trailing slash tolerated). */
|
|
92
|
+
keycloakUrl: string;
|
|
93
|
+
/** Realm name, e.g. "ondewo-ccai-platform". */
|
|
94
|
+
realm: string;
|
|
95
|
+
/** Public SDK client id, e.g. "ondewo-sip-cai-sdk-public". NO client_secret (Q1). */
|
|
96
|
+
clientId: string;
|
|
97
|
+
/** 2FA-exempt technical-user email. */
|
|
98
|
+
username: string;
|
|
99
|
+
/** Technical-user password. */
|
|
100
|
+
password: string;
|
|
101
|
+
/** Optional cap (seconds) on how long the auto-refresh loop runs after login. */
|
|
102
|
+
tokenExpirationInS?: number;
|
|
103
|
+
/** Optional fetch override (tests inject a mock); defaults to the global fetch. */
|
|
104
|
+
fetchImpl?: TokenFetch;
|
|
105
|
+
/**
|
|
106
|
+
* When `false`, DISABLE TLS certificate verification on the Keycloak token request
|
|
107
|
+
* (opt-in insecure, for a self-signed local Envoy at `https://localhost:12001/auth`).
|
|
108
|
+
* Defaults to `true` (verify -- secure, unchanged behaviour). Ignored when a custom
|
|
109
|
+
* `fetchImpl` is injected. Node-only: implemented via an undici dispatcher, so it is a
|
|
110
|
+
* no-op in a browser bundle.
|
|
111
|
+
*/
|
|
112
|
+
keycloakVerifySsl?: boolean;
|
|
113
|
+
/** Optional clock override returning epoch ms (tests); defaults to Date.now. */
|
|
114
|
+
nowInMs?: () => number;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Error raised on any token-endpoint or token-shape failure. */
|
|
118
|
+
export class TokenError extends Error {
|
|
119
|
+
/**
|
|
120
|
+
* Construct a new {@link TokenError}.
|
|
121
|
+
*
|
|
122
|
+
* @param message - A human-readable description of the token failure.
|
|
123
|
+
*/
|
|
124
|
+
public constructor(message: string) {
|
|
125
|
+
super(message);
|
|
126
|
+
this.name = 'TokenError';
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Build the OIDC token endpoint URL for a realm, tolerating a trailing slash on `keycloakUrl` and an
|
|
132
|
+
* optional `/auth` relative path already baked into it.
|
|
133
|
+
*
|
|
134
|
+
* @param keycloakUrl - The base Keycloak URL (a trailing slash is tolerated and stripped).
|
|
135
|
+
* @param realm - The realm name, which is URL-encoded into the path.
|
|
136
|
+
* @returns The fully-qualified OIDC `openid-connect/token` endpoint URL for the realm.
|
|
137
|
+
*/
|
|
138
|
+
function buildTokenEndpoint(keycloakUrl: string, realm: string): string {
|
|
139
|
+
const base: string = keycloakUrl.replace(/\/+$/, '');
|
|
140
|
+
return `${base}/realms/${encodeURIComponent(realm)}/protocol/openid-connect/token`;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* POST an `application/x-www-form-urlencoded` body to the token endpoint and return the parsed JSON.
|
|
145
|
+
*
|
|
146
|
+
* @param tokenEndpoint - The absolute token-endpoint URL to POST to.
|
|
147
|
+
* @param params - The form parameters (grant type, client id, credentials, scope, etc.).
|
|
148
|
+
* @param fetchImpl - The fetch implementation used to perform the request.
|
|
149
|
+
* @returns A promise resolving to the parsed {@link KeycloakTokenResponse}.
|
|
150
|
+
* @throws {TokenError} On a non-2xx response, an unparseable body, or a missing `access_token`.
|
|
151
|
+
*/
|
|
152
|
+
async function postTokenRequest(
|
|
153
|
+
tokenEndpoint: string,
|
|
154
|
+
params: Record<string, string>,
|
|
155
|
+
fetchImpl: TokenFetch
|
|
156
|
+
): Promise<KeycloakTokenResponse> {
|
|
157
|
+
const body: string = new URLSearchParams(params).toString();
|
|
158
|
+
const response: TokenFetchResponse = await fetchImpl(tokenEndpoint, {
|
|
159
|
+
method: 'POST',
|
|
160
|
+
headers: {
|
|
161
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
162
|
+
Accept: 'application/json'
|
|
163
|
+
},
|
|
164
|
+
body
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
const text: string = await response.text();
|
|
168
|
+
if (!response.ok) {
|
|
169
|
+
throw new TokenError(`Keycloak token endpoint returned HTTP ${response.status}: ${text}`);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
let parsed: KeycloakTokenResponse;
|
|
173
|
+
try {
|
|
174
|
+
parsed = JSON.parse(text) as KeycloakTokenResponse;
|
|
175
|
+
} catch {
|
|
176
|
+
throw new TokenError(`Keycloak token endpoint returned a non-JSON body: ${text}`);
|
|
177
|
+
}
|
|
178
|
+
if (typeof parsed.access_token !== 'string' || parsed.access_token.length === 0) {
|
|
179
|
+
throw new TokenError('Keycloak token response did not contain an access_token');
|
|
180
|
+
}
|
|
181
|
+
return parsed;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Build the default {@link TokenFetch}: delegate to the global `fetch` (Node >= 18).
|
|
186
|
+
*
|
|
187
|
+
* When `verifySsl` is `false`, a cached undici `Agent` with `rejectUnauthorized: false` is attached
|
|
188
|
+
* to every request as its `dispatcher`, so the Keycloak token call skips TLS certificate verification
|
|
189
|
+
* (opt-in insecure; Node-only). The dispatcher is built once here and reused for all requests this
|
|
190
|
+
* transport makes (login + refreshes); the secure default never loads undici.
|
|
191
|
+
*
|
|
192
|
+
* @param verifySsl - Whether to verify the Keycloak server's TLS certificate.
|
|
193
|
+
* @returns A fetch layer bound to the chosen TLS-verification behaviour.
|
|
194
|
+
*/
|
|
195
|
+
function createDefaultFetch(verifySsl: boolean): TokenFetch {
|
|
196
|
+
let dispatcher: unknown;
|
|
197
|
+
if (!verifySsl) {
|
|
198
|
+
// Lazy require keeps undici out of the default (secure) code path.
|
|
199
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
200
|
+
const { Agent } = require('undici') as { Agent: new (options: unknown) => unknown };
|
|
201
|
+
dispatcher = new Agent({ connect: { rejectUnauthorized: false } });
|
|
202
|
+
}
|
|
203
|
+
return (url: string, init: TokenFetchInit): Promise<TokenFetchResponse> => {
|
|
204
|
+
const globalFetch: TokenFetch = globalThis.fetch;
|
|
205
|
+
return globalFetch(url, dispatcher === undefined ? init : { ...init, dispatcher });
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* A live access-token holder backed by a bounded auto-refresh loop. Obtain one from {@link login};
|
|
211
|
+
* read {@link getAuthorizationHeader} for the gRPC `Authorization` metadata and call {@link stop} when done.
|
|
212
|
+
*/
|
|
213
|
+
export class OfflineTokenProvider {
|
|
214
|
+
private readonly tokenEndpoint: string;
|
|
215
|
+
private readonly clientId: string;
|
|
216
|
+
private readonly tokenExpirationInS: number | undefined;
|
|
217
|
+
private readonly fetchImpl: TokenFetch;
|
|
218
|
+
private readonly nowInMs: () => number;
|
|
219
|
+
|
|
220
|
+
private accessToken: string | null;
|
|
221
|
+
private refreshToken: string | null;
|
|
222
|
+
private timer: ReturnType<typeof setTimeout> | null;
|
|
223
|
+
private stopped: boolean;
|
|
224
|
+
private deadlineInMs: number | null;
|
|
225
|
+
private onRefreshErrorHandler: ((error: unknown) => void) | null;
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Construct a provider from the login options. Does not perform any network I/O; call
|
|
229
|
+
* {@link bootstrap} (or the module-level {@link login}) to actually authenticate.
|
|
230
|
+
*
|
|
231
|
+
* @param options - The D18 offline-token login options (URL, realm, client id, credentials, overrides).
|
|
232
|
+
*/
|
|
233
|
+
public constructor(options: OfflineTokenLoginOptions) {
|
|
234
|
+
this.tokenEndpoint = buildTokenEndpoint(options.keycloakUrl, options.realm);
|
|
235
|
+
this.clientId = options.clientId;
|
|
236
|
+
this.tokenExpirationInS = options.tokenExpirationInS;
|
|
237
|
+
this.fetchImpl =
|
|
238
|
+
options.fetchImpl !== undefined ? options.fetchImpl : createDefaultFetch(options.keycloakVerifySsl ?? true);
|
|
239
|
+
this.nowInMs = options.nowInMs !== undefined ? options.nowInMs : Date.now;
|
|
240
|
+
|
|
241
|
+
this.accessToken = null;
|
|
242
|
+
this.refreshToken = null;
|
|
243
|
+
this.timer = null;
|
|
244
|
+
this.stopped = false;
|
|
245
|
+
this.deadlineInMs = null;
|
|
246
|
+
this.onRefreshErrorHandler = null;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Perform the one-time ROPC login and arm the first refresh. Awaited by {@link login}.
|
|
251
|
+
*
|
|
252
|
+
* @param username - The 2FA-exempt technical-user email.
|
|
253
|
+
* @param password - The technical-user password.
|
|
254
|
+
* @returns A promise that resolves once the access token is stored and the first refresh is armed.
|
|
255
|
+
* @throws {TokenError} If the token endpoint fails or the response carries no `refresh_token`
|
|
256
|
+
* (the SDK client lacks `directAccessGrants` + the `offline_access` scope).
|
|
257
|
+
*/
|
|
258
|
+
public async bootstrap(username: string, password: string): Promise<void> {
|
|
259
|
+
const tokenResponse: KeycloakTokenResponse = await postTokenRequest(
|
|
260
|
+
this.tokenEndpoint,
|
|
261
|
+
{
|
|
262
|
+
grant_type: 'password',
|
|
263
|
+
client_id: this.clientId,
|
|
264
|
+
username,
|
|
265
|
+
password,
|
|
266
|
+
scope: 'offline_access'
|
|
267
|
+
},
|
|
268
|
+
this.fetchImpl
|
|
269
|
+
);
|
|
270
|
+
|
|
271
|
+
this.accessToken = tokenResponse.access_token;
|
|
272
|
+
this.refreshToken = typeof tokenResponse.refresh_token === 'string' ? tokenResponse.refresh_token : null;
|
|
273
|
+
if (this.refreshToken === null) {
|
|
274
|
+
throw new TokenError(
|
|
275
|
+
'Keycloak token response did not contain a refresh_token; the SDK client must have ' +
|
|
276
|
+
'directAccessGrants + the offline_access scope (ondewo-sip-cai-sdk-public)'
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if (this.tokenExpirationInS !== undefined) {
|
|
281
|
+
const expirationInMs: number = this.tokenExpirationInS * 1000;
|
|
282
|
+
this.deadlineInMs = this.nowInMs() + expirationInMs;
|
|
283
|
+
}
|
|
284
|
+
this.scheduleRefresh(tokenResponse.expires_in);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Exchange the offline refresh token for a fresh access token and re-arm the next refresh.
|
|
289
|
+
* No-ops once {@link stop} has been called or the bounded deadline has elapsed.
|
|
290
|
+
*
|
|
291
|
+
* @returns A promise that resolves once the token is refreshed (or the loop has lapsed/stopped).
|
|
292
|
+
* @throws {TokenError} If the refresh token-endpoint call fails or returns an invalid body.
|
|
293
|
+
*/
|
|
294
|
+
private async refresh(): Promise<void> {
|
|
295
|
+
/* c8 ignore next 3 -- unreachable: stop() always clears the only timer that calls refresh() */
|
|
296
|
+
if (this.stopped) {
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
// Re-check the bounded deadline at fire time (not just at schedule time): once it has elapsed the
|
|
300
|
+
// loop stops with no further renewal -> the access token lapses -> re-login is required.
|
|
301
|
+
if (this.deadlineInMs !== null && this.nowInMs() >= this.deadlineInMs) {
|
|
302
|
+
this.stop();
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
const tokenResponse: KeycloakTokenResponse = await postTokenRequest(
|
|
306
|
+
this.tokenEndpoint,
|
|
307
|
+
{
|
|
308
|
+
grant_type: 'refresh_token',
|
|
309
|
+
client_id: this.clientId,
|
|
310
|
+
refresh_token: this.refreshToken as string
|
|
311
|
+
},
|
|
312
|
+
this.fetchImpl
|
|
313
|
+
);
|
|
314
|
+
|
|
315
|
+
this.accessToken = tokenResponse.access_token;
|
|
316
|
+
// Keycloak may rotate the offline refresh token; keep the newest one when present.
|
|
317
|
+
if (typeof tokenResponse.refresh_token === 'string' && tokenResponse.refresh_token.length > 0) {
|
|
318
|
+
this.refreshToken = tokenResponse.refresh_token;
|
|
319
|
+
}
|
|
320
|
+
this.scheduleRefresh(tokenResponse.expires_in);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Arm a single timer for the next refresh, clamped to the bounded deadline. Stops silently once
|
|
325
|
+
* `tokenExpirationInS` has elapsed (no further renewal -> access lapses -> re-login required).
|
|
326
|
+
*
|
|
327
|
+
* @param expiresInRaw - The access token lifetime in seconds from the token response, or
|
|
328
|
+
* `undefined`/non-positive to fall back to {@link MIN_REFRESH_DELAY_IN_S}.
|
|
329
|
+
*/
|
|
330
|
+
private scheduleRefresh(expiresInRaw: number | undefined): void {
|
|
331
|
+
if (this.stopped) {
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
const expiresInS: number =
|
|
335
|
+
typeof expiresInRaw === 'number' && expiresInRaw > 0 ? expiresInRaw : MIN_REFRESH_DELAY_IN_S;
|
|
336
|
+
let delayInS: number = Math.max(expiresInS - REFRESH_SKEW_IN_S, MIN_REFRESH_DELAY_IN_S);
|
|
337
|
+
|
|
338
|
+
if (this.deadlineInMs !== null) {
|
|
339
|
+
const remainingInMs: number = this.deadlineInMs - this.nowInMs();
|
|
340
|
+
if (remainingInMs <= 0) {
|
|
341
|
+
this.stop();
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
delayInS = Math.min(delayInS, remainingInMs / 1000);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
this.timer = setTimeout(() => {
|
|
348
|
+
this.refresh().catch((refreshError: unknown) => {
|
|
349
|
+
// Swallow a transient refresh failure but surface it so the caller can react; the next
|
|
350
|
+
// gRPC call gets the stale (possibly expired) token and re-logs in on UNAUTHENTICATED.
|
|
351
|
+
if (this.onRefreshErrorHandler !== null) {
|
|
352
|
+
this.onRefreshErrorHandler(refreshError);
|
|
353
|
+
}
|
|
354
|
+
});
|
|
355
|
+
}, delayInS * 1000);
|
|
356
|
+
// Do not keep the event loop alive solely for the refresh timer.
|
|
357
|
+
/* c8 ignore next 3 -- the else branch is unreachable: Node's Timeout always exposes unref() */
|
|
358
|
+
if (typeof this.timer.unref === 'function') {
|
|
359
|
+
this.timer.unref();
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Register a callback invoked with the error of a failed background refresh (optional diagnostics).
|
|
365
|
+
*
|
|
366
|
+
* @param handler - The callback receiving the error thrown by a failed background refresh.
|
|
367
|
+
* @returns Nothing.
|
|
368
|
+
*/
|
|
369
|
+
public onRefreshError(handler: (error: unknown) => void): void {
|
|
370
|
+
this.onRefreshErrorHandler = handler;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Read the current access token.
|
|
375
|
+
*
|
|
376
|
+
* @returns The current access token, or `null` before bootstrap / after the bounded loop has lapsed.
|
|
377
|
+
*/
|
|
378
|
+
public getAccessToken(): string | null {
|
|
379
|
+
return this.accessToken;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Build the value for an `Authorization` gRPC metadata header.
|
|
384
|
+
*
|
|
385
|
+
* @returns The header value `Bearer <access_token>`.
|
|
386
|
+
* @throws {TokenError} If no access token is available (login has not completed or has lapsed).
|
|
387
|
+
*/
|
|
388
|
+
public getAuthorizationHeader(): string {
|
|
389
|
+
if (this.accessToken === null) {
|
|
390
|
+
throw new TokenError('No access token available; login() has not completed or has lapsed');
|
|
391
|
+
}
|
|
392
|
+
return `Bearer ${this.accessToken}`;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Stop the auto-refresh loop. Idempotent; safe to call from any state.
|
|
397
|
+
*
|
|
398
|
+
* @returns Nothing.
|
|
399
|
+
*/
|
|
400
|
+
public stop(): void {
|
|
401
|
+
this.stopped = true;
|
|
402
|
+
if (this.timer !== null) {
|
|
403
|
+
clearTimeout(this.timer);
|
|
404
|
+
this.timer = null;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* One-time ROPC + offline_access login against the PUBLIC SDK client, returning a live token provider
|
|
411
|
+
* whose access token is auto-refreshed in the background until `tokenExpirationInS` elapses.
|
|
412
|
+
*
|
|
413
|
+
* @param options - The D18 offline-token login options (URL, realm, client id, credentials, overrides).
|
|
414
|
+
* @returns A promise resolving to a bootstrapped {@link OfflineTokenProvider} with a live access token.
|
|
415
|
+
* @throws {TokenError} If `options` is missing, a required string option is empty, or the token
|
|
416
|
+
* endpoint / response is invalid.
|
|
417
|
+
*/
|
|
418
|
+
export async function login(options: OfflineTokenLoginOptions): Promise<OfflineTokenProvider> {
|
|
419
|
+
if (options === undefined || options === null) {
|
|
420
|
+
throw new TokenError('login() requires an options object');
|
|
421
|
+
}
|
|
422
|
+
const requiredKeys: (keyof OfflineTokenLoginOptions)[] = ['keycloakUrl', 'realm', 'clientId', 'username', 'password'];
|
|
423
|
+
for (const key of requiredKeys) {
|
|
424
|
+
const value: unknown = options[key];
|
|
425
|
+
if (typeof value !== 'string' || value.length === 0) {
|
|
426
|
+
throw new TokenError(`login() option "${key}" is required and must be a non-empty string`);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
const provider: OfflineTokenProvider = new OfflineTokenProvider(options);
|
|
431
|
+
await provider.bootstrap(options.username, options.password);
|
|
432
|
+
return provider;
|
|
433
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ondewo/sip-client-nodejs",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.4.0",
|
|
4
4
|
"description": "ONDEWO Session Initiation Protocol (SIP) Client library for Nodejs",
|
|
5
5
|
"author": "ONDEWO GmbH <office@ondewo.com>",
|
|
6
6
|
"homepage": "https://ondewo.com",
|
|
@@ -21,9 +21,10 @@
|
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"@grpc/grpc-js": "^1.12.2",
|
|
24
|
-
"@types/node": "^
|
|
24
|
+
"@types/node": "^22.15.27",
|
|
25
25
|
"dotenv": "^16.4.5",
|
|
26
|
-
"google-protobuf": "3.21.4"
|
|
26
|
+
"google-protobuf": "3.21.4",
|
|
27
|
+
"undici": "^7.28.0"
|
|
27
28
|
},
|
|
28
29
|
"devDependencies": {
|
|
29
30
|
"@eslint/eslintrc": "^3.1.0",
|
|
@@ -32,6 +33,8 @@
|
|
|
32
33
|
"eslint": "^9.13.0",
|
|
33
34
|
"global": "^4.4.0",
|
|
34
35
|
"husky": "^9.1.6",
|
|
35
|
-
"prettier": "^3.3.3"
|
|
36
|
+
"prettier": "^3.3.3",
|
|
37
|
+
"c8": "^11.0.0",
|
|
38
|
+
"typescript": "^6.0.3"
|
|
36
39
|
}
|
|
37
40
|
}
|
package/public-api.js
CHANGED
|
File without changes
|