Resolves #421 Manual long-lived token

This commit is contained in:
estevez-dev 2019-08-26 18:04:40 +03:00
parent 37155901ef
commit fadfefd836
3 changed files with 71 additions and 4 deletions

View File

@ -303,7 +303,10 @@ class Connection {
_tempToken = null; _tempToken = null;
final storage = new FlutterSecureStorage(); final storage = new FlutterSecureStorage();
storage.write(key: "hacl_llt", value: "$_token").then((_) { storage.write(key: "hacl_llt", value: "$_token").then((_) {
SharedPreferences.getInstance().then((prefs) {
prefs.setBool("oauth-used", true);
completer.complete(); completer.complete();
});
}).catchError((e) { }).catchError((e) {
throw e; throw e;
}); });

View File

@ -155,14 +155,23 @@ class HAClientApp extends StatelessWidget {
"/configuration": (context) => PanelPage(title: "Configuration"), "/configuration": (context) => PanelPage(title: "Configuration"),
"/putchase": (context) => PurchasePage(title: "Support app development"), "/putchase": (context) => PurchasePage(title: "Support app development"),
"/log-view": (context) => LogViewPage(title: "Log"), "/log-view": (context) => LogViewPage(title: "Log"),
"/login": (_) => WebviewScaffold( "/login": (context) => WebviewScaffold(
url: "${Connection().oauthUrl}", url: "${Connection().oauthUrl}",
appBar: new AppBar( appBar: new AppBar(
leading: IconButton( leading: IconButton(
icon: Icon(Icons.help), icon: Icon(Icons.help),
onPressed: () => HAUtils.launchURLInCustomTab(context: context, url: "http://ha-client.homemade.systems/docs#authentication") onPressed: () => HAUtils.launchURLInCustomTab(context: context, url: "http://ha-client.homemade.systems/docs#authentication")
), ),
title: new Text("Login to your Home Assistant"), title: new Text("Login with HA"),
actions: <Widget>[
FlatButton(
child: Text("Manual", style: TextStyle(color: Colors.white)),
onPressed: () {
Navigator.of(context).pop();
Navigator.of(context).pushNamed("/connection-settings");
},
)
],
), ),
) )
}, },

View File

@ -16,10 +16,13 @@ class _ConnectionSettingsPageState extends State<ConnectionSettingsPage> {
String _newHassioPort = ""; String _newHassioPort = "";
String _socketProtocol = "wss"; String _socketProtocol = "wss";
String _newSocketProtocol = "wss"; String _newSocketProtocol = "wss";
String _longLivedToken = "";
String _newLongLivedToken = "";
bool _useLovelace = true; bool _useLovelace = true;
bool _newUseLovelace = true; bool _newUseLovelace = true;
String oauthUrl; String oauthUrl;
bool useOAuth = false;
@override @override
void initState() { void initState() {
@ -30,6 +33,23 @@ class _ConnectionSettingsPageState extends State<ConnectionSettingsPage> {
_loadSettings() async { _loadSettings() async {
SharedPreferences prefs = await SharedPreferences.getInstance(); SharedPreferences prefs = await SharedPreferences.getInstance();
final storage = new FlutterSecureStorage();
try {
useOAuth = prefs.getBool("oauth-used") ?? true;
} catch (e) {
useOAuth = true;
}
if (!useOAuth) {
try {
_longLivedToken = _newLongLivedToken =
await storage.read(key: "hacl_llt");
} catch (e) {
_longLivedToken = _newLongLivedToken = "";
await storage.delete(key: "hacl_llt");
}
}
setState(() { setState(() {
_hassioDomain = _newHassioDomain = prefs.getString("hassio-domain")?? ""; _hassioDomain = _newHassioDomain = prefs.getString("hassio-domain")?? "";
@ -48,7 +68,8 @@ class _ConnectionSettingsPageState extends State<ConnectionSettingsPage> {
(_newHassioPort != _hassioPort) || (_newHassioPort != _hassioPort) ||
(_newHassioDomain != _hassioDomain) || (_newHassioDomain != _hassioDomain) ||
(_newSocketProtocol != _socketProtocol) || (_newSocketProtocol != _socketProtocol) ||
(_newUseLovelace != _useLovelace)); (_newUseLovelace != _useLovelace) ||
(_newLongLivedToken != _longLivedToken));
} }
@ -58,6 +79,13 @@ class _ConnectionSettingsPageState extends State<ConnectionSettingsPage> {
} }
_newHassioDomain = _newHassioDomain.split("/")[0]; _newHassioDomain = _newHassioDomain.split("/")[0];
SharedPreferences prefs = await SharedPreferences.getInstance(); SharedPreferences prefs = await SharedPreferences.getInstance();
final storage = new FlutterSecureStorage();
if (_newLongLivedToken.isNotEmpty) {
prefs.setBool("oauth-used", false);
await storage.write(key: "hacl_llt", value: _newLongLivedToken);
} else if (!useOAuth) {
await storage.delete(key: "hacl_llt");
}
prefs.setString("hassio-domain", _newHassioDomain); prefs.setString("hassio-domain", _newHassioDomain);
prefs.setString("hassio-port", _newHassioPort); prefs.setString("hassio-port", _newHassioPort);
prefs.setString("hassio-protocol", _newSocketProtocol); prefs.setString("hassio-protocol", _newSocketProtocol);
@ -172,6 +200,33 @@ class _ConnectionSettingsPageState extends State<ConnectionSettingsPage> {
) )
], ],
), ),
Text(
"Authentication settings",
style: TextStyle(
color: Colors.black45,
fontSize: 20.0
),
),
Container(height: 10.0,),
Text(
"You can leave this field blank to make app generate new long-lived token automatically by asking you to login to your Home Assistant. Use this field only if you still want to use manually generated long-lived token. Leave it blank if you don't understand what we are talking about.",
style: TextStyle(color: Colors.redAccent),
),
new TextField(
decoration: InputDecoration(
labelText: "Long-lived token"
),
controller: new TextEditingController.fromValue(
new TextEditingValue(
text: _newLongLivedToken ?? '',
selection:
new TextSelection.collapsed(offset: _newLongLivedToken != null ? _newLongLivedToken.length : 0)
)
),
onChanged: (value) {
_newLongLivedToken = value;
}
),
], ],
), ),
); );