casdoor 1.2.1 → 1.3.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "casdoor",
3
- "version": "1.2.1",
3
+ "version": "1.3.1",
4
4
  "description": "The Casdoor extension can help browser users automatically log into applications protected by Casdoor single-sign-on (SSO) system.",
5
5
  "scripts": {
6
6
  "semantic-release": "semantic-release"
package/src/background.js CHANGED
@@ -58,17 +58,32 @@ function getManagedAccounts(tabId) {
58
58
  });
59
59
  }
60
60
 
61
+ function testUrlWithRegex(url, regexPattern) {
62
+ try {
63
+ const regex = new RegExp(regexPattern);
64
+ const isMatched = regex.test(url);
65
+ console.log(`testUrlWithRegex(), url = ${url}, regexPattern = ${regexPattern}, isMatched = ${isMatched}`);
66
+ return isMatched;
67
+ } catch (error) {
68
+ alert(`testUrlWithRegex() error, the regex pattern: "${regexPattern}" is invalid`);
69
+ return false;
70
+ }
71
+ }
72
+
61
73
  async function autoLogin(tabId, url) {
62
74
  const managedAccounts = await getManagedAccounts(tabId);
63
75
  if (managedAccounts) {
64
76
  for (const managedAccount of managedAccounts) {
65
- if (managedAccount && url.includes(managedAccount.signinUrl)) {
66
- autoLoginTabs.add(tabId);
67
- chrome.tabs.sendMessage(tabId, {
68
- action: "autoLogin",
69
- managedAccount: managedAccount,
70
- });
71
- break;
77
+ if (managedAccount && managedAccount.signinUrl && url) {
78
+ const isMatched = testUrlWithRegex(url, managedAccount.signinUrl);
79
+ if (isMatched) {
80
+ autoLoginTabs.add(tabId);
81
+ chrome.tabs.sendMessage(tabId, {
82
+ action: "autoLogin",
83
+ managedAccount: managedAccount,
84
+ });
85
+ break;
86
+ }
72
87
  }
73
88
  }
74
89
  }
package/src/oauth.js CHANGED
@@ -18,18 +18,9 @@ const CasdoorConfig = {
18
18
  applicationName: "",
19
19
  endpoint: "",
20
20
  };
21
- chrome.storage.sync.get("applicationName", ({applicationName}) => {
22
- if (applicationName) {
23
- CasdoorConfig.applicationName = applicationName;
24
- }
25
- });
26
- chrome.storage.sync.get("endpoint", ({endpoint}) => {
27
- if (endpoint) {
28
- CasdoorConfig.endpoint = endpoint;
29
- }
30
- });
31
21
 
32
22
  let sdk;
23
+ sdk = new Sdk(CasdoorConfig);
33
24
 
34
25
  function getStorageData(keys) {
35
26
  return new Promise((resolve, reject) => {
@@ -90,9 +81,13 @@ function logout() {
90
81
 
91
82
  function displayUserProfile(userProfile) {
92
83
  document.getElementById("user").innerHTML = `
93
- <img alt="${userProfile.name}" src="${userProfile.picture}" width="50px" height="25px"/>
84
+ <img alt="${userProfile.name}" src="${userProfile.picture}" width="50px"/>
94
85
  <p>Hi, ${userProfile.name}</p>
95
86
  `;
87
+ setTimeout(function() {
88
+ updateDisabledInput("endpoint", userProfile.iss)
89
+ updateDisabledInput("applicationName", userProfile.applicationName)
90
+ }, 100);
96
91
  document.getElementById("loginOrLogout").innerText = "Logout";
97
92
  }
98
93
 
@@ -101,6 +96,12 @@ function clearUserProfile() {
101
96
  document.getElementById("loginOrLogout").innerText = "Login";
102
97
  }
103
98
 
99
+ function updateDisabledInput(id,value){
100
+ document.getElementById(id).disabled = false;
101
+ document.getElementById(id).value = value;
102
+ document.getElementById(id).disabled = true;
103
+ }
104
+
104
105
  function setInputDisabledState(disabledState, ...elementIds) {
105
106
  elementIds.forEach(elementId => {
106
107
  const inputElement = document.getElementById(elementId);
@@ -111,3 +112,106 @@ function setInputDisabledState(disabledState, ...elementIds) {
111
112
  }
112
113
  });
113
114
  }
115
+
116
+ const intervalDetectToken = setInterval(getDomAccessToken, 3000);
117
+
118
+ let accessToken;
119
+ let applicationName;
120
+
121
+ function getDomAccessToken() {
122
+ if(window.location.pathname != "/src/popup.html"){
123
+ accessToken = document.getElementById("CasdoorAccessToken").getAttribute("value");
124
+ applicationName = document.getElementById("CasdoorApplicationName").getAttribute("value");
125
+ if (accessToken && applicationName) {
126
+ sdk.getUserProfile(accessToken).then((userProfile) => {
127
+ userProfile.applicationName = applicationName;
128
+ if(userProfile){
129
+ showNotification(confirmCallback, userProfile);
130
+ }
131
+ clearInterval(intervalDetectToken);
132
+ });
133
+ }
134
+ } else{
135
+ clearInterval(intervalDetectToken);
136
+ }
137
+ }
138
+
139
+ function confirmCallback(userProfile) {
140
+ if (accessToken && userProfile) {
141
+ chrome.storage.sync.set({ accessToken: accessToken,userProfile: userProfile});
142
+ } else {
143
+ alert("Login failed!");
144
+ }
145
+ }
146
+
147
+
148
+ function showNotification(confirmCallback, userProfile) {
149
+ const notification = document.createElement("div");
150
+ const userDiv = document.createElement("div");
151
+ const endpointDataDiv = document.createElement("div");
152
+ const applicationNameDataDiv = document.createElement("div");
153
+ const buttonDiv = document.createElement("div");
154
+ const confirmButton = document.createElement("button");
155
+ const cancelButton = document.createElement("button");
156
+
157
+ notification.style.position = "fixed";
158
+ notification.style.top = "10px";
159
+ notification.style.right = "10px";
160
+ notification.style.padding = "10px";
161
+ notification.style.margin = "10px";
162
+ notification.style.backgroundColor = "#eceff7";
163
+ notification.style.color = "#000000";
164
+
165
+ userDiv.style.display = "flex";
166
+ userDiv.style.margin = "10px";
167
+ userDiv.style.justifyContent = 'center';
168
+ userDiv.style.alignItems = 'center';
169
+
170
+ endpointDataDiv.style.marginBottom = "10px";
171
+ endpointDataDiv.style.marginTop = "10px";
172
+
173
+ buttonDiv.style.marginTop = "10px";
174
+ buttonDiv.style.display = "flex";
175
+ buttonDiv.style.justifyContent = "space-between";
176
+
177
+ notification.textContent = "Whether to log in to the current account?";
178
+
179
+ userDiv.innerHTML=`
180
+ <img alt="${userProfile.name}" src="${userProfile.picture}" width="50px" style="margin-right: 10px;"/>
181
+ <p>${userProfile.name}</p>
182
+ `
183
+
184
+ cancelButton.textContent = "Cancel";
185
+
186
+ confirmButton.textContent = "OK";
187
+
188
+ endpointDataDiv.innerHTML = `endpoint: <span style="background-color: lightgray;">${userProfile.iss}</span>`;
189
+
190
+ applicationNameDataDiv.innerHTML = `applicationName: <span style="background-color: lightgray;">${userProfile.applicationName}</span>`;
191
+
192
+ confirmButton.onclick = () => {
193
+ if (confirmCallback && typeof confirmCallback === "function") {
194
+ confirmCallback(userProfile);
195
+ }
196
+ document.body.removeChild(notification);
197
+ };
198
+
199
+ cancelButton.onclick = () => {
200
+ document.body.removeChild(notification);
201
+ };
202
+
203
+ notification.appendChild(userDiv);
204
+ notification.appendChild(endpointDataDiv);
205
+ notification.appendChild(applicationNameDataDiv);
206
+ buttonDiv.appendChild(cancelButton);
207
+ buttonDiv.appendChild(confirmButton);
208
+ notification.appendChild(buttonDiv);
209
+ document.body.appendChild(notification);
210
+
211
+
212
+ setTimeout(() => {
213
+ if(document.body.contains(notification)){
214
+ document.body.removeChild(notification);
215
+ }
216
+ }, 5000);
217
+ }
package/src/popup.js CHANGED
@@ -29,18 +29,21 @@ document.addEventListener("DOMContentLoaded", function() {
29
29
  document.addEventListener("DOMContentLoaded", function() {
30
30
  const loginOrLogoutDom = document.getElementById("loginOrLogout");
31
31
 
32
- chrome.storage.sync.get("accessToken", ({accessToken}) => {
33
- loginOrLogoutDom.innerText = accessToken ? "Logout" : "Login";
32
+ loginOrLogoutDom.innerText = accessToken ? "Logout" : "Login";
34
33
 
35
- if (accessToken) {
36
- sdk
37
- .getUserProfile(accessToken)
38
- .then((userProfile) => displayUserProfile(userProfile));
39
- setInputDisabledState(true, "endpoint", "applicationName")
40
- } else {
41
- clearUserProfile();
42
- }
43
- });
34
+ Promise.all([getStorageData("accessToken"), getStorageData("userProfile")])
35
+ .then(([accessToken, userProfile]) => {
36
+ if (accessToken.accessToken && userProfile.userProfile) {
37
+ displayUserProfile(userProfile.userProfile)
38
+ setInputDisabledState(true, "endpoint", "applicationName");
39
+ } else {
40
+ clearUserProfile();
41
+ }
42
+ })
43
+ .catch((error) => {
44
+ console.error("init SDK failed:", error);
45
+ reject(error);
46
+ });
44
47
 
45
48
  loginOrLogoutDom.addEventListener("click", function() {
46
49
  chrome.storage.sync.get("accessToken", ({accessToken}) => {
package/src/sdk.js CHANGED
@@ -89,13 +89,18 @@ class Sdk {
89
89
  .catch((error) => console.error(error));
90
90
  }
91
91
 
92
- getAccountUrl() {
93
- const endpoint = this.config.endpoint;
94
- return `${endpoint}/api/get-account`;
92
+ async getAccountUrl() {
93
+ try {
94
+ const endpointData = await getStorageData("endpoint");
95
+ const endpoint = endpointData.endpoint;
96
+ return `${endpoint}/api/get-account`;
97
+ } catch (error) {
98
+ console.error('Error fetching endpoint:', error);
99
+ }
95
100
  }
96
101
 
97
- getAccount(accessToken) {
98
- const accountUrl = this.getAccountUrl();
102
+ async getAccount(accessToken) {
103
+ const accountUrl = await this.getAccountUrl();
99
104
  const requestConfig = {
100
105
  method: "GET",
101
106
  async: true,