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/src/popup.css CHANGED
@@ -1,106 +1,162 @@
1
- .background {
1
+ [hidden] {
2
+ display: none !important;
3
+ }
4
+
5
+ body {
6
+ margin: 0;
7
+ font: 13px/1.5 -apple-system, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
8
+ color: #1f2328;
9
+ }
10
+
11
+ .popup {
2
12
  width: 300px;
3
- height: 210px;
13
+ padding: 14px 16px 16px;
14
+ }
15
+
16
+ .header {
17
+ display: flex;
18
+ align-items: center;
19
+ gap: 8px;
20
+ margin-bottom: 12px;
4
21
  }
5
22
 
6
- .title {
7
- text-align: center;
23
+ .header h1 {
24
+ margin: 0;
25
+ font-size: 16px;
8
26
  }
9
27
 
10
- .item {
28
+ .user {
11
29
  display: flex;
12
- justify-content: space-between;
13
30
  align-items: center;
14
- margin-top: 10px;
31
+ gap: 10px;
32
+ margin-bottom: 12px;
33
+ }
34
+
35
+ .user img {
36
+ border-radius: 50%;
37
+ object-fit: cover;
38
+ }
39
+
40
+ .user-name {
41
+ font-weight: 600;
42
+ }
43
+
44
+ .field {
45
+ display: block;
15
46
  margin-bottom: 10px;
16
47
  }
17
48
 
18
- .switch {
19
- position: relative;
20
- display : inline-block;
21
- width : 50px;
22
- height : 24px;
49
+ .field span {
50
+ display: block;
51
+ margin-bottom: 4px;
52
+ color: #59636e;
23
53
  }
24
54
 
25
- .switch input {
26
- opacity: 0;
27
- width : 0;
28
- height : 0;
55
+ .field input {
56
+ box-sizing: border-box;
57
+ width: 100%;
58
+ padding: 6px 8px;
59
+ border: 1px solid #d0d7de;
60
+ border-radius: 6px;
61
+ font: inherit;
29
62
  }
30
63
 
31
- .slider {
32
- position : absolute;
33
- cursor : pointer;
34
- top : 0;
35
- left : 0;
36
- right : 0;
37
- bottom : 0;
38
- background-color : #ccc;
39
- -webkit-transition: .4s;
40
- transition : .4s;
41
- border-radius : 34px;
64
+ .field input:disabled {
65
+ background: #f6f8fa;
66
+ color: #59636e;
42
67
  }
43
68
 
44
- .slider:before {
45
- position : absolute;
46
- content : "";
47
- height : 20px;
48
- width : 20px;
49
- left : 2px;
50
- top : 2px;
51
- background-color : white;
52
- -webkit-transition: .4s;
53
- transition : .4s;
54
- border-radius : 50%;
55
- }
56
-
57
- input:checked+.slider {
58
- background-color: rgb(0, 166, 255);
59
- }
60
-
61
- input:checked+.slider:before {
62
- -webkit-transform: translateX(26px);
63
- -ms-transform : translateX(26px);
64
- transform : translateX(26px);
65
- }
66
-
67
- .control {
68
- color: rgba(0, 0, 0, .85);
69
- font-size: 14px;
70
- list-style: none;
71
- max-width: 100%;
72
- text-align: center;
69
+ .row {
73
70
  display: flex;
74
- position: relative;
71
+ justify-content: space-between;
75
72
  align-items: center;
76
- justify-content: center;
73
+ margin: 4px 0 12px;
77
74
  }
78
75
 
79
- .control button {
80
- border-radius: 2px;
81
- border: 0;
82
- padding: 6px 8px 6px 8px;
83
- margin-bottom: 5px;
76
+ .sites {
77
+ margin-bottom: 10px;
78
+ padding: 8px 10px;
79
+ border-radius: 6px;
80
+ background: #f6f8fa;
84
81
  }
85
82
 
86
- .control .primary {
87
- color: #fff;
88
- border-color: #2d78d5;
89
- background: #2d78d5;
90
- text-shadow: 0 -1px 0 rgb(0 0 0 / 12%);
91
- box-shadow: 0 2px 0 rgb(0 0 0 / 5%);
83
+ .sites ul {
84
+ margin: 4px 0 0;
85
+ padding-left: 18px;
92
86
  }
93
87
 
94
- .control .primary:hover {
88
+ .muted {
89
+ color: #59636e;
90
+ }
91
+
92
+ .small {
93
+ margin-top: 10px;
94
+ font-size: 11px;
95
+ word-break: break-all;
96
+ }
97
+
98
+ #message {
99
+ margin-bottom: 10px;
100
+ }
101
+
102
+ #message:empty {
103
+ display: none;
104
+ }
105
+
106
+ button.primary {
107
+ width: 100%;
108
+ padding: 7px 0;
109
+ border: 0;
110
+ border-radius: 6px;
111
+ background: #5c6ac4;
95
112
  color: #fff;
96
- border-color: #5499e3;
97
- background: #5499e3;
113
+ font: inherit;
114
+ cursor: pointer;
98
115
  }
99
116
 
100
- #user {
101
- text-align: center;
117
+ button.primary:disabled {
118
+ opacity: .6;
119
+ cursor: default;
120
+ }
121
+
122
+ .switch {
123
+ position: relative;
124
+ display: inline-block;
125
+ width: 40px;
126
+ height: 22px;
102
127
  }
103
128
 
104
- .input {
105
- width: 180px;
106
- }
129
+ .switch input {
130
+ opacity: 0;
131
+ width: 0;
132
+ height: 0;
133
+ }
134
+
135
+ .slider {
136
+ position: absolute;
137
+ cursor: pointer;
138
+ inset: 0;
139
+ background-color: #ccc;
140
+ transition: .2s;
141
+ border-radius: 22px;
142
+ }
143
+
144
+ .slider:before {
145
+ position: absolute;
146
+ content: "";
147
+ height: 18px;
148
+ width: 18px;
149
+ left: 2px;
150
+ top: 2px;
151
+ background-color: white;
152
+ transition: .2s;
153
+ border-radius: 50%;
154
+ }
155
+
156
+ input:checked + .slider {
157
+ background-color: #5c6ac4;
158
+ }
159
+
160
+ input:checked + .slider:before {
161
+ transform: translateX(18px);
162
+ }
package/src/popup.html CHANGED
@@ -1,34 +1,47 @@
1
1
  <!DOCTYPE html>
2
2
  <html>
3
3
  <head>
4
+ <meta charset="utf-8">
4
5
  <link rel="stylesheet" href="popup.css">
5
6
  </head>
6
7
  <body>
7
- <div class="background">
8
- <h1 class="title" id="title">Casdoor</h1>
9
- <div id="user"></div>
10
- <div class="item">
11
- endpoint:
12
- <input class="input" id="endpoint" name="endpoint" placeholder="your casdoor server url"/>
8
+ <div class="popup">
9
+ <div class="header">
10
+ <img src="/images/icon48.png" alt="" width="24" height="24">
11
+ <h1>Casdoor</h1>
13
12
  </div>
14
- <div class="item">
15
- applicationName:
16
- <input class="input" id="applicationName" name="applicationName" placeholder="your application name"/>
13
+
14
+ <div id="user" class="user" hidden>
15
+ <img id="avatar" alt="" width="36" height="36">
16
+ <div>
17
+ <div id="userName" class="user-name"></div>
18
+ <div id="userOrg" class="muted"></div>
19
+ </div>
17
20
  </div>
18
- <div class="item">
19
- Enable auto login:
21
+
22
+ <label class="field">
23
+ <span data-i18n="endpoint"></span>
24
+ <input id="endpoint" placeholder="https://door.casdoor.com">
25
+ </label>
26
+ <label class="field">
27
+ <span data-i18n="application"></span>
28
+ <input id="applicationName" placeholder="app-built-in">
29
+ </label>
30
+
31
+ <div class="row">
32
+ <span data-i18n="autoFill"></span>
20
33
  <label class="switch">
21
- <input type="checkbox" id="enableAutoLogin">
34
+ <input type="checkbox" id="autoFill">
22
35
  <span class="slider"></span>
23
36
  </label>
24
37
  </div>
25
- <div class="control">
26
- <button class="primary" id="loginOrLogout">Login</button>
27
- </div>
38
+
39
+ <div id="sites" class="sites" hidden></div>
40
+ <div id="message" class="muted"></div>
41
+
42
+ <button class="primary" id="loginOrLogout"></button>
43
+ <div class="muted small"><span data-i18n="redirectUri"></span> <code id="redirectUri"></code></div>
28
44
  </div>
29
- <script src="config.js"></script>
30
- <script src="sdk.js"></script>
31
- <script src="oauth.js"></script>
32
45
  <script src="popup.js"></script>
33
46
  </body>
34
47
  </html>
package/src/popup.js CHANGED
@@ -12,80 +12,116 @@
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
14
 
15
- /* eslint-disable no-undef */
15
+ const isZh = (navigator.language || "").toLowerCase().startsWith("zh");
16
+ const strings = isZh
17
+ ? {
18
+ endpoint: "Casdoor 地址",
19
+ application: "应用名称",
20
+ autoFill: "自动填写登录表单",
21
+ redirectUri: "要加到应用回调地址里的地址:",
22
+ login: "登录 Casdoor",
23
+ logout: "退出",
24
+ sites: "可以代填的系统:",
25
+ accounts: "{n} 个账号",
26
+ canSave: "手动登录一次后保存",
27
+ noSites: "还没有可以代填的系统",
28
+ legacy: "这个 Casdoor 没有表单代填,按托管账号的登录地址代填",
29
+ errorEndpoint: "请填写以 http:// 或 https:// 开头的 Casdoor 地址和应用名称",
30
+ errorApplication: "找不到这个应用",
31
+ errorLogin: "登录失败:",
32
+ }
33
+ : {
34
+ endpoint: "Casdoor URL",
35
+ application: "Application name",
36
+ autoFill: "Fill sign-in forms automatically",
37
+ redirectUri: "Add this to the redirect URLs of the application:",
38
+ login: "Sign in to Casdoor",
39
+ logout: "Sign out",
40
+ sites: "Systems filled for you:",
41
+ accounts: "{n} account(s)",
42
+ canSave: "saved after you sign in by hand",
43
+ noSites: "No system is filled for you yet",
44
+ legacy: "This Casdoor has no form autofill, the sign-in URLs of the managed accounts are used",
45
+ errorEndpoint: "Please enter the Casdoor URL starting with http:// or https://, and the application name",
46
+ errorApplication: "The application is not found",
47
+ errorLogin: "Sign-in failed: ",
48
+ };
16
49
 
17
- document.addEventListener("DOMContentLoaded", function() {
18
- const enableAutoLoginDom = document.getElementById("enableAutoLogin");
50
+ const $ = (id) => document.getElementById(id);
19
51
 
20
- chrome.storage.sync.get("disableAutoLogin", ({disableAutoLogin}) => {
21
- enableAutoLoginDom.checked = !disableAutoLogin;
22
- });
52
+ function send(message) {
53
+ return new Promise((resolve) => chrome.runtime.sendMessage(message, resolve));
54
+ }
23
55
 
24
- enableAutoLoginDom.addEventListener("click", function() {
25
- chrome.storage.sync.set({disableAutoLogin: !enableAutoLoginDom.checked});
26
- });
27
- });
56
+ function renderSites(status) {
57
+ const box = $("sites");
58
+ box.textContent = "";
59
+ if (!status.user || !status.sites) {
60
+ box.hidden = true;
61
+ return;
62
+ }
63
+ box.hidden = false;
64
+ if (status.sites.length === 0) {
65
+ box.textContent = strings.noSites;
66
+ return;
67
+ }
68
+ const title = document.createElement("div");
69
+ title.textContent = strings.sites;
70
+ const list = document.createElement("ul");
71
+ for (const site of status.sites) {
72
+ const item = document.createElement("li");
73
+ const detail = site.accounts > 0 ? strings.accounts.replace("{n}", site.accounts) : strings.canSave;
74
+ item.textContent = isZh ? `${site.name}(${detail})` : `${site.name} (${detail})`;
75
+ list.append(item);
76
+ }
77
+ box.append(title, list);
78
+ }
28
79
 
29
- document.addEventListener("DOMContentLoaded", function() {
30
- const loginOrLogoutDom = document.getElementById("loginOrLogout");
80
+ async function render() {
81
+ const status = await send({type: "getStatus"});
82
+ $("endpoint").value = status.endpoint;
83
+ $("applicationName").value = status.applicationName;
84
+ $("autoFill").checked = status.autoFill;
85
+ $("redirectUri").textContent = status.redirectUri;
31
86
 
32
- loginOrLogoutDom.innerText = accessToken ? "Logout" : "Login";
87
+ const signedIn = !!status.user;
88
+ $("endpoint").disabled = signedIn;
89
+ $("applicationName").disabled = signedIn;
90
+ $("loginOrLogout").textContent = signedIn ? strings.logout : strings.login;
91
+ $("user").hidden = !signedIn;
92
+ if (signedIn) {
93
+ $("avatar").src = status.user.avatar || "/images/icon48.png";
94
+ $("userName").textContent = status.user.displayName || status.user.name;
95
+ $("userOrg").textContent = `${status.user.owner}/${status.user.name}`;
96
+ }
97
+ $("message").textContent = signedIn && status.mode === "legacy" ? (status.message || strings.legacy) : "";
98
+ renderSites(status);
99
+ return status;
100
+ }
33
101
 
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
- });
47
-
48
- loginOrLogoutDom.addEventListener("click", function() {
49
- chrome.storage.sync.get("accessToken", ({accessToken}) => {
50
- if (accessToken) {
51
- logout();
52
- } else {
53
- setInput()
54
- .then(login())
55
- .catch(error => {
56
- console.error('Error login:', error);
57
- });
58
- }
59
- });
102
+ document.addEventListener("DOMContentLoaded", async() => {
103
+ document.querySelectorAll("[data-i18n]").forEach((element) => {
104
+ element.textContent = strings[element.dataset.i18n];
60
105
  });
61
- });
106
+ let status = await render();
62
107
 
63
- document.addEventListener("DOMContentLoaded", function() {
64
- const applicationNameDom = document.getElementById("applicationName");
65
- chrome.storage.sync.get("applicationName", ({applicationName}) => {
66
- if (applicationName) {
67
- applicationNameDom.value = applicationName;
68
- }
69
- });
108
+ $("autoFill").addEventListener("change", () => send({type: "setAutoFill", enabled: $("autoFill").checked}));
70
109
 
71
- const endpointDom = document.getElementById("endpoint");
72
- chrome.storage.sync.get("endpoint", ({endpoint}) => {
73
- if (endpoint) {
74
- endpointDom.value = endpoint;
110
+ $("loginOrLogout").addEventListener("click", async() => {
111
+ if (status.user) {
112
+ await send({type: "logout"});
113
+ status = await render();
114
+ return;
115
+ }
116
+ $("loginOrLogout").disabled = true;
117
+ $("message").textContent = "";
118
+ const res = await send({type: "login", endpoint: $("endpoint").value, applicationName: $("applicationName").value});
119
+ $("loginOrLogout").disabled = false;
120
+ if (res && res.error) {
121
+ const errors = {endpoint: strings.errorEndpoint, application: strings.errorApplication};
122
+ $("message").textContent = errors[res.error] || strings.errorLogin + res.error;
123
+ return;
75
124
  }
125
+ status = await render();
76
126
  });
77
127
  });
78
-
79
- function setInput() {
80
- return new Promise((resolve, reject) => {
81
- const endpointDom = document.getElementById("endpoint");
82
- const applicationNameDom = document.getElementById("applicationName");
83
- chrome.storage.sync.set({ endpoint: endpointDom.value, applicationName: applicationNameDom.value }, function() {
84
- if (chrome.runtime.lastError) {
85
- reject(chrome.runtime.lastError.message);
86
- } else {
87
- resolve();
88
- }
89
- });
90
- });
91
- }
package/src/config.js DELETED
@@ -1,47 +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
- const UsernameXpaths = [
16
- "//input[contains(@id,'username')]",
17
- "//input[contains(@name,'username')]",
18
- "//input[contains(@name,'account')]",
19
- "//input[contains(@name,'login')]",
20
- "//input[contains(@placeholder,'username')]",
21
- "//input[contains(@placeholder,'用户名')]",
22
- "//input[contains(@placeholder,'账号')]",
23
- "//input[contains(@id,'phone')]",
24
- "//input[contains(@name,'phone')]",
25
- "//input[contains(@placeholder,'phone')]",
26
- "//input[contains(@placeholder,'手机')]",
27
- "//input[contains(@id,'email')]",
28
- "//input[contains(@name,'email')]",
29
- "//input[contains(@placeholder,'email')]",
30
- "//input[contains(@placeholder,'邮箱')]",
31
- ];
32
-
33
- const PasswordXpaths = [
34
- "//input[@type='password']",
35
- "//input[contains(@id,'password')]",
36
- "//input[contains(@name,'password')]",
37
- "//input[contains(@placeholder,'password')]",
38
- "//input[contains(@placeholder,'密码')]",
39
- ];
40
-
41
- const SubmitXpaths = [
42
- "//button[@type='submit']",
43
- "//input[contains(@name,'commit')]",
44
- "//button[contains(string(),'Login')]",
45
- "//button[contains(string(),'Sign in')]",
46
- "//button[contains(string(),'登录')]",
47
- ];