Skip to content
This repository has been archived by the owner on Mar 5, 2021. It is now read-only.

Commit

Permalink
Fix special cases with hostnames and ports for HTTP Basic Auth
Browse files Browse the repository at this point in the history
  • Loading branch information
elisee committed Mar 18, 2019
1 parent 52739dc commit 1d7fbf3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
7 changes: 4 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,15 @@ electron.app.on("login", (event, webContents, request, authInfo, callback) => {
event.preventDefault();

const parsedUrl = url.parse(request.url);
const host = `${parsedUrl.hostname}:${parsedUrl.port}`;
const auth = httpAuthByHosts[host];
const port = parsedUrl.port != null ? parsedUrl.port : (parsedUrl.protocol === "https:" ? 443 : 80);
const hostnameAndPort = `${parsedUrl.hostname}:${port}`;
const auth = httpAuthByHosts[hostnameAndPort];

if (auth == null) {
// Since this might race with the set-http-auth event above,
// try again a second later
setTimeout(() => {
const auth = httpAuthByHosts[host];
const auth = httpAuthByHosts[hostnameAndPort];
if (auth == null) callback(null, null);
else callback(auth.username, auth.password);
}, 1000);
Expand Down
17 changes: 11 additions & 6 deletions src/renderer/tabs/openServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,14 @@ function makeServerPane(serverEntry: ServerEntry) {
retryButton.addEventListener("click", onRetryButtonClick);

// Automatically add insecure protocol if none is already provided in the hostname
const protocol = serverEntry.hostname.startsWith("http") ? "" : "http://";
const host = `${serverEntry.hostname}:${serverEntry.port}`;
const baseUrl = protocol + host;
const protocol = serverEntry.hostname.startsWith("https://") ? "https://" : "http://";

let hostname = serverEntry.hostname;
if (hostname.startsWith("http://")) hostname = hostname.substring("http://".length);
else if (hostname.startsWith("https://")) hostname = hostname.substring("https://".length);
const hostnameAndPort = `${hostname}:${serverEntry.port}`;

const baseUrl = protocol + hostnameAndPort;

function tryConnecting() {
statusElt.textContent = i18n.t("common:server.connecting", { baseUrl });
Expand Down Expand Up @@ -141,10 +146,10 @@ function makeServerPane(serverEntry: ServerEntry) {
paneElt.appendChild(webviewElt);
webviewElt.focus();

const buildHost = `${serverEntry.hostname}:${serverInfo.buildPort}`;
const buildHostnameAndPort = `${hostname}:${serverInfo.buildPort}`;
const auth = { username: "superpowers", password: serverEntry.password };
electron.ipcRenderer.send("set-http-auth", host, auth);
electron.ipcRenderer.send("set-http-auth", buildHost, auth);
electron.ipcRenderer.send("set-http-auth", hostnameAndPort, auth);
electron.ipcRenderer.send("set-http-auth", buildHostnameAndPort, auth);
}

tryConnecting();
Expand Down

0 comments on commit 1d7fbf3

Please sign in to comment.