2017-01-30 03:34:45 +01:00
|
|
|
import * as HAWS from 'home-assistant-js-websocket';
|
2016-05-29 01:03:51 +02:00
|
|
|
|
2018-05-17 23:51:07 +02:00
|
|
|
import fetchToken from '../common/auth/fetch_token.js';
|
|
|
|
import refreshToken_ from '../common/auth/refresh_token.js';
|
|
|
|
import parseQuery from '../common/util/parse_query.js';
|
2018-05-10 20:25:36 +02:00
|
|
|
|
2017-01-30 03:34:45 +01:00
|
|
|
window.HAWS = HAWS;
|
2016-05-29 01:03:51 +02:00
|
|
|
|
2018-05-10 20:25:36 +02:00
|
|
|
const init = window.createHassConnection = function (password, accessToken) {
|
2017-01-30 03:34:45 +01:00
|
|
|
const proto = window.location.protocol === 'https:' ? 'wss' : 'ws';
|
2018-05-16 13:54:29 +02:00
|
|
|
const url = `${proto}://${window.location.host}/api/websocket?${__BUILD__}`;
|
2017-02-01 09:09:50 +01:00
|
|
|
const options = {
|
|
|
|
setupRetry: 10,
|
|
|
|
};
|
2018-05-10 20:25:36 +02:00
|
|
|
if (password) {
|
2017-01-30 03:34:45 +01:00
|
|
|
options.authToken = password;
|
2018-05-10 20:25:36 +02:00
|
|
|
} else if (accessToken) {
|
|
|
|
options.accessToken = accessToken;
|
2016-05-29 03:32:42 +02:00
|
|
|
}
|
2017-01-30 03:34:45 +01:00
|
|
|
return HAWS.createConnection(url, options)
|
|
|
|
.then(function (conn) {
|
|
|
|
HAWS.subscribeEntities(conn);
|
|
|
|
HAWS.subscribeConfig(conn);
|
|
|
|
return conn;
|
|
|
|
});
|
|
|
|
};
|
2016-05-29 03:32:42 +02:00
|
|
|
|
2018-05-10 20:25:36 +02:00
|
|
|
function redirectLogin() {
|
|
|
|
const urlBase = __DEV__ ? '/home-assistant-polymer/src' : `/frontend_${__BUILD__}`;
|
|
|
|
document.location = `${urlBase}/authorize.html?response_type=code&client_id=${window.clientId}&redirect_uri=/`;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.refreshToken = () =>
|
|
|
|
refreshToken_(window.clientId, window.tokens.refresh_token).then((accessTokenResp) => {
|
|
|
|
window.tokens.access_token = accessTokenResp.access_token;
|
|
|
|
localStorage.tokens = JSON.stringify(window.tokens);
|
|
|
|
return accessTokenResp.access_token;
|
|
|
|
}, () => redirectLogin());
|
|
|
|
|
|
|
|
function resolveCode(code) {
|
|
|
|
fetchToken(window.clientId, code).then((tokens) => {
|
|
|
|
localStorage.tokens = JSON.stringify(tokens);
|
|
|
|
// Refresh the page and have tokens in place.
|
|
|
|
document.location = location.pathname;
|
|
|
|
}, (err) => {
|
|
|
|
// eslint-disable-next-line
|
|
|
|
console.error('Resolve token failed', err);
|
|
|
|
alert('Unable to fetch tokens');
|
|
|
|
redirectLogin();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function main() {
|
|
|
|
if (location.search) {
|
|
|
|
const query = parseQuery(location.search.substr(1));
|
|
|
|
if (query.code) {
|
|
|
|
resolveCode(query.code);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (localStorage.tokens) {
|
|
|
|
window.tokens = JSON.parse(localStorage.tokens);
|
|
|
|
window.hassConnection = init(null, window.tokens.access_token).catch((err) => {
|
|
|
|
if (err !== HAWS.ERR_INVALID_AUTH) throw err;
|
|
|
|
|
|
|
|
return window.refreshToken().then(accessToken => init(null, accessToken));
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
redirectLogin();
|
|
|
|
}
|
|
|
|
|
|
|
|
function mainLegacy() {
|
|
|
|
if (window.noAuth === '1') {
|
|
|
|
window.hassConnection = init();
|
|
|
|
} else if (window.localStorage.authToken) {
|
|
|
|
window.hassConnection = init(window.localStorage.authToken);
|
|
|
|
} else {
|
|
|
|
window.hassConnection = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (window.clientId) {
|
|
|
|
main();
|
2017-01-30 03:34:45 +01:00
|
|
|
} else {
|
2018-05-10 20:25:36 +02:00
|
|
|
mainLegacy();
|
2016-07-20 08:33:03 +02:00
|
|
|
}
|
2018-01-27 20:50:13 +01:00
|
|
|
|
|
|
|
window.addEventListener('error', (e) => {
|
|
|
|
const homeAssistant = document.querySelector('home-assistant');
|
|
|
|
if (homeAssistant && homeAssistant.hass && homeAssistant.hass.callService) {
|
|
|
|
homeAssistant.hass.callService('system_log', 'write', {
|
2018-05-16 13:54:29 +02:00
|
|
|
logger: `frontend.${__DEV__ ? 'js_dev' : 'js'}.${__BUILD__}.${__VERSION__.replace('.', '')}`,
|
2018-01-27 20:50:13 +01:00
|
|
|
message: `${e.filename}:${e.lineno}:${e.colno} ${e.message}`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|