Skip to content

Commit

Permalink
Fix HTTP Auth not working in Chrome
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyrat committed Jun 15, 2024
1 parent 4721b50 commit f6046e1
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 12 deletions.
12 changes: 12 additions & 0 deletions src/background/NetworkAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ import { kee } from "./KF";
export class NetworkAuth {
constructor() { }

public provideCredentialsAsyncBlockingCallback(
requestDetails: any,
callback: (response: chrome.webRequest.BlockingResponse) => void
) {
this.provideCredentialsAsync(requestDetails)
.then(result => callback(result))
.catch(reason => {
KeeLog.error("AsyncBlockingCallback promise failed", reason);
callback({ cancel: false });
});
}

public async provideCredentialsAsync(
requestDetails: any
): Promise<chrome.webRequest.BlockingResponse> {
Expand Down
61 changes: 49 additions & 12 deletions src/background/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,18 +272,55 @@ chrome.runtime.onConnect.addListener(async port => {

// With MV3 we must always listen to httpauth requests and decide whether to handle them
// based on whether we have already initialised the pinia store, got connected to KPRPC, have open DBs, etc.
chrome.webRequest.onAuthRequired.addListener(
async (requestDetails): Promise<chrome.webRequest.BlockingResponse> => {
if (KeeLog?.debug) KeeLog.debug("onAuthRequired request started");
// We may crash at startup / session restore if we're not initialised yet
await Promise.race([initialised, new Promise(resolve => self.setTimeout(resolve, 20000))]);
if (KeeLog?.debug) KeeLog.debug("onAuthRequired request ongoing");
const result = await networkAuth.provideCredentialsAsync(requestDetails);
return result;
},
{ urls: ["<all_urls>"] },
[isFirefox() ? "blocking" : "asyncBlocking"]
);
if (isFirefox()) {
chrome.webRequest.onAuthRequired.addListener(
async (requestDetails): Promise<chrome.webRequest.BlockingResponse> => {
if (KeeLog?.debug) KeeLog.debug("onAuthRequired request started");
// We may crash at startup / session restore if we're not initialised yet
await Promise.race([initialised, new Promise(resolve => self.setTimeout(resolve, 20000))]);
if (KeeLog?.debug) KeeLog.debug("onAuthRequired request ongoing");
const result = await networkAuth.provideCredentialsAsync(requestDetails);
return result;
},
{ urls: ["<all_urls>"] },
["blocking"]
);
} else {
chrome.webRequest.onAuthRequired.addListener(
(requestDetails, callback) => {
if (KeeLog?.debug) KeeLog.debug("onAuthRequired request started");
Promise.race([
initialised,
new Promise(resolve => self.setTimeout(resolve, 20000))
])
.then(() => {
if (KeeLog?.debug) KeeLog.debug("onAuthRequired request ongoing");
networkAuth.provideCredentialsAsyncBlockingCallback(requestDetails, callback);
});
},
{ urls: ["<all_urls>"] },
["asyncBlocking"]
);
}

// This can be used instead once Chrome either supports Promises in "blocking" mode
// or, more likely, a new flag, which then Firefox would have to support too but we
// could still just use a conditional in the mean time to keep compatibility with a
// wider range of Firefox versions.
// https://github.com/w3c/webextensions/issues/490
// https://issues.chromium.org/issues/41483002
// chrome.webRequest.onAuthRequired.addListener(
// async (requestDetails): Promise<chrome.webRequest.BlockingResponse> => {
// if (KeeLog?.debug) KeeLog.debug("onAuthRequired request started");
// // We may crash at startup / session restore if we're not initialised yet
// await Promise.race([initialised, new Promise(resolve => self.setTimeout(resolve, 20000))]);
// if (KeeLog?.debug) KeeLog.debug("onAuthRequired request ongoing");
// const result = await networkAuth.provideCredentialsAsync(requestDetails);
// return result;
// },
// { urls: ["<all_urls>"] },
// [isFirefox() ? "blocking" : "asyncBlockingPromise"]
// );

(async () => {
await ensureStarted();
Expand Down

0 comments on commit f6046e1

Please sign in to comment.