getOtp method

Future<String> getOtp()

Get the current OTP (One-Time Passcode) for enrollment requests

Implementation

Future<String> getOtp() async {
  if (!_initialized) {
    throw Exception('Manager not initialized. Call initialize() first.');
  }

  try {
    _logger.info('Fetching OTP for $atSign');

    // Use the otp:get command as shown in auth_cli.dart
    final otpCommand = 'otp:get\n';

    String? response = await _atClient.getRemoteSecondary()!.executeCommand(otpCommand, auth: true);

    if (response == null) {
      throw Exception('No response received from OTP command');
    }

    // Parse the response - typically returns "data:<otp>"
    if (response.startsWith('data:')) {
      response = response.replaceFirst(RegExp(r'^data:'), '');
    }

    _logger.info('Successfully retrieved OTP');
    return response.trim();
  } catch (e, s) {
    _logger.severe('Failed to get OTP: $e', e, s);
    rethrow;
  }
}