This repository has been archived on 2023-11-18. You can view files and clone it, but cannot push or open issues or pull requests.
ha_client/lib/auth_manager.class.dart

45 lines
1.6 KiB
Dart
Raw Normal View History

2019-03-26 00:18:30 +02:00
part of 'main.dart';
class AuthManager {
static final AuthManager _instance = AuthManager._internal();
factory AuthManager() {
return _instance;
}
AuthManager._internal();
2019-03-29 13:09:34 +02:00
Future getTempToken({String oauthUrl}) {
2019-03-26 00:18:30 +02:00
Completer completer = Completer();
final flutterWebviewPlugin = new FlutterWebviewPlugin();
flutterWebviewPlugin.onUrlChanged.listen((String url) {
if (url.startsWith("http://ha-client.homemade.systems/service/auth_callback.html")) {
String authCode = url.split("=")[1];
Logger.d("We have auth code. Getting temporary access token...");
Connection().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("Gottemp token");
String tempToken = json.decode(response)['access_token'];
Logger.d("Closing webview...");
flutterWebviewPlugin.close();
2019-04-05 14:07:03 +03:00
eventBus.fire(StartAuthEvent(oauthUrl, false));
2019-03-26 00:18:30 +02:00
completer.complete(tempToken);
}).catchError((e) {
flutterWebviewPlugin.close();
Logger.e("Error getting temp token: ${e.toString()}");
2019-04-05 14:07:03 +03:00
eventBus.fire(StartAuthEvent(oauthUrl, false));
2019-04-19 21:43:52 +03:00
completer.completeError(HAError("Error getting temp token"));
2019-03-26 00:18:30 +02:00
});
}
});
Logger.d("Launching OAuth: $oauthUrl");
2019-04-05 14:07:03 +03:00
eventBus.fire(StartAuthEvent(oauthUrl, true));
2019-03-26 00:18:30 +02:00
return completer.future;
}
}