oiu-core1 0.1.16 → 0.1.17
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/index-P82YfPLk.js +223 -0
- package/dist/main-D8ytGMCz.js +4161 -0
- package/dist/main.js +1 -1
- package/package.json +2 -4
- package/dist/index-D5juBzHq.js +0 -2659
- package/dist/main-FWBSwGGL.js +0 -19770
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { WebStorageStateStore as g, UserManager as h } from "oidc-client-ts";
|
|
2
|
+
import { A as p, P as s, a as o, d as l } from "./main-D8ytGMCz.js";
|
|
3
|
+
function w(c, e, t) {
|
|
4
|
+
return new _(c, e, t);
|
|
5
|
+
}
|
|
6
|
+
class _ extends p {
|
|
7
|
+
/**
|
|
8
|
+
* Initialize a new instance of the Auth service
|
|
9
|
+
* @param prefix
|
|
10
|
+
* @param settings
|
|
11
|
+
* @param store
|
|
12
|
+
* @param popupPolicy Popup policy to use for authentication. By default pop ups are disabled
|
|
13
|
+
* because they don't work properly on Edge. If you want to enable pop up authentication simply set this flag to false.
|
|
14
|
+
*/
|
|
15
|
+
constructor(e, t, r) {
|
|
16
|
+
super();
|
|
17
|
+
const { prefix: i, popupPolicy: a = s.Never, onIdentityCreated: n } = r, u = new g({ prefix: i, store: t });
|
|
18
|
+
this._userManager = new h({ ...e, userStore: u }), this._popUpPolicy = a, this._onIdentityCreated = n, this._userManager.events.addUserSignedOut(async () => {
|
|
19
|
+
console.warn("User SignedOut event triggger"), await this._userManager.removeUser(), this._updateState("signedout");
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
/** @inheritdoc */
|
|
23
|
+
getUnsafeInstance() {
|
|
24
|
+
return this._userManager;
|
|
25
|
+
}
|
|
26
|
+
/** @inheritdoc */
|
|
27
|
+
getUser() {
|
|
28
|
+
return this._userManager.getUser();
|
|
29
|
+
}
|
|
30
|
+
/** @inheritdoc */
|
|
31
|
+
async getIdentity() {
|
|
32
|
+
if (this._identity) return this._identity;
|
|
33
|
+
const e = await this.getUser(), t = this._getIdentity(e);
|
|
34
|
+
return this._updateState("update", t), t;
|
|
35
|
+
}
|
|
36
|
+
/** @inheritdoc */
|
|
37
|
+
async clear(e) {
|
|
38
|
+
await this._userManager.clearStaleState(), e && (await this._userManager.removeUser(), this._updateState("update"));
|
|
39
|
+
}
|
|
40
|
+
/** @inheritdoc */
|
|
41
|
+
async signIn(e, t, r, i = !1) {
|
|
42
|
+
const a = this._createArguments(e, t);
|
|
43
|
+
if (!i) {
|
|
44
|
+
const n = await this.trySilentSignIn(e, a);
|
|
45
|
+
if (n) return n;
|
|
46
|
+
}
|
|
47
|
+
switch (this._popUpPolicy) {
|
|
48
|
+
case s.Always:
|
|
49
|
+
return await this.tryPopUpSignIn(e, a);
|
|
50
|
+
case s.Never:
|
|
51
|
+
return await this.tryRedirectSignIn(e, a);
|
|
52
|
+
case s.RedirectIfFails: {
|
|
53
|
+
const n = await this.tryPopUpSignIn(e, a);
|
|
54
|
+
return n && n.status !== o.Fail ? n : (r?.("Please authorize the pop up window."), await Promise.delay(5e3), await this.tryRedirectSignIn(e, a));
|
|
55
|
+
}
|
|
56
|
+
default:
|
|
57
|
+
throw new Error(`Invalid popup policy ${this._popUpPolicy}.`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/** @inheritdoc */
|
|
61
|
+
async completeSignIn(e) {
|
|
62
|
+
try {
|
|
63
|
+
let t;
|
|
64
|
+
switch (this._popUpPolicy) {
|
|
65
|
+
case s.Always:
|
|
66
|
+
await this._userManager.signinPopupCallback(e);
|
|
67
|
+
break;
|
|
68
|
+
case s.Never:
|
|
69
|
+
t = await this._userManager.signinCallback(e);
|
|
70
|
+
break;
|
|
71
|
+
case s.RedirectIfFails:
|
|
72
|
+
try {
|
|
73
|
+
await this._userManager.signinPopupCallback(e);
|
|
74
|
+
} catch (i) {
|
|
75
|
+
console.warn("Popup is not authorized: ", i), t = await this._userManager.signinCallback(e);
|
|
76
|
+
}
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
t || (t = await this._userManager.getUser());
|
|
80
|
+
const r = this._getIdentity(t);
|
|
81
|
+
return this._updateState("signin", r), this._success(t && t.state);
|
|
82
|
+
} catch (t) {
|
|
83
|
+
return console.error("There was an error signing in: ", t), this._error("There was an error signing in.");
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/** @inheritdoc */
|
|
87
|
+
async silentSignIn(e, t) {
|
|
88
|
+
const r = this._createArguments(e, t);
|
|
89
|
+
return await this.trySilentSignIn(e, r);
|
|
90
|
+
}
|
|
91
|
+
/** @inheritdoc */
|
|
92
|
+
async completeSilentSignin(e) {
|
|
93
|
+
try {
|
|
94
|
+
return await this._userManager.signinSilentCallback(e), this._success();
|
|
95
|
+
} catch (t) {
|
|
96
|
+
return console.error("There was an error signing in: ", t), this._error("There was an error signing in.");
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/** @inheritdoc */
|
|
100
|
+
async signOut(e) {
|
|
101
|
+
const t = this._createArguments(e);
|
|
102
|
+
switch (this._popUpPolicy) {
|
|
103
|
+
case s.Always:
|
|
104
|
+
return this.tryPopUpSignOut(e, t);
|
|
105
|
+
case s.Never:
|
|
106
|
+
return this.tryRedirectSignOut(e, t);
|
|
107
|
+
default:
|
|
108
|
+
case s.RedirectIfFails:
|
|
109
|
+
throw new Error("RedirectIfFails is not supported for sign out.");
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/** @inheritdoc */
|
|
113
|
+
async completeSignOut(e) {
|
|
114
|
+
try {
|
|
115
|
+
switch (this._popUpPolicy) {
|
|
116
|
+
case s.Always:
|
|
117
|
+
await this._userManager.signoutPopupCallback(e);
|
|
118
|
+
break;
|
|
119
|
+
case s.Never:
|
|
120
|
+
await this._userManager.signoutCallback(e);
|
|
121
|
+
break;
|
|
122
|
+
case s.RedirectIfFails:
|
|
123
|
+
throw new Error("RedirectIfFails is not supported for sign out.");
|
|
124
|
+
}
|
|
125
|
+
return this._updateState("signout"), this._success();
|
|
126
|
+
} catch (t) {
|
|
127
|
+
const r = t;
|
|
128
|
+
return console.error(`There was an error trying to log out '${t}'.`), this._error(r.message);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
// #region ========================================= Protected =========================================
|
|
132
|
+
async trySilentSignIn(e, t) {
|
|
133
|
+
console.log("Try silent sign in");
|
|
134
|
+
try {
|
|
135
|
+
const r = await this._userManager.signinSilent({
|
|
136
|
+
extraQueryParams: t
|
|
137
|
+
}), i = this._getIdentity(r);
|
|
138
|
+
return this._updateState("silentSignIn", i), this._success(e);
|
|
139
|
+
} catch (r) {
|
|
140
|
+
console.warn("Silent authentication error: ", r);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
async tryRedirectSignIn(e, t) {
|
|
144
|
+
console.log("Try redirect sign in");
|
|
145
|
+
try {
|
|
146
|
+
return await this._userManager.signinRedirect(t), this._redirect(e);
|
|
147
|
+
} catch (r) {
|
|
148
|
+
return console.error("Redirect authentication error: ", r), this._error(r);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
async tryPopUpSignIn(e, t) {
|
|
152
|
+
console.log("Try popup sign in");
|
|
153
|
+
try {
|
|
154
|
+
const r = await this._userManager.signinPopup(t), i = this._getIdentity(r);
|
|
155
|
+
return this._updateState("popupSignIn", i), this._success(e);
|
|
156
|
+
} catch (r) {
|
|
157
|
+
const i = r;
|
|
158
|
+
return i.message.indexOf("closed") !== -1 && i.message.indexOf("user") ? this._error("The user closed the window.") : this._error("Something went wrong. Please try again later.");
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
async tryPopUpSignOut(e, t) {
|
|
162
|
+
try {
|
|
163
|
+
return await this._userManager.signoutPopup(t), this._updateState("popupSignOut", void 0), this._success(e);
|
|
164
|
+
} catch (r) {
|
|
165
|
+
return console.info("Popup signout error: ", r), this._error("Something went wrong. Try again later.");
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
async tryRedirectSignOut(e, t) {
|
|
169
|
+
try {
|
|
170
|
+
return await this._userManager.signoutRedirect(t), this._redirect(e);
|
|
171
|
+
} catch (r) {
|
|
172
|
+
const i = r;
|
|
173
|
+
return console.error("Redirect signout error: ", r), this._error(i.message);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
// #endregion
|
|
177
|
+
// #region ========================================= Private =========================================
|
|
178
|
+
_getIdentity(e) {
|
|
179
|
+
if (!e) return;
|
|
180
|
+
const t = {
|
|
181
|
+
id_token: e.id_token,
|
|
182
|
+
session_state: e.session_state,
|
|
183
|
+
access_token: e.access_token,
|
|
184
|
+
refresh_token: e.refresh_token,
|
|
185
|
+
token_type: e.token_type,
|
|
186
|
+
scope: e.scope,
|
|
187
|
+
expires_at: e.expires_at ?? 3600,
|
|
188
|
+
profile: {
|
|
189
|
+
id: e.profile.sub,
|
|
190
|
+
firstName: e.profile.given_name ?? "Unknown",
|
|
191
|
+
lastName: e.profile.family_name ?? "Unknown",
|
|
192
|
+
username: e.profile.preferred_username ?? "unknown",
|
|
193
|
+
roles: e.profile.role ?? []
|
|
194
|
+
}
|
|
195
|
+
}, r = l(e.access_token);
|
|
196
|
+
return t.profile.firstName = r.given_name, t.profile.lastName = r.family_name, t.profile.roles = r.role, t.profile.username = r.preferred_username, this._onIdentityCreated?.(t, e, r), t;
|
|
197
|
+
}
|
|
198
|
+
_createArguments(e, t) {
|
|
199
|
+
return {
|
|
200
|
+
// redirectMethod: 'replace',
|
|
201
|
+
// state: state,
|
|
202
|
+
useReplaceToNavigate: !0,
|
|
203
|
+
data: e,
|
|
204
|
+
extraQueryParams: t
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
_success(e) {
|
|
208
|
+
return { status: o.Success, state: e };
|
|
209
|
+
}
|
|
210
|
+
_error(e) {
|
|
211
|
+
return { status: o.Fail, message: e };
|
|
212
|
+
}
|
|
213
|
+
_redirect(e) {
|
|
214
|
+
return { status: o.Redirect, state: e };
|
|
215
|
+
}
|
|
216
|
+
_updateState(e, t) {
|
|
217
|
+
t !== this._identity && (this._identity = t, this._notifySubscribers(e, t));
|
|
218
|
+
}
|
|
219
|
+
// #endregion
|
|
220
|
+
}
|
|
221
|
+
export {
|
|
222
|
+
w as default
|
|
223
|
+
};
|