casdoor 1.1.0 → 1.2.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/README.md +4 -0
- package/img/demo.gif +0 -0
- package/manifest.json +2 -2
- package/package.json +1 -1
- package/src/background.js +74 -22
- package/src/content-script.js +25 -7
- package/src/oauth.js +48 -13
- package/src/popup.js +19 -7
- package/src/sdk.js +4 -1
package/README.md
CHANGED
package/img/demo.gif
ADDED
|
Binary file
|
package/manifest.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
|
-
"name": "Casdoor",
|
|
3
|
-
"description": "
|
|
2
|
+
"name": "Casdoor Browser Plugin",
|
|
3
|
+
"description": "Casdoor Browser Plugin can help the users automatically log into applications protected by Casdoor identity provider.",
|
|
4
4
|
"version": "1.0.0",
|
|
5
5
|
"manifest_version": 3,
|
|
6
6
|
"permissions": [
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "casdoor",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.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
|
@@ -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"
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
|
|
25
|
-
} else
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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);
|
package/src/content-script.js
CHANGED
|
@@ -98,17 +98,35 @@ window.onload = () => {
|
|
|
98
98
|
|
|
99
99
|
chrome.runtime.onMessage.addListener((message) => {
|
|
100
100
|
if (message.action === "autoLogin") {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
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 === "
|
|
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;
|
package/src/oauth.js
CHANGED
|
@@ -29,22 +29,57 @@ chrome.storage.sync.get("endpoint", ({endpoint}) => {
|
|
|
29
29
|
}
|
|
30
30
|
});
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
let sdk;
|
|
33
|
+
|
|
34
|
+
function getStorageData(keys) {
|
|
35
|
+
return new Promise((resolve, reject) => {
|
|
36
|
+
chrome.storage.sync.get(keys, (data) => {
|
|
37
|
+
if (chrome.runtime.lastError) {
|
|
38
|
+
reject(chrome.runtime.lastError.message);
|
|
39
|
+
} else {
|
|
40
|
+
resolve(data);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function initInputParamSdk(){
|
|
47
|
+
return new Promise((resolve, reject) => {
|
|
48
|
+
Promise.all([getStorageData("applicationName"), getStorageData("endpoint")])
|
|
49
|
+
.then(([applicationData, endpointData]) => {
|
|
50
|
+
const applicationName = applicationData.applicationName;
|
|
51
|
+
const endpoint = endpointData.endpoint;
|
|
52
|
+
|
|
53
|
+
if (applicationName && endpoint) {
|
|
54
|
+
CasdoorConfig.applicationName = applicationName;
|
|
55
|
+
CasdoorConfig.endpoint = endpoint;
|
|
56
|
+
}
|
|
57
|
+
sdk = new Sdk(CasdoorConfig);
|
|
58
|
+
resolve(sdk);
|
|
59
|
+
})
|
|
60
|
+
.catch((error) => {
|
|
61
|
+
console.error("init SDK failed:", error);
|
|
62
|
+
reject(error);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
}
|
|
33
66
|
|
|
34
67
|
// eslint-disable-next-line no-unused-vars
|
|
35
68
|
function login() {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
69
|
+
initInputParamSdk().then((sdk) => {
|
|
70
|
+
sdk.login((accessToken) => {
|
|
71
|
+
if (accessToken) {
|
|
72
|
+
chrome.storage.sync.set({accessToken}, () => {
|
|
73
|
+
sdk
|
|
74
|
+
.getUserProfile(accessToken)
|
|
75
|
+
.then((userProfile) => displayUserProfile(userProfile));
|
|
76
|
+
});
|
|
77
|
+
setInputDisabledState(true, "endpoint", "applicationName");
|
|
78
|
+
} else {
|
|
79
|
+
alert("Login failed!");
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
})
|
|
48
83
|
}
|
|
49
84
|
|
|
50
85
|
// eslint-disable-next-line no-unused-vars
|
package/src/popup.js
CHANGED
|
@@ -47,7 +47,11 @@ document.addEventListener("DOMContentLoaded", function() {
|
|
|
47
47
|
if (accessToken) {
|
|
48
48
|
logout();
|
|
49
49
|
} else {
|
|
50
|
-
|
|
50
|
+
setInput()
|
|
51
|
+
.then(login())
|
|
52
|
+
.catch(error => {
|
|
53
|
+
console.error('Error login:', error);
|
|
54
|
+
});
|
|
51
55
|
}
|
|
52
56
|
});
|
|
53
57
|
});
|
|
@@ -60,9 +64,6 @@ document.addEventListener("DOMContentLoaded", function() {
|
|
|
60
64
|
applicationNameDom.value = applicationName;
|
|
61
65
|
}
|
|
62
66
|
});
|
|
63
|
-
applicationNameDom.addEventListener("input", function(e) {
|
|
64
|
-
chrome.storage.sync.set({applicationName: e.target.value});
|
|
65
|
-
});
|
|
66
67
|
|
|
67
68
|
const endpointDom = document.getElementById("endpoint");
|
|
68
69
|
chrome.storage.sync.get("endpoint", ({endpoint}) => {
|
|
@@ -70,7 +71,18 @@ document.addEventListener("DOMContentLoaded", function() {
|
|
|
70
71
|
endpointDom.value = endpoint;
|
|
71
72
|
}
|
|
72
73
|
});
|
|
73
|
-
endpointDom.addEventListener("input", function(e) {
|
|
74
|
-
chrome.storage.sync.set({endpoint: e.target.value});
|
|
75
|
-
});
|
|
76
74
|
});
|
|
75
|
+
|
|
76
|
+
function setInput() {
|
|
77
|
+
return new Promise((resolve, reject) => {
|
|
78
|
+
const endpointDom = document.getElementById("endpoint");
|
|
79
|
+
const applicationNameDom = document.getElementById("applicationName");
|
|
80
|
+
chrome.storage.sync.set({ endpoint: endpointDom.value, applicationName: applicationNameDom.value }, function() {
|
|
81
|
+
if (chrome.runtime.lastError) {
|
|
82
|
+
reject(chrome.runtime.lastError.message);
|
|
83
|
+
} else {
|
|
84
|
+
resolve();
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
}
|
package/src/sdk.js
CHANGED
|
@@ -125,6 +125,9 @@ class Sdk {
|
|
|
125
125
|
};
|
|
126
126
|
return fetch(applicationUrl, requestConfig)
|
|
127
127
|
.then((response) => response.json())
|
|
128
|
-
.catch((error) =>
|
|
128
|
+
.catch((error) => {
|
|
129
|
+
console.error(error)
|
|
130
|
+
alert("failed to get application");
|
|
131
|
+
});
|
|
129
132
|
}
|
|
130
133
|
}
|