Loheide-eu-mitarbeiterplatt.../app/Http/Controllers/Api/AuthController.php
Friederich Loheide f1202bea08
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m50s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 7m34s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m45s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m30s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m7s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 7m9s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m8s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m5s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 48s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m5s
Sicherheitsupdate nach Audit (0.29.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 11:34:54 +00:00

92 lines
3.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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);
// Höchstens 20 Zugänge je Person – der älteste fällt weg
$excess = $user->tokens()->count() - 19;
if ($excess > 0) {
$user->tokens()->oldest()->limit($excess)->get()->each->delete();
}
$token = $user->createToken(trim($data['device_name']));
Audit::log('api.token_created', ['user' => $user->id, 'device' => $data['device_name']]);
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(),
];
}
}