Token generation: - Replace Str::ulid() with bin2hex(random_bytes(32)) for 256-bit entropy - Store SHA-256 hash in database, never plaintext tokens - Hash input before lookup on all token endpoints Invitation tokens: - InvitationService: generate crypto random, store hash, pass plain token transiently for email URL via UserInvitation::$plainToken - InvitationController show/accept: hash input before DB lookup - AcceptInvitationRequest: hash token before invitation lookup - Migration: widen user_invitations.token and artists.portal_token from char(26) to char(64) for SHA-256 hex digests Portal token auth: - PortalTokenController: remove Schema::hasTable() runtime checks, hash token before lookup, return shaped response via PortalEventResource instead of raw model data - Create PortalEventResource (name, dates, status only — no internals) - Handle missing production_requests table gracefully via try/catch Portal token middleware: - Implement full token validation: extract from Bearer header or ?token= query param, hash, look up in artists/production_requests, verify event exists and is not draft/closed, set portal context on request - Return generic 401 on any failure (no information leakage) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
97 lines
2.9 KiB
PHP
97 lines
2.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Api\V1;
|
|
|
|
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
|
|
{
|
|
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;
|
|
|
|
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,
|
|
],
|
|
'token' => $sanctumToken,
|
|
], 'Uitnodiging geaccepteerd');
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|