casdoor 1.1.0 → 1.2.0

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/README.md CHANGED
@@ -4,6 +4,10 @@ The Casdoor extension can help browser users automatically log into applications
4
4
 
5
5
  This extension is very simple to use. We will show you the steps below.
6
6
 
7
+ ## Demo video
8
+
9
+ ![AutoLogin](./img/demo.gif)
10
+
7
11
  ## Usage
8
12
 
9
13
  ### Install the extension
package/img/demo.gif ADDED
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "casdoor",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
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
@@ -14,32 +14,84 @@
14
14
 
15
15
  let autoLoginTabs = new Set();
16
16
 
17
- chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
18
- if (tab.url && tab.status === "complete" && !autoLoginTabs.has(tabId) ) {
19
- const url = tab.url;
20
-
21
- chrome.tabs.sendMessage(tabId, {action: "setManagedAccounts"},
17
+ chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
18
+ if (tab.url && tab.status === "complete") {
19
+ const [UserLoggedIn, DisableAutoLogin] = await Promise.all([
20
+ isUserLoggedIn(tabId),
21
+ isDisableAutoLogin(tabId),
22
+ ]);
23
+ if (UserLoggedIn && !DisableAutoLogin && !autoLoginTabs.has(tabId)) {
24
+ autoLogin(tabId, tab.url);
25
+ }
26
+ }
27
+ });
28
+
29
+ async function isUserLoggedIn(tabId) {
30
+ return new Promise((resolve, reject) => {
31
+ chrome.tabs.sendMessage(
32
+ tabId,
33
+ { action: "isUserLoggedIn" },
34
+ function (UserLoggedIn) {
35
+ if (chrome.runtime.lastError) {
36
+ reject(chrome.runtime.lastError.message);
37
+ } else {
38
+ resolve(UserLoggedIn);
39
+ }
40
+ }
41
+ );
42
+ });
43
+ }
44
+
45
+ function getManagedAccounts(tabId) {
46
+ return new Promise((resolve, reject) => {
47
+ chrome.tabs.sendMessage(
48
+ tabId,
49
+ { action: "getManagedAccounts" },
22
50
  function (managedAccounts) {
23
51
  if (chrome.runtime.lastError) {
24
- console.error("Error sending first message:", chrome.runtime.lastError);
25
- } else if (managedAccounts) {
26
- for (const managedAccount of managedAccounts) {
27
- if (managedAccount && url.includes(managedAccount.signinUrl)) {
28
- autoLoginTabs.add(tabId);
29
- chrome.tabs.sendMessage(tabId, {
30
- action: "autoLogin",
31
- managedAccount: managedAccount,
32
- });
33
- }
34
- }
35
- }
36
- }
37
- );
38
- }
39
- });
52
+ reject(chrome.runtime.lastError.message);
53
+ } else {
54
+ resolve(managedAccounts);
55
+ }
56
+ }
57
+ );
58
+ });
59
+ }
60
+
61
+ async function autoLogin(tabId, url) {
62
+ const managedAccounts = await getManagedAccounts(tabId);
63
+ if (managedAccounts) {
64
+ 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;
72
+ }
73
+ }
74
+ }
75
+ }
76
+
77
+ function isDisableAutoLogin(tabId) {
78
+ return new Promise((resolve, reject) => {
79
+ chrome.tabs.sendMessage(
80
+ tabId,
81
+ { action: "isDisableAutoLogin" },
82
+ function (DisableAutoLogin) {
83
+ if (chrome.runtime.lastError) {
84
+ reject(chrome.runtime.lastError);
85
+ } else {
86
+ resolve(DisableAutoLogin);
87
+ }
88
+ }
89
+ );
90
+ });
91
+ }
40
92
 
41
93
  function clearAutoLoginTabs() {
42
94
  autoLoginTabs.clear();
43
95
  }
44
96
 
45
- setInterval(clearAutoLoginTabs, 10000);
97
+ setInterval(clearAutoLoginTabs, 10000);
@@ -98,17 +98,35 @@ window.onload = () => {
98
98
 
99
99
  chrome.runtime.onMessage.addListener((message) => {
100
100
  if (message.action === "autoLogin") {
101
- chrome.storage.sync.get("disableAutoLogin", ({ disableAutoLogin }) => {
102
- if (!disableAutoLogin) {
103
- const managedAccount = message.managedAccount;
104
- autoLogin(managedAccount.username, managedAccount.password);
105
- }
106
- });
101
+ const managedAccount = message.managedAccount;
102
+ autoLogin(managedAccount.username, managedAccount.password);
103
+ }
104
+ });
105
+
106
+ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
107
+ if (message.action === "isUserLoggedIn") {
108
+ chrome.storage.sync.get("accessToken", ({accessToken}) => {
109
+ if(accessToken){
110
+ sendResponse(true);
111
+ } else{
112
+ sendResponse(false);
113
+ }
114
+ });
115
+ return true;
116
+ }
117
+ });
118
+
119
+ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
120
+ if (message.action === "isDisableAutoLogin") {
121
+ chrome.storage.sync.get("disableAutoLogin", ({ disableAutoLogin }) => {
122
+ sendResponse(disableAutoLogin);
123
+ });
124
+ return true;
107
125
  }
108
126
  });
109
127
 
110
128
  chrome.runtime.onMessage.addListener(function(message, sender,sendResponse) {
111
- if (message.action === "setManagedAccounts") {
129
+ if (message.action === "getManagedAccounts") {
112
130
  chrome.storage.sync.get("accessToken", ({accessToken}) => {
113
131
  sdk.getAccount(accessToken).then((account) => {
114
132
  const managedAccounts = account.data.managedAccounts;