Security

The Security module brings together three complementary subsystems that secure NeoPHP applications:

  • Auth — session or JWT authentication, role management, password hashing.
  • CSRF — protection against Cross-Site Request Forgery attacks via session tokens.
  • Middleware — declarative, attribute-based authorization pipeline for PHP 8, with built-in middlewares and support for custom middlewares.

Module Structure

Security/
├── Auth/
│   ├── AuthManager.php                    Entry point for authentication
│   ├── AuthModule.php
│   ├── JwtManager.php                     JWT HMAC-SHA256 with no dependency
│   ├── PasswordManager.php                bcrypt hashing (cost 12)
│   ├── Collector/  AuthCollector
│   ├── Exception/  AuthException, JwtException
│   ├── Extension/  AuthControllerExtension, AuthViewExtension
│   └── Guard/      SessionGuard, TokenGuard
├── Csrf/
│   ├── CsrfManager.php                    Single token per session
│   ├── CsrfTokenManager.php               Named tokens with expiration
│   ├── CsrfModule.php
│   ├── Exception/  CsrfException
│   ├── Extension/  CsrfViewExtension
│   └── Token/      CsrfToken
└── Middleware/
    ├── MiddlewareManager.php              Pipeline orchestrator
    ├── MiddlewareModule.php
    ├── Interface/  MiddlewareInterface
    ├── Attribute/  Middleware, IsGranted
    ├── Default/    Auth, Guest, IsGranted, Role, Csrf, RateLimit, AuthRateLimit
    ├── Exception/  MiddlewareException
    ├── Extension/  MiddlewareControllerExtension
    └── Commands/   MakeMiddlewareCommand

Documentation by Component

ComponentDescriptionREADME
AuthSession/JWT, roles, bcrypt, JwtManagerAuth/README.md
CsrfSession token, named tokens, Twig csrf_token()Csrf/README.md
MiddlewareAttribute pipeline, built-in and custom middlewaresMiddleware/README.md

Controller Extensions

MethodComponent
auth()Auth — access to AuthManager
getPasswordManager()Auth — access to PasswordManager
getMiddleware()Middleware — access to MiddlewareManager

Twig Extensions

FunctionComponentDescription
auth_check()Authtrue if the user is logged in
auth_user()AuthCurrent user object
auth_has_role(role)Authtrue if the user has the given role
csrf_token(id?)CsrfCSRF token for forms