retrievePinSecurely method

Future<String?> retrievePinSecurely({
  1. String? username,
})

Retrieve PIN with biometric protection

Implementation

Future<String?> retrievePinSecurely({String? username}) async {
  try {
    OnairosDebugHelper.log('🔓 Retrieving PIN with biometric protection');

    // Always require biometrics for PIN retrieval
    if (await _isBiometricsAvailable()) {
      final authenticated = await _authenticateWithBiometrics('Access your secure PIN');
      if (!authenticated) {
        throw Exception('Biometric authentication required to access PIN');
      }
    }

    final pinDataJson = await _secureStorage.read(key: 'onairos_pin_secure');
    if (pinDataJson != null) {
      final pinData = jsonDecode(pinDataJson) as Map<String, dynamic>;

      // Check if username matches (if provided)
      if (username != null && pinData['username'] != username) {
        OnairosDebugHelper.log('⚠️ PIN username mismatch');
        return null;
      }

      OnairosDebugHelper.log('✅ PIN retrieved successfully');
      return pinData['pin'] as String;
    }

    OnairosDebugHelper.log('⚠️ No PIN found');
    return null;
  } catch (e) {
    OnairosDebugHelper.log('❌ Error retrieving PIN: $e');
    return null;
  }
}