casdoor 1.3.2 → 1.5.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/.eslintrc.js +1 -0
- package/.github/workflows/release.yaml +8 -4
- package/README.md +15 -3
- package/manifest.json +14 -15
- package/package.json +6 -6
- package/src/background.js +333 -77
- package/src/content-script.js +311 -110
- package/src/match.js +92 -0
- package/src/popup.css +133 -77
- package/src/popup.html +31 -18
- package/src/popup.js +101 -65
- package/src/config.js +0 -47
- package/src/oauth.js +0 -219
- package/src/sdk.js +0 -138
package/src/oauth.js
DELETED
|
@@ -1,219 +0,0 @@
|
|
|
1
|
-
// Copyright 2022 The Casdoor Authors. All Rights Reserved.
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
|
|
15
|
-
/* eslint-disable no-undef */
|
|
16
|
-
|
|
17
|
-
const CasdoorConfig = {
|
|
18
|
-
applicationName: "",
|
|
19
|
-
endpoint: "",
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
let sdk;
|
|
23
|
-
sdk = new Sdk(CasdoorConfig);
|
|
24
|
-
|
|
25
|
-
function getStorageData(keys) {
|
|
26
|
-
return new Promise((resolve, reject) => {
|
|
27
|
-
chrome.storage.sync.get(keys, (data) => {
|
|
28
|
-
if (chrome.runtime.lastError) {
|
|
29
|
-
reject(chrome.runtime.lastError.message);
|
|
30
|
-
} else {
|
|
31
|
-
resolve(data);
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function initInputParamSdk(){
|
|
38
|
-
return new Promise((resolve, reject) => {
|
|
39
|
-
Promise.all([getStorageData("applicationName"), getStorageData("endpoint")])
|
|
40
|
-
.then(([applicationData, endpointData]) => {
|
|
41
|
-
const applicationName = applicationData.applicationName;
|
|
42
|
-
const endpoint = endpointData.endpoint;
|
|
43
|
-
|
|
44
|
-
if (applicationName && endpoint) {
|
|
45
|
-
CasdoorConfig.applicationName = applicationName;
|
|
46
|
-
CasdoorConfig.endpoint = endpoint;
|
|
47
|
-
}
|
|
48
|
-
sdk = new Sdk(CasdoorConfig);
|
|
49
|
-
resolve(sdk);
|
|
50
|
-
})
|
|
51
|
-
.catch((error) => {
|
|
52
|
-
console.error("init SDK failed:", error);
|
|
53
|
-
reject(error);
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// eslint-disable-next-line no-unused-vars
|
|
59
|
-
function login() {
|
|
60
|
-
initInputParamSdk().then((sdk) => {
|
|
61
|
-
sdk.login((accessToken) => {
|
|
62
|
-
if (accessToken) {
|
|
63
|
-
chrome.storage.sync.set({accessToken}, () => {
|
|
64
|
-
sdk
|
|
65
|
-
.getUserProfile(accessToken)
|
|
66
|
-
.then((userProfile) => displayUserProfile(userProfile));
|
|
67
|
-
});
|
|
68
|
-
setInputDisabledState(true, "endpoint", "applicationName");
|
|
69
|
-
} else {
|
|
70
|
-
alert("Login failed!");
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
})
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// eslint-disable-next-line no-unused-vars
|
|
77
|
-
function logout() {
|
|
78
|
-
chrome.storage.sync.set({accessToken: ""}, () => clearUserProfile());
|
|
79
|
-
setInputDisabledState(false, "endpoint", "applicationName");
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
function displayUserProfile(userProfile) {
|
|
83
|
-
document.getElementById("user").innerHTML = `
|
|
84
|
-
<img alt="${userProfile.name}" src="${userProfile.picture}" width="50px"/>
|
|
85
|
-
<p>Hi, ${userProfile.name}</p>
|
|
86
|
-
`;
|
|
87
|
-
setTimeout(function() {
|
|
88
|
-
updateDisabledInput("endpoint", userProfile.iss)
|
|
89
|
-
updateDisabledInput("applicationName", userProfile.applicationName)
|
|
90
|
-
}, 100);
|
|
91
|
-
document.getElementById("loginOrLogout").innerText = "Logout";
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function clearUserProfile() {
|
|
95
|
-
document.getElementById("user").innerHTML = "";
|
|
96
|
-
document.getElementById("loginOrLogout").innerText = "Login";
|
|
97
|
-
}
|
|
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
|
-
|
|
105
|
-
function setInputDisabledState(disabledState, ...elementIds) {
|
|
106
|
-
elementIds.forEach(elementId => {
|
|
107
|
-
const inputElement = document.getElementById(elementId);
|
|
108
|
-
if (inputElement) {
|
|
109
|
-
inputElement.disabled = disabledState;
|
|
110
|
-
} else {
|
|
111
|
-
console.warn(`No element found with ID: ${elementId}`);
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const intervalDetectToken = setInterval(getDomAccessToken, 3000);
|
|
117
|
-
|
|
118
|
-
let accessToken;
|
|
119
|
-
let applicationName;
|
|
120
|
-
|
|
121
|
-
function getDomAccessToken() {
|
|
122
|
-
const accessTokenDom = document.getElementById("CasdoorAccessToken")
|
|
123
|
-
const applicationNameDom = document.getElementById("CasdoorApplicationName")
|
|
124
|
-
if(window.location.pathname != "/src/popup.html" && accessTokenDom && applicationNameDom){
|
|
125
|
-
accessToken = accessTokenDom.getAttribute("value");
|
|
126
|
-
applicationName = applicationNameDom.getAttribute("value");
|
|
127
|
-
if (accessToken && applicationName) {
|
|
128
|
-
sdk.getUserProfile(accessToken).then((userProfile) => {
|
|
129
|
-
userProfile.applicationName = applicationName;
|
|
130
|
-
if(userProfile){
|
|
131
|
-
showNotification(confirmCallback, userProfile);
|
|
132
|
-
}
|
|
133
|
-
clearInterval(intervalDetectToken);
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
} else{
|
|
137
|
-
clearInterval(intervalDetectToken);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
function confirmCallback(userProfile) {
|
|
142
|
-
if (accessToken && userProfile) {
|
|
143
|
-
chrome.storage.sync.set({ accessToken: accessToken,userProfile: userProfile});
|
|
144
|
-
} else {
|
|
145
|
-
alert("Login failed!");
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
function showNotification(confirmCallback, userProfile) {
|
|
151
|
-
const notification = document.createElement("div");
|
|
152
|
-
const userDiv = document.createElement("div");
|
|
153
|
-
const endpointDataDiv = document.createElement("div");
|
|
154
|
-
const applicationNameDataDiv = document.createElement("div");
|
|
155
|
-
const buttonDiv = document.createElement("div");
|
|
156
|
-
const confirmButton = document.createElement("button");
|
|
157
|
-
const cancelButton = document.createElement("button");
|
|
158
|
-
|
|
159
|
-
notification.style.position = "fixed";
|
|
160
|
-
notification.style.top = "10px";
|
|
161
|
-
notification.style.right = "10px";
|
|
162
|
-
notification.style.padding = "10px";
|
|
163
|
-
notification.style.margin = "10px";
|
|
164
|
-
notification.style.backgroundColor = "#eceff7";
|
|
165
|
-
notification.style.color = "#000000";
|
|
166
|
-
|
|
167
|
-
userDiv.style.display = "flex";
|
|
168
|
-
userDiv.style.margin = "10px";
|
|
169
|
-
userDiv.style.justifyContent = 'center';
|
|
170
|
-
userDiv.style.alignItems = 'center';
|
|
171
|
-
|
|
172
|
-
endpointDataDiv.style.marginBottom = "10px";
|
|
173
|
-
endpointDataDiv.style.marginTop = "10px";
|
|
174
|
-
|
|
175
|
-
buttonDiv.style.marginTop = "10px";
|
|
176
|
-
buttonDiv.style.display = "flex";
|
|
177
|
-
buttonDiv.style.justifyContent = "space-between";
|
|
178
|
-
|
|
179
|
-
notification.textContent = "Whether to log in to the current account?";
|
|
180
|
-
|
|
181
|
-
userDiv.innerHTML=`
|
|
182
|
-
<img alt="${userProfile.name}" src="${userProfile.picture}" width="50px" style="margin-right: 10px;"/>
|
|
183
|
-
<p>${userProfile.name}</p>
|
|
184
|
-
`
|
|
185
|
-
|
|
186
|
-
cancelButton.textContent = "Cancel";
|
|
187
|
-
|
|
188
|
-
confirmButton.textContent = "OK";
|
|
189
|
-
|
|
190
|
-
endpointDataDiv.innerHTML = `endpoint: <span style="background-color: lightgray;">${userProfile.iss}</span>`;
|
|
191
|
-
|
|
192
|
-
applicationNameDataDiv.innerHTML = `applicationName: <span style="background-color: lightgray;">${userProfile.applicationName}</span>`;
|
|
193
|
-
|
|
194
|
-
confirmButton.onclick = () => {
|
|
195
|
-
if (confirmCallback && typeof confirmCallback === "function") {
|
|
196
|
-
confirmCallback(userProfile);
|
|
197
|
-
}
|
|
198
|
-
document.body.removeChild(notification);
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
cancelButton.onclick = () => {
|
|
202
|
-
document.body.removeChild(notification);
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
notification.appendChild(userDiv);
|
|
206
|
-
notification.appendChild(endpointDataDiv);
|
|
207
|
-
notification.appendChild(applicationNameDataDiv);
|
|
208
|
-
buttonDiv.appendChild(cancelButton);
|
|
209
|
-
buttonDiv.appendChild(confirmButton);
|
|
210
|
-
notification.appendChild(buttonDiv);
|
|
211
|
-
document.body.appendChild(notification);
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
setTimeout(() => {
|
|
215
|
-
if(document.body.contains(notification)){
|
|
216
|
-
document.body.removeChild(notification);
|
|
217
|
-
}
|
|
218
|
-
}, 5000);
|
|
219
|
-
}
|
package/src/sdk.js
DELETED
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
// Copyright 2022 The Casdoor Authors. All Rights Reserved.
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
|
|
15
|
-
/* eslint-disable no-undef */
|
|
16
|
-
|
|
17
|
-
// eslint-disable-next-line no-unused-vars
|
|
18
|
-
class Sdk {
|
|
19
|
-
constructor(config) {
|
|
20
|
-
this.config = config || {
|
|
21
|
-
endpoint: "",
|
|
22
|
-
applicationName: "",
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
getRedirectUri() {
|
|
27
|
-
return encodeURIComponent(
|
|
28
|
-
chrome.identity.getRedirectURL("")
|
|
29
|
-
);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
getSignInUrl(clientId) {
|
|
33
|
-
const endpoint = this.config.endpoint;
|
|
34
|
-
const redirectUri = this.getRedirectUri();
|
|
35
|
-
const scope = "profile";
|
|
36
|
-
const state = this.config.applicationName;
|
|
37
|
-
return `${endpoint}/login/oauth/authorize?client_id=${clientId}&response_type=token&redirect_uri=${redirectUri}&scope=${scope}&state=${state}`;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
getAccessTokenFromRedirectUrl(redirectUrl) {
|
|
41
|
-
if (redirectUrl) {
|
|
42
|
-
const accessTokenMatch = redirectUrl.match(/#access_token=([^&]*)/);
|
|
43
|
-
const accessToken = accessTokenMatch ? accessTokenMatch[1] : "";
|
|
44
|
-
return accessToken;
|
|
45
|
-
}
|
|
46
|
-
return "";
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
login(func) {
|
|
50
|
-
this.getApplication().then((res) => {
|
|
51
|
-
// refer: https://developer.chrome.com/docs/extensions/reference/identity/#method-launchWebAuthFlow
|
|
52
|
-
chrome.identity.launchWebAuthFlow(
|
|
53
|
-
{
|
|
54
|
-
url: this.getSignInUrl(res.data.clientId),
|
|
55
|
-
interactive: true,
|
|
56
|
-
},
|
|
57
|
-
(redirectUrl) => {
|
|
58
|
-
let accessToken = "";
|
|
59
|
-
if (!chrome.runtime.lastError && redirectUrl) {
|
|
60
|
-
accessToken = this.getAccessTokenFromRedirectUrl(redirectUrl);
|
|
61
|
-
}
|
|
62
|
-
try {
|
|
63
|
-
func(accessToken);
|
|
64
|
-
} catch (error) {
|
|
65
|
-
console.error(error);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
);
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
getUserProfileUrl() {
|
|
73
|
-
const endpoint = this.config.endpoint;
|
|
74
|
-
return `${endpoint}/api/userinfo`;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
getUserProfile(accessToken) {
|
|
78
|
-
const userProfileUrl = this.getUserProfileUrl();
|
|
79
|
-
const requestConfig = {
|
|
80
|
-
method: "GET",
|
|
81
|
-
async: true,
|
|
82
|
-
headers: {
|
|
83
|
-
"Content-Type": "application/json",
|
|
84
|
-
Authorization: `Bearer ${accessToken}`,
|
|
85
|
-
},
|
|
86
|
-
};
|
|
87
|
-
return fetch(userProfileUrl, requestConfig)
|
|
88
|
-
.then((response) => response.json())
|
|
89
|
-
.catch((error) => console.error(error));
|
|
90
|
-
}
|
|
91
|
-
|
|
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
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
async getAccount(accessToken) {
|
|
103
|
-
const accountUrl = await this.getAccountUrl();
|
|
104
|
-
const requestConfig = {
|
|
105
|
-
method: "GET",
|
|
106
|
-
async: true,
|
|
107
|
-
headers: {
|
|
108
|
-
"Content-Type": "application/json",
|
|
109
|
-
Authorization: `Bearer ${accessToken}`,
|
|
110
|
-
},
|
|
111
|
-
};
|
|
112
|
-
return fetch(accountUrl, requestConfig)
|
|
113
|
-
.then((response) => response.json())
|
|
114
|
-
.catch((error) => console.error(error));
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
getApplicationUrl(id) {
|
|
118
|
-
const endpoint = this.config.endpoint;
|
|
119
|
-
return `${endpoint}/api/get-application?id=${id}`;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
getApplication() {
|
|
123
|
-
const applicationUrl = this.getApplicationUrl(`admin/${this.config.applicationName}`);
|
|
124
|
-
const requestConfig = {
|
|
125
|
-
method: "GET",
|
|
126
|
-
async: true,
|
|
127
|
-
headers: {
|
|
128
|
-
"Content-Type": "application/json",
|
|
129
|
-
},
|
|
130
|
-
};
|
|
131
|
-
return fetch(applicationUrl, requestConfig)
|
|
132
|
-
.then((response) => response.json())
|
|
133
|
-
.catch((error) => {
|
|
134
|
-
console.error(error)
|
|
135
|
-
alert("failed to get application");
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
}
|