|
| 1 | +# Authentication & Authorization |
| 2 | + |
| 3 | +PipeFlow includes a minimal authentication layer built on plugs and a user provider. |
| 4 | +The goal is to make it easy to assign a `current_user` and authorize actions without |
| 5 | +locking you into a storage strategy. |
| 6 | + |
| 7 | +## Auth plug (current_user) |
| 8 | + |
| 9 | +The `AuthPlug` reads the user id from the session and assigns it to the connection: |
| 10 | + |
| 11 | +- `assigns['current_user']` |
| 12 | +- `private['current_user']` |
| 13 | + |
| 14 | +Enable it by keeping `AuthPlug` in the global plug list (default). |
| 15 | + |
| 16 | +## User provider |
| 17 | + |
| 18 | +Implement `PipeFlow\Auth\UserProviderInterface` to fetch and validate users. The |
| 19 | +provider is bound into the container, so you can swap in your own implementation: |
| 20 | + |
| 21 | +```php |
| 22 | +app(PipeFlow\Foundation\Container::class)->instance( |
| 23 | + PipeFlow\Auth\UserProviderInterface::class, |
| 24 | + new App\Auth\DbUserProvider() |
| 25 | +); |
| 26 | +``` |
| 27 | + |
| 28 | +Required methods: |
| 29 | +- `findById($id)` |
| 30 | +- `findByCredentials(array $credentials)` |
| 31 | +- `validateCredentials($user, array $credentials)` |
| 32 | +- `getUserId($user)` |
| 33 | + |
| 34 | +## Auth manager |
| 35 | + |
| 36 | +`PipeFlow\Auth\AuthManager` offers convenience APIs: |
| 37 | + |
| 38 | +- `attempt($conn, $credentials, $remember = false)` |
| 39 | +- `login($conn, $user, $remember = false)` |
| 40 | +- `logout($conn)` |
| 41 | +- `userFromSession($conn)` |
| 42 | + |
| 43 | +## Password hashing |
| 44 | + |
| 45 | +Use `PipeFlow\Security\PasswordHasher` for hashing/verifying: |
| 46 | + |
| 47 | +```php |
| 48 | +$hash = app(PipeFlow\Security\PasswordHasher::class)->hash($password); |
| 49 | +$isValid = app(PipeFlow\Security\PasswordHasher::class)->verify($password, $hash); |
| 50 | +``` |
| 51 | + |
| 52 | +## Authorization (gates) |
| 53 | + |
| 54 | +`PipeFlow\Auth\AuthorizationGate` is a lightweight policy registry: |
| 55 | + |
| 56 | +```php |
| 57 | +$gate = app(PipeFlow\Auth\AuthorizationGate::class); |
| 58 | +$gate->define('posts.update', fn($user, $post) => $user->id === $post->user_id); |
| 59 | +``` |
| 60 | + |
| 61 | +In views/templates, use `can()` to check abilities. |
| 62 | + |
| 63 | +## Remember-me (optional) |
| 64 | + |
| 65 | +`AuthManager` can set a remember cookie when `remember=true`. For a production system |
| 66 | +back this with a secure token store and rotation policy. |
| 67 | + |
| 68 | +Configuration: |
| 69 | +- `AUTH_REMEMBER_COOKIE` (default: `pipeflow_remember`) |
0 commit comments