feat: API auth with custom headers

This commit is contained in:
cogwheel
2025-08-16 15:51:27 +05:30
parent 37dece4263
commit b33069fdea
21 changed files with 1854 additions and 736 deletions

View File

@@ -27,10 +27,10 @@ class InputValidationService {
return null;
}
/// Validate URL
/// Validate URL (enhanced version for server addresses)
static String? validateUrl(String? value, {bool required = true}) {
if (value == null || value.isEmpty) {
return required ? 'URL is required' : null;
return required ? 'Server address is required' : null;
}
final trimmed = value.trim();
@@ -38,21 +38,58 @@ class InputValidationService {
// Add protocol if missing
String urlToValidate = trimmed;
if (!trimmed.startsWith('http://') && !trimmed.startsWith('https://')) {
urlToValidate = 'https://$trimmed';
urlToValidate = 'http://$trimmed';
}
try {
final uri = Uri.parse(urlToValidate);
if (!uri.hasScheme || !uri.hasAuthority) {
return 'Please enter a valid URL';
// Validate scheme
if (!uri.hasScheme || (uri.scheme != 'http' && uri.scheme != 'https')) {
return 'Use http:// or https:// only';
}
// Validate host
if (!uri.hasAuthority || uri.host.isEmpty) {
return 'Please enter a server address (e.g., 192.168.1.10:3000)';
}
// Validate port if specified
if (uri.hasPort) {
if (uri.port < 1 || uri.port > 65535) {
return 'Port must be between 1 and 65535';
}
}
// Validate IP address format if it looks like an IP
if (_isIPAddress(uri.host) && !_isValidIPAddress(uri.host)) {
return 'Invalid IP address format (use 192.168.1.10)';
}
} catch (e) {
return 'Please enter a valid URL';
return 'Invalid server address format';
}
return null;
}
/// Check if a string looks like an IP address
static bool _isIPAddress(String host) {
return RegExp(r'^\d+\.\d+\.\d+\.\d+$').hasMatch(host);
}
/// Validate IP address format
static bool _isValidIPAddress(String ip) {
final parts = ip.split('.');
if (parts.length != 4) return false;
for (final part in parts) {
final num = int.tryParse(part);
if (num == null || num < 0 || num > 255) return false;
}
return true;
}
/// Validate password strength
static String? validatePassword(String? value, {bool checkStrength = true}) {
if (value == null || value.isEmpty) {