forked from apiato/apiato
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP - move email verification functionality to middleware - only impl…
…emented for API
- Loading branch information
1 parent
3ab026a
commit 24ccf5b
Showing
14 changed files
with
162 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
app/Containers/AppSection/Authentication/Exceptions/EmailNotVerifiedException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace App\Containers\AppSection\Authentication\Exceptions; | ||
|
||
use App\Ship\Parents\Exceptions\Exception; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class EmailNotVerifiedException extends Exception | ||
{ | ||
protected $code = Response::HTTP_FORBIDDEN; | ||
protected $message = 'Your email address is not verified.'; | ||
} |
12 changes: 0 additions & 12 deletions
12
app/Containers/AppSection/Authentication/Exceptions/UserNotConfirmedException.php
This file was deleted.
Oops, something went wrong.
61 changes: 61 additions & 0 deletions
61
app/Containers/AppSection/Authentication/Middlewares/EnsureEmailIsVerified.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace App\Containers\AppSection\Authentication\Middlewares; | ||
|
||
use App\Containers\AppSection\Authentication\Exceptions\EmailNotVerifiedException; | ||
use App\Ship\Parents\Middlewares\Middleware as ParentMiddleware; | ||
use Closure; | ||
use Illuminate\Http\Request; | ||
|
||
class EnsureEmailIsVerified extends ParentMiddleware | ||
{ | ||
/** | ||
* Exclude these routes from authentication check. | ||
* | ||
* Note: `$request->is('api/fragment*')` https://laravel.com/docs/7.x/requests | ||
* | ||
* @var array | ||
*/ | ||
protected array $except = [ | ||
'v1/oauth/token', | ||
'v1/clients/web/login', | ||
]; | ||
|
||
/** | ||
* Handle an incoming request. | ||
* | ||
* @param Request $request | ||
* @param Closure $next | ||
* @param null $redirectToRoute | ||
* @return mixed | ||
* @throws EmailNotVerifiedException | ||
*/ | ||
public function handle(Request $request, Closure $next, $redirectToRoute = null): mixed | ||
{ | ||
if (!$this->emailVerificationRequired() || !$request->user()) { | ||
return $next($request); | ||
} | ||
|
||
foreach ($this->except as $excludedRoute) { | ||
if ($request->path() === $excludedRoute) { | ||
return $next($request); | ||
} | ||
} | ||
|
||
if (!$this->isEmailVerified($request->user())) { | ||
throw new EmailNotVerifiedException(); | ||
} | ||
|
||
return $next($request); | ||
} | ||
|
||
private function emailVerificationRequired() | ||
{ | ||
return config('appSection-authentication.require_email_verification'); | ||
} | ||
|
||
private function isEmailVerified($user): bool | ||
{ | ||
return !is_null($user->email_verified_at); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 0 additions & 28 deletions
28
app/Containers/AppSection/Authentication/Tasks/CheckIfUserEmailIsConfirmedTask.php
This file was deleted.
Oops, something went wrong.
58 changes: 58 additions & 0 deletions
58
app/Containers/AppSection/Authentication/Tests/Unit/EnsureEmailIsVerifiedMiddlewareTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace App\Containers\AppSection\Authentication\Tests\Unit; | ||
|
||
use App\Containers\AppSection\Authentication\Exceptions\EmailNotVerifiedException; | ||
use App\Containers\AppSection\Authentication\Middlewares\EnsureEmailIsVerified; | ||
use App\Containers\AppSection\Authentication\Tests\TestCase; | ||
use Illuminate\Http\Request; | ||
|
||
/** | ||
* Class EnsureEmailIsVerifiedMiddlewareTest. | ||
* | ||
* @group authentication | ||
* @group unit | ||
*/ | ||
class EnsureEmailIsVerifiedMiddlewareTest extends TestCase | ||
{ | ||
private Request $request; | ||
private EnsureEmailIsVerified $middleware; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
config(['appSection-authentication.require_email_verification' => true]); | ||
$this->request = Request::create('/user/profile'); | ||
$this->middleware = new EnsureEmailIsVerified(); | ||
} | ||
|
||
public function testIfEmailVerificationIsDisabled_ShouldSkipProcessing(): void | ||
{ | ||
config(['appSection-authentication.require_email_verification' => false]); | ||
|
||
$this->middleware->handle($this->request, function ($req) { | ||
$this->assertInstanceOf(Request::class, $req); | ||
}); | ||
} | ||
|
||
public function testIfUserNotAuthenticated_ShouldSkipProcessing(): void | ||
{ | ||
$this->middleware->handle($this->request, function ($req) { | ||
$this->assertInstanceOf(Request::class, $req); | ||
}); | ||
} | ||
|
||
public function testAPI_IfEmailVerificationIsRequired_GivenEmailNotVerified_ShouldThrowException(): void | ||
{ | ||
$this->expectException(EmailNotVerifiedException::class); | ||
|
||
$user = $this->getTestingUser(['email_verified_at' => null]); | ||
$this->request->merge(['user' => $user]); | ||
$this->request->headers->set('Accept', 'application/json'); | ||
$this->request->setUserResolver(fn () => $user); | ||
|
||
$this->middleware->handle($this->request, static function () { | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.