casdoor 1.0.2 → 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/.eslintrc.js CHANGED
@@ -17,8 +17,14 @@ module.exports = {
17
17
  "space-in-parens": ["error", "never"],
18
18
  "object-curly-spacing": ["error", "never"],
19
19
  "array-bracket-spacing": ["error", "never"],
20
- "comma-spacing": ["error", {before: false, after: true}],
21
- "arrow-spacing": ["error", {before: true, after: true}],
20
+ "comma-spacing": ["error", {
21
+ before: false,
22
+ after: true
23
+ }],
24
+ "arrow-spacing": ["error", {
25
+ before: true,
26
+ after: true
27
+ }],
22
28
  "space-before-blocks": ["error", "always"],
23
29
  "spaced-comment": ["error", "always"],
24
30
  "block-spacing": ["error", "never"],
@@ -43,11 +49,21 @@ module.exports = {
43
49
  ],
44
50
  "no-multiple-empty-lines": [
45
51
  "error",
46
- {max: 1, maxBOF: 0, maxEOF: 0},
52
+ {
53
+ max: 1,
54
+ maxBOF: 0,
55
+ maxEOF: 0
56
+ },
47
57
  ],
48
- "space-unary-ops": ["error", {words: true, nonwords: false}],
58
+ "space-unary-ops": ["error", {
59
+ words: true,
60
+ nonwords: false
61
+ }],
49
62
  "space-infix-ops": "error",
50
- "key-spacing": ["error", {beforeColon: false, afterColon: true}],
63
+ "key-spacing": ["error", {
64
+ beforeColon: false,
65
+ afterColon: true
66
+ }],
51
67
  "comma-style": ["error", "last"],
52
68
  "comma-dangle": [
53
69
  "error",
package/.releaserc.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
- "debug": true,
3
- "branches": "master",
4
- "plugins": [
5
- "@semantic-release/commit-analyzer",
6
- "@semantic-release/release-notes-generator",
7
- "@semantic-release/npm",
8
- "@semantic-release/github"
9
- ]
10
- }
2
+ "debug": true,
3
+ "branches": "master",
4
+ "plugins": [
5
+ "@semantic-release/commit-analyzer",
6
+ "@semantic-release/release-notes-generator",
7
+ "@semantic-release/npm",
8
+ "@semantic-release/github"
9
+ ]
10
+ }
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/manifest.json CHANGED
@@ -1,40 +1,46 @@
1
1
  {
2
- "name": "Casdoor",
3
- "description": "The Casdoor extension can help browser users automatically log into applications protected by Casdoor single-sign-on (SSO) system.",
4
- "version": "1.0.0",
5
- "manifest_version": 3,
6
- "permissions": ["storage", "identity"],
7
- "action": {
8
- "default_popup": "/src/popup.html",
9
- "default_icon": {
10
- "16": "/images/icon16.png",
11
- "32": "/images/icon32.png",
12
- "48": "/images/icon48.png",
13
- "128": "/images/icon128.png"
14
- }
15
- },
16
- "icons": {
17
- "16": "/images/icon16.png",
18
- "32": "/images/icon32.png",
19
- "48": "/images/icon48.png",
20
- "128": "/images/icon128.png"
21
- },
22
- "content_scripts": [
23
- {
24
- "matches": [
25
- "https://*/*",
26
- "http://*/*"
27
- ],
28
- "js": [
29
- "/src/content-script.js",
30
- "/src/sdk.js",
31
- "/src/oauth.js",
32
- "/src/config.js"
33
- ]
34
- }
35
- ],
36
- "host_permissions": [
2
+ "name": "Casdoor",
3
+ "description": "The Casdoor extension can help browser users automatically log into applications protected by Casdoor single-sign-on (SSO) system.",
4
+ "version": "1.0.0",
5
+ "manifest_version": 3,
6
+ "permissions": [
7
+ "storage",
8
+ "identity"
9
+ ],
10
+ "action": {
11
+ "default_popup": "/src/popup.html",
12
+ "default_icon": {
13
+ "16": "/images/icon16.png",
14
+ "32": "/images/icon32.png",
15
+ "48": "/images/icon48.png",
16
+ "128": "/images/icon128.png"
17
+ }
18
+ },
19
+ "icons": {
20
+ "16": "/images/icon16.png",
21
+ "32": "/images/icon32.png",
22
+ "48": "/images/icon48.png",
23
+ "128": "/images/icon128.png"
24
+ },
25
+ "content_scripts": [
26
+ {
27
+ "matches": [
37
28
  "https://*/*",
38
29
  "http://*/*"
39
- ]
30
+ ],
31
+ "js": [
32
+ "/src/content-script.js",
33
+ "/src/sdk.js",
34
+ "/src/oauth.js",
35
+ "/src/config.js"
36
+ ]
37
+ }
38
+ ],
39
+ "background": {
40
+ "service_worker": "/src/background.js"
41
+ },
42
+ "host_permissions": [
43
+ "https://*/*",
44
+ "http://*/*"
45
+ ]
40
46
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "casdoor",
3
- "version": "1.0.2",
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"
@@ -0,0 +1,97 @@
1
+ // Copyright 2024 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
+ let autoLoginTabs = new Set();
16
+
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" },
50
+ function (managedAccounts) {
51
+ if (chrome.runtime.lastError) {
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
+ }
92
+
93
+ function clearAutoLoginTabs() {
94
+ autoLoginTabs.clear();
95
+ }
96
+
97
+ setInterval(clearAutoLoginTabs, 10000);
package/src/config.js CHANGED
@@ -15,8 +15,11 @@
15
15
  const UsernameXpaths = [
16
16
  "//input[contains(@id,'username')]",
17
17
  "//input[contains(@name,'username')]",
18
+ "//input[contains(@name,'account')]",
19
+ "//input[contains(@name,'login')]",
18
20
  "//input[contains(@placeholder,'username')]",
19
21
  "//input[contains(@placeholder,'用户名')]",
22
+ "//input[contains(@placeholder,'账号')]",
20
23
  "//input[contains(@id,'phone')]",
21
24
  "//input[contains(@name,'phone')]",
22
25
  "//input[contains(@placeholder,'phone')]",
@@ -37,6 +40,7 @@ const PasswordXpaths = [
37
40
 
38
41
  const SubmitXpaths = [
39
42
  "//button[@type='submit']",
43
+ "//input[contains(@name,'commit')]",
40
44
  "//button[contains(string(),'Login')]",
41
45
  "//button[contains(string(),'Sign in')]",
42
46
  "//button[contains(string(),'登录')]",
@@ -94,18 +94,45 @@ window.onload = () => {
94
94
  autoFillCasdoorConfig();
95
95
  }
96
96
  });
97
+ };
98
+
99
+ chrome.runtime.onMessage.addListener((message) => {
100
+ if (message.action === "autoLogin") {
101
+ const managedAccount = message.managedAccount;
102
+ autoLogin(managedAccount.username, managedAccount.password);
103
+ }
104
+ });
97
105
 
98
- const url = window.location.href;
99
- chrome.storage.sync.get("managedAccounts", ({managedAccounts}) => {
100
- for (const managedAccount of managedAccounts) {
101
- if (managedAccount.signinUrl === url) {
102
- chrome.storage.sync.get("disableAutoLogin", ({disableAutoLogin}) => {
103
- if (!disableAutoLogin) {
104
- autoLogin(managedAccount.username, managedAccount.password);
105
- }
106
- });
107
- break;
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);
108
113
  }
109
- }
110
- });
111
- };
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;
125
+ }
126
+ });
127
+
128
+ chrome.runtime.onMessage.addListener(function(message, sender,sendResponse) {
129
+ if (message.action === "getManagedAccounts") {
130
+ chrome.storage.sync.get("accessToken", ({accessToken}) => {
131
+ sdk.getAccount(accessToken).then((account) => {
132
+ const managedAccounts = account.data.managedAccounts;
133
+ sendResponse(managedAccounts);
134
+ });
135
+ });
136
+ return true;
137
+ }
138
+ });
package/src/oauth.js CHANGED
@@ -40,7 +40,7 @@ function login() {
40
40
  .getUserProfile(accessToken)
41
41
  .then((userProfile) => displayUserProfile(userProfile));
42
42
  });
43
- setInputDisabledState(true, "endpoint", "applicationName")
43
+ setInputDisabledState(true, "endpoint", "applicationName");
44
44
  } else {
45
45
  alert("Login failed!");
46
46
  }
@@ -50,7 +50,7 @@ function login() {
50
50
  // eslint-disable-next-line no-unused-vars
51
51
  function logout() {
52
52
  chrome.storage.sync.set({accessToken: ""}, () => clearUserProfile());
53
- setInputDisabledState(false, "endpoint", "applicationName")
53
+ setInputDisabledState(false, "endpoint", "applicationName");
54
54
  }
55
55
 
56
56
  function displayUserProfile(userProfile) {
@@ -66,13 +66,13 @@ function clearUserProfile() {
66
66
  document.getElementById("loginOrLogout").innerText = "Login";
67
67
  }
68
68
 
69
- function setInputDisabledState(disabledState, ...elementIds) {
70
- elementIds.forEach(elementId => {
71
- const inputElement = document.getElementById(elementId);
72
- if (inputElement) {
73
- inputElement.disabled = disabledState;
74
- } else {
75
- console.warn(`No element found with ID: ${elementId}`);
76
- }
77
- });
69
+ function setInputDisabledState(disabledState, ...elementIds) {
70
+ elementIds.forEach(elementId => {
71
+ const inputElement = document.getElementById(elementId);
72
+ if (inputElement) {
73
+ inputElement.disabled = disabledState;
74
+ } else {
75
+ console.warn(`No element found with ID: ${elementId}`);
76
+ }
77
+ });
78
78
  }
package/src/popup.html CHANGED
@@ -1,34 +1,34 @@
1
1
  <!DOCTYPE html>
2
2
  <html>
3
- <head>
3
+ <head>
4
4
  <link rel="stylesheet" href="popup.css">
5
- </head>
6
- <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"/>
13
- </div>
14
- <div class="item">
15
- applicationName:
16
- <input class="input" id="applicationName" name="applicationName" placeholder="your application name"/>
17
- </div>
18
- <div class="item">
19
- Enable auto login:
20
- <label class="switch">
21
- <input type="checkbox" id="enableAutoLogin">
22
- <span class="slider"></span>
23
- </label>
24
- </div>
25
- <div class="control">
26
- <button class="primary" id="loginOrLogout">Login</button>
27
- </div>
5
+ </head>
6
+ <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"/>
28
13
  </div>
29
- <script src="config.js"></script>
30
- <script src="sdk.js"></script>
31
- <script src="oauth.js"></script>
32
- <script src="popup.js"></script>
33
- </body>
14
+ <div class="item">
15
+ applicationName:
16
+ <input class="input" id="applicationName" name="applicationName" placeholder="your application name"/>
17
+ </div>
18
+ <div class="item">
19
+ Enable auto login:
20
+ <label class="switch">
21
+ <input type="checkbox" id="enableAutoLogin">
22
+ <span class="slider"></span>
23
+ </label>
24
+ </div>
25
+ <div class="control">
26
+ <button class="primary" id="loginOrLogout">Login</button>
27
+ </div>
28
+ </div>
29
+ <script src="config.js"></script>
30
+ <script src="sdk.js"></script>
31
+ <script src="oauth.js"></script>
32
+ <script src="popup.js"></script>
33
+ </body>
34
34
  </html>
package/src/popup.js CHANGED
@@ -36,10 +36,6 @@ document.addEventListener("DOMContentLoaded", function() {
36
36
  sdk
37
37
  .getUserProfile(accessToken)
38
38
  .then((userProfile) => displayUserProfile(userProfile));
39
- sdk.getAccount(accessToken).then((account) => {
40
- const managedAccounts = account.data.managedAccounts;
41
- chrome.storage.sync.set({managedAccounts});
42
- });
43
39
  setInputDisabledState(true, "endpoint", "applicationName")
44
40
  } else {
45
41
  clearUserProfile();
package/src/sdk.js CHANGED
@@ -13,6 +13,7 @@
13
13
  // limitations under the License.
14
14
 
15
15
  /* eslint-disable no-undef */
16
+
16
17
  // eslint-disable-next-line no-unused-vars
17
18
  class Sdk {
18
19
  constructor(config) {
@@ -38,8 +39,8 @@ class Sdk {
38
39
 
39
40
  getAccessTokenFromRedirectUrl(redirectUrl) {
40
41
  if (redirectUrl) {
41
- const accessTokenMatch = redirectUrl.match(/#access_token=([^&]*)/);
42
- const accessToken = accessTokenMatch ? accessTokenMatch[1] : "";
42
+ const accessTokenMatch = redirectUrl.match(/#access_token=([^&]*)/);
43
+ const accessToken = accessTokenMatch ? accessTokenMatch[1] : "";
43
44
  return accessToken;
44
45
  }
45
46
  return "";
@@ -90,7 +91,7 @@ class Sdk {
90
91
 
91
92
  getAccountUrl() {
92
93
  const endpoint = this.config.endpoint;
93
- return `${endpoint}/api/get-account?managedAccounts=1`;
94
+ return `${endpoint}/api/get-account`;
94
95
  }
95
96
 
96
97
  getAccount(accessToken) {