Skip to content

Commit 67d9956

Browse files
Catch decryption errors during connection saving
The `_showPassphraseDialog` method now returns a boolean indicating whether the dialog was closed without saving. The `_saveAndTestConnection` method now catches `SSHKeyDecryptError` when parsing the private key. If this error occurs, it calls `_showPassphraseDialog` to prompt the user for the passphrase. This ensures that if an encrypted key is provided and the user cancels the passphrase dialog, the connection is not saved.
1 parent 77646a1 commit 67d9956

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

lib/presentation/screens/ssh_manager/add_connection_form.dart

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ class _AddConnectionFormState extends ConsumerState<AddConnectionForm> {
126126
on SSHKeyDecryptError {
127127
// Key is encrypted, show passphrase dialog
128128
await _showPassphraseDialog(keyContent);
129-
} catch (e) {
129+
}
130+
catch (e) {
130131
setState(() {
131132
_errorMessage = 'Error parsing private key: ${e.toString()}';
132133
privateKeyController.text = '';
@@ -135,13 +136,13 @@ class _AddConnectionFormState extends ConsumerState<AddConnectionForm> {
135136
}
136137

137138
// Passphrase dialog to decrypt the private key
138-
Future<void> _showPassphraseDialog(String encryptedKey) async {
139+
Future<bool> _showPassphraseDialog(String encryptedKey) async {
139140
final TextEditingController passphraseController = TextEditingController();
140141
bool isDecrypting = false;
141142
String? dialogError;
142143
bool isPassphraseVisible = false;
143144

144-
await showDialog<void>(
145+
await showDialog<bool>(
145146
context: context,
146147
barrierDismissible: false,
147148

@@ -242,7 +243,6 @@ class _AddConnectionFormState extends ConsumerState<AddConnectionForm> {
242243
try {
243244
// Try to decrypt the key with the provided passphrase
244245
final keyPair = SSHKeyPair.fromPem(encryptedKey, passphraseController.text);
245-
debugPrint('Decrypted key: ${keyPair.first.toPem()}');
246246

247247
setState(() {
248248
privateKeyController.text = keyPair.first.toPem();
@@ -287,6 +287,7 @@ class _AddConnectionFormState extends ConsumerState<AddConnectionForm> {
287287
);
288288
},
289289
);
290+
return false; // Return false to indicate dialog closed without saving
290291
}
291292

292293
bool _validatePrivateKey(String key) {
@@ -404,11 +405,10 @@ class _AddConnectionFormState extends ConsumerState<AddConnectionForm> {
404405
return false;
405406
}
406407
on SSHKeyDecryptError {
407-
setState(() {
408-
_errorMessage = 'Private key is encrypted. Please select the key file again and provide the passphrase.';
409-
});
410-
return false;
411-
} catch (e) {
408+
// Key is encrypted, show passphrase dialog
409+
return await _showPassphraseDialog(privateKeyController.text);
410+
}
411+
catch (e) {
412412
setState(() {
413413
if (e.toString().contains('algorithm negotiation fail')) {
414414
_errorMessage = 'Failed to negotiate SSH algorithms. The server may use incompatible settings.';
@@ -537,7 +537,8 @@ class _AddConnectionFormState extends ConsumerState<AddConnectionForm> {
537537
_errorMessage = 'Error: ${e.toString()}';
538538
}
539539
});
540-
} finally {
540+
}
541+
finally {
541542
setState(() {
542543
_isTesting = false;
543544
_isSaving = false;

0 commit comments

Comments
 (0)