security: migrate auth tokens to httpOnly cookies (hybrid bearer token approach)
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>
This commit is contained in:
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Http\Controllers\Api\V1\Traits\SetAuthCookie;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Api\V1\AcceptInvitationRequest;
|
||||
use App\Http\Requests\Api\V1\StoreInvitationRequest;
|
||||
@@ -16,6 +17,7 @@ use Illuminate\Support\Facades\Gate;
|
||||
|
||||
final class InvitationController extends Controller
|
||||
{
|
||||
use SetAuthCookie;
|
||||
public function __construct(
|
||||
private readonly InvitationService $invitationService,
|
||||
) {}
|
||||
@@ -63,6 +65,7 @@ final class InvitationController extends Controller
|
||||
);
|
||||
|
||||
$sanctumToken = $user->createToken('auth-token')->plainTextToken;
|
||||
$cookieName = $this->resolveCookieName($request);
|
||||
|
||||
return $this->success([
|
||||
'user' => [
|
||||
@@ -72,8 +75,8 @@ final class InvitationController extends Controller
|
||||
'full_name' => $user->full_name,
|
||||
'email' => $user->email,
|
||||
],
|
||||
'token' => $sanctumToken,
|
||||
], 'Uitnodiging geaccepteerd');
|
||||
], 'Uitnodiging geaccepteerd')
|
||||
->withCookie($this->makeAuthCookie($cookieName, $sanctumToken));
|
||||
}
|
||||
|
||||
public function revoke(Organisation $organisation, UserInvitation $invitation): JsonResponse
|
||||
|
||||
Reference in New Issue
Block a user