@arkitektbedriftene/fe-lib 6.0.0 → 6.1.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/dist/oidc/AuthManager.d.ts +53 -0
- package/dist/oidc/oidc.d.ts +1 -0
- package/dist/oidc.es.js +202 -109
- package/package.json +1 -1
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { SigninPopupArgs, SigninRedirectArgs, SigninSilentArgs, SignoutRedirectArgs, User, UserManagerSettings } from 'oidc-client-ts';
|
|
2
|
+
export type UserInfo = {
|
|
3
|
+
email: string;
|
|
4
|
+
firmaccess: string;
|
|
5
|
+
firstname: string;
|
|
6
|
+
lastname: string;
|
|
7
|
+
username: string;
|
|
8
|
+
fc: string;
|
|
9
|
+
externalid: string;
|
|
10
|
+
role: string;
|
|
11
|
+
};
|
|
12
|
+
export type ImpersonationData = {
|
|
13
|
+
impersonatedUser: UserInfo;
|
|
14
|
+
impersonationToken: string;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* A simpler wrapper around UserManager that combines normal authentication
|
|
18
|
+
* with impersonation functionality without React context dependency.
|
|
19
|
+
*/
|
|
20
|
+
export declare class AuthManager {
|
|
21
|
+
private userManager;
|
|
22
|
+
private storageKey;
|
|
23
|
+
constructor(settings: UserManagerSettings);
|
|
24
|
+
/**
|
|
25
|
+
* Get impersonation user from localStorage
|
|
26
|
+
*/
|
|
27
|
+
getImpersonationData(): ImpersonationData | null;
|
|
28
|
+
/**
|
|
29
|
+
* Get the current user, preferring impersonated user if available
|
|
30
|
+
*/
|
|
31
|
+
getUser(): Promise<({
|
|
32
|
+
access_token: string;
|
|
33
|
+
profile: UserInfo;
|
|
34
|
+
impersonating: boolean;
|
|
35
|
+
}) | null>;
|
|
36
|
+
signinRedirect(args?: SigninRedirectArgs): Promise<void>;
|
|
37
|
+
signinPopup(args?: SigninPopupArgs): Promise<User>;
|
|
38
|
+
signinSilent(args?: SigninSilentArgs): Promise<User | null>;
|
|
39
|
+
signinCallback(): Promise<User | null>;
|
|
40
|
+
signoutRedirect(args?: SignoutRedirectArgs): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Set impersonation with access token
|
|
43
|
+
*/
|
|
44
|
+
setImpersonation(accessToken: string): void;
|
|
45
|
+
/**
|
|
46
|
+
* Clear impersonation and return to normal user
|
|
47
|
+
*/
|
|
48
|
+
clearImpersonation(): void;
|
|
49
|
+
/**
|
|
50
|
+
* Handle impersonation callback from URL parameters
|
|
51
|
+
*/
|
|
52
|
+
handleImpersonationCallback(urlStr: string): void;
|
|
53
|
+
}
|
package/dist/oidc/oidc.d.ts
CHANGED
package/dist/oidc.es.js
CHANGED
|
@@ -1,235 +1,328 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
var v = Object.defineProperty;
|
|
2
|
+
var R = (t, e, r) => e in t ? v(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
|
|
3
|
+
var g = (t, e, r) => R(t, typeof e != "symbol" ? e + "" : e, r);
|
|
4
|
+
import { jsx as A } from "react/jsx-runtime";
|
|
5
|
+
import { createContext as I, useContext as k, useMemo as U, useEffect as f, useState as L, useRef as x, useCallback as d } from "react";
|
|
6
|
+
import { UserManager as P } from "oidc-client-ts";
|
|
3
7
|
export * from "oidc-client-ts";
|
|
4
|
-
import
|
|
5
|
-
import { jwtDecode as
|
|
6
|
-
const
|
|
8
|
+
import T from "use-local-storage-state";
|
|
9
|
+
import { jwtDecode as y } from "jwt-decode";
|
|
10
|
+
const E = I(
|
|
7
11
|
null
|
|
8
|
-
),
|
|
9
|
-
const t = E
|
|
12
|
+
), b = () => {
|
|
13
|
+
const t = k(E);
|
|
10
14
|
if (!t)
|
|
11
15
|
throw new Error(
|
|
12
16
|
"useImpersonationContext must be used within a ImpersonationContextProvider"
|
|
13
17
|
);
|
|
14
18
|
return t;
|
|
15
|
-
},
|
|
19
|
+
}, V = ({
|
|
16
20
|
children: t
|
|
17
21
|
}) => {
|
|
18
|
-
const [e,
|
|
22
|
+
const [e, r] = T("impersonationState", {
|
|
19
23
|
defaultValue: {}
|
|
20
|
-
}), s =
|
|
24
|
+
}), s = U(() => ({
|
|
21
25
|
accessToken: e == null ? void 0 : e.accessToken,
|
|
22
26
|
userInfo: e == null ? void 0 : e.userInfo,
|
|
23
|
-
setImpersonation: (
|
|
24
|
-
|
|
27
|
+
setImpersonation: (o, i) => {
|
|
28
|
+
r({ accessToken: o, userInfo: i });
|
|
25
29
|
}
|
|
26
30
|
}), []);
|
|
27
|
-
return /* @__PURE__ */
|
|
28
|
-
},
|
|
29
|
-
const { setImpersonation: e } =
|
|
30
|
-
|
|
31
|
-
const { search:
|
|
31
|
+
return /* @__PURE__ */ A(E.Provider, { value: s, children: t });
|
|
32
|
+
}, q = (t) => {
|
|
33
|
+
const { setImpersonation: e } = b();
|
|
34
|
+
f(() => {
|
|
35
|
+
const { search: r } = window.location, s = new URLSearchParams(r).get(
|
|
32
36
|
"impersonateAccessToken"
|
|
33
37
|
);
|
|
34
38
|
if (s && e) {
|
|
35
|
-
const
|
|
36
|
-
e(s,
|
|
39
|
+
const o = y(s);
|
|
40
|
+
e(s, o), t();
|
|
37
41
|
}
|
|
38
42
|
}, []);
|
|
39
43
|
};
|
|
40
|
-
function
|
|
44
|
+
function B(t) {
|
|
41
45
|
return JSON.parse(t).map((s) => {
|
|
42
46
|
const [
|
|
43
|
-
n,
|
|
44
47
|
o,
|
|
45
|
-
u,
|
|
46
48
|
i,
|
|
49
|
+
u,
|
|
50
|
+
a,
|
|
47
51
|
l
|
|
48
|
-
] = s.split("|"), m =
|
|
52
|
+
] = s.split("|"), m = a.split(",");
|
|
49
53
|
return {
|
|
50
|
-
CustomerExternalId:
|
|
51
|
-
CustomerFirmId:
|
|
54
|
+
CustomerExternalId: o,
|
|
55
|
+
CustomerFirmId: i,
|
|
52
56
|
CustomerFirmName: u,
|
|
53
57
|
CustomerUserRoles: m,
|
|
54
58
|
CustomerUserIsAdmin: l === "True" || l === "1"
|
|
55
59
|
};
|
|
56
60
|
});
|
|
57
61
|
}
|
|
58
|
-
const
|
|
62
|
+
const G = [
|
|
59
63
|
"97",
|
|
60
64
|
"131",
|
|
61
65
|
"132",
|
|
62
66
|
"141"
|
|
63
|
-
],
|
|
67
|
+
], H = [
|
|
64
68
|
"3"
|
|
65
|
-
],
|
|
69
|
+
], Q = [
|
|
66
70
|
"0"
|
|
67
|
-
]
|
|
71
|
+
];
|
|
72
|
+
class W {
|
|
73
|
+
constructor(e) {
|
|
74
|
+
g(this, "userManager");
|
|
75
|
+
g(this, "storageKey", "impersonationState");
|
|
76
|
+
this.userManager = new P(e);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Get impersonation user from localStorage
|
|
80
|
+
*/
|
|
81
|
+
getImpersonationData() {
|
|
82
|
+
try {
|
|
83
|
+
const e = localStorage.getItem(this.storageKey);
|
|
84
|
+
if (e) {
|
|
85
|
+
const r = JSON.parse(e);
|
|
86
|
+
if (r.impersonationToken && r.impersonatedUser)
|
|
87
|
+
return r;
|
|
88
|
+
}
|
|
89
|
+
} catch (e) {
|
|
90
|
+
console.error("Failed to load impersonation from storage:", e);
|
|
91
|
+
}
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Get the current user, preferring impersonated user if available
|
|
96
|
+
*/
|
|
97
|
+
async getUser() {
|
|
98
|
+
const e = this.getImpersonationData();
|
|
99
|
+
if (e)
|
|
100
|
+
return {
|
|
101
|
+
access_token: e.impersonationToken,
|
|
102
|
+
profile: e.impersonatedUser,
|
|
103
|
+
impersonating: !0
|
|
104
|
+
};
|
|
105
|
+
const r = await this.userManager.getUser();
|
|
106
|
+
return !r || r.expired ? null : {
|
|
107
|
+
access_token: r.access_token,
|
|
108
|
+
profile: r.profile,
|
|
109
|
+
impersonating: !1
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
async signinRedirect(e) {
|
|
113
|
+
await this.userManager.signinRedirect(e);
|
|
114
|
+
}
|
|
115
|
+
async signinPopup(e) {
|
|
116
|
+
return await this.userManager.signinPopup(e);
|
|
117
|
+
}
|
|
118
|
+
async signinSilent(e) {
|
|
119
|
+
return await this.userManager.signinSilent(e);
|
|
120
|
+
}
|
|
121
|
+
async signinCallback() {
|
|
122
|
+
const e = await this.userManager.signinCallback();
|
|
123
|
+
return e || null;
|
|
124
|
+
}
|
|
125
|
+
async signoutRedirect(e) {
|
|
126
|
+
this.clearImpersonation(), await this.userManager.signoutRedirect(e);
|
|
127
|
+
}
|
|
128
|
+
// Impersonation methods
|
|
129
|
+
/**
|
|
130
|
+
* Set impersonation with access token
|
|
131
|
+
*/
|
|
132
|
+
setImpersonation(e) {
|
|
133
|
+
try {
|
|
134
|
+
const s = {
|
|
135
|
+
impersonatedUser: y(e),
|
|
136
|
+
impersonationToken: e
|
|
137
|
+
};
|
|
138
|
+
localStorage.setItem(this.storageKey, JSON.stringify(s));
|
|
139
|
+
} catch (r) {
|
|
140
|
+
console.error("Failed to decode impersonation token:", r);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Clear impersonation and return to normal user
|
|
145
|
+
*/
|
|
146
|
+
clearImpersonation() {
|
|
147
|
+
localStorage.removeItem(this.storageKey);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Handle impersonation callback from URL parameters
|
|
151
|
+
*/
|
|
152
|
+
handleImpersonationCallback(e) {
|
|
153
|
+
const s = new URL(e).searchParams.get("impersonateAccessToken");
|
|
154
|
+
if (!s)
|
|
155
|
+
throw new Error("Missing url param 'impersonateAccessToken'");
|
|
156
|
+
this.setImpersonation(s);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
const O = ({
|
|
68
160
|
userManager: t,
|
|
69
161
|
context: e,
|
|
70
|
-
children:
|
|
162
|
+
children: r
|
|
71
163
|
}) => {
|
|
72
|
-
const [s,
|
|
164
|
+
const [s, o] = L({
|
|
73
165
|
user: null,
|
|
74
166
|
isLoading: !0,
|
|
75
167
|
isAuthenticated: !1,
|
|
76
168
|
isError: !1,
|
|
77
169
|
error: null
|
|
78
|
-
}),
|
|
79
|
-
|
|
80
|
-
|
|
170
|
+
}), i = x(!1);
|
|
171
|
+
f(() => {
|
|
172
|
+
i.current || (i.current = !0, (async () => {
|
|
81
173
|
try {
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
user:
|
|
174
|
+
const n = await t.getUser();
|
|
175
|
+
o({
|
|
176
|
+
user: n,
|
|
85
177
|
isLoading: !1,
|
|
86
|
-
isAuthenticated:
|
|
178
|
+
isAuthenticated: n ? !n.expired : !1,
|
|
87
179
|
isError: !1,
|
|
88
180
|
error: null
|
|
89
181
|
});
|
|
90
|
-
} catch (
|
|
91
|
-
|
|
182
|
+
} catch (n) {
|
|
183
|
+
o({
|
|
92
184
|
user: null,
|
|
93
185
|
isLoading: !1,
|
|
94
186
|
isAuthenticated: !1,
|
|
95
187
|
isError: !0,
|
|
96
|
-
error:
|
|
188
|
+
error: n instanceof Error ? n : new Error("Unknown error during auth")
|
|
97
189
|
});
|
|
98
190
|
}
|
|
99
191
|
})());
|
|
100
|
-
}, [t]),
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
user:
|
|
192
|
+
}, [t]), f(() => {
|
|
193
|
+
const n = (p) => {
|
|
194
|
+
o({
|
|
195
|
+
user: p,
|
|
104
196
|
isLoading: !1,
|
|
105
|
-
isAuthenticated: !
|
|
197
|
+
isAuthenticated: !p.expired,
|
|
106
198
|
isError: !1,
|
|
107
199
|
error: null
|
|
108
200
|
});
|
|
109
201
|
};
|
|
110
|
-
t.events.addUserLoaded(
|
|
111
|
-
const
|
|
112
|
-
|
|
202
|
+
t.events.addUserLoaded(n);
|
|
203
|
+
const c = () => {
|
|
204
|
+
o({
|
|
113
205
|
...s,
|
|
114
206
|
user: null,
|
|
115
207
|
isAuthenticated: !1
|
|
116
208
|
});
|
|
117
209
|
};
|
|
118
|
-
t.events.addUserUnloaded(
|
|
119
|
-
const w = (
|
|
120
|
-
|
|
210
|
+
t.events.addUserUnloaded(c);
|
|
211
|
+
const w = (p) => {
|
|
212
|
+
o({
|
|
121
213
|
...s,
|
|
122
214
|
isLoading: !1,
|
|
123
215
|
isError: !0,
|
|
124
|
-
error:
|
|
216
|
+
error: p
|
|
125
217
|
});
|
|
126
218
|
};
|
|
127
219
|
return t.events.addSilentRenewError(w), () => {
|
|
128
|
-
t.events.removeUserLoaded(
|
|
220
|
+
t.events.removeUserLoaded(n), t.events.removeUserUnloaded(c), t.events.removeSilentRenewError(w);
|
|
129
221
|
};
|
|
130
222
|
}, [t]);
|
|
131
223
|
const u = d(async () => {
|
|
132
|
-
const
|
|
133
|
-
return
|
|
134
|
-
user:
|
|
224
|
+
const n = await t.signinCallback();
|
|
225
|
+
return o({
|
|
226
|
+
user: n ?? null,
|
|
135
227
|
isLoading: !1,
|
|
136
|
-
isAuthenticated:
|
|
228
|
+
isAuthenticated: n ? !n.expired : !1,
|
|
137
229
|
isError: !1,
|
|
138
230
|
error: null
|
|
139
|
-
}),
|
|
140
|
-
}, [t]),
|
|
141
|
-
async (
|
|
231
|
+
}), n ?? void 0;
|
|
232
|
+
}, [t]), a = d(
|
|
233
|
+
async (n) => {
|
|
142
234
|
try {
|
|
143
|
-
await t.signinRedirect(
|
|
144
|
-
} catch (
|
|
145
|
-
console.error(
|
|
235
|
+
await t.signinRedirect(n);
|
|
236
|
+
} catch (c) {
|
|
237
|
+
console.error(c);
|
|
146
238
|
}
|
|
147
239
|
},
|
|
148
240
|
[t]
|
|
149
241
|
), l = d(
|
|
150
|
-
async (
|
|
242
|
+
async (n) => await t.signinPopup(n),
|
|
151
243
|
[t]
|
|
152
244
|
), m = d(
|
|
153
|
-
async (
|
|
245
|
+
async (n) => {
|
|
154
246
|
try {
|
|
155
|
-
return await t.signinSilent(
|
|
156
|
-
} catch (
|
|
157
|
-
return
|
|
247
|
+
return await t.signinSilent(n);
|
|
248
|
+
} catch (c) {
|
|
249
|
+
return o({
|
|
158
250
|
user: null,
|
|
159
251
|
isLoading: !1,
|
|
160
252
|
isAuthenticated: !1,
|
|
161
253
|
isError: !0,
|
|
162
|
-
error:
|
|
254
|
+
error: c instanceof Error ? c : new Error("Unknown error during silent signin")
|
|
163
255
|
}), null;
|
|
164
256
|
}
|
|
165
257
|
},
|
|
166
258
|
[t]
|
|
167
|
-
),
|
|
168
|
-
async (
|
|
259
|
+
), h = d(
|
|
260
|
+
async (n) => {
|
|
169
261
|
try {
|
|
170
|
-
await t.signoutRedirect(
|
|
171
|
-
} catch (
|
|
172
|
-
console.error(
|
|
262
|
+
await t.signoutRedirect(n);
|
|
263
|
+
} catch (c) {
|
|
264
|
+
console.error(c);
|
|
173
265
|
}
|
|
174
266
|
},
|
|
175
267
|
[t]
|
|
176
|
-
), S =
|
|
268
|
+
), S = U(
|
|
177
269
|
() => ({
|
|
178
270
|
state: s,
|
|
179
271
|
handleSigninCallback: u,
|
|
180
|
-
redirectToSignin:
|
|
272
|
+
redirectToSignin: a,
|
|
181
273
|
signinSilent: m,
|
|
182
274
|
signinPopup: l,
|
|
183
|
-
logout:
|
|
275
|
+
logout: h
|
|
184
276
|
}),
|
|
185
277
|
[
|
|
186
278
|
s,
|
|
187
279
|
u,
|
|
188
|
-
|
|
280
|
+
a,
|
|
189
281
|
m,
|
|
190
282
|
l,
|
|
191
|
-
|
|
283
|
+
h
|
|
192
284
|
]
|
|
193
285
|
);
|
|
194
|
-
return /* @__PURE__ */
|
|
195
|
-
},
|
|
196
|
-
const e =
|
|
286
|
+
return /* @__PURE__ */ A(e.Provider, { value: S, children: r });
|
|
287
|
+
}, C = (t) => {
|
|
288
|
+
const e = k(t);
|
|
197
289
|
if (!e)
|
|
198
290
|
throw new Error("useAuthContext must be used within an AuthProvider");
|
|
199
291
|
return e;
|
|
200
|
-
},
|
|
201
|
-
const { state: e } =
|
|
292
|
+
}, F = (t) => {
|
|
293
|
+
const { state: e } = C(t);
|
|
202
294
|
return e;
|
|
203
|
-
},
|
|
204
|
-
const { state:
|
|
205
|
-
return
|
|
206
|
-
|
|
207
|
-
(
|
|
208
|
-
(u) => setTimeout(() => u(
|
|
295
|
+
}, K = (t, e) => {
|
|
296
|
+
const { state: r, handleSigninCallback: s } = C(t), o = x(!1);
|
|
297
|
+
return f(() => {
|
|
298
|
+
o.current || (o.current = !0, s().then(
|
|
299
|
+
(i) => new Promise(
|
|
300
|
+
(u) => setTimeout(() => u(i), 0)
|
|
209
301
|
)
|
|
210
|
-
).then((
|
|
211
|
-
}, [s]),
|
|
212
|
-
},
|
|
213
|
-
const e =
|
|
302
|
+
).then((i) => e == null ? void 0 : e(i)));
|
|
303
|
+
}, [s]), r;
|
|
304
|
+
}, X = (t) => {
|
|
305
|
+
const e = I(null);
|
|
214
306
|
return {
|
|
215
307
|
AuthContext: e,
|
|
216
|
-
AuthProvider: ({ children:
|
|
217
|
-
useAuthContext: () =>
|
|
218
|
-
useAuthState: () =>
|
|
219
|
-
useSigninCallback: (
|
|
308
|
+
AuthProvider: ({ children: a }) => /* @__PURE__ */ A(O, { userManager: t, context: e, children: a }),
|
|
309
|
+
useAuthContext: () => C(e),
|
|
310
|
+
useAuthState: () => F(e),
|
|
311
|
+
useSigninCallback: (a) => K(e, a),
|
|
220
312
|
getAccessToken: async () => {
|
|
221
|
-
const
|
|
222
|
-
return
|
|
313
|
+
const a = await t.getUser();
|
|
314
|
+
return a ? a.access_token : null;
|
|
223
315
|
}
|
|
224
316
|
};
|
|
225
317
|
};
|
|
226
318
|
export {
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
319
|
+
W as AuthManager,
|
|
320
|
+
V as ImpersonationContextProvider,
|
|
321
|
+
G as ROLESAdminInCompany,
|
|
322
|
+
Q as ROLESAnsatt,
|
|
323
|
+
H as ROLESMAKSAdministrator,
|
|
324
|
+
X as createAuthContext,
|
|
325
|
+
B as parseUserProfileFCString,
|
|
326
|
+
q as useImpersonationCallback,
|
|
327
|
+
b as useImpersonationContext
|
|
235
328
|
};
|