This project demonstrates the Proxy design pattern for implementing authentication and authorization in a Laravel 11 application. It provides a secure way to control access to sensitive resources by acting as a gatekeeper before delegating requests to the underlying service.
- Proxy Pattern: This project showcases how the Proxy pattern can be used to add an extra layer of security and control to your application. The
AuthProxy
class intercepts requests, performs authentication and authorization checks, and only then allows access to theRealResourceService
if the user has the required permissions. - Separation of Concerns: The Proxy pattern helps to separate the authentication and authorization logic from the business logic of the
RealResourceService
. This makes the code more modular, testable, and maintainable. - Laravel Integration: The project seamlessly integrates with Laravel's authentication features, dependency injection container, and routing system.
App\Services\ResourceService
: Interface defining the contract for retrieving sensitive data.App\Services\RealResourceService
: Concrete implementation ofResourceService
, responsible for fetching sensitive data (currently from the database).App\Services\AuthServiceInterface
: Interface defining the contract for authentication and authorization checks.App\Services\RealAuthService
: Concrete implementation ofAuthServiceInterface
, responsible for authenticating users and checking their permissions against the database.App\Services\AuthProxy
: The Proxy class. It implementsResourceService
, intercepts requests, performs authentication and authorization usingAuthServiceInterface
, and then delegates to theRealResourceService
if authorized.App\Http\Controllers\ProtectedController
: Controller that handles requests for protected resources. It injects theResourceService
interface, allowing it to work with either theAuthProxy
or theRealResourceService
directly (depending on configuration).App\Models\User
: Eloquent model representing a user.App\Models\Permission
: Eloquent model representing a permission, linked to a user.App\Models\SensitiveData
: Eloquent model representing sensitive data, linked to a user.Database\Seeders\DatabaseSeeder
: Seeders to populate the database with users, permissions, and sensitive data.tests\Unit
: Unit tests to verify the behavior of the services and the proxy.
- Request: A user attempts to access the
/protected
route. - Middleware: The
auth
middleware ensures that the user is authenticated. - Controller: The
ProtectedController
is invoked. - Dependency Injection: Laravel's service container injects an instance of
ResourceService
into the controller. Crucially, theAppServiceProvider
binds theResourceService
interface to eitherAuthProxy
orRealResourceService
based on theapp.use_auth_proxy
configuration value. - Proxy Interception (if enabled): If the
AuthProxy
is injected:- The
AuthProxy
checks if the user is authenticated and has the required 'read' permission using theRealAuthService
. - If the user is not authorized, a 403 Forbidden error is thrown.
- If the user is authorized, the
AuthProxy
delegates the request to theRealResourceService
.
- The
- Data Retrieval: The
RealResourceService
fetches the sensitive data from the database. - Response: The controller renders the
ProtectedData
Inertia view, passing the sensitive data to the component.
-
Clone the repository:
git clone <repository_url> cd laravel-auth-proxy
-
Install dependencies:
composer install
-
Copy environment file:
cp .env.example .env
Configure your database settings in the
.env
file. -
Generate application key:
php artisan key:generate
-
Migrate the database:
php artisan migrate
-
Seed the database:
php artisan db:seed
-
Install and compile assets:
npm install npm run dev
-
Start the development server:
php artisan serve
Access the application in your browser (usually at
http://localhost:8000
).
This project includes a docker-compose.yml
file for running the application in a Docker container using Laravel Sail.
-
Ensure you have Docker and Docker Compose installed.
-
Copy environment file:
cp .env.example .env
Configure your database settings in the
.env
file (DB_HOST should bemysql
). -
Start Sail:
./vendor/bin/sail up
Access the application in your browser at
http://localhost
.
-
config/app.php
: Theapp.use_auth_proxy
configuration value controls whether theAuthProxy
is used. Set this value totrue
to enable the proxy, orfalse
to bypass it. You can also set it in your.env
file:USE_AUTH_PROXY=true
The project includes a suite of unit tests to verify the correctness of the implementation.
-
Run all tests:
./vendor/bin/sail artisan test
-
Run only unit tests:
./vendor/bin/sail artisan test --testsuite Unit
- Remember to register or create a user and grant the user
read
permissions using a seeder or tinker. - Always hash passwords when storing them in the database.
- Consider enabling or disabling the AuthProxy to enable or disable security access by updating the
.env
Contributions are welcome! Please submit a pull request with a clear description of your changes.