Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 6m10s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Failing after 6m3s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 7m33s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 7m25s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m28s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 7m27s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m26s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m27s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 44s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m12s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
99 lines
3.3 KiB
PHP
99 lines
3.3 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Api;
|
||
|
||
use App\Http\Controllers\Controller;
|
||
use App\Models\User;
|
||
use App\Providers\FortifyServiceProvider;
|
||
use App\Support\Audit;
|
||
use Illuminate\Http\JsonResponse;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Support\Facades\Hash;
|
||
use Illuminate\Support\Facades\RateLimiter;
|
||
use Illuminate\Support\Str;
|
||
use Illuminate\Validation\ValidationException;
|
||
|
||
class AuthController extends Controller
|
||
{
|
||
/**
|
||
* Token für eine App (nur Konten mit Portal-Passwort; bei Anmeldung über
|
||
* Microsoft & Co. erzeugt man das Token unter „Profil → Apps & Geräte“).
|
||
*/
|
||
public function token(Request $request): JsonResponse
|
||
{
|
||
$data = $request->validate([
|
||
'email' => ['required', 'email'],
|
||
'password' => ['required', 'string'],
|
||
'device_name' => ['required', 'string', 'max:100'],
|
||
]);
|
||
|
||
// Je E-Mail-Adresse begrenzen (zusätzlich zur Begrenzung je IP) – gegen Durchprobieren von Passwörtern
|
||
$key = 'api-token:'.Str::lower($data['email']);
|
||
|
||
if (RateLimiter::tooManyAttempts($key, 5)) {
|
||
throw ValidationException::withMessages(['email' => 'Zu viele Versuche. Bitte in einigen Minuten erneut versuchen.']);
|
||
}
|
||
|
||
$user = User::query()->where('email', Str::lower($data['email']))->first();
|
||
$valid = Hash::check($data['password'], $user?->password ?? FortifyServiceProvider::dummyHash());
|
||
|
||
if (! $user || ! $user->is_active || $user->password === null || ! $valid) {
|
||
RateLimiter::hit($key, 300);
|
||
|
||
throw ValidationException::withMessages(['email' => 'Diese Kombination aus Zugangsdaten wurde nicht gefunden.']);
|
||
}
|
||
|
||
RateLimiter::clear($key);
|
||
|
||
return self::issue($user, $data['device_name']);
|
||
}
|
||
|
||
/**
|
||
* Neuer App-Zugang. Höchstens 20 je Person – der älteste fällt weg.
|
||
*/
|
||
public static function issue(User $user, string $deviceName): JsonResponse
|
||
{
|
||
$excess = $user->tokens()->count() - 19;
|
||
|
||
if ($excess > 0) {
|
||
$user->tokens()->oldest()->limit($excess)->get()->each->delete();
|
||
}
|
||
|
||
$token = $user->createToken(trim($deviceName));
|
||
Audit::log('api.token_created', ['user' => $user->id, 'device' => $deviceName]);
|
||
|
||
return response()->json(['token' => $token->plainTextToken, 'user' => self::profile($user)], 201);
|
||
}
|
||
|
||
public function me(Request $request): JsonResponse
|
||
{
|
||
return response()->json(['user' => self::profile($request->user())]);
|
||
}
|
||
|
||
public function revoke(Request $request): JsonResponse
|
||
{
|
||
$request->user()->currentAccessToken()?->delete();
|
||
|
||
return response()->json(['revoked' => true]);
|
||
}
|
||
|
||
/**
|
||
* @return array<string, mixed>
|
||
*/
|
||
private static function profile(User $user): array
|
||
{
|
||
return [
|
||
'id' => $user->id,
|
||
'name' => $user->name,
|
||
'first_name' => $user->first_name,
|
||
'last_name' => $user->last_name,
|
||
'email' => $user->email,
|
||
'job_title' => $user->job_title,
|
||
'department' => $user->department,
|
||
'location' => $user->location,
|
||
'avatar_url' => $user->avatarUrl(256),
|
||
'cover_url' => $user->coverUrl(),
|
||
'unread_notifications' => $user->unreadNotifications()->count(),
|
||
];
|
||
}
|
||
}
|