Check token info and update to new info

This commit is contained in:
2024-02-02 16:09:43 +08:00
parent cd31d117af
commit 6476d7bf8c
10 changed files with 141 additions and 14 deletions

View File

@@ -6,6 +6,8 @@ PODS:
- Flutter (1.0.0) - Flutter (1.0.0)
- irondash_engine_context (0.0.1): - irondash_engine_context (0.0.1):
- Flutter - Flutter
- package_info_plus (0.4.5):
- Flutter
- path_provider_foundation (0.0.1): - path_provider_foundation (0.0.1):
- Flutter - Flutter
- FlutterMacOS - FlutterMacOS
@@ -20,6 +22,7 @@ DEPENDENCIES:
- device_info_plus (from `.symlinks/plugins/device_info_plus/ios`) - device_info_plus (from `.symlinks/plugins/device_info_plus/ios`)
- Flutter (from `Flutter`) - Flutter (from `Flutter`)
- irondash_engine_context (from `.symlinks/plugins/irondash_engine_context/ios`) - irondash_engine_context (from `.symlinks/plugins/irondash_engine_context/ios`)
- package_info_plus (from `.symlinks/plugins/package_info_plus/ios`)
- path_provider_foundation (from `.symlinks/plugins/path_provider_foundation/darwin`) - path_provider_foundation (from `.symlinks/plugins/path_provider_foundation/darwin`)
- shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/darwin`) - shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/darwin`)
- super_native_extensions (from `.symlinks/plugins/super_native_extensions/ios`) - super_native_extensions (from `.symlinks/plugins/super_native_extensions/ios`)
@@ -33,6 +36,8 @@ EXTERNAL SOURCES:
:path: Flutter :path: Flutter
irondash_engine_context: irondash_engine_context:
:path: ".symlinks/plugins/irondash_engine_context/ios" :path: ".symlinks/plugins/irondash_engine_context/ios"
package_info_plus:
:path: ".symlinks/plugins/package_info_plus/ios"
path_provider_foundation: path_provider_foundation:
:path: ".symlinks/plugins/path_provider_foundation/darwin" :path: ".symlinks/plugins/path_provider_foundation/darwin"
shared_preferences_foundation: shared_preferences_foundation:
@@ -45,10 +50,11 @@ SPEC CHECKSUMS:
device_info_plus: 7545d84d8d1b896cb16a4ff98c19f07ec4b298ea device_info_plus: 7545d84d8d1b896cb16a4ff98c19f07ec4b298ea
Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7 Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7
irondash_engine_context: 3458bf979b90d616ffb8ae03a150bafe2e860cc9 irondash_engine_context: 3458bf979b90d616ffb8ae03a150bafe2e860cc9
package_info_plus: 115f4ad11e0698c8c1c5d8a689390df880f47e85
path_provider_foundation: 29f094ae23ebbca9d3d0cec13889cd9060c0e943 path_provider_foundation: 29f094ae23ebbca9d3d0cec13889cd9060c0e943
shared_preferences_foundation: 5b919d13b803cadd15ed2dc053125c68730e5126 shared_preferences_foundation: 5b919d13b803cadd15ed2dc053125c68730e5126
super_native_extensions: 4916b3c627a9c7fffdc48a23a9eca0b1ac228fa7 super_native_extensions: 4916b3c627a9c7fffdc48a23a9eca0b1ac228fa7
PODFILE CHECKSUM: 819463e6a0290f5a72f145ba7cde16e8b6ef0796 PODFILE CHECKSUM: 819463e6a0290f5a72f145ba7cde16e8b6ef0796
COCOAPODS: 1.14.2 COCOAPODS: 1.15.0

View File

@@ -207,6 +207,19 @@ class FilePickerDelegate: NSObject, UIDocumentPickerDelegate {
result(FlutterMethodNotImplemented) result(FlutterMethodNotImplemented)
} }
} }
let deviceChannel = FlutterMethodChannel(
name: "lifegpc.eh_downloader_flutter/device",
binaryMessenger: controller.binaryMessenger
)
deviceChannel.setMethodCallHandler { (call, result) in
switch call.method {
case "deviceName":
result(UIDevice.current.name)
default:
result(FlutterMethodNotImplemented)
}
}
GeneratedPluginRegistrant.register(with: self) GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions) return super.application(application, didFinishLaunchingWithOptions: launchOptions)
} }

View File

@@ -1,7 +1,9 @@
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import 'api/status.dart'; import 'api/status.dart';
import 'api/token.dart';
import 'api/user.dart'; import 'api/user.dart';
import 'globals.dart'; import 'globals.dart';
import 'platform/device.dart';
final _log = Logger("AuthInfo"); final _log = Logger("AuthInfo");
@@ -11,6 +13,8 @@ class AuthInfo {
BUser? get user => _user; BUser? get user => _user;
ServerStatus? _status; ServerStatus? _status;
ServerStatus? get status => _status; ServerStatus? get status => _status;
Token? _token;
Token? get token => _token;
bool get isAuthed => (_user != null); bool get isAuthed => (_user != null);
bool _checked = false; bool _checked = false;
bool get checked => _checked; bool get checked => _checked;
@@ -29,6 +33,42 @@ class AuthInfo {
_status = (await api.getStatus()).unwrap(); _status = (await api.getStatus()).unwrap();
} }
Future<void> checkSessionInfo() async {
final data = (await api.getToken()).unwrap();
_token = data.token;
final d = await device;
final cv = await clientVersion;
final cp = clientPlatform;
String? client;
String? ed;
String? ecv;
String? ecp;
if (_token!.client != "flutter") {
client = "flutter";
}
if (_token!.device != d) {
ed = d;
}
if (_token!.clientVersion != cv) {
ecv = cv;
}
if (_token!.clientPlatform != cp) {
ecp = cp;
}
if (client != null || ed != null || ecv != null || ecp != null) {
try {
final re = await api.updateToken(
client: client,
device: ed,
clientVersion: ecv,
clientPlatform: ecp);
_token = re.unwrap();
} catch (e) {
_log.warning("Failed to update token:", e);
}
}
}
Future<bool> checkAuth() async { Future<bool> checkAuth() async {
_isChecking = true; _isChecking = true;
try { try {
@@ -46,6 +86,7 @@ class AuthInfo {
} }
_checked = true; _checked = true;
await getServerStatus(); await getServerStatus();
await checkSessionInfo();
return re.ok; return re.ok;
} finally { } finally {
_isChecking = false; _isChecking = false;

View File

@@ -3,7 +3,6 @@ import 'package:flutter/scheduler.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'globals.dart'; import 'globals.dart';
import 'platform/device.dart'; import 'platform/device.dart';
@@ -18,14 +17,6 @@ class LoginPage extends StatefulWidget {
State<LoginPage> createState() => _LoginPageState(); State<LoginPage> createState() => _LoginPageState();
} }
Future<String?> get clientVersion async {
try {
return (await PackageInfo.fromPlatform()).version;
} catch (_) {
return null;
}
}
Future<bool> login(String username, String password) async { Future<bool> login(String username, String password) async {
String baseUrl = api.baseUrl!; String baseUrl = api.baseUrl!;
final u = Uri.parse(baseUrl); final u = Uri.parse(baseUrl);
@@ -37,7 +28,7 @@ Future<bool> login(String username, String password) async {
httpOnly: true, httpOnly: true,
secure: u.scheme == 'https', secure: u.scheme == 'https',
client: "flutter", client: "flutter",
device: device, device: await device,
clientVersion: await clientVersion, clientVersion: await clientVersion,
clientPlatform: clientPlatform); clientPlatform: clientPlatform);
if (re.ok) return true; if (re.ok) return true;

View File

@@ -1 +1,10 @@
export './device_other.dart' if (dart.library.html) './device_web.dart'; export './device_other.dart' if (dart.library.html) './device_web.dart';
import 'package:package_info_plus/package_info_plus.dart';
Future<String?> get clientVersion async {
try {
return (await PackageInfo.fromPlatform()).version;
} catch (_) {
return null;
}
}

View File

@@ -1,6 +1,21 @@
import 'dart:io'; import 'dart:io';
import 'package:flutter/services.dart';
import 'package:logging/logging.dart';
String? get device => null; const _platform = MethodChannel("lifegpc.eh_downloader_flutter/device");
final _log = Logger("platformDevice");
String? _device;
Future<String?> get device async {
if (_device == null) {
try {
_device = await _platform.invokeMethod<String>("deviceName");
} catch (e) {
_log.warning("Failed to get device:", e);
}
}
return _device;
}
String? get clientPlatform { String? get clientPlatform {
if (Platform.isAndroid) return "android"; if (Platform.isAndroid) return "android";

View File

@@ -3,7 +3,7 @@ import 'package:ua_parser_js/ua_parser_js.dart';
final _uaParser = UAParser(); final _uaParser = UAParser();
String? _device; String? _device;
String? get device { Future<String?> get device {
if (_device == null) { if (_device == null) {
final ua = _uaParser.getResult(); final ua = _uaParser.getResult();
_device = ua.browser.name; _device = ua.browser.name;
@@ -11,7 +11,7 @@ String? get device {
_device = "$_device ${ua.browser.version}"; _device = "$_device ${ua.browser.version}";
} }
} }
return _device; return Future.value(_device);
} }
String? get clientPlatform => "web"; String? get clientPlatform => "web";

View File

@@ -15,6 +15,7 @@ struct _MyApplication {
char** dart_entrypoint_arguments; char** dart_entrypoint_arguments;
FlMethodChannel* path_channel; FlMethodChannel* path_channel;
FlMethodChannel* saf_channel; FlMethodChannel* saf_channel;
FlMethodChannel* device_channel;
}; };
G_DEFINE_TYPE(MyApplication, my_application, GTK_TYPE_APPLICATION) G_DEFINE_TYPE(MyApplication, my_application, GTK_TYPE_APPLICATION)
@@ -186,6 +187,21 @@ static void on_saf_channel_call(FlMethodChannel* channel, FlMethodCall* method_c
} }
} }
static void on_device_channel_call(FlMethodChannel* channel, FlMethodCall* method_call,
gpointer user_data) {
const gchar* method = fl_method_call_get_name(method_call);
if (g_strcmp0(method, "deviceName") == 0) {
const gchar* name = g_get_host_name();
if (name) {
fl_method_call_respond_success(method_call, fl_value_new_string(name), nullptr);
} else {
fl_method_call_respond_success(method_call, fl_value_new_null(), nullptr);
}
} else {
fl_method_call_respond_not_implemented(method_call, nullptr);
}
}
// Implements GApplication::activate. // Implements GApplication::activate.
static void my_application_activate(GApplication* application) { static void my_application_activate(GApplication* application) {
MyApplication* self = MY_APPLICATION(application); MyApplication* self = MY_APPLICATION(application);
@@ -239,6 +255,10 @@ static void my_application_activate(GApplication* application) {
fl_engine_get_binary_messenger(fl_view_get_engine(view)), "lifegpc.eh_downloader_flutter/saf", fl_engine_get_binary_messenger(fl_view_get_engine(view)), "lifegpc.eh_downloader_flutter/saf",
FL_METHOD_CODEC(codec)); FL_METHOD_CODEC(codec));
fl_method_channel_set_method_call_handler(self->saf_channel, on_saf_channel_call, self, nullptr); fl_method_channel_set_method_call_handler(self->saf_channel, on_saf_channel_call, self, nullptr);
self->device_channel = fl_method_channel_new(
fl_engine_get_binary_messenger(fl_view_get_engine(view)), "lifegpc.eh_downloader_flutter/device",
FL_METHOD_CODEC(codec));
fl_method_channel_set_method_call_handler(self->device_channel, on_device_channel_call, self, nullptr);
gtk_widget_grab_focus(GTK_WIDGET(view)); gtk_widget_grab_focus(GTK_WIDGET(view));
} }
@@ -268,6 +288,7 @@ static void my_application_dispose(GObject* object) {
g_clear_pointer(&self->dart_entrypoint_arguments, g_strfreev); g_clear_pointer(&self->dart_entrypoint_arguments, g_strfreev);
g_clear_object(&self->path_channel); g_clear_object(&self->path_channel);
g_clear_object(&self->saf_channel); g_clear_object(&self->saf_channel);
g_clear_object(&self->device_channel);
G_OBJECT_CLASS(my_application_parent_class)->dispose(object); G_OBJECT_CLASS(my_application_parent_class)->dispose(object);
} }

View File

@@ -137,6 +137,18 @@ class MainFlutterWindow: NSWindow {
} }
} }
let deviceChannel = FlutterMethodChannel(
name: "lifegpc.eh_downloader_flutter/device",
binaryMessenger: flutterViewController.engine.binaryMessenger)
deviceChannel.setMethodCallHandler { (call, result) in
switch call.method {
case "deviceName":
result(Host.current().localizedName)
default:
result(FlutterMethodNotImplemented)
}
}
RegisterGeneratedPlugins(registry: flutterViewController) RegisterGeneratedPlugins(registry: flutterViewController)
super.awakeFromNib() super.awakeFromNib()

View File

@@ -248,6 +248,25 @@ bool FlutterWindow::OnCreate() {
result->NotImplemented(); result->NotImplemented();
} }
}); });
flutter::MethodChannel<> device(flutter_controller_->engine()->messenger(), "lifegpc.eh_downloader_flutter/device",
&flutter::StandardMethodCodec::GetInstance());
device.SetMethodCallHandler([&](const flutter::MethodCall<>& call, std::unique_ptr<flutter::MethodResult<>> result) {
if (call.method_name() == "deviceName") {
wchar_t name[MAX_COMPUTERNAME_LENGTH + 1];
if (!GetComputerNameW(name, MAX_COMPUTERNAME_LENGTH)) {
result->Success();
return;
}
std::string deviceName;
if (!wchar_util::wstr_to_str(deviceName, name, CP_UTF8)) {
result->Error("ERROR", "Failed to convert device name to UTF-8.");
return;
}
result->Success(deviceName);
} else {
result->NotImplemented();
}
});
SetChildContent(flutter_controller_->view()->GetNativeWindow()); SetChildContent(flutter_controller_->view()->GetNativeWindow());