@gandalan/weblibs 1.0.8 → 1.0.10

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 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/api/RESTClient.js CHANGED
@@ -21,6 +21,8 @@ export class RESTClient {
21
21
  this.token = token;
22
22
  this.isJWT = isJWT;
23
23
 
24
+ axios.defaults.baseURL = url;
25
+
24
26
  if (this.token && !isJWT) {
25
27
  axios.defaults.headers.common["X-Gdl-AuthToken"] = this.token;
26
28
  }
@@ -96,7 +98,7 @@ export class RESTClient {
96
98
 
97
99
  async refreshToken() {
98
100
  try {
99
- await axios.put(`${this.baseurl}/LoginJwt/Refresh`, { token: localStorage.getItem("IDAS_AuthJwtRefreshToken") })
101
+ await this.put("/LoginJwt/Refresh", { token: localStorage.getItem("IDAS_AuthJwtRefreshToken") })
100
102
  .then(resp => {
101
103
  this.updateJwtToken(resp.data);
102
104
  });
@@ -120,7 +122,7 @@ export class RESTClient {
120
122
 
121
123
  async get(uri, noJWT = false) {
122
124
  try {
123
- const response = await axios.get(this.baseurl + uri, this.getUrlOptions(noJWT));
125
+ const response = await axios.get(uri, this.getUrlOptions(noJWT));
124
126
  this.lastError = "";
125
127
  return response.data;
126
128
  } catch (error) {
@@ -130,7 +132,7 @@ export class RESTClient {
130
132
 
131
133
  async getFile(uri) {
132
134
  try {
133
- const response = await axios.get(this.baseurl + uri, { responseType: "blob" });
135
+ const response = await axios.get(uri, { responseType: "blob" });
134
136
  let fileName = "1000.pdf";
135
137
  if (response.headers["content-disposition"]) {
136
138
  fileName = response.headers["content-disposition"].split(";")[1];
@@ -146,7 +148,7 @@ export class RESTClient {
146
148
  async getRaw(uri, noJWT = false) {
147
149
  let response = {};
148
150
  try {
149
- response = await axios.get(this.baseurl + uri, this.getUrlOptions(noJWT))
151
+ response = await axios.get(uri, this.getUrlOptions(noJWT))
150
152
  this.lastError = "";
151
153
  } catch (error) {
152
154
  this.handleError(error);
@@ -156,7 +158,7 @@ export class RESTClient {
156
158
 
157
159
  async post(uri, formData, noJWT = false) {
158
160
  try {
159
- const response = await axios.post(this.baseurl + uri, formData, this.getUrlOptions(noJWT));
161
+ const response = await axios.post(uri, formData, this.getUrlOptions(noJWT));
160
162
  this.lastError = "";
161
163
  return response;
162
164
  } catch (error) {
@@ -166,7 +168,7 @@ export class RESTClient {
166
168
 
167
169
  async put(uri, formData, noJWT = false) {
168
170
  try {
169
- const response = await axios.put(this.baseurl + uri, formData, this.getUrlOptions(noJWT));
171
+ const response = await axios.put(uri, formData, this.getUrlOptions(noJWT));
170
172
  this.lastError = "";
171
173
  return response;
172
174
  } catch (error) {
@@ -176,7 +178,7 @@ export class RESTClient {
176
178
 
177
179
  async delete(uri, noJWT = false) {
178
180
  try {
179
- const response = await axios.delete(this.baseurl + uri, this.getUrlOptions(noJWT));
181
+ const response = await axios.delete(uri, this.getUrlOptions(noJWT));
180
182
  this.lastError = "";
181
183
  return response;
182
184
  } catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gandalan/weblibs",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "description": "WebLibs for Gandalan JS/TS/Svelte projects",
5
5
  "keywords": [
6
6
  "gandalan"