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>
100 lines
3.1 KiB
PHP
100 lines
3.1 KiB
PHP
<?php
|
|
|
|
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;
|
|
use App\Http\Resources\Api\V1\InvitationResource;
|
|
use App\Models\Organisation;
|
|
use App\Models\UserInvitation;
|
|
use App\Services\InvitationService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Support\Facades\Gate;
|
|
|
|
final class InvitationController extends Controller
|
|
{
|
|
use SetAuthCookie;
|
|
public function __construct(
|
|
private readonly InvitationService $invitationService,
|
|
) {}
|
|
|
|
public function invite(StoreInvitationRequest $request, Organisation $organisation): JsonResponse
|
|
{
|
|
Gate::authorize('invite', $organisation);
|
|
|
|
$invitation = $this->invitationService->invite(
|
|
$organisation,
|
|
$request->validated('email'),
|
|
$request->validated('role'),
|
|
$request->user(),
|
|
);
|
|
|
|
return $this->created(
|
|
new InvitationResource($invitation->load(['organisation', 'invitedBy'])),
|
|
'Uitnodiging verstuurd',
|
|
);
|
|
}
|
|
|
|
public function show(string $token): JsonResponse
|
|
{
|
|
$hashedToken = hash('sha256', $token);
|
|
|
|
$invitation = UserInvitation::where('token', $hashedToken)
|
|
->with(['organisation', 'invitedBy'])
|
|
->first();
|
|
|
|
if (! $invitation) {
|
|
return $this->notFound('Uitnodiging niet gevonden');
|
|
}
|
|
|
|
return $this->success(new InvitationResource($invitation));
|
|
}
|
|
|
|
public function accept(AcceptInvitationRequest $request, string $token): JsonResponse
|
|
{
|
|
$hashedToken = hash('sha256', $token);
|
|
$invitation = UserInvitation::where('token', $hashedToken)->firstOrFail();
|
|
|
|
$user = $this->invitationService->accept(
|
|
$invitation,
|
|
$request->validated('password'),
|
|
);
|
|
|
|
$sanctumToken = $user->createToken('auth-token')->plainTextToken;
|
|
$cookieName = $this->resolveCookieName($request);
|
|
|
|
return $this->success([
|
|
'user' => [
|
|
'id' => $user->id,
|
|
'first_name' => $user->first_name,
|
|
'last_name' => $user->last_name,
|
|
'full_name' => $user->full_name,
|
|
'email' => $user->email,
|
|
],
|
|
], 'Uitnodiging geaccepteerd')
|
|
->withCookie($this->makeAuthCookie($cookieName, $sanctumToken));
|
|
}
|
|
|
|
public function revoke(Organisation $organisation, UserInvitation $invitation): JsonResponse
|
|
{
|
|
// Verify invitation belongs to this organisation
|
|
if ($invitation->organisation_id !== $organisation->id) {
|
|
return $this->notFound('Uitnodiging niet gevonden');
|
|
}
|
|
|
|
Gate::authorize('invite', $organisation);
|
|
|
|
if (! $invitation->isPending()) {
|
|
return $this->error('Alleen openstaande uitnodigingen kunnen worden ingetrokken.', 422);
|
|
}
|
|
|
|
$invitation->markAsExpired();
|
|
|
|
return response()->json(null, 204);
|
|
}
|
|
}
|