Skip to content

Commit

Permalink
feat: Implement auto login (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamYSF authored Mar 13, 2024
1 parent 98f229c commit 0cb36d9
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 20 deletions.
3 changes: 3 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
]
}
],
"background": {
"service_worker": "/src/background.js"
},
"host_permissions": [
"https://*/*",
"http://*/*"
Expand Down
45 changes: 45 additions & 0 deletions src/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2024 The Casdoor Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

let autoLoginTabs = new Set();

chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (tab.url && tab.status === "complete" && !autoLoginTabs.has(tabId) ) {
const url = tab.url;

chrome.tabs.sendMessage(tabId, {action: "setManagedAccounts"},
function (managedAccounts) {
if (chrome.runtime.lastError) {
console.error("Error sending first message:", chrome.runtime.lastError);
} else if (managedAccounts) {
for (const managedAccount of managedAccounts) {
if (managedAccount && url.includes(managedAccount.signinUrl)) {
autoLoginTabs.add(tabId);
chrome.tabs.sendMessage(tabId, {
action: "autoLogin",
managedAccount: managedAccount,
});
}
}
}
}
);
}
});

function clearAutoLoginTabs() {
autoLoginTabs.clear();
}

setInterval(clearAutoLoginTabs, 10000);
4 changes: 4 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
const UsernameXpaths = [
"//input[contains(@id,'username')]",
"//input[contains(@name,'username')]",
"//input[contains(@name,'account')]",
"//input[contains(@name,'login')]",
"//input[contains(@placeholder,'username')]",
"//input[contains(@placeholder,'用户名')]",
"//input[contains(@placeholder,'账号')]",
"//input[contains(@id,'phone')]",
"//input[contains(@name,'phone')]",
"//input[contains(@placeholder,'phone')]",
Expand All @@ -37,6 +40,7 @@ const PasswordXpaths = [

const SubmitXpaths = [
"//button[@type='submit']",
"//input[contains(@name,'commit')]",
"//button[contains(string(),'Login')]",
"//button[contains(string(),'Sign in')]",
"//button[contains(string(),'登录')]",
Expand Down
37 changes: 23 additions & 14 deletions src/content-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,27 @@ window.onload = () => {
autoFillCasdoorConfig();
}
});

const url = window.location.href;
chrome.storage.sync.get("managedAccounts", ({managedAccounts}) => {
for (const managedAccount of managedAccounts) {
if (managedAccount.signinUrl === url) {
chrome.storage.sync.get("disableAutoLogin", ({disableAutoLogin}) => {
if (!disableAutoLogin) {
autoLogin(managedAccount.username, managedAccount.password);
}
});
break;
}
}
});
};

chrome.runtime.onMessage.addListener((message) => {
if (message.action === "autoLogin") {
chrome.storage.sync.get("disableAutoLogin", ({ disableAutoLogin }) => {
if (!disableAutoLogin) {
const managedAccount = message.managedAccount;
autoLogin(managedAccount.username, managedAccount.password);
}
});
}
});

chrome.runtime.onMessage.addListener(function(message, sender,sendResponse) {
if (message.action === "setManagedAccounts") {
chrome.storage.sync.get("accessToken", ({accessToken}) => {
sdk.getAccount(accessToken).then((account) => {
const managedAccounts = account.data.managedAccounts;
sendResponse(managedAccounts);
});
});
return true;
}
});
6 changes: 1 addition & 5 deletions src/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ document.addEventListener("DOMContentLoaded", function() {
sdk
.getUserProfile(accessToken)
.then((userProfile) => displayUserProfile(userProfile));
sdk.getAccount(accessToken).then((account) => {
const managedAccounts = account.data.managedAccounts;
chrome.storage.sync.set({managedAccounts});
});
setInputDisabledState(true, "endpoint", "applicationName");
setInputDisabledState(true, "endpoint", "applicationName")
} else {
clearUserProfile();
}
Expand Down
2 changes: 1 addition & 1 deletion src/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class Sdk {

getAccountUrl() {
const endpoint = this.config.endpoint;
return `${endpoint}/api/get-account?managedAccounts=1`;
return `${endpoint}/api/get-account`;
}

getAccount(accessToken) {
Expand Down

0 comments on commit 0cb36d9

Please sign in to comment.