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

@@ -1,7 +1,9 @@
import 'package:logging/logging.dart';
import 'api/status.dart';
import 'api/token.dart';
import 'api/user.dart';
import 'globals.dart';
import 'platform/device.dart';
final _log = Logger("AuthInfo");
@@ -11,6 +13,8 @@ class AuthInfo {
BUser? get user => _user;
ServerStatus? _status;
ServerStatus? get status => _status;
Token? _token;
Token? get token => _token;
bool get isAuthed => (_user != null);
bool _checked = false;
bool get checked => _checked;
@@ -29,6 +33,42 @@ class AuthInfo {
_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 {
_isChecking = true;
try {
@@ -46,6 +86,7 @@ class AuthInfo {
}
_checked = true;
await getServerStatus();
await checkSessionInfo();
return re.ok;
} finally {
_isChecking = false;

View File

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

View File

@@ -1 +1,10 @@
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 '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 {
if (Platform.isAndroid) return "android";

View File

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