Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m25s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 6m46s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 7m52s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 8m5s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 10m33s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 10m53s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 9m1s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 8m49s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 54s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m4s
- /uploads nur über das Portal: UploadController prüft Sitzung bzw. App-Token; frei nur die Dateien der Anmeldeseite (Logos, Symbole, Anmeldehintergrund, Stylesheet, Schrift); public/uploads/.htaccess leitet alles an index.php (Migration schreibt sie, da der Ordner vor dem Updater geschützt ist); Systemzustand prüft die Regel; nginx-Hinweis in docs/SECURITY.md - Passwort-Anmeldung abschaltbar (PasswordLogin): Login-Formular, Passwort vergessen, App-Token und /api/v1/config; nur mit verknüpftem Konto abschaltbar, automatische Rückkehr ohne nutzbaren Dienst, Notausgang INHAUS_PASSWORD_LOGIN - Titelbilder: profile_covers (bis 12), zufällig je Seitenaufruf - ImageCompressor: Uploads im Backend (SafeUpload) und WordPress-Import auf ≤ 2400 px komprimiert, Format bleibt Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
108 lines
3.9 KiB
PHP
108 lines
3.9 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Api;
|
||
|
||
use App\Http\Controllers\Controller;
|
||
use App\Models\User;
|
||
use App\Plugins\PluginAccess;
|
||
use App\Plugins\PluginManager;
|
||
use App\Providers\FortifyServiceProvider;
|
||
use App\Support\Audit;
|
||
use App\Support\PasswordLogin;
|
||
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
|
||
{
|
||
if (! app(PasswordLogin::class)->enabled()) {
|
||
throw ValidationException::withMessages(['email' => 'Die Anmeldung mit Passwort ist abgeschaltet – bitte „Im Browser anmelden“ wählen.']);
|
||
}
|
||
|
||
$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(),
|
||
// Aktive Plugins, die die Person nutzen darf – die App blendet danach Bereiche ein (z. B. chat, gruppen)
|
||
'plugins' => array_values(array_filter(array_keys(app(PluginManager::class)->loaded()), fn (string $slug) => app(PluginAccess::class)->allows($slug, $user))),
|
||
];
|
||
}
|
||
}
|