Back to webview oauth

This commit is contained in:
Yegor Vialov 2020-01-27 21:12:05 +00:00
parent 41e552dce5
commit cf6039b279
6 changed files with 37 additions and 65 deletions

View File

@ -48,20 +48,6 @@
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</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>
<service

View File

@ -24,7 +24,6 @@ import 'package:in_app_purchase/in_app_purchase.dart';
import 'plugins/circular_slider/single_circular_slider.dart';
import 'plugins/dynamic_multi_column_layout.dart';
import 'plugins/spoiler_card.dart';
import 'package:uni_links/uni_links.dart';
import 'package:workmanager/workmanager.dart' as workManager;
import 'package:geolocator/geolocator.dart';
import 'package:battery/battery.dart';
@ -203,10 +202,22 @@ class HAClientApp extends StatelessWidget {
),
"/log-view": (context) => LogViewPage(title: "Log"),
"/whats-new": (context) => WhatsNewPage(),
"/test": (_) => new WebviewScaffold(
url: "https://www.google.com",
"/auth": (context) => new WebviewScaffold(
url: "${ConnectionManager().oauthUrl}",
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));
},
)
],
),
)
},

View File

@ -9,46 +9,37 @@ class AuthManager {
}
AuthManager._internal();
StreamSubscription deepLinksSubscription;
Future start({String oauthUrl}) {
Completer completer = Completer();
deepLinksSubscription?.cancel();
deepLinksSubscription = getUriLinksStream().listen((Uri uri) {
Logger.d("[LINKED AUTH] We got something private");
_getTempToken(oauthUrl, uri.queryParameters["code"])
.then((tempToken) => completer.complete(tempToken))
.catchError((_){
completer.completeError(HAError("Auth error"));
});
}, 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(
endPoint: "/auth/token",
contentType: "application/x-www-form-urlencoded",
includeAuthHeader: false,
data: "grant_type=authorization_code&code=$authCode&client_id=${Uri.encodeComponent('http://ha-client.homemade.systems')}"
final flutterWebviewPlugin = new FlutterWebviewPlugin();
flutterWebviewPlugin.onUrlChanged.listen((String url) {
if (url.startsWith("http://ha-client.homemade.systems/service/auth_callback.html")) {
Logger.d("url=$url");
String authCode = url.split("=")[1];
Logger.d("authCode=$authCode");
Logger.d("We have auth code. Getting temporary access token...");
ConnectionManager().sendHTTPPost(
endPoint: "/auth/token",
contentType: "application/x-www-form-urlencoded",
includeAuthHeader: false,
data: "grant_type=authorization_code&code=$authCode&client_id=${Uri.encodeComponent('http://ha-client.homemade.systems')}"
).then((response) {
Logger.d("Got temp token");
String tempToken = json.decode(response)['access_token'];
Logger.d("Closing webview...");
eventBus.fire(StartAuthEvent(oauthUrl, false));
completer.complete(tempToken);
}).catchError((e) {
//flutterWebviewPlugin.close();
Logger.e("Error getting temp token: ${e.toString()}");
eventBus.fire(StartAuthEvent(oauthUrl, false));
completer.completeError(HAError("Error getting temp token"));
});
}).whenComplete(() => flutterWebviewPlugin.close());
}
});
Logger.d("Launching OAuth");
eventBus.fire(StartAuthEvent(oauthUrl, true));
return completer.future;
}
}
}

View File

@ -61,7 +61,7 @@ class ConnectionManager {
oauthUrl = "$httpWebHost/auth/authorize?client_id=${Uri.encodeComponent(
'http://ha-client.homemade.systems')}&redirect_uri=${Uri
.encodeComponent(
'haclient://auth')}";
'http://ha-client.homemade.systems/service/auth_callback.html')}";
settingsLoaded = true;
} catch (e) {
completer.completeError(HAError("Error reading login details", actions: [HAErrorAction.tryAgain(type: HAErrorActionType.FULL_RELOAD), HAErrorAction.loginAgain()]));

View File

@ -239,6 +239,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
_showOAuth();
} else {
_preventAppRefresh = false;
Navigator.of(context).pop();
}
});
}
@ -252,9 +253,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
void _showOAuth() {
_preventAppRefresh = true;
Launcher.launchURLInCustomTab(
url: ConnectionManager().oauthUrl
);
Navigator.of(context).pushNamed("/auth", arguments: {"url": ConnectionManager().oauthUrl});
}
_setErrorState(HAError e) {
@ -436,20 +435,6 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
},
),
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(
leading: Icon(Icons.help),
title: Text("Help"),

View File

@ -23,7 +23,6 @@ dependencies:
flutter_custom_tabs: ^0.6.0
flutter_webview_plugin: ^0.3.10+1
firebase_messaging: ^5.1.6
uni_links: ^0.2.0
flutter_secure_storage: ^3.3.1+1
device_info: ^0.4.0+3
flutter_local_notifications: ^0.8.4