@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.
Files changed (2) hide show
  1. package/api/IDAS.js +54 -65
  2. 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
- return new Promise((resolve, reject) => {
13
- let promise = this.authorize()
14
- .then(() => {
15
- let idas = new IDAS();
16
- return idas.authenticateWithJwt(authJwtCallbackPath)
17
- });
18
- resolve(promise);
19
- });
20
- },
21
-
22
- async authorize() {
23
- let urlParams = new URLSearchParams(location.search);
24
- if (urlParams.has("m")) {
25
- localStorage.setItem("IDAS_MandantGuid", urlParams.get("m"));
26
- }
27
- if (urlParams.has("a")) {
28
- localStorage.setItem("IDAS_ApiBaseUrl", urlParams.get("a"));
29
- apiBaseUrl = urlParams.get("a");
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
- .then(() => {
42
- window.location.search = "";
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
- authorizeWithJwt(jwtToken, mandant = "") {
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
- return new Promise(async (resolve, reject) => {
60
- // no valid JWT, but try to use "refresh token" first to retrive new JWT
61
- let refreshClient = new RESTClient(apiBaseUrl, "");
62
- await refreshClient.checkRefreshToken(authJwtToken, () => {
63
- authJwtToken = undefined;
64
- // ... so repeat authenticate (should lead to /Session login page)
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");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gandalan/weblibs",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "WebLibs for Gandalan JS/TS/Svelte projects",
5
5
  "keywords": [
6
6
  "gandalan"