From e6d6dacad100179f1429f50151e7ffd3e6bb9d4f Mon Sep 17 00:00:00 2001 From: jld3103 Date: Wed, 20 Jul 2022 18:13:15 +0200 Subject: [PATCH 1/2] nextcloud: Add user-agent override --- packages/nextcloud/lib/src/client.dart | 34 +++++++++--------------- packages/nextcloud/test/client_test.dart | 11 +------- packages/nextcloud/test/helper.dart | 6 ++--- 3 files changed, 16 insertions(+), 35 deletions(-) diff --git a/packages/nextcloud/lib/src/client.dart b/packages/nextcloud/lib/src/client.dart index b535a846..e9467004 100644 --- a/packages/nextcloud/lib/src/client.dart +++ b/packages/nextcloud/lib/src/client.dart @@ -9,7 +9,7 @@ class NextcloudClient { this.password, this.language, this.appType = AppType.unknown, - this.userAgentSuffix, + this.userAgentOverride, }) { final authentication = username != null && password != null ? HttpBasicAuth(username: username!, password: password!) @@ -55,17 +55,13 @@ class NextcloudClient { } /// Headers that should be used for all requests - late final commonHeaders = () { - // ignore: no_leading_underscores_for_local_identifiers - final _userAgent = userAgent; - return { - 'OCS-APIRequest': 'true', - 'Accept': 'application/json', - if (_userAgent != null) ...{ - 'User-Agent': _userAgent, - } - }; - }(); + late final commonHeaders = { + 'OCS-APIRequest': 'true', + 'Accept': 'application/json', + if (userAgent != null) ...{ + 'User-Agent': userAgent!, + }, + }; T _addCommonSettings(final T apiClient) { var newApiClient = apiClient; @@ -91,17 +87,11 @@ class NextcloudClient { /// App type the client will register as. Only relevant for notifications. See [AppType] for explanations. final AppType appType; - /// Will be appended to the user-agent from [appType] - final String? userAgentSuffix; + /// Overrides the user-agent set by [appType] + final String? userAgentOverride; - /// User-agent made up from the user-agent from [appType] and the [userAgentSuffix] - String? get userAgent { - if (appType.userAgent != null || userAgentSuffix != null) { - return [appType.userAgent, userAgentSuffix].where((final a) => a != null).join(); - } - - return null; - } + /// User-agent made up from the user-agent from [userAgentOverride] and the [appType] + late String? userAgent = userAgentOverride ?? appType.userAgent; NextcloudWebDAVClient? _webdav; diff --git a/packages/nextcloud/test/client_test.dart b/packages/nextcloud/test/client_test.dart index 519f4b6b..2acbe9e4 100644 --- a/packages/nextcloud/test/client_test.dart +++ b/packages/nextcloud/test/client_test.dart @@ -21,18 +21,9 @@ Future main() async { test('User-Agent with suffix', () async { client = await TestHelper.getPreparedClient( dockerImageName, - userAgentSuffix: 'test', + userAgentOverride: 'test', ); expect(client.userAgent, 'test'); }); - - test('User-Agent from AppType with suffix', () async { - client = await TestHelper.getPreparedClient( - dockerImageName, - appType: AppType.nextcloud, - userAgentSuffix: ' test', - ); - expect(client.userAgent, '${AppType.nextcloud.userAgent} test'); - }); }); } diff --git a/packages/nextcloud/test/helper.dart b/packages/nextcloud/test/helper.dart index c31fe5a0..9228183a 100644 --- a/packages/nextcloud/test/helper.dart +++ b/packages/nextcloud/test/helper.dart @@ -21,7 +21,7 @@ class TestNextcloudClient extends NextcloudClient { super.password, super.language, super.appType, - super.userAgentSuffix, + super.userAgentOverride, }); final String containerID; @@ -138,7 +138,7 @@ class TestHelper { final String? password = defaultPassword, final bool useAppPassword = false, final AppType appType = AppType.unknown, - final String? userAgentSuffix, + final String? userAgentOverride, }) async { // ignore: prefer_asserts_with_message assert(!useAppPassword || (username != null && password != null)); @@ -198,7 +198,7 @@ class TestHelper { username: username, password: clientPassword, appType: appType, - userAgentSuffix: userAgentSuffix, + userAgentOverride: userAgentOverride, ); var iteration = 0; From 0a11e3ae51b63cb54a8df757285cd759af7ffc37 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Wed, 20 Jul 2022 18:13:36 +0200 Subject: [PATCH 2/2] harbour: Use proper user-agent --- packages/harbour/lib/main.dart | 2 ++ packages/harbour/lib/src/blocs/login.dart | 3 +-- packages/harbour/lib/src/harbour.dart | 1 + packages/harbour/lib/src/models/account.dart | 7 ++----- packages/harbour/lib/src/pages/login/login.dart | 2 ++ packages/harbour/lib/src/utils/global.dart | 9 +++++++++ 6 files changed, 17 insertions(+), 7 deletions(-) create mode 100644 packages/harbour/lib/src/utils/global.dart diff --git a/packages/harbour/lib/main.dart b/packages/harbour/lib/main.dart index 13112077..a1851264 100644 --- a/packages/harbour/lib/main.dart +++ b/packages/harbour/lib/main.dart @@ -25,6 +25,8 @@ Future main() async { FlutterNativeSplash.preserve(widgetsBinding: WidgetsBinding.instance); + await Global.init(); + final platform = getHarbourPlatform(); await platform.init?.call(); diff --git a/packages/harbour/lib/src/blocs/login.dart b/packages/harbour/lib/src/blocs/login.dart index 29d4e5cb..12158935 100644 --- a/packages/harbour/lib/src/blocs/login.dart +++ b/packages/harbour/lib/src/blocs/login.dart @@ -35,8 +35,7 @@ class LoginBloc extends $LoginBloc { try { final client = NextcloudClient( url, - userAgentSuffix: userAgentSuffix, - appType: appType, + userAgentOverride: userAgentOverride(), ); final status = (await client.core.getStatus())!; diff --git a/packages/harbour/lib/src/harbour.dart b/packages/harbour/lib/src/harbour.dart index 0202481b..d67139e8 100644 --- a/packages/harbour/lib/src/harbour.dart +++ b/packages/harbour/lib/src/harbour.dart @@ -69,6 +69,7 @@ part 'utils/app_implementation.dart'; part 'utils/confirmation_dialog.dart'; part 'utils/custom_timeago.dart'; part 'utils/env.dart'; +part 'utils/global.dart'; part 'utils/global_options.dart'; part 'utils/hex_color.dart'; part 'utils/missing_permission_exception.dart'; diff --git a/packages/harbour/lib/src/models/account.dart b/packages/harbour/lib/src/models/account.dart index 7b29d374..55995326 100644 --- a/packages/harbour/lib/src/models/account.dart +++ b/packages/harbour/lib/src/models/account.dart @@ -9,9 +9,7 @@ import 'package:settings/settings.dart'; part 'account.g.dart'; -// TODO: https://github.com/jld3103/nextcloud-harbour/issues/9 -const userAgentSuffix = ' // Harbour'; -const appType = AppType.nextcloud; +String userAgentOverride() => 'Harbour ${Global.packageInfo.version}+${Global.packageInfo.buildNumber}'; @JsonSerializable() class Account { @@ -54,8 +52,7 @@ class Account { serverURL, username: username, password: password ?? appPassword, - userAgentSuffix: userAgentSuffix, - appType: appType, + userAgentOverride: userAgentOverride(), ); } diff --git a/packages/harbour/lib/src/pages/login/login.dart b/packages/harbour/lib/src/pages/login/login.dart index 60a0b36b..a3effbb1 100644 --- a/packages/harbour/lib/src/pages/login/login.dart +++ b/packages/harbour/lib/src/pages/login/login.dart @@ -73,6 +73,7 @@ class _LoginPageState extends State { final Env? env, ) => { + HttpHeaders.userAgentHeader: userAgentOverride(), if (env != null) ...{ HttpHeaders.authorizationHeader: 'Basic ${base64.encode(utf8.encode('${env.testUsername}:${env.testPassword}'))}', @@ -136,6 +137,7 @@ class _LoginPageState extends State { ? WebView( javascriptMode: JavascriptMode.unrestricted, zoomEnabled: false, + userAgent: userAgentOverride(), onWebViewCreated: (final controller) async { _webViewController = controller; final url = diff --git a/packages/harbour/lib/src/utils/global.dart b/packages/harbour/lib/src/utils/global.dart new file mode 100644 index 00000000..77756937 --- /dev/null +++ b/packages/harbour/lib/src/utils/global.dart @@ -0,0 +1,9 @@ +part of '../harbour.dart'; + +class Global { + static Future init() async { + packageInfo = await PackageInfo.fromPlatform(); + } + + static late PackageInfo packageInfo; +}