Back to webview oauth
This commit is contained in:
parent
41e552dce5
commit
cf6039b279
@ -48,20 +48,6 @@
|
|||||||
<action android:name="android.intent.action.MAIN"/>
|
<action android:name="android.intent.action.MAIN"/>
|
||||||
<category android:name="android.intent.category.LAUNCHER"/>
|
<category android:name="android.intent.category.LAUNCHER"/>
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
|
|
||||||
<intent-filter android:autoVerify="true">
|
|
||||||
<action android:name="android.intent.action.VIEW" />
|
|
||||||
<category android:name="android.intent.category.DEFAULT" />
|
|
||||||
<category android:name="android.intent.category.BROWSABLE" />
|
|
||||||
<data
|
|
||||||
android:scheme="haclient"
|
|
||||||
android:host="auth" />
|
|
||||||
</intent-filter>
|
|
||||||
<intent-filter>
|
|
||||||
<action android:name="android.intent.action.SEND"/>
|
|
||||||
<category android:name="android.intent.category.DEFAULT"/>
|
|
||||||
<data android:mimeType="text/plain"/>
|
|
||||||
</intent-filter>
|
|
||||||
</activity>
|
</activity>
|
||||||
|
|
||||||
<service
|
<service
|
||||||
|
@ -24,7 +24,6 @@ import 'package:in_app_purchase/in_app_purchase.dart';
|
|||||||
import 'plugins/circular_slider/single_circular_slider.dart';
|
import 'plugins/circular_slider/single_circular_slider.dart';
|
||||||
import 'plugins/dynamic_multi_column_layout.dart';
|
import 'plugins/dynamic_multi_column_layout.dart';
|
||||||
import 'plugins/spoiler_card.dart';
|
import 'plugins/spoiler_card.dart';
|
||||||
import 'package:uni_links/uni_links.dart';
|
|
||||||
import 'package:workmanager/workmanager.dart' as workManager;
|
import 'package:workmanager/workmanager.dart' as workManager;
|
||||||
import 'package:geolocator/geolocator.dart';
|
import 'package:geolocator/geolocator.dart';
|
||||||
import 'package:battery/battery.dart';
|
import 'package:battery/battery.dart';
|
||||||
@ -203,10 +202,22 @@ class HAClientApp extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
"/log-view": (context) => LogViewPage(title: "Log"),
|
"/log-view": (context) => LogViewPage(title: "Log"),
|
||||||
"/whats-new": (context) => WhatsNewPage(),
|
"/whats-new": (context) => WhatsNewPage(),
|
||||||
"/test": (_) => new WebviewScaffold(
|
"/auth": (context) => new WebviewScaffold(
|
||||||
url: "https://www.google.com",
|
url: "${ConnectionManager().oauthUrl}",
|
||||||
appBar: new AppBar(
|
appBar: new AppBar(
|
||||||
title: new Text("Widget webview"),
|
leading: IconButton(
|
||||||
|
icon: Icon(Icons.help),
|
||||||
|
onPressed: () => Launcher.launchURLInCustomTab(context: context, url: "http://ha-client.homemade.systems/docs#authentication")
|
||||||
|
),
|
||||||
|
title: new Text("Login with HA"),
|
||||||
|
actions: <Widget>[
|
||||||
|
FlatButton(
|
||||||
|
child: Text("Manual", style: TextStyle(color: Colors.white)),
|
||||||
|
onPressed: () {
|
||||||
|
eventBus.fire(ShowPageEvent(path: "/connection-settings", goBackFirst: true));
|
||||||
|
},
|
||||||
|
)
|
||||||
|
],
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
|
@ -9,29 +9,16 @@ class AuthManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
AuthManager._internal();
|
AuthManager._internal();
|
||||||
StreamSubscription deepLinksSubscription;
|
|
||||||
|
|
||||||
Future start({String oauthUrl}) {
|
Future start({String oauthUrl}) {
|
||||||
Completer completer = Completer();
|
Completer completer = Completer();
|
||||||
deepLinksSubscription?.cancel();
|
final flutterWebviewPlugin = new FlutterWebviewPlugin();
|
||||||
deepLinksSubscription = getUriLinksStream().listen((Uri uri) {
|
flutterWebviewPlugin.onUrlChanged.listen((String url) {
|
||||||
Logger.d("[LINKED AUTH] We got something private");
|
if (url.startsWith("http://ha-client.homemade.systems/service/auth_callback.html")) {
|
||||||
_getTempToken(oauthUrl, uri.queryParameters["code"])
|
Logger.d("url=$url");
|
||||||
.then((tempToken) => completer.complete(tempToken))
|
String authCode = url.split("=")[1];
|
||||||
.catchError((_){
|
Logger.d("authCode=$authCode");
|
||||||
completer.completeError(HAError("Auth error"));
|
Logger.d("We have auth code. Getting temporary access token...");
|
||||||
});
|
|
||||||
}, onError: (err) {
|
|
||||||
Logger.e("[LINKED AUTH] Error handling linked auth: $e");
|
|
||||||
completer.completeError(HAError("Auth error"));
|
|
||||||
});
|
|
||||||
Logger.d("Launching OAuth");
|
|
||||||
eventBus.fire(StartAuthEvent(oauthUrl, true));
|
|
||||||
return completer.future;
|
|
||||||
}
|
|
||||||
|
|
||||||
Future _getTempToken(String oauthUrl,String authCode) {
|
|
||||||
Completer completer = Completer();
|
|
||||||
ConnectionManager().sendHTTPPost(
|
ConnectionManager().sendHTTPPost(
|
||||||
endPoint: "/auth/token",
|
endPoint: "/auth/token",
|
||||||
contentType: "application/x-www-form-urlencoded",
|
contentType: "application/x-www-form-urlencoded",
|
||||||
@ -40,14 +27,18 @@ class AuthManager {
|
|||||||
).then((response) {
|
).then((response) {
|
||||||
Logger.d("Got temp token");
|
Logger.d("Got temp token");
|
||||||
String tempToken = json.decode(response)['access_token'];
|
String tempToken = json.decode(response)['access_token'];
|
||||||
|
Logger.d("Closing webview...");
|
||||||
eventBus.fire(StartAuthEvent(oauthUrl, false));
|
eventBus.fire(StartAuthEvent(oauthUrl, false));
|
||||||
completer.complete(tempToken);
|
completer.complete(tempToken);
|
||||||
}).catchError((e) {
|
}).catchError((e) {
|
||||||
//flutterWebviewPlugin.close();
|
|
||||||
Logger.e("Error getting temp token: ${e.toString()}");
|
Logger.e("Error getting temp token: ${e.toString()}");
|
||||||
eventBus.fire(StartAuthEvent(oauthUrl, false));
|
eventBus.fire(StartAuthEvent(oauthUrl, false));
|
||||||
completer.completeError(HAError("Error getting temp token"));
|
completer.completeError(HAError("Error getting temp token"));
|
||||||
|
}).whenComplete(() => flutterWebviewPlugin.close());
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
Logger.d("Launching OAuth");
|
||||||
|
eventBus.fire(StartAuthEvent(oauthUrl, true));
|
||||||
return completer.future;
|
return completer.future;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,7 +61,7 @@ class ConnectionManager {
|
|||||||
oauthUrl = "$httpWebHost/auth/authorize?client_id=${Uri.encodeComponent(
|
oauthUrl = "$httpWebHost/auth/authorize?client_id=${Uri.encodeComponent(
|
||||||
'http://ha-client.homemade.systems')}&redirect_uri=${Uri
|
'http://ha-client.homemade.systems')}&redirect_uri=${Uri
|
||||||
.encodeComponent(
|
.encodeComponent(
|
||||||
'haclient://auth')}";
|
'http://ha-client.homemade.systems/service/auth_callback.html')}";
|
||||||
settingsLoaded = true;
|
settingsLoaded = true;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
completer.completeError(HAError("Error reading login details", actions: [HAErrorAction.tryAgain(type: HAErrorActionType.FULL_RELOAD), HAErrorAction.loginAgain()]));
|
completer.completeError(HAError("Error reading login details", actions: [HAErrorAction.tryAgain(type: HAErrorActionType.FULL_RELOAD), HAErrorAction.loginAgain()]));
|
||||||
|
@ -239,6 +239,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
|
|||||||
_showOAuth();
|
_showOAuth();
|
||||||
} else {
|
} else {
|
||||||
_preventAppRefresh = false;
|
_preventAppRefresh = false;
|
||||||
|
Navigator.of(context).pop();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -252,9 +253,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
|
|||||||
|
|
||||||
void _showOAuth() {
|
void _showOAuth() {
|
||||||
_preventAppRefresh = true;
|
_preventAppRefresh = true;
|
||||||
Launcher.launchURLInCustomTab(
|
Navigator.of(context).pushNamed("/auth", arguments: {"url": ConnectionManager().oauthUrl});
|
||||||
url: ConnectionManager().oauthUrl
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_setErrorState(HAError e) {
|
_setErrorState(HAError e) {
|
||||||
@ -436,20 +435,6 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
|
|||||||
},
|
},
|
||||||
),
|
),
|
||||||
Divider(),
|
Divider(),
|
||||||
new ListTile(
|
|
||||||
leading: Icon(Icons.build),
|
|
||||||
title: Text("TEST"),
|
|
||||||
onTap: () {
|
|
||||||
Navigator.of(context).pop();
|
|
||||||
final flutterWebviewPlugin = new FlutterWebviewPlugin();
|
|
||||||
flutterWebviewPlugin.onUrlChanged.listen((String url) {
|
|
||||||
Logger.d("[Webview] URL Changed: $url");
|
|
||||||
});
|
|
||||||
Logger.d("[Webview] Listener attached");
|
|
||||||
Navigator.of(context).pushNamed("/test");
|
|
||||||
},
|
|
||||||
),
|
|
||||||
Divider(),
|
|
||||||
new ListTile(
|
new ListTile(
|
||||||
leading: Icon(Icons.help),
|
leading: Icon(Icons.help),
|
||||||
title: Text("Help"),
|
title: Text("Help"),
|
||||||
|
@ -23,7 +23,6 @@ dependencies:
|
|||||||
flutter_custom_tabs: ^0.6.0
|
flutter_custom_tabs: ^0.6.0
|
||||||
flutter_webview_plugin: ^0.3.10+1
|
flutter_webview_plugin: ^0.3.10+1
|
||||||
firebase_messaging: ^5.1.6
|
firebase_messaging: ^5.1.6
|
||||||
uni_links: ^0.2.0
|
|
||||||
flutter_secure_storage: ^3.3.1+1
|
flutter_secure_storage: ^3.3.1+1
|
||||||
device_info: ^0.4.0+3
|
device_info: ^0.4.0+3
|
||||||
flutter_local_notifications: ^0.8.4
|
flutter_local_notifications: ^0.8.4
|
||||||
|
Reference in New Issue
Block a user