@gandalan/weblibs 1.0.30 → 1.0.32

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
@@ -1,4 +1,4 @@
1
- import { isInvalid } from "./authUtils";
1
+ import { isInvalid, currentToken} from "./authUtils";
2
2
  import { RESTClient } from "./RESTClient";
3
3
  import jwt_decode from 'jwt-decode';
4
4
 
@@ -26,20 +26,18 @@ class IDAS
26
26
  auth = {
27
27
  _self: this,
28
28
  getCurrentAuthToken() {
29
- return this._self.settings.jwtToken;
29
+ return currentToken;
30
30
  },
31
31
  getRights() {
32
- const token = this._self.settings.jwtToken;
33
- if (!token)
32
+ if (!currentToken)
34
33
  return [];
35
- const decoded = jwt_decode(token);
34
+ const decoded = jwt_decode(currentToken);
36
35
  return decoded.rights;
37
36
  },
38
37
  getRoles() {
39
- const token = this._self.settings.jwtToken;
40
- if (!token)
38
+ if (!currentToken)
41
39
  return [];
42
- const decoded = jwt_decode(token);
40
+ const decoded = jwt_decode(currentToken);
43
41
  return decoded.role;
44
42
  },
45
43
  hasRight(code)
@@ -51,10 +49,9 @@ class IDAS
51
49
  return this.getRoles().some(r => r === code);
52
50
  },
53
51
  getUsername() {
54
- const token = this._self.settings.jwtToken;
55
- if (!token)
52
+ if (!currentToken)
56
53
  return undefined;
57
- const decoded = jwt_decode(token);
54
+ const decoded = jwt_decode(currentToken);
58
55
  return decoded.id;
59
56
  }
60
57
  };
package/api/RESTClient.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import axios from "axios";
2
- import { isInvalid, tryRenew } from "./authUtils";
2
+ import { isInvalid, tryRenew, currentToken, currentRefreshToken } from "./authUtils";
3
3
 
4
4
  export class RESTClient {
5
5
  lastError = "";
@@ -12,25 +12,26 @@ export class RESTClient {
12
12
  this.axiosInstance = axios.create({
13
13
  baseURL: settings.apiBaseurl,
14
14
  headers: {
15
- "Authorization" : `Bearer ${ settings.jwtToken }`
15
+ "Authorization" : `Bearer ${currentToken}`
16
16
  }
17
17
  });
18
18
 
19
- this.axiosInstance.interceptors.request.use(async (config) => {
19
+ /*this.axiosInstance.interceptors.request.use(async (config) => {
20
+ console.log("intercept", config.baseURL, config.url);
20
21
  await this.checkTokenBeforeRequest(config);
21
22
  return config;
22
- });
23
+ });*/
23
24
  }
24
25
 
25
- async checkTokenBeforeRequest(config) {
26
- if (this.settings.jwtToken && isInvalid(this.settings)) { // ignore custom/different JWT tokens
26
+ /*async checkTokenBeforeRequest(config) {
27
+ if (currentToken && isInvalid(this.settings)) { // ignore custom/different JWT tokens
27
28
  await tryRenew(this.settings);
28
- console.log(`Updating Header with new JWT Token: ${this.settings.jwtToken}`);
29
+ console.log(`Updating Header with new JWT Token: ${currentToken}`);
29
30
  this.axiosInstance.headers = {
30
- "Authorization" : `Bearer ${ this.settings.jwtToken }`
31
+ "Authorization" : `Bearer ${currentToken}`
31
32
  }
32
33
  }
33
- }
34
+ }*/
34
35
 
35
36
  getUrlOptions() {
36
37
  return { withCredentials: false };
package/api/authUtils.js CHANGED
@@ -1,11 +1,14 @@
1
- import { RESTClient } from "./RESTClient";
2
1
  import jwt_decode from "jwt-decode";
3
2
 
3
+ export let currentToken = undefined;
4
+ export let currentRefreshToken = undefined;
5
+
4
6
  export async function initIDAS(appToken) {
5
7
 
6
8
  let jwtToken = "";
7
9
  let mandantGuid = "";
8
10
  let apiBaseurl = "https://api.dev.idas-cloudservices.net/api/";
11
+ let authUrl = apiBaseurl;
9
12
  let jwtRefreshToken = localStorage.getItem("IDAS_AuthJwtRefreshToken");
10
13
 
11
14
  let urlParams = new URLSearchParams(location.search);
@@ -14,7 +17,12 @@ export async function initIDAS(appToken) {
14
17
  if (urlParams.has("j")) jwtToken = urlParams.get("j");
15
18
  if (urlParams.has("t")) jwtRefreshToken = urlParams.get("t");
16
19
 
17
- let settings = { appToken, mandantGuid, apiBaseurl, jwtToken, jwtRefreshToken };
20
+ authUrl = apiBaseurl;
21
+ currentToken = jwtToken;
22
+ currentRefreshToken = jwtRefreshToken;
23
+ localStorage.setItem("IDAS_AuthJwtRefreshToken", jwtRefreshToken);
24
+
25
+ let settings = { appToken, mandantGuid, apiBaseurl, authUrl };
18
26
  try {
19
27
  await setup(settings);
20
28
  if (isInvalid(settings))
@@ -30,10 +38,10 @@ export async function initIDAS(appToken) {
30
38
  export async function setup(settings)
31
39
  {
32
40
  console.log("Setup IDAS");
33
- if (!settings.jwtToken && !settings.jwtRefreshToken)
34
- throw("Either jwtToken or jwtRefreshToken must be set to authenticate");
41
+ if (!currentToken && !currentRefreshToken)
42
+ throw("Either currentToken or currentRefreshToken must be set to authenticate");
35
43
 
36
- if (settings.jwtRefreshToken && isInvalid(settings))
44
+ if (currentRefreshToken && isInvalid(settings))
37
45
  {
38
46
  await tryRenew(settings);
39
47
  if (isInvalid(settings))
@@ -41,13 +49,14 @@ export async function setup(settings)
41
49
 
42
50
  } else {
43
51
  console.log("Settings already have a valid JWT token, nothing to do");
44
- let decoded = jwt_decode(settings.jwtToken);
52
+ let decoded = jwt_decode(currentToken);
45
53
  let refreshToken = decoded["refreshToken"] || "";
46
54
  if (refreshToken)
47
55
  {
48
56
  console.log("Got new refresh token:", refreshToken);
49
- settings.jwtRefreshToken = refreshToken;
50
57
  localStorage.setItem("IDAS_AuthJwtRefreshToken", refreshToken);
58
+ currentRefreshToken = refreshToken;
59
+ startRefreshTimer(settings);
51
60
  }
52
61
  let mandantGuid = decoded["mandantGuid"] || "";
53
62
  if (mandantGuid)
@@ -56,11 +65,29 @@ export async function setup(settings)
56
65
  console.log("Setup finished", settings);
57
66
  }
58
67
 
68
+ let timerRef = undefined;
69
+ function startRefreshTimer(settings)
70
+ {
71
+ if (timerRef)
72
+ clearInterval(timerRef);
73
+ timerRef = setInterval(() => {
74
+ if (currentToken)
75
+ {
76
+ let decoded = jwt_decode(currentToken);
77
+ const utcNow = Date.parse(new Date().toUTCString()) / 1000;
78
+ if (decoded && utcNow > decoded.exp - 120)
79
+ {
80
+ tryRenew(settings); // fire & forget/don't await --pr
81
+ }
82
+ }
83
+ }, 5000);
84
+ }
85
+
59
86
  export function isInvalid(settings)
60
87
  {
61
- if (!settings.jwtToken)
88
+ if (!currentToken)
62
89
  return true;
63
- let decoded = jwt_decode(settings.jwtToken);
90
+ let decoded = jwt_decode(currentToken);
64
91
  const utcNow = Date.parse(new Date().toUTCString()) / 1000;
65
92
  if (decoded && decoded.exp > utcNow)
66
93
  return false;
@@ -70,20 +97,26 @@ export function isInvalid(settings)
70
97
  export async function tryRenew(settings)
71
98
  {
72
99
  console.log("try to refresh");
73
- const renewSettings = { ...settings, jwtToken : undefined };
74
- let api = new RESTClient(renewSettings);
75
- const payload = { "Token" : settings.jwtRefreshToken };
76
- const response = await api.put("LoginJwt/Refresh", payload);
77
- settings.jwtToken = response.data;
78
- console.log("Got JWT token:", response.data);
79
-
80
- let decoded = jwt_decode(settings.jwtToken);
100
+
101
+ const url = settings.authUrl || settings.apiBaseurl;
102
+ const payload = { "Token" : currentRefreshToken };
103
+ const response = await fetch(url+"LoginJwt/Refresh", {
104
+ method : "PUT",
105
+ body : JSON.stringify(payload),
106
+ headers: { 'Content-Type': 'application/json' }
107
+ });
108
+ const token = await response.json();
109
+ currentToken = token;
110
+ //console.log("Got JWT token:", currentToken);
111
+
112
+ let decoded = jwt_decode(currentToken);
81
113
  let refreshToken = decoded["refreshToken"] || "";
82
114
  if (refreshToken)
83
115
  {
84
116
  console.log("Got new refresh token:", refreshToken);
85
- settings.jwtRefreshToken = refreshToken;
117
+ currentRefreshToken = refreshToken;
86
118
  localStorage.setItem("IDAS_AuthJwtRefreshToken", refreshToken);
119
+ startRefreshTimer(settings);
87
120
  }
88
121
 
89
122
  let mandantGuid = decoded["mandantGuid"] || "";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gandalan/weblibs",
3
- "version": "1.0.30",
3
+ "version": "1.0.32",
4
4
  "description": "WebLibs for Gandalan JS/TS/Svelte projects",
5
5
  "keywords": [
6
6
  "gandalan"