|
| 1 | +import 'dart:io'; |
| 2 | + |
| 3 | +import 'package:convert/convert.dart'; |
| 4 | +import 'package:dio/dio.dart'; |
| 5 | +import 'package:flutter/material.dart'; |
| 6 | +import 'package:flutter_secure_storage/flutter_secure_storage.dart'; |
| 7 | +import 'package:get_it/get_it.dart'; |
| 8 | +import 'package:paperless_app/i18n.dart'; |
| 9 | + |
| 10 | +import '../api.dart'; |
| 11 | + |
| 12 | +void handleDioError(DioError e, BuildContext ctx) { |
| 13 | + if (e.error.runtimeType == HandshakeException && |
| 14 | + SelfSignedCertHttpOverride.lastFailedCert != null) { |
| 15 | + String fingerprint = |
| 16 | + hex.encode(SelfSignedCertHttpOverride.lastFailedCert!.sha1); |
| 17 | + showDialog( |
| 18 | + context: ctx, |
| 19 | + builder: (BuildContext ctx) { |
| 20 | + return AlertDialog( |
| 21 | + backgroundColor: Colors.red.shade900, |
| 22 | + title: Text("This is not a secure connection".i18n), |
| 23 | + content: Text( |
| 24 | + "The certificate for %s is not valid. Do you still want to trust the certificate with fingerprint %s?" |
| 25 | + .i18n |
| 26 | + .fill([e.requestOptions.uri.host, fingerprint]) + |
| 27 | + "\n" + |
| 28 | + "WARNING: The connection is not secure if you do not compare the fingerprint." |
| 29 | + .i18n), |
| 30 | + actions: [ |
| 31 | + TextButton( |
| 32 | + onPressed: () { |
| 33 | + API.trustedCertificateSha512 = |
| 34 | + SelfSignedCertHttpOverride.toSha512( |
| 35 | + SelfSignedCertHttpOverride.lastFailedCert!); |
| 36 | + GetIt.I<FlutterSecureStorage>().write( |
| 37 | + key: "trustedCertificateSha512", |
| 38 | + value: API.trustedCertificateSha512); |
| 39 | + Navigator.of(ctx).pop(); |
| 40 | + }, |
| 41 | + child: Text('Yes'.i18n), |
| 42 | + ), |
| 43 | + TextButton( |
| 44 | + onPressed: () { |
| 45 | + // Close the dialog |
| 46 | + Navigator.of(ctx).pop(); |
| 47 | + }, |
| 48 | + child: Text('No'.i18n), |
| 49 | + ), |
| 50 | + ], |
| 51 | + ); |
| 52 | + }); |
| 53 | + } else |
| 54 | + showDialog( |
| 55 | + context: ctx, |
| 56 | + builder: (BuildContext ctx) { |
| 57 | + return AlertDialog( |
| 58 | + title: Text("Error while connecting to server".i18n), |
| 59 | + content: Text(e.toString())); |
| 60 | + }); |
| 61 | +} |
0 commit comments