Add webfinger validation rule

staging
Daniel Supernault 2025-04-14 04:51:30 -06:00
rodzic 5a32bfe304
commit 1356757715
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 23740873EE6F76A1
1 zmienionych plików z 62 dodań i 0 usunięć

Wyświetl plik

@ -0,0 +1,62 @@
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class WebFinger implements Rule
{
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
if (! is_string($value)) {
return false;
}
$mention = $value;
if (str_starts_with($mention, '@')) {
$mention = substr($mention, 1);
}
$parts = explode('@', $mention);
if (count($parts) !== 2) {
return false;
}
[$username, $domain] = $parts;
if (empty($username) ||
! preg_match('/^[a-zA-Z0-9_.-]+$/', $username) ||
strlen($username) >= 80) {
return false;
}
if (empty($domain) ||
! str_contains($domain, '.') ||
! preg_match('/^[a-zA-Z0-9.-]+$/', $domain) ||
strlen($domain) >= 255) {
return false;
}
// Optional: Check if domain resolves (can be enabled for stricter validation)
// return checkdnsrr($domain, 'A') || checkdnsrr($domain, 'AAAA') || checkdnsrr($domain, 'MX');
return true;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The :attribute must be a valid WebFinger address (username@domain.tld or @username@domain.tld)';
}
}