3
0
Fork 0
trifid_mobile/lib/screens/siteConfig/CertificateDetailsScreen.dart

171 lines
6.0 KiB
Dart
Raw Permalink Normal View History

2020-07-27 20:43:58 +00:00
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
2021-04-29 21:39:02 +00:00
import 'package:mobile_nebula/components/FormPage.dart';
import 'package:mobile_nebula/components/SpecialSelectableText.dart';
2020-07-27 20:43:58 +00:00
import 'package:mobile_nebula/components/config/ConfigItem.dart';
import 'package:mobile_nebula/components/config/ConfigSection.dart';
import 'package:mobile_nebula/models/Certificate.dart';
import 'package:mobile_nebula/services/utils.dart';
2021-04-29 21:39:02 +00:00
//TODO: add a primary toggle if we have multiple sites
2020-07-27 20:43:58 +00:00
/// Displays the details of a CertificateInfo object. Respects incomplete objects (missing validity or rawCert)
class CertificateDetailsScreen extends StatefulWidget {
2021-04-29 21:39:02 +00:00
const CertificateDetailsScreen(this.certInfo, {Key key, this.onDelete, this.onSave, this.newCert = false, this.choosePrimary = false}) : super(key: key);
2020-07-27 20:43:58 +00:00
2021-04-29 21:39:02 +00:00
final CertificateInfo certInfo;
2020-07-27 20:43:58 +00:00
final Function onDelete;
2021-04-29 21:39:02 +00:00
final ValueChanged<bool> onSave;
final bool newCert;
final bool choosePrimary;
2020-07-27 20:43:58 +00:00
@override
_CertificateDetailsScreenState createState() => _CertificateDetailsScreenState();
}
class _CertificateDetailsScreenState extends State<CertificateDetailsScreen> {
2021-04-29 21:39:02 +00:00
bool primary;
bool changed = false;
@override
void initState() {
primary = widget.certInfo.primary ?? false;
super.initState();
}
2020-07-27 20:43:58 +00:00
@override
Widget build(BuildContext context) {
2021-04-29 21:39:02 +00:00
return FormPage(
2020-07-27 20:43:58 +00:00
title: 'Certificate Details',
2021-04-29 21:39:02 +00:00
onSave: () {
Navigator.pop(context);
widget.onSave(widget.choosePrimary == true ? primary : false);
},
changed: widget.newCert || changed,
2020-07-27 20:43:58 +00:00
child: Column(children: [
2021-04-29 21:39:02 +00:00
widget.choosePrimary ? _buildPrimaryChooser() : Container(),
2020-07-27 20:43:58 +00:00
_buildID(),
_buildFilters(),
_buildValid(),
_buildAdvanced(),
_buildDelete(),
]),
);
}
2021-04-29 21:39:02 +00:00
Widget _buildPrimaryChooser() {
return ConfigSection(children: <Widget>[
ConfigItem(
label: Text('Primary Certificate'),
content: Container(
alignment: Alignment.centerRight,
child: Switch.adaptive(
value: primary,
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
onChanged: (v) {
setState(() {
changed = true;
primary = v;
});
})),
)]);
}
2020-07-27 20:43:58 +00:00
Widget _buildID() {
return ConfigSection(children: <Widget>[
2021-04-29 21:39:02 +00:00
ConfigItem(label: Text('Name'), content: SpecialSelectableText(widget.certInfo.cert.details.name)),
2020-07-27 20:43:58 +00:00
ConfigItem(
label: Text('Type'),
2021-04-29 21:39:02 +00:00
content: Text(widget.certInfo.cert.details.isCa ? 'CA certificate' : 'Client certificate')),
2020-07-27 20:43:58 +00:00
]);
}
Widget _buildValid() {
var valid = Text('yes');
2021-04-29 21:39:02 +00:00
if (widget.certInfo.validity != null && !widget.certInfo.validity.valid) {
valid = Text(widget.certInfo.validity.valid ? 'yes' : widget.certInfo.validity.reason,
2020-07-27 20:43:58 +00:00
style: TextStyle(color: CupertinoColors.systemRed.resolveFrom(context)));
}
return ConfigSection(
label: 'VALIDITY',
children: <Widget>[
ConfigItem(label: Text('Valid?'), content: valid),
ConfigItem(
label: Text('Created'),
2021-04-29 21:39:02 +00:00
content: SpecialSelectableText(widget.certInfo.cert.details.notBefore.toLocal().toString())),
2020-07-27 20:43:58 +00:00
ConfigItem(
label: Text('Expires'),
2021-04-29 21:39:02 +00:00
content: SpecialSelectableText(widget.certInfo.cert.details.notAfter.toLocal().toString())),
2020-07-27 20:43:58 +00:00
],
);
}
Widget _buildFilters() {
List<Widget> items = [];
2021-04-29 21:39:02 +00:00
if (widget.certInfo.cert.details.groups.length > 0) {
2020-07-27 20:43:58 +00:00
items.add(ConfigItem(
2021-04-29 21:39:02 +00:00
label: Text('Groups'), content: SpecialSelectableText(widget.certInfo.cert.details.groups.join(', '))));
2020-07-27 20:43:58 +00:00
}
2021-04-29 21:39:02 +00:00
if (widget.certInfo.cert.details.ips.length > 0) {
2020-07-27 20:43:58 +00:00
items
2021-04-29 21:39:02 +00:00
.add(ConfigItem(label: Text('IPs'), content: SpecialSelectableText(widget.certInfo.cert.details.ips.join(', '))));
2020-07-27 20:43:58 +00:00
}
2021-04-29 21:39:02 +00:00
if (widget.certInfo.cert.details.subnets.length > 0) {
2020-07-27 20:43:58 +00:00
items.add(ConfigItem(
2021-04-29 21:39:02 +00:00
label: Text('Subnets'), content: SpecialSelectableText(widget.certInfo.cert.details.subnets.join(', '))));
2020-07-27 20:43:58 +00:00
}
return items.length > 0
2021-04-29 21:39:02 +00:00
? ConfigSection(label: widget.certInfo.cert.details.isCa ? 'FILTERS' : 'DETAILS', children: items)
2020-07-27 20:43:58 +00:00
: Container();
}
Widget _buildAdvanced() {
return ConfigSection(
children: <Widget>[
ConfigItem(
label: Text('Fingerprint'),
2021-04-29 21:39:02 +00:00
content: SpecialSelectableText(widget.certInfo.cert.fingerprint,
2020-07-27 20:43:58 +00:00
style: TextStyle(fontFamily: 'RobotoMono', fontSize: 14)),
crossAxisAlignment: CrossAxisAlignment.start),
ConfigItem(
label: Text('Public Key'),
2021-04-29 21:39:02 +00:00
content: SpecialSelectableText(widget.certInfo.cert.details.publicKey,
2020-07-27 20:43:58 +00:00
style: TextStyle(fontFamily: 'RobotoMono', fontSize: 14)),
crossAxisAlignment: CrossAxisAlignment.start),
2021-04-29 21:39:02 +00:00
widget.certInfo.rawCert != null
2020-07-27 20:43:58 +00:00
? ConfigItem(
label: Text('PEM Format'),
2021-04-29 21:39:02 +00:00
content: SpecialSelectableText(widget.certInfo.rawCert,
2020-07-27 20:43:58 +00:00
style: TextStyle(fontFamily: 'RobotoMono', fontSize: 14)),
crossAxisAlignment: CrossAxisAlignment.start)
: Container(),
],
);
}
Widget _buildDelete() {
if (widget.onDelete == null) {
return Container();
}
2021-04-29 21:39:02 +00:00
var title = widget.certInfo.cert.details.isCa ? 'Delete CA?' : 'Delete cert?';
2020-07-27 20:43:58 +00:00
return Padding(
padding: EdgeInsets.only(top: 50, bottom: 10, left: 10, right: 10),
child: SizedBox(
width: double.infinity,
child: PlatformButton(
child: Text('Delete'),
color: CupertinoColors.systemRed.resolveFrom(context),
onPressed: () => Utils.confirmDelete(context, title, () async {
Navigator.pop(context);
widget.onDelete();
}))));
}
}