@gandalan/weblibs 1.0.8 → 1.0.9
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/IDAS.js +54 -65
- package/package.json +1 -1
package/api/IDAS.js
CHANGED
|
@@ -8,82 +8,71 @@ let authJwtCallbackPath = localStorage.getItem("IDAS_AuthJwtCallbackPath") || ""
|
|
|
8
8
|
let authJwtToken;
|
|
9
9
|
|
|
10
10
|
export let IDASFactory = {
|
|
11
|
-
async create(
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if (urlParams.has("j")) { // it is JWT
|
|
32
|
-
let idas = new IDAS();
|
|
33
|
-
idas.authorizeWithJwt(urlParams.get("j"));
|
|
34
|
-
window.location.search = "";
|
|
35
|
-
return Promise.reject("redirect is required");
|
|
36
|
-
}
|
|
37
|
-
if (urlParams.has("t")) { // it is authToken
|
|
38
|
-
localStorage.setItem("IDAS_AuthJwtRefreshToken", urlParams.get("t"));
|
|
11
|
+
async create(settings = {
|
|
12
|
+
appToken : localStorage.getItem("IDAS_AppToken"),
|
|
13
|
+
mandantGuid : localStorage.getItem("IDAS_MandantGuid"),
|
|
14
|
+
apiBaseurl : localStorage.getItem("IDAS_ApiBaseUrl"),
|
|
15
|
+
jwtRefreshToken : localStorage.getItem("IDAS_AuthJwtRefreshToken"),
|
|
16
|
+
jwtCallbackPath : localStorage.getItem("IDAS_AuthJwtCallbackPath")
|
|
17
|
+
})
|
|
18
|
+
{
|
|
19
|
+
apiBaseUrl = settings.apiBaseurl;
|
|
20
|
+
let idas = undefined;
|
|
21
|
+
|
|
22
|
+
if (settings.jwtToken) // it is JWT
|
|
23
|
+
{
|
|
24
|
+
console.log("init: with JWT token");
|
|
25
|
+
idas = new IDAS();
|
|
26
|
+
idas.initWithJWTtoken(settings.jwtToken);
|
|
27
|
+
}
|
|
28
|
+
else if (settings.jwtRefreshToken) // it is authToken
|
|
29
|
+
{
|
|
30
|
+
console.log("init: with refresh/classic token");
|
|
39
31
|
let refreshClient = new RESTClient(apiBaseUrl, "");
|
|
40
|
-
await refreshClient.refreshToken()
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
});
|
|
44
|
-
return Promise.reject("redirect is required");
|
|
32
|
+
await refreshClient.refreshToken();
|
|
33
|
+
idas = new IDAS();
|
|
34
|
+
await idas.authenticateWithJwt(authJwtCallbackPath);
|
|
45
35
|
}
|
|
46
|
-
|
|
36
|
+
|
|
37
|
+
return idas;
|
|
38
|
+
}
|
|
47
39
|
}
|
|
48
40
|
|
|
49
|
-
class IDAS
|
|
41
|
+
class IDAS
|
|
42
|
+
{
|
|
50
43
|
restClient = undefined;
|
|
51
44
|
|
|
52
|
-
|
|
45
|
+
initWithJWTtoken(jwtToken)
|
|
46
|
+
{
|
|
53
47
|
authJwtToken = jwtToken;
|
|
54
|
-
mandant && localStorage.setItem("IDAS_MandantGuid", mandant);
|
|
55
48
|
this.restClient = new RESTClient(apiBaseUrl, jwtToken, true);
|
|
56
49
|
}
|
|
57
50
|
|
|
58
|
-
async authenticateWithJwt(authPath)
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
new IDAS().authenticateWithJwt(authPath);
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
// still not valid JWT -> authenticate
|
|
69
|
-
if (!refreshClient.token) {
|
|
70
|
-
localStorage.setItem("IDAS_AuthJwtCallbackPath", authPath || "");
|
|
71
|
-
const authEndpoint = (new URL(window.location.href).origin) + authPath;
|
|
72
|
-
let authUrlCallback = `${authEndpoint}?r=%target%&j=%jwt%&m=%mandant%`;
|
|
73
|
-
authUrlCallback = authUrlCallback.replace("%target%", encodeURIComponent(window.location.href));
|
|
74
|
-
|
|
75
|
-
const url = new URL(apiBaseUrl);
|
|
76
|
-
url.pathname = "/Session";
|
|
77
|
-
url.search = `?a=${appToken}&r=${encodeURIComponent(authUrlCallback)}`;
|
|
78
|
-
let jwtUrl = url.toString();
|
|
79
|
-
|
|
80
|
-
window.location = jwtUrl;
|
|
81
|
-
reject("not authenticated yet");
|
|
82
|
-
} else {
|
|
83
|
-
this.authorizeWithJwt(refreshClient.token);
|
|
84
|
-
resolve(this);
|
|
85
|
-
}
|
|
51
|
+
async authenticateWithJwt(authPath)
|
|
52
|
+
{
|
|
53
|
+
let refreshClient = new RESTClient(apiBaseUrl, "");
|
|
54
|
+
await refreshClient.checkRefreshToken(authJwtToken, () => {
|
|
55
|
+
authJwtToken = undefined;
|
|
56
|
+
// ... so repeat authenticate (should lead to /Session login page)
|
|
57
|
+
new IDAS().authenticateWithJwt(authPath);
|
|
86
58
|
});
|
|
59
|
+
|
|
60
|
+
// still not valid JWT -> authenticate
|
|
61
|
+
if (!refreshClient.token) {
|
|
62
|
+
localStorage.setItem("IDAS_AuthJwtCallbackPath", authPath || "");
|
|
63
|
+
const authEndpoint = (new URL(window.location.href).origin) + authPath;
|
|
64
|
+
let authUrlCallback = `${authEndpoint}?r=%target%&j=%jwt%&m=%mandant%`;
|
|
65
|
+
authUrlCallback = authUrlCallback.replace("%target%", encodeURIComponent(window.location.href));
|
|
66
|
+
|
|
67
|
+
const url = new URL(apiBaseUrl);
|
|
68
|
+
url.pathname = "/Session";
|
|
69
|
+
url.search = `?a=${appToken}&r=${encodeURIComponent(authUrlCallback)}`;
|
|
70
|
+
let jwtUrl = url.toString();
|
|
71
|
+
|
|
72
|
+
window.location = jwtUrl;
|
|
73
|
+
} else {
|
|
74
|
+
this.initWithJWTtoken(refreshClient.token);
|
|
75
|
+
}
|
|
87
76
|
}
|
|
88
77
|
|
|
89
78
|
mandantGuid = localStorage.getItem("IDAS_MandantGuid");
|