Resolves #277 Remove legacy password support

This commit is contained in:
Yegor Vialov 2019-01-25 22:27:13 +02:00
parent 949c8ee44e
commit 724d32dbe2
3 changed files with 6 additions and 46 deletions

View File

@ -3,7 +3,6 @@ part of 'main.dart';
class HomeAssistant { class HomeAssistant {
String _webSocketAPIEndpoint; String _webSocketAPIEndpoint;
String _password; String _password;
String _authType;
bool _useLovelace = false; bool _useLovelace = false;
IOWebSocketChannel _hassioChannel; IOWebSocketChannel _hassioChannel;
@ -56,10 +55,9 @@ class HomeAssistant {
_messageQueue = SendMessageQueue(messageExpirationTime); _messageQueue = SendMessageQueue(messageExpirationTime);
} }
void updateSettings(String url, String password, String authType, bool useLovelace) { void updateSettings(String url, String password, bool useLovelace) {
_webSocketAPIEndpoint = url; _webSocketAPIEndpoint = url;
_password = password; _password = password;
_authType = authType;
_useLovelace = useLovelace; _useLovelace = useLovelace;
Logger.d( "Use lovelace is $_useLovelace"); Logger.d( "Use lovelace is $_useLovelace");
} }
@ -213,7 +211,7 @@ class HomeAssistant {
_handleMessage(String message) { _handleMessage(String message) {
var data = json.decode(message); var data = json.decode(message);
if (data["type"] == "auth_required") { if (data["type"] == "auth_required") {
_sendAuthMessageRaw('{"type": "auth","$_authType": "$_password"}'); _sendAuthMessageRaw('{"type": "auth","access_token": "$_password"}');
} else if (data["type"] == "auth_ok") { } else if (data["type"] == "auth_ok") {
_completeConnecting(null); _completeConnecting(null);
_sendSubscribe(); _sendSubscribe();
@ -550,17 +548,10 @@ class HomeAssistant {
String url = "$homeAssistantWebHost/api/history/period/$startTime?&filter_entity_id=$entityId"; String url = "$homeAssistantWebHost/api/history/period/$startTime?&filter_entity_id=$entityId";
Logger.d("[Sending] ==> $url"); Logger.d("[Sending] ==> $url");
http.Response historyResponse; http.Response historyResponse;
if (_authType == "access_token") { historyResponse = await http.get(url, headers: {
historyResponse = await http.get(url, headers: {
"authorization": "Bearer $_password", "authorization": "Bearer $_password",
"Content-Type": "application/json" "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); var history = json.decode(historyResponse.body);
if (history is List) { if (history is List) {
Logger.d( "[Received] <== ${history.first.length} history recors"); Logger.d( "[Received] <== ${history.first.length} history recors");

View File

@ -142,7 +142,6 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
//Map _instanceConfig; //Map _instanceConfig;
String _webSocketApiEndpoint; String _webSocketApiEndpoint;
String _password; String _password;
String _authType;
//int _uiViewsCount = 0; //int _uiViewsCount = 0;
String _instanceHost; String _instanceHost;
StreamSubscription _stateSubscription; StreamSubscription _stateSubscription;
@ -200,7 +199,6 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
_webSocketApiEndpoint = "${prefs.getString('hassio-protocol')}://$domain:$port/api/websocket"; _webSocketApiEndpoint = "${prefs.getString('hassio-protocol')}://$domain:$port/api/websocket";
homeAssistantWebHost = "${prefs.getString('hassio-res-protocol')}://$domain:$port"; homeAssistantWebHost = "${prefs.getString('hassio-res-protocol')}://$domain:$port";
_password = prefs.getString('hassio-password'); _password = prefs.getString('hassio-password');
_authType = prefs.getString('hassio-auth-type');
_useLovelaceUI = prefs.getBool('use-lovelace') ?? false; _useLovelaceUI = prefs.getBool('use-lovelace') ?? false;
if ((domain == null) || (port == null) || (_password == null) || if ((domain == null) || (port == null) || (_password == null) ||
(domain.length == 0) || (port.length == 0) || (_password.length == 0)) { (domain.length == 0) || (port.length == 0) || (_password.length == 0)) {
@ -250,7 +248,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
} }
_refreshData() async { _refreshData() async {
_homeAssistant.updateSettings(_webSocketApiEndpoint, _password, _authType, _useLovelaceUI); _homeAssistant.updateSettings(_webSocketApiEndpoint, _password, _useLovelaceUI);
_hideBottomBar(); _hideBottomBar();
_showInfoBottomBar(progress: true,); _showInfoBottomBar(progress: true,);
await _homeAssistant.fetch().then((result) { await _homeAssistant.fetch().then((result) {

View File

@ -18,8 +18,6 @@ class _ConnectionSettingsPageState extends State<ConnectionSettingsPage> {
String _newHassioPassword = ""; String _newHassioPassword = "";
String _socketProtocol = "wss"; String _socketProtocol = "wss";
String _newSocketProtocol = "wss"; String _newSocketProtocol = "wss";
String _authType = "access_token";
String _newAuthType = "access_token";
bool _useLovelace = false; bool _useLovelace = false;
bool _newUseLovelace = false; bool _newUseLovelace = false;
@ -37,7 +35,6 @@ class _ConnectionSettingsPageState extends State<ConnectionSettingsPage> {
_hassioPort = _newHassioPort = prefs.getString("hassio-port") ?? ""; _hassioPort = _newHassioPort = prefs.getString("hassio-port") ?? "";
_hassioPassword = _newHassioPassword = prefs.getString("hassio-password") ?? ""; _hassioPassword = _newHassioPassword = prefs.getString("hassio-password") ?? "";
_socketProtocol = _newSocketProtocol = prefs.getString("hassio-protocol") ?? 'wss'; _socketProtocol = _newSocketProtocol = prefs.getString("hassio-protocol") ?? 'wss';
_authType = _newAuthType = prefs.getString("hassio-auth-type") ?? 'access_token';
try { try {
_useLovelace = _newUseLovelace = prefs.getBool("use-lovelace") ?? false; _useLovelace = _newUseLovelace = prefs.getBool("use-lovelace") ?? false;
} catch (e) { } catch (e) {
@ -51,7 +48,6 @@ class _ConnectionSettingsPageState extends State<ConnectionSettingsPage> {
(_newHassioPort != _hassioPort) || (_newHassioPort != _hassioPort) ||
(_newHassioDomain != _hassioDomain) || (_newHassioDomain != _hassioDomain) ||
(_newSocketProtocol != _socketProtocol) || (_newSocketProtocol != _socketProtocol) ||
(_newAuthType != _authType) ||
(_newUseLovelace != _useLovelace)); (_newUseLovelace != _useLovelace));
} }
@ -66,7 +62,6 @@ class _ConnectionSettingsPageState extends State<ConnectionSettingsPage> {
prefs.setString("hassio-password", _newHassioPassword); prefs.setString("hassio-password", _newHassioPassword);
prefs.setString("hassio-protocol", _newSocketProtocol); prefs.setString("hassio-protocol", _newSocketProtocol);
prefs.setString("hassio-res-protocol", _newSocketProtocol == "wss" ? "https" : "http"); prefs.setString("hassio-res-protocol", _newSocketProtocol == "wss" ? "https" : "http");
prefs.setString("hassio-auth-type", _newAuthType);
prefs.setBool("use-lovelace", _newUseLovelace); prefs.setBool("use-lovelace", _newUseLovelace);
} }
@ -154,33 +149,9 @@ class _ConnectionSettingsPageState extends State<ConnectionSettingsPage> {
"Try ports 80 and 443 if default is not working and you don't know why.", "Try ports 80 and 443 if default is not working and you don't know why.",
style: TextStyle(color: Colors.grey), 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( new TextField(
decoration: InputDecoration( decoration: InputDecoration(
labelText: _newAuthType == "access_token" ? "Access token" : "API password" labelText: "Access token"
), ),
controller: new TextEditingController.fromValue( controller: new TextEditingController.fromValue(
new TextEditingValue( new TextEditingValue(