From 724d32dbe23a9c36e7146f43f3568a85cc23daff Mon Sep 17 00:00:00 2001 From: Yegor Vialov Date: Fri, 25 Jan 2019 22:27:13 +0200 Subject: [PATCH] Resolves #277 Remove legacy password support --- lib/home_assistant.class.dart | 17 ++++------------- lib/main.dart | 4 +--- lib/settings.page.dart | 31 +------------------------------ 3 files changed, 6 insertions(+), 46 deletions(-) diff --git a/lib/home_assistant.class.dart b/lib/home_assistant.class.dart index 845cf08..817624e 100644 --- a/lib/home_assistant.class.dart +++ b/lib/home_assistant.class.dart @@ -3,7 +3,6 @@ part of 'main.dart'; class HomeAssistant { String _webSocketAPIEndpoint; String _password; - String _authType; bool _useLovelace = false; IOWebSocketChannel _hassioChannel; @@ -56,10 +55,9 @@ class HomeAssistant { _messageQueue = SendMessageQueue(messageExpirationTime); } - void updateSettings(String url, String password, String authType, bool useLovelace) { + void updateSettings(String url, String password, bool useLovelace) { _webSocketAPIEndpoint = url; _password = password; - _authType = authType; _useLovelace = useLovelace; Logger.d( "Use lovelace is $_useLovelace"); } @@ -213,7 +211,7 @@ class HomeAssistant { _handleMessage(String message) { var data = json.decode(message); if (data["type"] == "auth_required") { - _sendAuthMessageRaw('{"type": "auth","$_authType": "$_password"}'); + _sendAuthMessageRaw('{"type": "auth","access_token": "$_password"}'); } else if (data["type"] == "auth_ok") { _completeConnecting(null); _sendSubscribe(); @@ -550,17 +548,10 @@ class HomeAssistant { String url = "$homeAssistantWebHost/api/history/period/$startTime?&filter_entity_id=$entityId"; Logger.d("[Sending] ==> $url"); http.Response historyResponse; - if (_authType == "access_token") { - historyResponse = await http.get(url, headers: { + historyResponse = await http.get(url, headers: { "authorization": "Bearer $_password", "Content-Type": "application/json" - }); - } else { - historyResponse = await http.get(url, headers: { - "X-HA-Access": "$_password", - "Content-Type": "application/json" - }); - } + }); var history = json.decode(historyResponse.body); if (history is List) { Logger.d( "[Received] <== ${history.first.length} history recors"); diff --git a/lib/main.dart b/lib/main.dart index 6e53c1c..be1f198 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -142,7 +142,6 @@ class _MainPageState extends State with WidgetsBindingObserver { //Map _instanceConfig; String _webSocketApiEndpoint; String _password; - String _authType; //int _uiViewsCount = 0; String _instanceHost; StreamSubscription _stateSubscription; @@ -200,7 +199,6 @@ class _MainPageState extends State with WidgetsBindingObserver { _webSocketApiEndpoint = "${prefs.getString('hassio-protocol')}://$domain:$port/api/websocket"; homeAssistantWebHost = "${prefs.getString('hassio-res-protocol')}://$domain:$port"; _password = prefs.getString('hassio-password'); - _authType = prefs.getString('hassio-auth-type'); _useLovelaceUI = prefs.getBool('use-lovelace') ?? false; if ((domain == null) || (port == null) || (_password == null) || (domain.length == 0) || (port.length == 0) || (_password.length == 0)) { @@ -250,7 +248,7 @@ class _MainPageState extends State with WidgetsBindingObserver { } _refreshData() async { - _homeAssistant.updateSettings(_webSocketApiEndpoint, _password, _authType, _useLovelaceUI); + _homeAssistant.updateSettings(_webSocketApiEndpoint, _password, _useLovelaceUI); _hideBottomBar(); _showInfoBottomBar(progress: true,); await _homeAssistant.fetch().then((result) { diff --git a/lib/settings.page.dart b/lib/settings.page.dart index 09a6ac5..ac75a04 100644 --- a/lib/settings.page.dart +++ b/lib/settings.page.dart @@ -18,8 +18,6 @@ class _ConnectionSettingsPageState extends State { String _newHassioPassword = ""; String _socketProtocol = "wss"; String _newSocketProtocol = "wss"; - String _authType = "access_token"; - String _newAuthType = "access_token"; bool _useLovelace = false; bool _newUseLovelace = false; @@ -37,7 +35,6 @@ class _ConnectionSettingsPageState extends State { _hassioPort = _newHassioPort = prefs.getString("hassio-port") ?? ""; _hassioPassword = _newHassioPassword = prefs.getString("hassio-password") ?? ""; _socketProtocol = _newSocketProtocol = prefs.getString("hassio-protocol") ?? 'wss'; - _authType = _newAuthType = prefs.getString("hassio-auth-type") ?? 'access_token'; try { _useLovelace = _newUseLovelace = prefs.getBool("use-lovelace") ?? false; } catch (e) { @@ -51,7 +48,6 @@ class _ConnectionSettingsPageState extends State { (_newHassioPort != _hassioPort) || (_newHassioDomain != _hassioDomain) || (_newSocketProtocol != _socketProtocol) || - (_newAuthType != _authType) || (_newUseLovelace != _useLovelace)); } @@ -66,7 +62,6 @@ class _ConnectionSettingsPageState extends State { prefs.setString("hassio-password", _newHassioPassword); prefs.setString("hassio-protocol", _newSocketProtocol); prefs.setString("hassio-res-protocol", _newSocketProtocol == "wss" ? "https" : "http"); - prefs.setString("hassio-auth-type", _newAuthType); prefs.setBool("use-lovelace", _newUseLovelace); } @@ -154,33 +149,9 @@ class _ConnectionSettingsPageState extends State { "Try ports 80 and 443 if default is not working and you don't know why.", style: TextStyle(color: Colors.grey), ), - new Row( - mainAxisSize: MainAxisSize.min, - children: [ - Flexible( - child: Text( - "Login with access token (HA >= 0.78.0)", - softWrap: true, - maxLines: 2, - ), - ), - Switch( - value: (_newAuthType == "access_token"), - onChanged: (value) { - setState(() { - _newAuthType = value ? "access_token" : "api_password"; - }); - }, - ) - ], - ), - new Text( - "You should use access token for HA >= 0.84.1. Legacy password will not work there.", - style: TextStyle(color: Colors.grey), - ), new TextField( decoration: InputDecoration( - labelText: _newAuthType == "access_token" ? "Access token" : "API password" + labelText: "Access token" ), controller: new TextEditingController.fromValue( new TextEditingValue(