@gandalan/weblibs 1.1.55 → 1.1.59
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/api/fluentApi.js +2 -0
- package/api/fluentAuthBuilder.js +75 -15
- package/package.json +1 -1
package/api/fluentApi.js
CHANGED
|
@@ -197,6 +197,8 @@ export function createApi() {
|
|
|
197
197
|
this.token = globalThis.idasTokens.token;
|
|
198
198
|
// eslint-disable-next-line no-undef
|
|
199
199
|
this.refreshToken = globalThis.idasTokens.refreshToken;
|
|
200
|
+
// eslint-disable-next-line no-undef
|
|
201
|
+
this.appToken = globalThis.idasTokens.appToken;
|
|
200
202
|
return this;
|
|
201
203
|
},
|
|
202
204
|
|
package/api/fluentAuthBuilder.js
CHANGED
|
@@ -7,12 +7,15 @@ import { isTokenValid, getRefreshToken } from "./fluentApi";
|
|
|
7
7
|
* @property {string} appToken - The application token.
|
|
8
8
|
* @property {string} token - The JWT token.
|
|
9
9
|
* @property {string} refreshToken - The refresh token.
|
|
10
|
-
* @property {function(string) :
|
|
11
|
-
* @property {function(string) :
|
|
12
|
-
* @property {function(string) :
|
|
13
|
-
* @property {function(string) :
|
|
14
|
-
* @property {
|
|
10
|
+
* @property {function(string) : FluentAuth} useAppToken - Sets the application token and returns the FluentApi object.
|
|
11
|
+
* @property {function(string) : FluentAuth} useBaseUrl - Sets the base URL for authentication and returns the FluentApi object.
|
|
12
|
+
* @property {function(string|null) : FluentAuth} useToken - Sets the JWT token and returns the FluentApi object.
|
|
13
|
+
* @property {function(string|null) : FluentAuth} useRefreshToken - Sets the refresh token and returns the FluentApi object.
|
|
14
|
+
* @property {function() : string} authenticate - Authenticates the user with username and password, or refreshes the token.
|
|
15
15
|
* @property {Function} tryRefreshToken - Attempts to refresh the authentication token using the refresh token.
|
|
16
|
+
* @property {Function} redirectToLogin - Redirects to the login page.
|
|
17
|
+
* @property {Function} init - Initializes the authentication object.
|
|
18
|
+
* @property {function(string,string) : string} login - Logs in with the provided credentials.
|
|
16
19
|
*/
|
|
17
20
|
|
|
18
21
|
/**
|
|
@@ -68,14 +71,12 @@ export function authBuilder() {
|
|
|
68
71
|
},
|
|
69
72
|
|
|
70
73
|
/**
|
|
71
|
-
* Authenticates the user with
|
|
72
|
-
*
|
|
73
|
-
* @
|
|
74
|
-
* @param {string} password
|
|
75
|
-
* @returns
|
|
74
|
+
* Authenticates the user with the JWT token or refreshes the token with
|
|
75
|
+
* the refreshToken set before
|
|
76
|
+
* @return {string} the JWT token
|
|
76
77
|
*/
|
|
77
|
-
async authenticate(
|
|
78
|
-
console.log("authenticating:", this.token ? `token set, exp: ${jwtDecode(this.token).exp - (Date.now() / 1000)}` : "no token,", this.refreshToken);
|
|
78
|
+
async authenticate() {
|
|
79
|
+
console.log("authenticating:", this.token ? `token set, exp: ${jwtDecode(this.token).exp - (Date.now() / 1000)}` : "no token,", this.refreshToken, this.appToken);
|
|
79
80
|
|
|
80
81
|
if (this.token && isTokenValid(this.token))
|
|
81
82
|
return this.token;
|
|
@@ -97,6 +98,17 @@ export function authBuilder() {
|
|
|
97
98
|
return this.token;
|
|
98
99
|
}
|
|
99
100
|
|
|
101
|
+
throw new Error("not authenticated");
|
|
102
|
+
},
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Login with credentials and return the JWT token
|
|
106
|
+
* @param {string} username
|
|
107
|
+
* @param {string} password
|
|
108
|
+
* @return {string} the JWT token
|
|
109
|
+
*/
|
|
110
|
+
async login(username = "", password = "")
|
|
111
|
+
{
|
|
100
112
|
if (username && password) {
|
|
101
113
|
const payload = { "Email": username, "Password": password, "AppToken": this.appToken };
|
|
102
114
|
const res = await fetch(`${this.authUrl}/LoginJwt`,
|
|
@@ -104,16 +116,15 @@ export function authBuilder() {
|
|
|
104
116
|
const temptoken = await res.json();
|
|
105
117
|
if (temptoken) {
|
|
106
118
|
this.token = temptoken;
|
|
119
|
+
this.refreshToken = getRefreshToken(temptoken);
|
|
107
120
|
return this.token;
|
|
108
121
|
}
|
|
109
122
|
}
|
|
110
|
-
|
|
111
123
|
throw new Error("not authenticated");
|
|
112
124
|
},
|
|
113
125
|
|
|
114
126
|
/**
|
|
115
|
-
* try to refresh the token using the
|
|
116
|
-
*
|
|
127
|
+
* try to refresh the JWT token by using the refreshToken
|
|
117
128
|
* @async
|
|
118
129
|
* @private
|
|
119
130
|
* @param {string} [refreshToken=""]
|
|
@@ -129,5 +140,54 @@ export function authBuilder() {
|
|
|
129
140
|
});
|
|
130
141
|
return res.ok ? await res.json() : null;
|
|
131
142
|
},
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Initializes the authentication object. Before calling, set the token and refresh token if available.
|
|
146
|
+
* If the token is not set, the refresh token will be used to try to refresh the token.
|
|
147
|
+
* If the token is not valid, the user will be redirected to the login page.
|
|
148
|
+
* If tokens are valid, they will be stored in the global variable idasTokens.
|
|
149
|
+
*/
|
|
150
|
+
async init()
|
|
151
|
+
{
|
|
152
|
+
if (!this.token && this.refreshToken)
|
|
153
|
+
{
|
|
154
|
+
this.token = await this.tryRefreshToken(this.refreshToken);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (this.token && isTokenValid(this.token))
|
|
158
|
+
{
|
|
159
|
+
this.refreshToken = getRefreshToken(this.token);
|
|
160
|
+
localStorage.setItem("idas-refresh-token", this.refreshToken);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (!isTokenValid(this.token))
|
|
164
|
+
{
|
|
165
|
+
this.redirectToLogin();
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// eslint-disable-next-line no-undef
|
|
169
|
+
globalThis.idasTokens = { token: this.token, refreshToken: this.refreshToken, appToken: this.appToken };
|
|
170
|
+
},
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Redirect to the login page
|
|
174
|
+
* @private
|
|
175
|
+
*/
|
|
176
|
+
redirectToLogin() {
|
|
177
|
+
if (!window) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const redirectAfterAuth = new URL(window.location.href).origin;
|
|
182
|
+
let redirectUrl = `${redirectAfterAuth}?r=%target%&j=%jwt%&m=%mandant%`;
|
|
183
|
+
redirectUrl = redirectUrl.replace("%target%", encodeURIComponent(window.location.href));
|
|
184
|
+
|
|
185
|
+
const url = new URL(this.authUrl);
|
|
186
|
+
url.pathname = "/Session";
|
|
187
|
+
url.search = `?a=${this.appToken}&r=${encodeURIComponent(redirectUrl)}`;
|
|
188
|
+
let loginUrl = url.toString();
|
|
189
|
+
|
|
190
|
+
window.location.href = loginUrl;
|
|
191
|
+
}
|
|
132
192
|
};
|
|
133
193
|
}
|