Skip to content

Commit

Permalink
Fix https check and always propagate errors if get auth fails (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob committed Jul 23, 2019
1 parent f56c569 commit aeb8a32
Showing 1 changed file with 7 additions and 19 deletions.
26 changes: 7 additions & 19 deletions lib/auth.ts
Expand Up @@ -108,7 +108,7 @@ async function tokenRequest(
// Ensure that the hassUrl is hosted on https.
const a = document.createElement("a");
a.href = hassUrl;
if (a.protocol !== "http:" && a.hostname !== "localhost") {
if (a.protocol === "http:" && a.hostname !== "localhost") {
throw ERR_INVALID_HTTPS_TO_HTTP;
}
}
Expand Down Expand Up @@ -223,15 +223,9 @@ export async function getAuth(options: getAuthOptions = {}): Promise<Auth> {

// Use auth code if it was passed in
if (!data && options.authCode && hassUrl && clientId) {
try {
data = await fetchToken(hassUrl, clientId, options.authCode);
if (options.saveTokens) {
options.saveTokens(data);
}
} catch (err) {
// Do we want to tell user we were unable to fetch tokens?
// For now we don't do anything, having rest of code pick it up.
console.log("Unable to fetch access token", err);
data = await fetchToken(hassUrl, clientId, options.authCode);
if (options.saveTokens) {
options.saveTokens(data);
}
}

Expand All @@ -243,15 +237,9 @@ export async function getAuth(options: getAuthOptions = {}): Promise<Auth> {
if ("auth_callback" in query) {
// Restore state
const state = decodeOAuthState(query.state);
try {
data = await fetchToken(state.hassUrl, state.clientId, query.code);
if (options.saveTokens) {
options.saveTokens(data);
}
} catch (err) {
// Do we want to tell user we were unable to fetch tokens?
// For now we don't do anything, having rest of code pick it up.
console.log("Unable to fetch access token", err);
data = await fetchToken(state.hassUrl, state.clientId, query.code);
if (options.saveTokens) {
options.saveTokens(data);
}
}
}
Expand Down

0 comments on commit aeb8a32

Please sign in to comment.