Core Concepts
The security layer consists of four main pillars:- Identities: Objects representing the current requester (e.g., a logged-in user or a guest).
- Scopes and Permissions: Granular strings that define what an identity is allowed to do.
- Authenticators: Services that turn credentials (like a JWT) into an Identity.
- Middleware: The glue that executes authentication and enforces authorization on routes.
The Security Flow
- Request Arrival: An HTTP request enters the application.
- Authentication: An authentication middleware (like
AuthenticationMiddleware) uses anAuthenticatorto identify the requester and sets anAuthIdentityon the request. - Authorization: The
SecurityMiddlewarecompares theAuthIdentity’s granted scopes and permissions against theRouteAuthorizationdefined in theRoute. - Controller: If authorized, the controller receives the request and can access the identity via
$request->getAuthIdentity().
Route Security Levels
Apivalk supports three levels of route security. The level is determined by whether aRouteAuthorization is present and what scopes/permissions it requires.
1. Public Route (No Authentication)
A route without aRouteAuthorization is fully public. Anyone can access it, including anonymous users. The SecurityMiddleware passes the request through without any checks.
2. Authenticated Route (No Specific Scopes)
A route that requires the user to be authenticated but does not require any specific scopes or permissions. This is useful for endpoints like “get my profile” where you just need to know the user is logged in. Pass aRouteAuthorization with only the security scheme name and omit (or pass empty arrays for) scopes and permissions. The SecurityMiddleware will still enforce that the user is authenticated (not a GuestAuthIdentity), returning a 401 Unauthorized for anonymous requests.
3. Scoped Route (Specific Scopes and/or Permissions)
A route that requires the user to be authenticated and possess specific scopes and/or permissions. TheSecurityMiddleware checks each required scope and permission against the user’s identity:
- Missing scope/permission + authenticated user =
403 Forbidden - Missing scope/permission + anonymous user =
401 Unauthorized
BearerAuth with the admin:settings scope and admin:settings:read permission.
Connection to OpenAPI
The first argument ofRouteAuthorization is the security scheme name. This name must match the name parameter of a SecuritySchemeObject in your ComponentsObject. This is how Apivalk connects a route to its security definition in the generated OpenAPI spec, and how Swagger UI knows to show the “Authorize” button for that endpoint.
Keep in mind: Future Auto-Discovery. Apivalk is moving towards full auto-discovery of security schemes. In the future, it will be able to automatically detect and document your security configuration (including names and versions) directly from your middleware and authenticators.See the OpenAPI Generator documentation for full examples including JWT Bearer, API Key, and OAuth2 setups.
Examples: Getting Started with Middleware
To use the security system, you typically need to register two middlewares in yourMiddlewareStack.