feat: keyboard-interactive auth (#349)

This commit is contained in:
lollipopkit
2024-05-07 15:22:31 +08:00
parent 026e414388
commit d0523c1e54
15 changed files with 137 additions and 69 deletions

View File

@@ -61,13 +61,16 @@ extension DialogX on BuildContext {
static final _recoredPwd = <String, String>{};
/// Show a dialog to input password
///
/// [hostId] set it to null to skip remembering the password
Future<String?> showPwdDialog({
String? user,
required String hostId,
String? hostId,
String? title,
}) async {
if (!mounted) return null;
return await showRoundDialog<String>(
title: Text(user ?? l10n.pwd),
title: Text(title ?? hostId ?? l10n.pwd),
child: Input(
controller: TextEditingController(text: _recoredPwd[hostId]),
autoFocus: true,
@@ -75,7 +78,7 @@ extension DialogX on BuildContext {
obscureText: true,
onSubmitted: (val) {
pop(val);
if (Stores.setting.rememberPwdInMem.fetch()) {
if (hostId != null && Stores.setting.rememberPwdInMem.fetch()) {
_recoredPwd[hostId] = val;
}
},

View File

@@ -81,9 +81,9 @@ extension SSHClientX on SSHClient {
isRequestingPwd = true;
final user = Miscs.pwdRequestWithUserReg.firstMatch(data)?.group(1);
if (context == null) return;
final pwd = await context.showPwdDialog(user: user, hostId: id);
final pwd = await context.showPwdDialog(title: user, hostId: id);
if (pwd == null || pwd.isEmpty) {
session.kill(SSHSignal.INT);
session.kill(SSHSignal.TERM);
} else {
session.stdin.add('$pwd\n'.uint8List);
}

20
lib/core/utils/auth.dart Normal file
View File

@@ -0,0 +1,20 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:toolbox/core/extension/context/dialog.dart';
import 'package:toolbox/data/model/server/server_private_info.dart';
import 'package:toolbox/data/res/provider.dart';
abstract final class KeybordInteractive {
static FutureOr<List<String>?> defaultHandle(
ServerPrivateInfo spi, {
BuildContext? ctx,
}) async {
try {
final res = await (ctx ?? Pros.app.ctx)?.showPwdDialog(title: spi.id);
return res == null ? null : [res];
} catch (e) {
return null;
}
}
}

View File

@@ -56,6 +56,9 @@ Future<SSHClient> genClient(
///
/// Must pass this param when use multi-thread and key login
ServerPrivateInfo? jumpSpi,
/// Handle keyboard-interactive authentication
FutureOr<List<String>?> Function(SSHUserInfoRequest)? onKeyboardInteractive,
}) async {
onStatus?.call(GenSSHClientStatus.socket);
@@ -109,6 +112,9 @@ Future<SSHClient> genClient(
socket,
username: spi.user,
onPasswordRequest: () => spi.pwd,
onUserInfoRequest: onKeyboardInteractive,
printDebug: debugPrint,
printTrace: debugPrint,
);
}
privateKey ??= getPrivateKey(keyId);
@@ -119,5 +125,8 @@ Future<SSHClient> genClient(
username: spi.user,
// Must use [compute] here, instead of [Computer.shared.start]
identities: await compute(loadIndentity, privateKey),
onUserInfoRequest: onKeyboardInteractive,
printDebug: debugPrint,
printTrace: debugPrint,
);
}