@trimble-oss/trimble-id-react 1.0.3 → 1.0.4-rc.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.
|
@@ -104,36 +104,36 @@ class Z {
|
|
|
104
104
|
await this.cacheStorage.clear();
|
|
105
105
|
}
|
|
106
106
|
}
|
|
107
|
-
const x = 5 * 6e4, j = ["code", "state"], A = 600 * 1e3, U = (
|
|
108
|
-
let e =
|
|
109
|
-
if (!
|
|
107
|
+
const x = 5 * 6e4, j = ["code", "state"], A = 600 * 1e3, U = (n) => n.split("?")[0], ee = (n) => n.split("?")[1], b = (n) => {
|
|
108
|
+
let e = n;
|
|
109
|
+
if (!n.startsWith("?"))
|
|
110
110
|
try {
|
|
111
|
-
e = new URL(
|
|
111
|
+
e = new URL(n).search;
|
|
112
112
|
} catch {
|
|
113
113
|
}
|
|
114
114
|
return new URLSearchParams(e);
|
|
115
|
-
}, te = (
|
|
115
|
+
}, te = (n = window.location.href, e) => {
|
|
116
116
|
if (e != null) {
|
|
117
|
-
const
|
|
118
|
-
if (
|
|
117
|
+
const o = U(e), i = U(n ?? "");
|
|
118
|
+
if (o !== i)
|
|
119
119
|
return !1;
|
|
120
120
|
}
|
|
121
|
-
const t = b(
|
|
122
|
-
for (const
|
|
123
|
-
if (!t.has(
|
|
121
|
+
const t = b(n);
|
|
122
|
+
for (const o of j)
|
|
123
|
+
if (!t.has(o))
|
|
124
124
|
return !1;
|
|
125
125
|
return !0;
|
|
126
|
-
},
|
|
127
|
-
id:
|
|
128
|
-
name: `${
|
|
129
|
-
given_name:
|
|
130
|
-
family_name:
|
|
131
|
-
picture:
|
|
132
|
-
email:
|
|
133
|
-
email_verified:
|
|
134
|
-
...
|
|
135
|
-
}),
|
|
136
|
-
class
|
|
126
|
+
}, ne = (n) => ({
|
|
127
|
+
id: n.sub,
|
|
128
|
+
name: `${n.given_name} ${n.family_name}`,
|
|
129
|
+
given_name: n.given_name,
|
|
130
|
+
family_name: n.family_name,
|
|
131
|
+
picture: n.picture,
|
|
132
|
+
email: n.email,
|
|
133
|
+
email_verified: n.email_verified,
|
|
134
|
+
...n.account_id && { account_id: n.account_id }
|
|
135
|
+
}), oe = "@TID_COOKIE";
|
|
136
|
+
class ie {
|
|
137
137
|
/**
|
|
138
138
|
* Retrieve a cookie from the browser
|
|
139
139
|
* @param {string} key - Key to retrieve the cookies
|
|
@@ -154,12 +154,12 @@ class re {
|
|
|
154
154
|
* @example Save cookies with a custom domain
|
|
155
155
|
* cookiesStorage.set('key',{data:{...}},{domain:'https://example.com/subpath'})
|
|
156
156
|
*/
|
|
157
|
-
set(e, t,
|
|
158
|
-
let
|
|
159
|
-
window.location.protocol === "https:" && (
|
|
157
|
+
set(e, t, o) {
|
|
158
|
+
let i = {};
|
|
159
|
+
window.location.protocol === "https:" && (i = {
|
|
160
160
|
secure: !0,
|
|
161
161
|
sameSite: "none"
|
|
162
|
-
}),
|
|
162
|
+
}), o?.expires && (i.expires = o.expires), o?.domain && (i.domain = o.domain), m.set(e, JSON.stringify(t), i);
|
|
163
163
|
}
|
|
164
164
|
/**
|
|
165
165
|
* Remove a cookie from the browser
|
|
@@ -171,11 +171,11 @@ class re {
|
|
|
171
171
|
* cookiesStorage.remove('key',{domain:'https://example.com/subpath'})
|
|
172
172
|
*/
|
|
173
173
|
remove(e, t) {
|
|
174
|
-
const
|
|
175
|
-
t?.domain && (
|
|
174
|
+
const o = {};
|
|
175
|
+
t?.domain && (o.domain = t.domain), m.remove(e, o);
|
|
176
176
|
}
|
|
177
177
|
}
|
|
178
|
-
class
|
|
178
|
+
class re {
|
|
179
179
|
/**
|
|
180
180
|
* Cookie full key to store and retrieve the information from cookies
|
|
181
181
|
* @type {string}
|
|
@@ -191,15 +191,15 @@ class ie {
|
|
|
191
191
|
* @param {CookiesManagerOptions} options - Configuration for the managing the cookies
|
|
192
192
|
*/
|
|
193
193
|
constructor(e) {
|
|
194
|
-
this.cookieKey = `${
|
|
194
|
+
this.cookieKey = `${oe}.${e.clientId}`, this.cookiesStorage = new ie();
|
|
195
195
|
}
|
|
196
196
|
/**
|
|
197
197
|
* Store cookies in the browser
|
|
198
198
|
* @param {Partial<CookieValue>} value - information to store
|
|
199
199
|
*/
|
|
200
200
|
save(e) {
|
|
201
|
-
const
|
|
202
|
-
this.cookiesStorage.set(this.cookieKey,
|
|
201
|
+
const o = { ...this.get() || {}, ...e };
|
|
202
|
+
this.cookiesStorage.set(this.cookieKey, o, {
|
|
203
203
|
expires: 1
|
|
204
204
|
});
|
|
205
205
|
}
|
|
@@ -241,7 +241,7 @@ class D extends Error {
|
|
|
241
241
|
}
|
|
242
242
|
class ae extends Error {
|
|
243
243
|
}
|
|
244
|
-
const u = "@trimble-oss/trimble-id-react", g = "1.0.
|
|
244
|
+
const u = "@trimble-oss/trimble-id-react", g = "1.0.4-rc.1", se = {
|
|
245
245
|
configurationEndpoint: "",
|
|
246
246
|
clientId: "",
|
|
247
247
|
redirectUrl: "",
|
|
@@ -280,20 +280,20 @@ class ce {
|
|
|
280
280
|
*/
|
|
281
281
|
constructor(e) {
|
|
282
282
|
const { config: t = se } = e;
|
|
283
|
-
if (this.redirectUrl = t.redirectUrl, t.configurationEndpoint == null || t.configurationEndpoint
|
|
283
|
+
if (this.redirectUrl = t.redirectUrl, t.configurationEndpoint == null || t.configurationEndpoint === "")
|
|
284
284
|
throw new Error("Configuration endpoint not defined");
|
|
285
|
-
if (t.clientId == null || t.clientId
|
|
285
|
+
if (t.clientId == null || t.clientId === "")
|
|
286
286
|
throw new Error("Consumer key is not defined");
|
|
287
|
-
this.cookiesManager = new
|
|
287
|
+
this.cookiesManager = new re({
|
|
288
288
|
clientId: t.clientId
|
|
289
289
|
});
|
|
290
|
-
const
|
|
291
|
-
let
|
|
292
|
-
|
|
290
|
+
const o = this.cookiesManager.get(), i = new $(t.configurationEndpoint);
|
|
291
|
+
let r = new R(
|
|
292
|
+
i,
|
|
293
293
|
t.clientId,
|
|
294
294
|
t.redirectUrl
|
|
295
295
|
).WithScopes(t.scopes);
|
|
296
|
-
t.logoutRedirectUrl && (
|
|
296
|
+
t.logoutRedirectUrl && (r = r.WithLogoutRedirect(t.logoutRedirectUrl)), o?.code_verifier != null && (r = r.WithProofKeyForCodeExchange(o.code_verifier)), this.tokenProvider = r, this.cacheManager = new Z(), this.clientId = t.clientId, h.sendInitEvent("TIDClient", this.clientId, u, g), this.cleanupExpiredState();
|
|
297
297
|
}
|
|
298
298
|
/**
|
|
299
299
|
* Clean up expired state payloads to prevent storage bloat
|
|
@@ -302,8 +302,8 @@ class ce {
|
|
|
302
302
|
try {
|
|
303
303
|
const e = this.cookiesManager.getStatePayload();
|
|
304
304
|
if (e) {
|
|
305
|
-
const t = atob(e),
|
|
306
|
-
Date.now() -
|
|
305
|
+
const t = atob(e), o = JSON.parse(t);
|
|
306
|
+
Date.now() - o.timestamp > A && this.cookiesManager.clearStatePayload();
|
|
307
307
|
}
|
|
308
308
|
} catch {
|
|
309
309
|
this.cookiesManager.clearStatePayload();
|
|
@@ -324,11 +324,11 @@ class ce {
|
|
|
324
324
|
*/
|
|
325
325
|
async loginWithRedirect(e) {
|
|
326
326
|
h.sendMethodEvent(this.loginWithRedirect.name, this.clientId, u, g);
|
|
327
|
-
const { onRedirect: t } = e || {},
|
|
328
|
-
this.cookiesManager.save({ state_payload:
|
|
329
|
-
const
|
|
330
|
-
this.cookiesManager.save({ code_verifier:
|
|
331
|
-
const c = await this.tokenProvider.GetOAuthRedirect(
|
|
327
|
+
const { onRedirect: t } = e || {}, o = window.location.pathname + window.location.search, i = this.createStatePayload(o);
|
|
328
|
+
this.cookiesManager.save({ state_payload: i });
|
|
329
|
+
const r = R.GenerateCodeVerifier();
|
|
330
|
+
this.cookiesManager.save({ code_verifier: r }), this.tokenProvider = this.tokenProvider.WithProofKeyForCodeExchange(r);
|
|
331
|
+
const c = await this.tokenProvider.GetOAuthRedirect(i);
|
|
332
332
|
t != null ? t(c) : window.location.assign(c);
|
|
333
333
|
}
|
|
334
334
|
/**
|
|
@@ -348,10 +348,10 @@ class ce {
|
|
|
348
348
|
const t = this.cookiesManager.get();
|
|
349
349
|
if (t == null || t?.code_verifier == null)
|
|
350
350
|
throw new ae("Code verifier not available");
|
|
351
|
-
const
|
|
351
|
+
const o = ee(e), i = b(e), r = i.get("identity_provider") ?? "", c = i.get("state") ?? "", d = this.validateStatePayload(c);
|
|
352
352
|
if (!d.isValid)
|
|
353
353
|
throw new Error(`State validation failed: ${d.error}`);
|
|
354
|
-
return await this.tokenProvider.ValidateQuery(
|
|
354
|
+
return await this.tokenProvider.ValidateQuery(o), await this.generateToken(r), {
|
|
355
355
|
authState: c,
|
|
356
356
|
returnTo: d.redirectTo
|
|
357
357
|
};
|
|
@@ -362,7 +362,7 @@ class ce {
|
|
|
362
362
|
* @return {Promise<void>} Empty promise
|
|
363
363
|
*/
|
|
364
364
|
async generateToken(e) {
|
|
365
|
-
const t = await this.tokenProvider.RetrieveToken(),
|
|
365
|
+
const t = await this.tokenProvider.RetrieveToken(), o = await this.tokenProvider.RetrieveRefreshToken(), i = await this.tokenProvider.RetrieveIdToken(), r = await this.tokenProvider.RetrieveTokenExpiry(), d = new Date(r).getTime(), k = this.tokenProvider?._scopes?.join(" ");
|
|
366
366
|
await this.cacheManager.setToken({
|
|
367
367
|
scope: k,
|
|
368
368
|
state: "",
|
|
@@ -370,11 +370,11 @@ class ce {
|
|
|
370
370
|
identity_provider: e,
|
|
371
371
|
token_type: "bearer",
|
|
372
372
|
access_token: t,
|
|
373
|
-
refresh_token:
|
|
374
|
-
id_token:
|
|
373
|
+
refresh_token: o,
|
|
374
|
+
id_token: i,
|
|
375
375
|
expires_at: d
|
|
376
376
|
});
|
|
377
|
-
const v = J(
|
|
377
|
+
const v = J(i), p = ne(v);
|
|
378
378
|
await this.cacheManager.setUser(p), await this.reloadCodeVerifier();
|
|
379
379
|
}
|
|
380
380
|
async reloadCodeVerifier() {
|
|
@@ -387,10 +387,10 @@ class ce {
|
|
|
387
387
|
* @return {string} - Base64 encoded state payload
|
|
388
388
|
*/
|
|
389
389
|
createStatePayload(e) {
|
|
390
|
-
const t = this.generateNonce(),
|
|
390
|
+
const t = this.generateNonce(), o = Date.now();
|
|
391
391
|
return btoa(JSON.stringify({
|
|
392
392
|
redirectTo: e,
|
|
393
|
-
timestamp:
|
|
393
|
+
timestamp: o,
|
|
394
394
|
nonce: t
|
|
395
395
|
}));
|
|
396
396
|
}
|
|
@@ -414,18 +414,18 @@ class ce {
|
|
|
414
414
|
const t = this.cookiesManager.getStatePayload();
|
|
415
415
|
if (!t)
|
|
416
416
|
return { isValid: !1, error: "No stored state found" };
|
|
417
|
-
const
|
|
418
|
-
if (!
|
|
417
|
+
const o = atob(e), i = JSON.parse(o), r = atob(t), c = JSON.parse(r);
|
|
418
|
+
if (!i.nonce || !i.timestamp || !i.redirectTo)
|
|
419
419
|
return { isValid: !1, error: "Invalid state payload structure" };
|
|
420
|
-
if (
|
|
420
|
+
if (i.nonce !== c.nonce)
|
|
421
421
|
return { isValid: !1, error: "State nonce mismatch" };
|
|
422
|
-
const k = Date.now() -
|
|
422
|
+
const k = Date.now() - i.timestamp;
|
|
423
423
|
return k > A ? { isValid: !1, error: "State expired - possible replay attack" } : k < 0 ? {
|
|
424
424
|
isValid: !1,
|
|
425
425
|
error: "State timestamp is in the future - possible replay attack"
|
|
426
426
|
} : (this.cookiesManager.clearStatePayload(), {
|
|
427
427
|
isValid: !0,
|
|
428
|
-
redirectTo:
|
|
428
|
+
redirectTo: i.redirectTo
|
|
429
429
|
});
|
|
430
430
|
} catch {
|
|
431
431
|
return { isValid: !1, error: "Invalid state format" };
|
|
@@ -465,14 +465,14 @@ class ce {
|
|
|
465
465
|
if (e?.expires_at == null || e?.expires_at < t.getTime()) {
|
|
466
466
|
try {
|
|
467
467
|
await this.tokenProvider.RetrieveToken();
|
|
468
|
-
} catch (
|
|
468
|
+
} catch (o) {
|
|
469
469
|
throw h.sendExceptionEvent(
|
|
470
470
|
this.getAccessTokenSilently.name,
|
|
471
|
-
|
|
471
|
+
o.message,
|
|
472
472
|
this.clientId,
|
|
473
473
|
u,
|
|
474
474
|
g
|
|
475
|
-
), new O(
|
|
475
|
+
), new O(o.message);
|
|
476
476
|
}
|
|
477
477
|
await this.generateToken(e?.identity_provider ?? ""), e = await this.cacheManager.getToken();
|
|
478
478
|
}
|
|
@@ -500,14 +500,14 @@ class ce {
|
|
|
500
500
|
if (e?.expires_at == null || e?.expires_at < t.getTime()) {
|
|
501
501
|
try {
|
|
502
502
|
await this.tokenProvider.RetrieveToken();
|
|
503
|
-
} catch (
|
|
503
|
+
} catch (o) {
|
|
504
504
|
throw h.sendExceptionEvent(
|
|
505
505
|
this.getTokens.name,
|
|
506
|
-
|
|
506
|
+
o.message,
|
|
507
507
|
this.clientId,
|
|
508
508
|
u,
|
|
509
509
|
g
|
|
510
|
-
), new O(
|
|
510
|
+
), new O(o.message);
|
|
511
511
|
}
|
|
512
512
|
await this.generateToken(e?.identity_provider ?? ""), e = await this.cacheManager.getToken();
|
|
513
513
|
}
|
|
@@ -531,12 +531,12 @@ class ce {
|
|
|
531
531
|
*/
|
|
532
532
|
async logout(e) {
|
|
533
533
|
h.sendMethodEvent(this.logout.name, this.clientId, u, g);
|
|
534
|
-
const { onRedirect: t, disabledAutoRedirect:
|
|
534
|
+
const { onRedirect: t, disabledAutoRedirect: o } = e || {};
|
|
535
535
|
this.cacheManager && await this.cacheManager.clear(), this.cookiesManager && this.cookiesManager.clear();
|
|
536
|
-
const
|
|
536
|
+
const i = await this.tokenProvider.GetOAuthLogoutRedirect("state");
|
|
537
537
|
if (t != null)
|
|
538
|
-
return t(
|
|
539
|
-
|
|
538
|
+
return t(i);
|
|
539
|
+
o || window.location.assign(i);
|
|
540
540
|
}
|
|
541
541
|
/**
|
|
542
542
|
* Check if the user still has a valid session
|
|
@@ -565,8 +565,8 @@ class ce {
|
|
|
565
565
|
const e = await this.cacheManager.getToken();
|
|
566
566
|
if (e == null)
|
|
567
567
|
return;
|
|
568
|
-
const t = e.access_token,
|
|
569
|
-
this.tokenProvider = this.tokenProvider.WithAccessToken(t,
|
|
568
|
+
const t = e.access_token, o = e.refresh_token || "", i = e.id_token, r = e.expires_at;
|
|
569
|
+
this.tokenProvider = this.tokenProvider.WithAccessToken(t, r).WithRefreshToken(o).WithIdToken(i);
|
|
570
570
|
}
|
|
571
571
|
/**
|
|
572
572
|
* Get a http bearer token client to use it for another SDK (Ex: Processing framework)
|
|
@@ -583,11 +583,11 @@ class ce {
|
|
|
583
583
|
return this.redirectUrl;
|
|
584
584
|
}
|
|
585
585
|
}
|
|
586
|
-
const T = H(null), K = () => L(T) ?? {}, de = (
|
|
586
|
+
const T = H(null), K = () => L(T) ?? {}, de = (n, e) => {
|
|
587
587
|
switch (e.type) {
|
|
588
588
|
case "INIT":
|
|
589
589
|
return {
|
|
590
|
-
...
|
|
590
|
+
...n,
|
|
591
591
|
isLoading: !1,
|
|
592
592
|
isAuthenticated: e.user != null,
|
|
593
593
|
user: e.user,
|
|
@@ -597,7 +597,7 @@ const T = H(null), K = () => L(T) ?? {}, de = (o, e) => {
|
|
|
597
597
|
case "HANDLE_CALLBACK_COMPLETE":
|
|
598
598
|
case "GET_ACCESS_TOKEN_COMPLETE":
|
|
599
599
|
return {
|
|
600
|
-
...
|
|
600
|
+
...n,
|
|
601
601
|
isLoading: !1,
|
|
602
602
|
isAuthenticated: e.user != null,
|
|
603
603
|
user: e.user,
|
|
@@ -605,13 +605,13 @@ const T = H(null), K = () => L(T) ?? {}, de = (o, e) => {
|
|
|
605
605
|
};
|
|
606
606
|
case "LOGOUT":
|
|
607
607
|
return {
|
|
608
|
-
...
|
|
608
|
+
...n,
|
|
609
609
|
isAuthenticated: !1,
|
|
610
610
|
user: void 0
|
|
611
611
|
};
|
|
612
612
|
case "ERROR":
|
|
613
613
|
return {
|
|
614
|
-
...
|
|
614
|
+
...n,
|
|
615
615
|
isLoading: !1,
|
|
616
616
|
error: e.error
|
|
617
617
|
};
|
|
@@ -619,46 +619,46 @@ const T = H(null), K = () => L(T) ?? {}, de = (o, e) => {
|
|
|
619
619
|
}, le = {
|
|
620
620
|
isLoading: !0,
|
|
621
621
|
isAuthenticated: !1
|
|
622
|
-
}, he = (
|
|
623
|
-
if (
|
|
622
|
+
}, he = (n) => {
|
|
623
|
+
if (n == null || Object.keys(n).length <= 0)
|
|
624
624
|
return !0;
|
|
625
|
-
const e = Object.keys(
|
|
625
|
+
const e = Object.keys(n);
|
|
626
626
|
for (const t of e)
|
|
627
|
-
if (
|
|
627
|
+
if (n[t] != null && n[t] !== "")
|
|
628
628
|
return !1;
|
|
629
629
|
return !0;
|
|
630
|
-
}, ue = (
|
|
630
|
+
}, ue = (n) => !he(n.config), ge = (n) => {
|
|
631
631
|
const {
|
|
632
632
|
children: e,
|
|
633
633
|
configurationEndpoint: t,
|
|
634
|
-
clientId:
|
|
635
|
-
redirectUrl:
|
|
636
|
-
logoutRedirectUrl:
|
|
634
|
+
clientId: o,
|
|
635
|
+
redirectUrl: i,
|
|
636
|
+
logoutRedirectUrl: r,
|
|
637
637
|
scopes: c,
|
|
638
638
|
onRedirectCallback: d,
|
|
639
639
|
checkRedirectUrlMatch: k
|
|
640
|
-
} =
|
|
640
|
+
} = n;
|
|
641
641
|
if (L(T) != null)
|
|
642
642
|
throw new Error("TID Provider already defined");
|
|
643
643
|
const p = {
|
|
644
644
|
config: {
|
|
645
645
|
configurationEndpoint: t ?? "",
|
|
646
|
-
clientId:
|
|
647
|
-
redirectUrl:
|
|
648
|
-
logoutRedirectUrl:
|
|
646
|
+
clientId: o ?? "",
|
|
647
|
+
redirectUrl: i ?? "",
|
|
648
|
+
logoutRedirectUrl: r ?? "",
|
|
649
649
|
scopes: c ?? [""]
|
|
650
650
|
}
|
|
651
|
-
}, [a] = Q(
|
|
651
|
+
}, [a] = Q(n.tidClient ?? new ce(p)), [w, f] = X(de, le), E = q(!1), W = M(
|
|
652
652
|
() => k ? a.getRedirectUrl() : void 0,
|
|
653
653
|
[k, a]
|
|
654
654
|
);
|
|
655
655
|
N(() => {
|
|
656
|
-
E.current || (
|
|
656
|
+
E.current || (n.tidClient != null && ue({
|
|
657
657
|
config: {
|
|
658
658
|
configurationEndpoint: t,
|
|
659
|
-
clientId:
|
|
660
|
-
redirectUrl:
|
|
661
|
-
logoutRedirectUrl:
|
|
659
|
+
clientId: o,
|
|
660
|
+
redirectUrl: i,
|
|
661
|
+
logoutRedirectUrl: r,
|
|
662
662
|
scopes: c
|
|
663
663
|
}
|
|
664
664
|
}) && console.warn(
|
|
@@ -723,11 +723,11 @@ const T = H(null), K = () => L(T) ?? {}, de = (o, e) => {
|
|
|
723
723
|
[w, S, _, P, I, C]
|
|
724
724
|
);
|
|
725
725
|
return /* @__PURE__ */ V(T.Provider, { value: G, children: e });
|
|
726
|
-
}, me = K, Te = ge, ve = ({ renderComponent:
|
|
727
|
-
const { isAuthenticated: t, isLoading:
|
|
726
|
+
}, me = K, Te = ge, ve = ({ renderComponent: n, loader: e }) => {
|
|
727
|
+
const { isAuthenticated: t, isLoading: o, loginWithRedirect: i } = K();
|
|
728
728
|
return N(() => {
|
|
729
|
-
!
|
|
730
|
-
}, [
|
|
729
|
+
!o && !t && (async () => await i())();
|
|
730
|
+
}, [o, t, i]), t ? n : e || /* @__PURE__ */ V(z, {});
|
|
731
731
|
};
|
|
732
732
|
export {
|
|
733
733
|
ve as AuthenticationGuard,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(d,c){typeof exports=="object"&&typeof module<"u"?c(exports,require("@trimble-oss/trimble-id"),require("es-cookie"),require("jwt-decode"),require("react"),require("react/jsx-runtime")):typeof define=="function"&&define.amd?define(["exports","@trimble-oss/trimble-id","es-cookie","jwt-decode","react","react/jsx-runtime"],c):(d=typeof globalThis<"u"?globalThis:d||self,c(d.ReactTID={},d.trimbleId,d.esCookie,d.jwt_decode,d.React,d.jsxRuntime))})(this,(function(d,c,b,K,l,m){"use strict";function W(n){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(n){for(const t in n)if(t!=="default"){const i=Object.getOwnPropertyDescriptor(n,t);Object.defineProperty(e,t,i.get?i:{enumerable:!0,get:()=>n[t]})}}return e.default=n,Object.freeze(e)}const v=W(b);class G{generateCache=(function(){const e={token:void 0,user:void 0};return{async getToken(){return e.token},async getUser(){return e.user},async storeToken(t){e.token=t},async storeUser(t){e.user=t},clear(){return e.token=void 0,e.user=void 0,Promise.resolve(void 0)}}})()}class H{cacheStorage;constructor(){this.cacheStorage=new G().generateCache}async setToken(e){await this.cacheStorage.storeToken(e)}async setUser(e){await this.cacheStorage.storeUser(e)}async getUser(){return this.cacheStorage.getUser()}async getToken(){return this.cacheStorage.getToken()}async clear(){await this.cacheStorage.clear()}}const E=5*6e4,F=["code","state"],S=600*1e3,C=n=>n.split("?")[0],$=n=>n.split("?")[1],P=n=>{let e=n;if(!n.startsWith("?"))try{e=new URL(n).search}catch{}return new URLSearchParams(e)},q=(n=window.location.href,e)=>{if(e!=null){const i=C(e),o=C(n??"");if(i!==o)return!1}const t=P(n);for(const i of F)if(!t.has(i))return!1;return!0},B=n=>({id:n.sub,name:`${n.given_name} ${n.family_name}`,given_name:n.given_name,family_name:n.family_name,picture:n.picture,email:n.email,email_verified:n.email_verified,...n.account_id&&{account_id:n.account_id}}),J="@TID_COOKIE";class j{get(e){const t=v.get(e);if(t==null)throw new Error("Cookie not found");return JSON.parse(t)}set(e,t,i){let o={};window.location.protocol==="https:"&&(o={secure:!0,sameSite:"none"}),i?.expires&&(o.expires=i.expires),i?.domain&&(o.domain=i.domain),v.set(e,JSON.stringify(t),o)}remove(e,t){const i={};t?.domain&&(i.domain=t.domain),v.remove(e,i)}}class z{cookieKey;cookiesStorage;constructor(e){this.cookieKey=`${J}.${e.clientId}`,this.cookiesStorage=new j}save(e){const i={...this.get()||{},...e};this.cookiesStorage.set(this.cookieKey,i,{expires:1})}getStatePayload(){return this.get()?.state_payload}clearStatePayload(){const e=this.get();e&&(delete e.state_payload,this.cookiesStorage.set(this.cookieKey,e,{expires:1}))}get(){try{return this.cookiesStorage.get(this.cookieKey)}catch{return}}clear(){this.cookiesStorage.remove(this.cookieKey)}}class _ extends Error{}class A extends Error{}class Q extends Error{}const h="@trimble-oss/trimble-id-react",g="1.0.3",X={configurationEndpoint:"",clientId:"",redirectUrl:"",logoutRedirectUrl:"",scopes:[]};class M{tokenProvider;cacheManager;cookiesManager;clientId;redirectUrl;constructor(e){const{config:t=X}=e;if(this.redirectUrl=t.redirectUrl,t.configurationEndpoint==null||t.configurationEndpoint=="")throw new Error("Configuration endpoint not defined");if(t.clientId==null||t.clientId=="")throw new Error("Consumer key is not defined");this.cookiesManager=new z({clientId:t.clientId});const i=this.cookiesManager.get(),o=new c.OpenIdEndpointProvider(t.configurationEndpoint);let r=new c.AuthorizationCodeGrantTokenProvider(o,t.clientId,t.redirectUrl).WithScopes(t.scopes);t.logoutRedirectUrl&&(r=r.WithLogoutRedirect(t.logoutRedirectUrl)),i?.code_verifier!=null&&(r=r.WithProofKeyForCodeExchange(i.code_verifier)),this.tokenProvider=r,this.cacheManager=new H,this.clientId=t.clientId,c.AnalyticsHttpClient.sendInitEvent("TIDClient",this.clientId,h,g),this.cleanupExpiredState()}cleanupExpiredState(){try{const e=this.cookiesManager.getStatePayload();if(e){const t=atob(e),i=JSON.parse(t);Date.now()-i.timestamp>S&&this.cookiesManager.clearStatePayload()}}catch{this.cookiesManager.clearStatePayload()}}async loginWithRedirect(e){c.AnalyticsHttpClient.sendMethodEvent(this.loginWithRedirect.name,this.clientId,h,g);const{onRedirect:t}=e||{},i=window.location.pathname+window.location.search,o=this.createStatePayload(i);this.cookiesManager.save({state_payload:o});const r=c.AuthorizationCodeGrantTokenProvider.GenerateCodeVerifier();this.cookiesManager.save({code_verifier:r}),this.tokenProvider=this.tokenProvider.WithProofKeyForCodeExchange(r);const u=await this.tokenProvider.GetOAuthRedirect(o);t!=null?t(u):window.location.assign(u)}async handleCallback(e=window.location.href){const t=this.cookiesManager.get();if(t==null||t?.code_verifier==null)throw new Q("Code verifier not available");const i=$(e),o=P(e),r=o.get("identity_provider")??"",u=o.get("state")??"",f=this.validateStatePayload(u);if(!f.isValid)throw new Error(`State validation failed: ${f.error}`);return await this.tokenProvider.ValidateQuery(i),await this.generateToken(r),{authState:u,returnTo:f.redirectTo}}async generateToken(e){const t=await this.tokenProvider.RetrieveToken(),i=await this.tokenProvider.RetrieveRefreshToken(),o=await this.tokenProvider.RetrieveIdToken(),r=await this.tokenProvider.RetrieveTokenExpiry(),f=new Date(r).getTime(),y=this.tokenProvider?._scopes?.join(" ");await this.cacheManager.setToken({scope:y,state:"",session_state:"",identity_provider:e,token_type:"bearer",access_token:t,refresh_token:i,id_token:o,expires_at:f});const I=K(o),w=B(I);await this.cacheManager.setUser(w),await this.reloadCodeVerifier()}async reloadCodeVerifier(){const e=await this.tokenProvider.RetrieveCodeVerifier();this.cookiesManager.save({code_verifier:e}),this.tokenProvider=this.tokenProvider.WithProofKeyForCodeExchange(e)}createStatePayload(e){const t=this.generateNonce(),i=Date.now();return btoa(JSON.stringify({redirectTo:e,timestamp:i,nonce:t}))}generateNonce(){const e=new Uint8Array(16);return crypto.getRandomValues(e),Array.from(e,t=>t.toString(16).padStart(2,"0")).join("")}validateStatePayload(e){try{if(!e||e.trim()==="")return{isValid:!1,error:"Empty state parameter"};const t=this.cookiesManager.getStatePayload();if(!t)return{isValid:!1,error:"No stored state found"};const i=atob(e),o=JSON.parse(i),r=atob(t),u=JSON.parse(r);if(!o.nonce||!o.timestamp||!o.redirectTo)return{isValid:!1,error:"Invalid state payload structure"};if(o.nonce!==u.nonce)return{isValid:!1,error:"State nonce mismatch"};const y=Date.now()-o.timestamp;return y>S?{isValid:!1,error:"State expired - possible replay attack"}:y<0?{isValid:!1,error:"State timestamp is in the future - possible replay attack"}:(this.cookiesManager.clearStatePayload(),{isValid:!0,redirectTo:o.redirectTo})}catch{return{isValid:!1,error:"Invalid state format"}}}async getUser(){return c.AnalyticsHttpClient.sendMethodEvent(this.getUser.name,this.clientId,h,g),await this.cacheManager.getUser()}async getAccessTokenSilently(){c.AnalyticsHttpClient.sendMethodEvent(this.getAccessTokenSilently.name,this.clientId,h,g);let e=await this.cacheManager.getToken();if(e==null)throw c.AnalyticsHttpClient.sendExceptionEvent(this.getAccessTokenSilently.name,"No token available",this.clientId,h,g),new A("No token available");const t=new Date(new Date().getTime()+E);if(e?.expires_at==null||e?.expires_at<t.getTime()){try{await this.tokenProvider.RetrieveToken()}catch(i){throw c.AnalyticsHttpClient.sendExceptionEvent(this.getAccessTokenSilently.name,i.message,this.clientId,h,g),new _(i.message)}await this.generateToken(e?.identity_provider??""),e=await this.cacheManager.getToken()}return e?.access_token||""}async getTokens(){c.AnalyticsHttpClient.sendMethodEvent(this.getTokens.name,this.clientId,h,g);let e=await this.cacheManager.getToken();if(e==null)throw c.AnalyticsHttpClient.sendExceptionEvent(this.getTokens.name,"No token available",this.clientId,h,g),new A("No token available");const t=new Date(new Date().getTime()+E);if(e?.expires_at==null||e?.expires_at<t.getTime()){try{await this.tokenProvider.RetrieveToken()}catch(i){throw c.AnalyticsHttpClient.sendExceptionEvent(this.getTokens.name,i.message,this.clientId,h,g),new _(i.message)}await this.generateToken(e?.identity_provider??""),e=await this.cacheManager.getToken()}return{access_token:e?.access_token||"",expires_at:e?.expires_at||0,id_token:e?.id_token||""}}async logout(e){c.AnalyticsHttpClient.sendMethodEvent(this.logout.name,this.clientId,h,g);const{onRedirect:t,disabledAutoRedirect:i}=e||{};this.cacheManager&&await this.cacheManager.clear(),this.cookiesManager&&this.cookiesManager.clear();const o=await this.tokenProvider.GetOAuthLogoutRedirect("state");if(t!=null)return t(o);i||window.location.assign(o)}async checkSession(){try{await this.getAccessTokenSilently()}catch{return!1}return!0}async loadUserSession(){await this.loadCacheSessionIntoSDK(),await this.checkSession()||await this.cacheManager.clear()}async loadCacheSessionIntoSDK(){const e=await this.cacheManager.getToken();if(e==null)return;const t=e.access_token,i=e.refresh_token||"",o=e.id_token,r=e.expires_at;this.tokenProvider=this.tokenProvider.WithAccessToken(t,r).WithRefreshToken(i).WithIdToken(o)}getBearerTokenHttpClient(e){return new c.BearerTokenHttpClientProvider(this.tokenProvider,e)}getRedirectUrl(){return this.redirectUrl}}const T=l.createContext(null),R=()=>l.useContext(T)??{},Y=(n,e)=>{switch(e.type){case"INIT":return{...n,isLoading:!1,isAuthenticated:e.user!=null,user:e.user,error:void 0};case"GET_TOKENS_COMPLETE":case"HANDLE_CALLBACK_COMPLETE":case"GET_ACCESS_TOKEN_COMPLETE":return{...n,isLoading:!1,isAuthenticated:e.user!=null,user:e.user,error:void 0};case"LOGOUT":return{...n,isAuthenticated:!1,user:void 0};case"ERROR":return{...n,isLoading:!1,error:e.error}}},Z={isLoading:!0,isAuthenticated:!1},ee=n=>{if(n==null||Object.keys(n).length<=0)return!0;const e=Object.keys(n);for(const t of e)if(n[t]!=null&&n[t]!=="")return!1;return!0},te=n=>!ee(n.config),ne=n=>{const{children:e,configurationEndpoint:t,clientId:i,redirectUrl:o,logoutRedirectUrl:r,scopes:u,onRedirectCallback:f,checkRedirectUrlMatch:y}=n;if(l.useContext(T)!=null)throw new Error("TID Provider already defined");const w={config:{configurationEndpoint:t??"",clientId:i??"",redirectUrl:o??"",logoutRedirectUrl:r??"",scopes:u??[""]}},[a]=l.useState(n.tidClient??new M(w)),[x,p]=l.useReducer(Y,Z),O=l.useRef(!1),ae=l.useMemo(()=>y?a.getRedirectUrl():void 0,[y,a]);l.useEffect(()=>{O.current||(n.tidClient!=null&&te({config:{configurationEndpoint:t,clientId:i,redirectUrl:o,logoutRedirectUrl:r,scopes:u}})&&console.warn("When TID client is pass as prop, any client configuration property sent directly to the TID Provider component will be ignored"),O.current=!0,(async()=>{try{let s;if(q(void 0,ae)){const k=await a.handleCallback();s=await a.getUser(),f?.(k)}else await a.loadUserSession(),s=await a.getUser();p({type:"INIT",user:s})}catch(s){let k=s;typeof s=="string"&&(k=new Error(s)),p({type:"ERROR",error:k})}})())},[a,f]);const D=l.useCallback(async()=>{const s=await a.getAccessTokenSilently(),k=await a.getUser();return p({type:"GET_ACCESS_TOKEN_COMPLETE",user:k}),s},[a]),U=l.useCallback(async()=>{const s=await a.getTokens(),k=await a.getUser();return p({type:"GET_TOKENS_COMPLETE",user:k}),s},[a]),L=l.useCallback(async s=>{await a.loginWithRedirect(s)},[a]),N=l.useCallback(async s=>{await a.logout(s),s?.disabledAutoRedirect!=null&&s.disabledAutoRedirect&&p({type:"LOGOUT"})},[a]),V=l.useCallback(async s=>{const k=await a.handleCallback(s),ce=await a.getUser();return p({type:"HANDLE_CALLBACK_COMPLETE",user:ce}),k},[a]),se=l.useMemo(()=>({...x,getAccessTokenSilently:D,getTokens:U,loginWithRedirect:L,handleCallback:V,logout:N}),[x,D,U,L,V,N]);return m.jsx(T.Provider,{value:se,children:e})},ie=R,oe=ne,re=({renderComponent:n,loader:e})=>{const{isAuthenticated:t,isLoading:i,loginWithRedirect:o}=R();return l.useEffect(()=>{!i&&!t&&(async()=>await o())()},[i,t,o]),t?n:e||m.jsx(m.Fragment,{})};d.AuthenticationGuard=re,d.TIDClient=M,d.TIDContext=T,d.TIDProvider=oe,d.useAuth=ie,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})}));
|
|
1
|
+
(function(d,c){typeof exports=="object"&&typeof module<"u"?c(exports,require("@trimble-oss/trimble-id"),require("es-cookie"),require("jwt-decode"),require("react"),require("react/jsx-runtime")):typeof define=="function"&&define.amd?define(["exports","@trimble-oss/trimble-id","es-cookie","jwt-decode","react","react/jsx-runtime"],c):(d=typeof globalThis<"u"?globalThis:d||self,c(d.ReactTID={},d.trimbleId,d.esCookie,d.jwt_decode,d.React,d.jsxRuntime))})(this,(function(d,c,b,K,l,m){"use strict";function W(n){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(n){for(const t in n)if(t!=="default"){const i=Object.getOwnPropertyDescriptor(n,t);Object.defineProperty(e,t,i.get?i:{enumerable:!0,get:()=>n[t]})}}return e.default=n,Object.freeze(e)}const v=W(b);class G{generateCache=(function(){const e={token:void 0,user:void 0};return{async getToken(){return e.token},async getUser(){return e.user},async storeToken(t){e.token=t},async storeUser(t){e.user=t},clear(){return e.token=void 0,e.user=void 0,Promise.resolve(void 0)}}})()}class H{cacheStorage;constructor(){this.cacheStorage=new G().generateCache}async setToken(e){await this.cacheStorage.storeToken(e)}async setUser(e){await this.cacheStorage.storeUser(e)}async getUser(){return this.cacheStorage.getUser()}async getToken(){return this.cacheStorage.getToken()}async clear(){await this.cacheStorage.clear()}}const E=5*6e4,F=["code","state"],S=600*1e3,C=n=>n.split("?")[0],$=n=>n.split("?")[1],P=n=>{let e=n;if(!n.startsWith("?"))try{e=new URL(n).search}catch{}return new URLSearchParams(e)},q=(n=window.location.href,e)=>{if(e!=null){const i=C(e),o=C(n??"");if(i!==o)return!1}const t=P(n);for(const i of F)if(!t.has(i))return!1;return!0},B=n=>({id:n.sub,name:`${n.given_name} ${n.family_name}`,given_name:n.given_name,family_name:n.family_name,picture:n.picture,email:n.email,email_verified:n.email_verified,...n.account_id&&{account_id:n.account_id}}),J="@TID_COOKIE";class j{get(e){const t=v.get(e);if(t==null)throw new Error("Cookie not found");return JSON.parse(t)}set(e,t,i){let o={};window.location.protocol==="https:"&&(o={secure:!0,sameSite:"none"}),i?.expires&&(o.expires=i.expires),i?.domain&&(o.domain=i.domain),v.set(e,JSON.stringify(t),o)}remove(e,t){const i={};t?.domain&&(i.domain=t.domain),v.remove(e,i)}}class z{cookieKey;cookiesStorage;constructor(e){this.cookieKey=`${J}.${e.clientId}`,this.cookiesStorage=new j}save(e){const i={...this.get()||{},...e};this.cookiesStorage.set(this.cookieKey,i,{expires:1})}getStatePayload(){return this.get()?.state_payload}clearStatePayload(){const e=this.get();e&&(delete e.state_payload,this.cookiesStorage.set(this.cookieKey,e,{expires:1}))}get(){try{return this.cookiesStorage.get(this.cookieKey)}catch{return}}clear(){this.cookiesStorage.remove(this.cookieKey)}}class _ extends Error{}class A extends Error{}class Q extends Error{}const h="@trimble-oss/trimble-id-react",g="1.0.4-rc.1",X={configurationEndpoint:"",clientId:"",redirectUrl:"",logoutRedirectUrl:"",scopes:[]};class M{tokenProvider;cacheManager;cookiesManager;clientId;redirectUrl;constructor(e){const{config:t=X}=e;if(this.redirectUrl=t.redirectUrl,t.configurationEndpoint==null||t.configurationEndpoint==="")throw new Error("Configuration endpoint not defined");if(t.clientId==null||t.clientId==="")throw new Error("Consumer key is not defined");this.cookiesManager=new z({clientId:t.clientId});const i=this.cookiesManager.get(),o=new c.OpenIdEndpointProvider(t.configurationEndpoint);let r=new c.AuthorizationCodeGrantTokenProvider(o,t.clientId,t.redirectUrl).WithScopes(t.scopes);t.logoutRedirectUrl&&(r=r.WithLogoutRedirect(t.logoutRedirectUrl)),i?.code_verifier!=null&&(r=r.WithProofKeyForCodeExchange(i.code_verifier)),this.tokenProvider=r,this.cacheManager=new H,this.clientId=t.clientId,c.AnalyticsHttpClient.sendInitEvent("TIDClient",this.clientId,h,g),this.cleanupExpiredState()}cleanupExpiredState(){try{const e=this.cookiesManager.getStatePayload();if(e){const t=atob(e),i=JSON.parse(t);Date.now()-i.timestamp>S&&this.cookiesManager.clearStatePayload()}}catch{this.cookiesManager.clearStatePayload()}}async loginWithRedirect(e){c.AnalyticsHttpClient.sendMethodEvent(this.loginWithRedirect.name,this.clientId,h,g);const{onRedirect:t}=e||{},i=window.location.pathname+window.location.search,o=this.createStatePayload(i);this.cookiesManager.save({state_payload:o});const r=c.AuthorizationCodeGrantTokenProvider.GenerateCodeVerifier();this.cookiesManager.save({code_verifier:r}),this.tokenProvider=this.tokenProvider.WithProofKeyForCodeExchange(r);const u=await this.tokenProvider.GetOAuthRedirect(o);t!=null?t(u):window.location.assign(u)}async handleCallback(e=window.location.href){const t=this.cookiesManager.get();if(t==null||t?.code_verifier==null)throw new Q("Code verifier not available");const i=$(e),o=P(e),r=o.get("identity_provider")??"",u=o.get("state")??"",f=this.validateStatePayload(u);if(!f.isValid)throw new Error(`State validation failed: ${f.error}`);return await this.tokenProvider.ValidateQuery(i),await this.generateToken(r),{authState:u,returnTo:f.redirectTo}}async generateToken(e){const t=await this.tokenProvider.RetrieveToken(),i=await this.tokenProvider.RetrieveRefreshToken(),o=await this.tokenProvider.RetrieveIdToken(),r=await this.tokenProvider.RetrieveTokenExpiry(),f=new Date(r).getTime(),y=this.tokenProvider?._scopes?.join(" ");await this.cacheManager.setToken({scope:y,state:"",session_state:"",identity_provider:e,token_type:"bearer",access_token:t,refresh_token:i,id_token:o,expires_at:f});const I=K(o),w=B(I);await this.cacheManager.setUser(w),await this.reloadCodeVerifier()}async reloadCodeVerifier(){const e=await this.tokenProvider.RetrieveCodeVerifier();this.cookiesManager.save({code_verifier:e}),this.tokenProvider=this.tokenProvider.WithProofKeyForCodeExchange(e)}createStatePayload(e){const t=this.generateNonce(),i=Date.now();return btoa(JSON.stringify({redirectTo:e,timestamp:i,nonce:t}))}generateNonce(){const e=new Uint8Array(16);return crypto.getRandomValues(e),Array.from(e,t=>t.toString(16).padStart(2,"0")).join("")}validateStatePayload(e){try{if(!e||e.trim()==="")return{isValid:!1,error:"Empty state parameter"};const t=this.cookiesManager.getStatePayload();if(!t)return{isValid:!1,error:"No stored state found"};const i=atob(e),o=JSON.parse(i),r=atob(t),u=JSON.parse(r);if(!o.nonce||!o.timestamp||!o.redirectTo)return{isValid:!1,error:"Invalid state payload structure"};if(o.nonce!==u.nonce)return{isValid:!1,error:"State nonce mismatch"};const y=Date.now()-o.timestamp;return y>S?{isValid:!1,error:"State expired - possible replay attack"}:y<0?{isValid:!1,error:"State timestamp is in the future - possible replay attack"}:(this.cookiesManager.clearStatePayload(),{isValid:!0,redirectTo:o.redirectTo})}catch{return{isValid:!1,error:"Invalid state format"}}}async getUser(){return c.AnalyticsHttpClient.sendMethodEvent(this.getUser.name,this.clientId,h,g),await this.cacheManager.getUser()}async getAccessTokenSilently(){c.AnalyticsHttpClient.sendMethodEvent(this.getAccessTokenSilently.name,this.clientId,h,g);let e=await this.cacheManager.getToken();if(e==null)throw c.AnalyticsHttpClient.sendExceptionEvent(this.getAccessTokenSilently.name,"No token available",this.clientId,h,g),new A("No token available");const t=new Date(new Date().getTime()+E);if(e?.expires_at==null||e?.expires_at<t.getTime()){try{await this.tokenProvider.RetrieveToken()}catch(i){throw c.AnalyticsHttpClient.sendExceptionEvent(this.getAccessTokenSilently.name,i.message,this.clientId,h,g),new _(i.message)}await this.generateToken(e?.identity_provider??""),e=await this.cacheManager.getToken()}return e?.access_token||""}async getTokens(){c.AnalyticsHttpClient.sendMethodEvent(this.getTokens.name,this.clientId,h,g);let e=await this.cacheManager.getToken();if(e==null)throw c.AnalyticsHttpClient.sendExceptionEvent(this.getTokens.name,"No token available",this.clientId,h,g),new A("No token available");const t=new Date(new Date().getTime()+E);if(e?.expires_at==null||e?.expires_at<t.getTime()){try{await this.tokenProvider.RetrieveToken()}catch(i){throw c.AnalyticsHttpClient.sendExceptionEvent(this.getTokens.name,i.message,this.clientId,h,g),new _(i.message)}await this.generateToken(e?.identity_provider??""),e=await this.cacheManager.getToken()}return{access_token:e?.access_token||"",expires_at:e?.expires_at||0,id_token:e?.id_token||""}}async logout(e){c.AnalyticsHttpClient.sendMethodEvent(this.logout.name,this.clientId,h,g);const{onRedirect:t,disabledAutoRedirect:i}=e||{};this.cacheManager&&await this.cacheManager.clear(),this.cookiesManager&&this.cookiesManager.clear();const o=await this.tokenProvider.GetOAuthLogoutRedirect("state");if(t!=null)return t(o);i||window.location.assign(o)}async checkSession(){try{await this.getAccessTokenSilently()}catch{return!1}return!0}async loadUserSession(){await this.loadCacheSessionIntoSDK(),await this.checkSession()||await this.cacheManager.clear()}async loadCacheSessionIntoSDK(){const e=await this.cacheManager.getToken();if(e==null)return;const t=e.access_token,i=e.refresh_token||"",o=e.id_token,r=e.expires_at;this.tokenProvider=this.tokenProvider.WithAccessToken(t,r).WithRefreshToken(i).WithIdToken(o)}getBearerTokenHttpClient(e){return new c.BearerTokenHttpClientProvider(this.tokenProvider,e)}getRedirectUrl(){return this.redirectUrl}}const T=l.createContext(null),R=()=>l.useContext(T)??{},Y=(n,e)=>{switch(e.type){case"INIT":return{...n,isLoading:!1,isAuthenticated:e.user!=null,user:e.user,error:void 0};case"GET_TOKENS_COMPLETE":case"HANDLE_CALLBACK_COMPLETE":case"GET_ACCESS_TOKEN_COMPLETE":return{...n,isLoading:!1,isAuthenticated:e.user!=null,user:e.user,error:void 0};case"LOGOUT":return{...n,isAuthenticated:!1,user:void 0};case"ERROR":return{...n,isLoading:!1,error:e.error}}},Z={isLoading:!0,isAuthenticated:!1},ee=n=>{if(n==null||Object.keys(n).length<=0)return!0;const e=Object.keys(n);for(const t of e)if(n[t]!=null&&n[t]!=="")return!1;return!0},te=n=>!ee(n.config),ne=n=>{const{children:e,configurationEndpoint:t,clientId:i,redirectUrl:o,logoutRedirectUrl:r,scopes:u,onRedirectCallback:f,checkRedirectUrlMatch:y}=n;if(l.useContext(T)!=null)throw new Error("TID Provider already defined");const w={config:{configurationEndpoint:t??"",clientId:i??"",redirectUrl:o??"",logoutRedirectUrl:r??"",scopes:u??[""]}},[a]=l.useState(n.tidClient??new M(w)),[x,p]=l.useReducer(Y,Z),O=l.useRef(!1),ae=l.useMemo(()=>y?a.getRedirectUrl():void 0,[y,a]);l.useEffect(()=>{O.current||(n.tidClient!=null&&te({config:{configurationEndpoint:t,clientId:i,redirectUrl:o,logoutRedirectUrl:r,scopes:u}})&&console.warn("When TID client is pass as prop, any client configuration property sent directly to the TID Provider component will be ignored"),O.current=!0,(async()=>{try{let s;if(q(void 0,ae)){const k=await a.handleCallback();s=await a.getUser(),f?.(k)}else await a.loadUserSession(),s=await a.getUser();p({type:"INIT",user:s})}catch(s){let k=s;typeof s=="string"&&(k=new Error(s)),p({type:"ERROR",error:k})}})())},[a,f]);const D=l.useCallback(async()=>{const s=await a.getAccessTokenSilently(),k=await a.getUser();return p({type:"GET_ACCESS_TOKEN_COMPLETE",user:k}),s},[a]),U=l.useCallback(async()=>{const s=await a.getTokens(),k=await a.getUser();return p({type:"GET_TOKENS_COMPLETE",user:k}),s},[a]),L=l.useCallback(async s=>{await a.loginWithRedirect(s)},[a]),N=l.useCallback(async s=>{await a.logout(s),s?.disabledAutoRedirect!=null&&s.disabledAutoRedirect&&p({type:"LOGOUT"})},[a]),V=l.useCallback(async s=>{const k=await a.handleCallback(s),ce=await a.getUser();return p({type:"HANDLE_CALLBACK_COMPLETE",user:ce}),k},[a]),se=l.useMemo(()=>({...x,getAccessTokenSilently:D,getTokens:U,loginWithRedirect:L,handleCallback:V,logout:N}),[x,D,U,L,V,N]);return m.jsx(T.Provider,{value:se,children:e})},ie=R,oe=ne,re=({renderComponent:n,loader:e})=>{const{isAuthenticated:t,isLoading:i,loginWithRedirect:o}=R();return l.useEffect(()=>{!i&&!t&&(async()=>await o())()},[i,t,o]),t?n:e||m.jsx(m.Fragment,{})};d.AuthenticationGuard=re,d.TIDClient=M,d.TIDContext=T,d.TIDProvider=oe,d.useAuth=ie,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})}));
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trimble-oss/trimble-id-react",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.4-rc.1",
|
|
5
5
|
"homepage": "https://github.com/trimble-oss/trimble-id-sdk-docs-for-react",
|
|
6
6
|
"author": "Trimble developers <developers@trimble.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"module": "./dist/trimble-id-react.es.js",
|
|
32
32
|
"types": "./dist/index.d.ts",
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@trimble-oss/trimble-id": "^1.0.1",
|
|
34
|
+
"@trimble-oss/trimble-id": "^1.0.3-rc.1",
|
|
35
35
|
"es-cookie": "^1.4.0",
|
|
36
36
|
"husky": "^8.0.3",
|
|
37
37
|
"jwt-decode": "^3.1.2"
|
|
@@ -56,7 +56,6 @@
|
|
|
56
56
|
"eslint-plugin-react-hooks": "^5.2.0",
|
|
57
57
|
"eslint-plugin-react-refresh": "^0.4.26",
|
|
58
58
|
"globals": "^16.2.0",
|
|
59
|
-
"gts": "^7.0.0",
|
|
60
59
|
"typescript-eslint": "^8.46.0",
|
|
61
60
|
"jest": "^30.0.0",
|
|
62
61
|
"jest-environment-jsdom": "^30.0.0",
|