Skip to content

Commit

Permalink
feat(login): Validate email format
Browse files Browse the repository at this point in the history
  • Loading branch information
asika32764 committed May 25, 2024
1 parent 917af55 commit cc71e39
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/Module/Api/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use function Windwalker\Query\uuid2bin;
use function Windwalker\uid;
use function Windwalker\validate;

#[Controller]
class AuthController
Expand All @@ -40,6 +41,8 @@ public function challenge(

RequestAssert::assert($email, 'No email');

$this->validateEmail($email);

$sessId = uid();
$user = $orm->findOne(User::class, compact('email'));

Expand Down Expand Up @@ -123,10 +126,7 @@ public function authenticate(
RequestAssert::assert($A, 'Invalid credentials');
RequestAssert::assert($M1, 'Invalid credentials');

if (!str_contains($email, '@')) {
// Todo: Use email filters
throw new \RuntimeException('Invalid Email format');
}
$this->validateEmail($email);

$user = $orm->findOne(User::class, compact('email'));

Expand Down Expand Up @@ -201,10 +201,7 @@ public function register(

RequestAssert::assert($email, 'No Email');

if (!str_contains($email, '@')) {
// Todo: Use email filters
throw new \RuntimeException('Invalid Email format');
}
$this->validateEmail($email);

$verifier = BigInteger::fromBase($verifier, 16);
$salt = BigInteger::fromBase($salt, 16);
Expand Down
14 changes: 14 additions & 0 deletions src/Module/Api/Traits/SRPValidationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use Lyrasoft\Luna\Auth\SRP\SRPService;
use Windwalker\Core\Form\Exception\ValidateFailException;
use Windwalker\Filter\Rule\EmailAddress;
use Windwalker\ORM\ORM;
use Windwalker\SRP\Exception\InvalidSessionProofException;

Expand Down Expand Up @@ -94,4 +96,16 @@ protected function srpValidate(
ErrorCode::INVALID_CREDENTIALS->throw();
}
}

protected function isEmail(string $email): bool
{
return (new EmailAddress())->test($email);
}

protected function validateEmail(string $email): void
{
if (!$this->isEmail($email)) {
throw new ValidateFailException('Invalid Email');
}
}
}

0 comments on commit cc71e39

Please sign in to comment.