Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add http basic auth support #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"storage",
"nativeMessaging",
"webNavigation",
"webRequest",
"webRequestBlocking",
"<all_urls>"
],
"content_security_policy": "default-src 'self'",
Expand Down Expand Up @@ -64,4 +66,4 @@
"description": "__MSG_cmdInsertOther__"
}
}
}
}
46 changes: 46 additions & 0 deletions src/background/httpAuth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { backend } from './backend';
import { BackendConnectionState } from 'common/backend-connection-state';
import BlockingResponse = chrome.webRequest.BlockingResponse;
import WebAuthenticationChallengeDetails = chrome.webRequest.WebAuthenticationChallengeDetails;
import WebResponseCacheDetails = chrome.webRequest.WebResponseCacheDetails;

export function interceptHttpBasicAuth(): void {
const pendingRequests: string[] = [];

function completed(requestDetails: WebResponseCacheDetails) {
const index = pendingRequests.indexOf(requestDetails.requestId);
if (index > -1) {
pendingRequests.splice(index, 1);
}
}

async function provideCredentials(
requestDetails: WebAuthenticationChallengeDetails
): Promise<BlockingResponse> {
// If we have seen this request before,
// then assume our credentials were bad,
// and give up.
if (pendingRequests.indexOf(requestDetails.requestId) !== -1) {
return {}; // the password we tried did not work
}
pendingRequests.push(requestDetails.requestId);

await backend.connect();
if (backend.state !== BackendConnectionState.Connected) {
return {}; // Fallback to manual input, do NOT return {cancel: true} as it will show 401.
}
const logins = await backend.getLogins(requestDetails.url);
if (!logins.length) {
return {};
}
const record = logins[0]; // TODO: can't show a UI at this point, but how do we choose it?
return { authCredentials: { username: record.login, password: record.password } };
}

const target = '*://*/*';
chrome.webRequest.onAuthRequired.addListener(provideCredentials, { urls: [target] }, [
'blocking'
]);
chrome.webRequest.onCompleted.addListener(completed, { urls: [target] });
chrome.webRequest.onErrorOccurred.addListener(completed, { urls: [target] });
}
2 changes: 2 additions & 0 deletions src/background/init.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { backend } from './backend';
import { interceptHttpBasicAuth } from './httpAuth';
import { createUIMenus, bindExtensionButtonClick } from './ui';
import { startInternalIpc } from './internal-ipc';
import { startCommandListener } from './commands';
Expand Down Expand Up @@ -35,6 +36,7 @@ async function start() {
createUIMenus();
bindExtensionButtonClick();
startInternalIpc();
interceptHttpBasicAuth();

await backend.init();
}