2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-05-13 20:43:11 +00:00

Adds new setting to control whether HTTPs is "strict" or not

- "non strict" allows use of self-signed server certificates for example
This commit is contained in:
Oliver Walters 2022-04-02 20:01:07 +11:00
parent eeb707a955
commit 38652cdba3
4 changed files with 43 additions and 13 deletions

View File

@ -6,6 +6,7 @@ import "package:flutter/foundation.dart";
import "package:http/http.dart" as http; import "package:http/http.dart" as http;
import "package:intl/intl.dart"; import "package:intl/intl.dart";
import "package:inventree/app_colors.dart"; import "package:inventree/app_colors.dart";
import "package:inventree/app_settings.dart";
import "package:open_file/open_file.dart"; import "package:open_file/open_file.dart";
import "package:cached_network_image/cached_network_image.dart"; import "package:cached_network_image/cached_network_image.dart";
@ -82,7 +83,7 @@ class APIResponse {
/* /*
* Custom FileService for caching network images * Custom FileService for caching network images
* Requires a custom badCertificateCallback, * Requires a custom badCertificateCallback,
* so we can accept "dodgy" certificates * so we can accept "dodgy" (e.g. self-signed) certificates
*/ */
class InvenTreeFileService extends FileService { class InvenTreeFileService extends FileService {
@ -142,6 +143,8 @@ class InvenTreeAPI {
// Minimum required API version for server // Minimum required API version for server
static const _minApiVersion = 7; static const _minApiVersion = 7;
bool _strictHttps = false;
// Endpoint for requesting an API token // Endpoint for requesting an API token
static const _URL_GET_TOKEN = "user/token/"; static const _URL_GET_TOKEN = "user/token/";
@ -308,6 +311,9 @@ class InvenTreeAPI {
username = username.trim(); username = username.trim();
password = password.trim(); password = password.trim();
// Cache the "strictHttps" setting, so we can use it later without async requirement
_strictHttps = await InvenTreeSettingsManager().getValue(INV_STRICT_HTTPS, false) as bool;
if (address.isEmpty || username.isEmpty || password.isEmpty) { if (address.isEmpty || username.isEmpty || password.isEmpty) {
showSnackIcon( showSnackIcon(
L10().incompleteDetails, L10().incompleteDetails,
@ -620,7 +626,9 @@ class InvenTreeAPI {
HttpClientRequest? _request; HttpClientRequest? _request;
var client = createClient(allowBadCert: true); final bool strictHttps = await InvenTreeSettingsManager().getValue(INV_STRICT_HTTPS, false) as bool;
var client = createClient(strictHttps: strictHttps);
// Attempt to open a connection to the server // Attempt to open a connection to the server
try { try {
@ -811,22 +819,22 @@ class InvenTreeAPI {
); );
} }
HttpClient createClient({bool allowBadCert = true}) { HttpClient createClient({bool strictHttps = false}) {
var client = HttpClient(); var client = HttpClient();
client.badCertificateCallback = (X509Certificate cert, String host, int port) { client.badCertificateCallback = (X509Certificate cert, String host, int port) {
// TODO - Introspection of actual certificate?
if (allowBadCert) { if (strictHttps) {
return true;
} else {
showServerError( showServerError(
L10().serverCertificateError, L10().serverCertificateError,
L10().serverCertificateInvalid, L10().serverCertificateInvalid,
); );
return false; return false;
} }
// Strict HTTPs not enforced, so we'll ignore the bad cert
return true;
}; };
// Set the connection timeout // Set the connection timeout
@ -874,7 +882,9 @@ class InvenTreeAPI {
HttpClientRequest? _request; HttpClientRequest? _request;
var client = createClient(allowBadCert: true); final bool strictHttps = await InvenTreeSettingsManager().getValue(INV_STRICT_HTTPS, false) as bool;
var client = createClient(strictHttps: strictHttps);
// Attempt to open a connection to the server // Attempt to open a connection to the server
try { try {
@ -1113,7 +1123,9 @@ class InvenTreeAPI {
CacheManager manager = CacheManager( CacheManager manager = CacheManager(
Config( Config(
key, key,
fileService: InvenTreeFileService(), fileService: InvenTreeFileService(
strictHttps: _strictHttps,
),
) )
); );

View File

@ -22,6 +22,8 @@ const String INV_STOCK_SHOW_HISTORY = "stockShowHistory";
const String INV_REPORT_ERRORS = "reportErrors"; const String INV_REPORT_ERRORS = "reportErrors";
const String INV_STRICT_HTTPS = "strictHttps";
class InvenTreeSettingsManager { class InvenTreeSettingsManager {
factory InvenTreeSettingsManager() { factory InvenTreeSettingsManager() {

@ -1 +1 @@
Subproject commit 5d2c2ce31b308b30b910ea5e40fbacf988c839af Subproject commit 9d44f97ded4ff7be3de7f51f305c493c99263508

View File

@ -29,6 +29,7 @@ class _InvenTreeAppSettingsState extends State<InvenTreeAppSettingsWidget> {
bool stockShowHistory = false; bool stockShowHistory = false;
bool reportErrors = true; bool reportErrors = true;
bool strictHttps = false;
@override @override
void initState() { void initState() {
@ -50,6 +51,7 @@ class _InvenTreeAppSettingsState extends State<InvenTreeAppSettingsWidget> {
stockShowHistory = await InvenTreeSettingsManager().getValue(INV_STOCK_SHOW_HISTORY, false) as bool; stockShowHistory = await InvenTreeSettingsManager().getValue(INV_STOCK_SHOW_HISTORY, false) as bool;
reportErrors = await InvenTreeSettingsManager().getValue(INV_REPORT_ERRORS, true) as bool; reportErrors = await InvenTreeSettingsManager().getValue(INV_REPORT_ERRORS, true) as bool;
strictHttps = await InvenTreeSettingsManager().getValue(INV_STRICT_HTTPS, false) as bool;
setState(() { setState(() {
}); });
@ -163,15 +165,29 @@ class _InvenTreeAppSettingsState extends State<InvenTreeAppSettingsWidget> {
Divider(height: 1), Divider(height: 1),
ListTile( ListTile(
title: Text( title: Text(
L10().errorReporting, L10().appSettings,
style: TextStyle(fontWeight: FontWeight.bold), style: TextStyle(fontWeight: FontWeight.bold),
), ),
leading: FaIcon(FontAwesomeIcons.bug), leading: FaIcon(FontAwesomeIcons.mobile),
),
ListTile(
title: Text(L10().strictHttps),
subtitle: Text(L10().strictHttpsDetails),
leading: FaIcon(FontAwesomeIcons.lock),
trailing: Switch(
value: strictHttps,
onChanged: (bool value) {
InvenTreeSettingsManager().setValue(INV_STRICT_HTTPS, value);
setState(() {
strictHttps = value;
});
},
),
), ),
ListTile( ListTile(
title: Text(L10().errorReportUpload), title: Text(L10().errorReportUpload),
subtitle: Text(L10().errorReportUploadDetails), subtitle: Text(L10().errorReportUploadDetails),
leading: FaIcon(FontAwesomeIcons.cloudUploadAlt), leading: FaIcon(FontAwesomeIcons.bug),
trailing: Switch( trailing: Switch(
value: reportErrors, value: reportErrors,
onChanged: (bool value) { onChanged: (bool value) {