Browse Source

Merge pull request #25 from jld3103/feature/user-agent

Add proper user-agent
pull/28/head
jld3103 2 years ago committed by GitHub
parent
commit
731906426b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      packages/harbour/lib/main.dart
  2. 3
      packages/harbour/lib/src/blocs/login.dart
  3. 1
      packages/harbour/lib/src/harbour.dart
  4. 7
      packages/harbour/lib/src/models/account.dart
  5. 2
      packages/harbour/lib/src/pages/login/login.dart
  6. 9
      packages/harbour/lib/src/utils/global.dart
  7. 34
      packages/nextcloud/lib/src/client.dart
  8. 11
      packages/nextcloud/test/client_test.dart
  9. 6
      packages/nextcloud/test/helper.dart

2
packages/harbour/lib/main.dart

@ -25,6 +25,8 @@ Future main() async {
FlutterNativeSplash.preserve(widgetsBinding: WidgetsBinding.instance); FlutterNativeSplash.preserve(widgetsBinding: WidgetsBinding.instance);
await Global.init();
final platform = getHarbourPlatform(); final platform = getHarbourPlatform();
await platform.init?.call(); await platform.init?.call();

3
packages/harbour/lib/src/blocs/login.dart

@ -35,8 +35,7 @@ class LoginBloc extends $LoginBloc {
try { try {
final client = NextcloudClient( final client = NextcloudClient(
url, url,
userAgentSuffix: userAgentSuffix, userAgentOverride: userAgentOverride(),
appType: appType,
); );
final status = (await client.core.getStatus())!; final status = (await client.core.getStatus())!;

1
packages/harbour/lib/src/harbour.dart

@ -69,6 +69,7 @@ part 'utils/app_implementation.dart';
part 'utils/confirmation_dialog.dart'; part 'utils/confirmation_dialog.dart';
part 'utils/custom_timeago.dart'; part 'utils/custom_timeago.dart';
part 'utils/env.dart'; part 'utils/env.dart';
part 'utils/global.dart';
part 'utils/global_options.dart'; part 'utils/global_options.dart';
part 'utils/hex_color.dart'; part 'utils/hex_color.dart';
part 'utils/missing_permission_exception.dart'; part 'utils/missing_permission_exception.dart';

7
packages/harbour/lib/src/models/account.dart

@ -9,9 +9,7 @@ import 'package:settings/settings.dart';
part 'account.g.dart'; part 'account.g.dart';
// TODO: https://github.com/jld3103/nextcloud-harbour/issues/9 String userAgentOverride() => 'Harbour ${Global.packageInfo.version}+${Global.packageInfo.buildNumber}';
const userAgentSuffix = ' // Harbour';
const appType = AppType.nextcloud;
@JsonSerializable() @JsonSerializable()
class Account { class Account {
@ -54,8 +52,7 @@ class Account {
serverURL, serverURL,
username: username, username: username,
password: password ?? appPassword, password: password ?? appPassword,
userAgentSuffix: userAgentSuffix, userAgentOverride: userAgentOverride(),
appType: appType,
); );
} }

2
packages/harbour/lib/src/pages/login/login.dart

@ -73,6 +73,7 @@ class _LoginPageState extends State<LoginPage> {
final Env? env, final Env? env,
) => ) =>
{ {
HttpHeaders.userAgentHeader: userAgentOverride(),
if (env != null) ...{ if (env != null) ...{
HttpHeaders.authorizationHeader: HttpHeaders.authorizationHeader:
'Basic ${base64.encode(utf8.encode('${env.testUsername}:${env.testPassword}'))}', 'Basic ${base64.encode(utf8.encode('${env.testUsername}:${env.testPassword}'))}',
@ -136,6 +137,7 @@ class _LoginPageState extends State<LoginPage> {
? WebView( ? WebView(
javascriptMode: JavascriptMode.unrestricted, javascriptMode: JavascriptMode.unrestricted,
zoomEnabled: false, zoomEnabled: false,
userAgent: userAgentOverride(),
onWebViewCreated: (final controller) async { onWebViewCreated: (final controller) async {
_webViewController = controller; _webViewController = controller;
final url = final url =

9
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;
}

34
packages/nextcloud/lib/src/client.dart

@ -9,7 +9,7 @@ class NextcloudClient {
this.password, this.password,
this.language, this.language,
this.appType = AppType.unknown, this.appType = AppType.unknown,
this.userAgentSuffix, this.userAgentOverride,
}) { }) {
final authentication = username != null && password != null final authentication = username != null && password != null
? HttpBasicAuth(username: username!, password: password!) ? HttpBasicAuth(username: username!, password: password!)
@ -55,17 +55,13 @@ class NextcloudClient {
} }
/// Headers that should be used for all requests /// Headers that should be used for all requests
late final commonHeaders = () { late final commonHeaders = <String, String>{
// ignore: no_leading_underscores_for_local_identifiers 'OCS-APIRequest': 'true',
final _userAgent = userAgent; 'Accept': 'application/json',
return <String, String>{ if (userAgent != null) ...{
'OCS-APIRequest': 'true', 'User-Agent': userAgent!,
'Accept': 'application/json', },
if (_userAgent != null) ...{ };
'User-Agent': _userAgent,
}
};
}();
T _addCommonSettings<T extends BaseApiClient>(final T apiClient) { T _addCommonSettings<T extends BaseApiClient>(final T apiClient) {
var newApiClient = 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. /// App type the client will register as. Only relevant for notifications. See [AppType] for explanations.
final AppType appType; final AppType appType;
/// Will be appended to the user-agent from [appType] /// Overrides the user-agent set by [appType]
final String? userAgentSuffix; final String? userAgentOverride;
/// User-agent made up from the user-agent from [appType] and the [userAgentSuffix] /// User-agent made up from the user-agent from [userAgentOverride] and the [appType]
String? get userAgent { late String? userAgent = userAgentOverride ?? appType.userAgent;
if (appType.userAgent != null || userAgentSuffix != null) {
return [appType.userAgent, userAgentSuffix].where((final a) => a != null).join();
}
return null;
}
NextcloudWebDAVClient? _webdav; NextcloudWebDAVClient? _webdav;

11
packages/nextcloud/test/client_test.dart

@ -21,18 +21,9 @@ Future main() async {
test('User-Agent with suffix', () async { test('User-Agent with suffix', () async {
client = await TestHelper.getPreparedClient( client = await TestHelper.getPreparedClient(
dockerImageName, dockerImageName,
userAgentSuffix: 'test', userAgentOverride: 'test',
); );
expect(client.userAgent, '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');
});
}); });
} }

6
packages/nextcloud/test/helper.dart

@ -21,7 +21,7 @@ class TestNextcloudClient extends NextcloudClient {
super.password, super.password,
super.language, super.language,
super.appType, super.appType,
super.userAgentSuffix, super.userAgentOverride,
}); });
final String containerID; final String containerID;
@ -138,7 +138,7 @@ class TestHelper {
final String? password = defaultPassword, final String? password = defaultPassword,
final bool useAppPassword = false, final bool useAppPassword = false,
final AppType appType = AppType.unknown, final AppType appType = AppType.unknown,
final String? userAgentSuffix, final String? userAgentOverride,
}) async { }) async {
// ignore: prefer_asserts_with_message // ignore: prefer_asserts_with_message
assert(!useAppPassword || (username != null && password != null)); assert(!useAppPassword || (username != null && password != null));
@ -198,7 +198,7 @@ class TestHelper {
username: username, username: username,
password: clientPassword, password: clientPassword,
appType: appType, appType: appType,
userAgentSuffix: userAgentSuffix, userAgentOverride: userAgentOverride,
); );
var iteration = 0; var iteration = 0;

Loading…
Cancel
Save