Updated key management to match current version
All checks were successful
/ mirror (push) Successful in 4s
/ build-stealth (push) Successful in 9m11s
/ build (push) Successful in 9m13s

This commit is contained in:
AlexisDanlos 2025-03-31 00:50:35 +02:00
parent 664dd4bb38
commit cef7a27e88

View File

@ -1,145 +1,144 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import '../../../../domain/services/cryptography/asymmetric_crypto_service.dart'; import 'package:dialer/services/cryptography/asymmetric_crypto_service.dart';
import 'package:provider/provider.dart';
/// Cryptographic key management page
class ManageKeysPage extends StatefulWidget { class ManageKeysPage extends StatefulWidget {
const ManageKeysPage({Key? key}) : super(key: key);
@override @override
_ManageKeysPageState createState() => _ManageKeysPageState(); _ManageKeysPageState createState() => _ManageKeysPageState();
} }
class _ManageKeysPageState extends State<ManageKeysPage> { class _ManageKeysPageState extends State<ManageKeysPage> {
String? publicKey; final AsymmetricCryptoService _cryptoService = AsymmetricCryptoService();
List<Map<String, dynamic>> _keys = [];
bool _isLoading = false; bool _isLoading = false;
bool _hasError = false;
String _errorMessage = '';
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_loadPublicKey(); _loadKeys();
} }
Future<void> _loadPublicKey() async { Future<void> _loadKeys() async {
setState(() { setState(() {
_isLoading = true; _isLoading = true;
_hasError = false;
}); });
try { try {
final cryptoService = context.read<AsymmetricCryptoService>(); List<Map<String, dynamic>> keys = await _cryptoService.getAllKeys();
final key = await cryptoService.getPublicKeyPem(); setState(() {
_keys = keys;
if (mounted) { });
setState(() {
publicKey = key;
_isLoading = false;
});
}
} catch (e) { } catch (e) {
if (mounted) { ScaffoldMessenger.of(context)
setState(() { .showSnackBar(SnackBar(content: Text('Error loading keys: $e')));
_hasError = true; } finally {
_errorMessage = 'Failed to load public key: $e'; setState(() {
_isLoading = false; _isLoading = false;
}); });
}
} }
} }
Future<void> _regenerateKeyPair() async { Future<void> _generateKey() async {
setState(() { setState(() {
_isLoading = true; _isLoading = true;
_hasError = false;
}); });
try { try {
final cryptoService = context.read<AsymmetricCryptoService>(); await _cryptoService.generateKeyPair();
await cryptoService.generateAndStoreKeyPair(); await _loadKeys();
await _loadPublicKey(); ScaffoldMessenger.of(context)
.showSnackBar(const SnackBar(content: Text('Key generated successfully')));
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('New key pair generated!')),
);
}
} catch (e) { } catch (e) {
if (mounted) { ScaffoldMessenger.of(context)
setState(() { .showSnackBar(SnackBar(content: Text('Error generating key: $e')));
_hasError = true; } finally {
_errorMessage = 'Failed to generate key pair: $e'; setState(() {
_isLoading = false; _isLoading = false;
}); });
}
ScaffoldMessenger.of(context).showSnackBar( }
SnackBar(content: Text('Error generating key pair: $e')),
); Future<void> _deleteKey(String alias) async {
} setState(() {
_isLoading = true;
});
try {
await _cryptoService.deleteKeyPair(alias);
await _loadKeys();
ScaffoldMessenger.of(context)
.showSnackBar(const SnackBar(content: Text('Key deleted successfully')));
} catch (e) {
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text('Error deleting key: $e')));
} finally {
setState(() {
_isLoading = false;
});
}
}
Future<void> _viewPublicKey(String alias) async {
try {
final publicKey = await _cryptoService.getPublicKey(alias);
showDialog(
context: context,
builder: (_) => AlertDialog(
title: const Text('Public Key'),
content: SingleChildScrollView(child: Text(publicKey)),
actions: [
TextButton(
child: const Text('Close'),
onPressed: () {
Navigator.pop(context);
},
)
],
),
);
} catch (e) {
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text('Error retrieving public key: $e')));
} }
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
backgroundColor: Colors.black,
appBar: AppBar( appBar: AppBar(
title: const Text('Key Management'), title: const Text('Manage Keys'),
), ),
body: Padding( body: _isLoading
padding: const EdgeInsets.all(16.0), ? const Center(child: CircularProgressIndicator())
child: _isLoading : _keys.isEmpty
? const Center(child: CircularProgressIndicator()) ? const Center(child: Text('No keys found'))
: Column( : ListView.builder(
crossAxisAlignment: CrossAxisAlignment.start, itemCount: _keys.length,
children: [ itemBuilder: (context, index) {
const Text( final keyData = _keys[index];
'Your Public Key', return ListTile(
style: TextStyle( title: Text(keyData['label'] ?? 'No label'),
color: Colors.white, subtitle: Text(keyData['alias'] ?? ''),
fontSize: 18, trailing: Row(
fontWeight: FontWeight.bold, mainAxisSize: MainAxisSize.min,
), children: [
), IconButton(
const SizedBox(height: 8), icon: const Icon(Icons.visibility),
Container( tooltip: 'View Public Key',
width: double.infinity, onPressed: () => _viewPublicKey(keyData['alias']),
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Colors.grey[900],
borderRadius: BorderRadius.circular(8),
),
child: _hasError
? Text(
_errorMessage,
style: const TextStyle(
color: Colors.red,
fontFamily: 'monospace',
fontSize: 12,
),
)
: Text(
publicKey ?? 'No key available',
style: const TextStyle(
color: Colors.white70,
fontFamily: 'monospace',
fontSize: 12,
),
), ),
), IconButton(
const SizedBox(height: 24), icon: const Icon(Icons.delete),
ElevatedButton( tooltip: 'Delete Key',
onPressed: _regenerateKeyPair, onPressed: () => _deleteKey(keyData['alias']),
child: const Text('Generate New Key Pair'), ),
), ],
if (_hasError) ...[ ),
const SizedBox(height: 16), );
const Text( },
'Note: Encryption features may be limited due to error.', ),
style: TextStyle(color: Colors.yellow), floatingActionButton: FloatingActionButton(
), onPressed: _generateKey,
], child: const Icon(Icons.add),
], tooltip: 'Generate New Key',
),
), ),
); );
} }