Backend: - CookieBearerToken middleware reads httpOnly cookie and injects Authorization header before Sanctum validates (prepended to API middleware group) - SetAuthCookie trait provides cookie creation/expiry helpers with per-app cookie names (crewli_admin_token, crewli_app_token, crewli_portal_token) - LoginController sets token via Set-Cookie, removes it from JSON body - LogoutController expires the auth cookie on logout - AuthRefreshController (POST /auth/refresh) rotates tokens with new cookie - InvitationController accept also sets token via cookie, not JSON body - All cookies: httpOnly, SameSite=Strict, Secure (in production) Frontend (all three SPAs): - Removed all localStorage token storage (apps/app, apps/portal) - Removed all JS-readable cookie token storage (apps/admin) - Removed Authorization: Bearer header interceptors from axios - Auth stores now rely on GET /auth/me to validate httpOnly cookie - Admin app: new Pinia auth store replaces useCookie-based auth pattern - withCredentials: true ensures browser sends cookies automatically Fixes security findings A13-1 (localStorage tokens) and A13-2 (admin cookie flags). Tokens are now invisible to JavaScript. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
114 lines
4.3 KiB
PHP
114 lines
4.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use Illuminate\Auth\Access\AuthorizationException;
|
|
use Illuminate\Auth\AuthenticationException;
|
|
use Illuminate\Database\QueryException;
|
|
use Illuminate\Foundation\Application;
|
|
use Illuminate\Foundation\Configuration\Exceptions;
|
|
use Illuminate\Foundation\Configuration\Middleware;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
return Application::configure(basePath: dirname(__DIR__))
|
|
->withRouting(
|
|
web: __DIR__.'/../routes/web.php',
|
|
api: __DIR__.'/../routes/api.php',
|
|
commands: __DIR__.'/../routes/console.php',
|
|
health: '/up',
|
|
apiPrefix: 'api/v1',
|
|
)
|
|
->withMiddleware(function (Middleware $middleware): void {
|
|
// API uses token-based auth, no CSRF needed
|
|
|
|
$middleware->append(\App\Http\Middleware\SecurityHeaders::class);
|
|
|
|
// Read httpOnly auth cookie and inject as Authorization header (before Sanctum)
|
|
$middleware->api(prepend: [
|
|
\App\Http\Middleware\CookieBearerToken::class,
|
|
]);
|
|
|
|
$middleware->alias([
|
|
'portal.token' => \App\Http\Middleware\PortalTokenMiddleware::class,
|
|
]);
|
|
})
|
|
->withExceptions(function (Exceptions $exceptions): void {
|
|
// Database connection / query errors → 503
|
|
$exceptions->render(function (QueryException|PDOException $e, Request $request) {
|
|
if ($request->expectsJson() || $request->is('api/*')) {
|
|
Log::error('Database error', [
|
|
'exception' => get_class($e),
|
|
'message' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString(),
|
|
]);
|
|
|
|
$response = ['message' => 'Service temporarily unavailable. Please try again later.'];
|
|
|
|
if (config('app.debug')) {
|
|
$response['debug'] = [
|
|
'exception' => get_class($e),
|
|
'message' => $e->getMessage(),
|
|
];
|
|
}
|
|
|
|
return response()->json($response, 503);
|
|
}
|
|
});
|
|
|
|
// 404 Not Found → friendly message
|
|
$exceptions->render(function (NotFoundHttpException $e, Request $request) {
|
|
if ($request->expectsJson() || $request->is('api/*')) {
|
|
return response()->json([
|
|
'message' => 'Resource not found.',
|
|
], 404);
|
|
}
|
|
});
|
|
|
|
// Authorization failures → log with user context
|
|
$exceptions->render(function (AuthorizationException $e, Request $request) {
|
|
if ($request->expectsJson() || $request->is('api/*')) {
|
|
Log::warning('Authorization denied', [
|
|
'user_id' => auth()->id(),
|
|
'ip' => $request->ip(),
|
|
'path' => $request->path(),
|
|
'method' => $request->method(),
|
|
]);
|
|
}
|
|
|
|
return null; // Let Laravel handle the 403 response normally
|
|
});
|
|
|
|
// All other unhandled exceptions → 500
|
|
// (ValidationException, AuthenticationException, and HttpException are handled by Laravel)
|
|
$exceptions->render(function (Throwable $e, Request $request) {
|
|
if ($request->expectsJson() || $request->is('api/*')) {
|
|
if ($e instanceof ValidationException
|
|
|| $e instanceof AuthenticationException
|
|
|| $e instanceof HttpException) {
|
|
return null; // Let Laravel handle these normally
|
|
}
|
|
|
|
Log::error('Unhandled exception', [
|
|
'exception' => get_class($e),
|
|
'message' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString(),
|
|
]);
|
|
|
|
$response = ['message' => 'An unexpected error occurred.'];
|
|
|
|
if (config('app.debug')) {
|
|
$response['debug'] = [
|
|
'exception' => get_class($e),
|
|
'message' => $e->getMessage(),
|
|
];
|
|
}
|
|
|
|
return response()->json($response, 500);
|
|
}
|
|
});
|
|
})->create();
|