handleAuth method

Future<void> handleAuth(
  1. String argument,
  2. FtpSession session
)

AUTH: Authenticate / negotiate TLS (RFC 4217 §4, RFC 2228).

Implementation

Future<void> handleAuth(String argument, FtpSession session) async {
  // Mode none: TLS not supported
  if (session.securityMode == FtpSecurityMode.none) {
    session.sendResponse('504 Security mechanism not understood');
    return;
  }
  // Implicit mode: AUTH refused by policy (connection is already TLS)
  if (session.securityMode == FtpSecurityMode.implicit) {
    session.sendResponse('534 AUTH not available on implicit TLS connection');
    return;
  }
  // Already upgraded: policy refusal (RFC 2228 §3: 534 for policy denial)
  if (session.tlsActive) {
    session.sendResponse('534 TLS already active');
    return;
  }
  // Validate mechanism
  final mechanism = argument.toUpperCase();
  if (mechanism != 'TLS' && mechanism != 'TLS-C') {
    session.sendResponse('504 Security mechanism not understood');
    return;
  }

  session.sendResponse('234 Proceed with TLS negotiation');

  try {
    await session.upgradeToTls();
    // RFC 4217 §4: reset transfer parameters after AUTH
    session.reinitialize();
    session.tlsActive = true;
  } catch (e) {
    logger.generalLog('TLS upgrade failed: $e');
    session.closeConnection();
  }
}