-
-
Notifications
You must be signed in to change notification settings - Fork 382
Add sanity check for server configuration #1136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
2879318
245d0b8
e2dd9bf
c787200
f4e2f1a
36dbcde
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /* | ||
| * This file is part of wger Workout Manager <https://github.com/wger-project>. | ||
| * Copyright (c) 2025 wger Team | ||
| * | ||
| * wger Workout Manager is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| */ | ||
|
|
||
| import 'dart:convert'; | ||
| import 'package:http/http.dart' as http; | ||
|
|
||
| /// Checks if the server's reverse proxy is configured correctly | ||
| /// by comparing pagination URLs with the base URL domain. | ||
| Future<SanityCheckResult> checkServerPaginationUrls({ | ||
| required String baseUrl, | ||
| required String token, | ||
| http.Client? client, | ||
| }) async { | ||
| final httpClient = client ?? http.Client(); | ||
|
|
||
| try { | ||
| final baseUri = Uri.parse(baseUrl); | ||
| final expectedHost = baseUri.host; | ||
| final expectedScheme = baseUri.scheme; | ||
|
|
||
| final response = await httpClient.get( | ||
| Uri.parse('$baseUrl/api/v2/exercise/?limit=1'), | ||
| headers: { | ||
| 'Authorization': 'Token $token', | ||
| 'Accept': 'application/json', | ||
| }, | ||
| ); | ||
|
|
||
| if (response.statusCode != 200) { | ||
| return const SanityCheckResult(isValid: false); | ||
| } | ||
|
|
||
| final data = jsonDecode(response.body) as Map<String, dynamic>; | ||
| final nextUrl = data['next'] as String?; | ||
|
|
||
| if (nextUrl == null) { | ||
| return const SanityCheckResult(isValid: true); | ||
| } | ||
|
|
||
| final nextUri = Uri.parse(nextUrl); | ||
|
|
||
| if (nextUri.host != expectedHost || nextUri.scheme != expectedScheme) { | ||
| return const SanityCheckResult(isValid: false); | ||
| } | ||
|
|
||
| return const SanityCheckResult(isValid: true); | ||
| } catch (e) { | ||
| return const SanityCheckResult(isValid: false); | ||
| } | ||
| } | ||
|
|
||
| /// Result of a server sanity check | ||
| class SanityCheckResult { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a boolean might be enough 😄 This can be useful if we added more checks, but at the moment there's nothing planed |
||
| const SanityCheckResult({ | ||
| required this.isValid, | ||
| }); | ||
|
|
||
| final bool isValid; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ import 'package:wger/widgets/auth/email_field.dart'; | |
| import 'package:wger/widgets/auth/password_field.dart'; | ||
| import 'package:wger/widgets/auth/server_field.dart'; | ||
| import 'package:wger/widgets/auth/username_field.dart'; | ||
| import 'package:wger/widgets/core/server_config_warning_dialog.dart'; | ||
|
|
||
| import '../providers/auth.dart'; | ||
|
|
||
|
|
@@ -206,6 +207,17 @@ class _AuthCardState extends State<AuthCard> { | |
| ); | ||
| return; | ||
| } | ||
|
|
||
| if (context.mounted && res == LoginActions.proceed) { | ||
| final authProvider = context.read<AuthProvider>(); | ||
| if (authProvider.serverConfigWarning) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be more consistent to use something like |
||
| if (context.mounted) { | ||
| showServerConfigWarning(context); | ||
| authProvider.clearServerConfigWarning(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (context.mounted) { | ||
| setState(() { | ||
| _isLoading = false; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * This file is part of wger Workout Manager <https://github.com/wger-project>. | ||
| * Copyright (c) 2025 wger Team | ||
| * | ||
| * wger Workout Manager is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| */ | ||
|
|
||
| import 'package:flutter/material.dart'; | ||
| import 'package:wger/helpers/misc.dart'; | ||
| import 'package:wger/l10n/generated/app_localizations.dart'; | ||
|
|
||
| /// Shows a warning dialog when server misconfiguration is detected | ||
| void showServerConfigWarning(BuildContext context) { | ||
| final i18n = AppLocalizations.of(context); | ||
|
|
||
| showDialog( | ||
| context: context, | ||
| builder: (context) => AlertDialog( | ||
| title: Row( | ||
| children: [ | ||
| const Icon(Icons.warning, color: Colors.orange, size: 28), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better use something like |
||
| const SizedBox(width: 12), | ||
| Expanded(child: Text(i18n.serverConfigIssueTitle)), | ||
| ], | ||
| ), | ||
| content: Text(i18n.serverConfigIssueMessage), | ||
| actions: [ | ||
| TextButton( | ||
| onPressed: () => Navigator.pop(context), | ||
| child: Text(i18n.understand), | ||
| ), | ||
| TextButton( | ||
| onPressed: () { | ||
| launchURL( | ||
| 'https://wger.readthedocs.io/en/latest/administration/errors.html#wrong-pagination-links', | ||
| context, | ||
| ); | ||
| Navigator.pop(context); | ||
| }, | ||
| child: Text(i18n.viewDocumentation), | ||
| ), | ||
| ], | ||
| ), | ||
| ); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would move this to the auth provider (while it's not strictly anything to do with authentication is related enough, plus we already have the min application version check there)