Herzen, Reichweite und App-Anbindung (0.30.0)
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
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>
This commit is contained in:
parent
d9fcc7a9b4
commit
b3e2b525e1
161 changed files with 1249 additions and 342 deletions
85
app/Http/Controllers/Api/AppController.php
Normal file
85
app/Http/Controllers/Api/AppController.php
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\IdentityProvider;
|
||||
use App\Models\User;
|
||||
use App\Push\AppDevice;
|
||||
use App\Support\AppConnect;
|
||||
use App\Support\Branding;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
/**
|
||||
* Schnittstellen speziell für die Inhaus-App: Erscheinungsbild vor der Anmeldung,
|
||||
* Anmeldung über den Browser und Push-Geräte.
|
||||
*/
|
||||
class AppController extends Controller
|
||||
{
|
||||
public function config(Branding $branding): JsonResponse
|
||||
{
|
||||
$icons = (array) ($branding->get('icons') ?? []);
|
||||
|
||||
return response()->json([
|
||||
'name' => $branding->portalName(),
|
||||
'primary_color' => $branding->primaryColor(),
|
||||
'logo_url' => $branding->logoUrl(),
|
||||
'logo_dark_url' => $branding->logoDarkUrl(),
|
||||
'icon_url' => $branding->url($icons[512] ?? $icons['512'] ?? $icons[192] ?? $icons['192'] ?? null) ?? $branding->faviconUrl(),
|
||||
'login' => [
|
||||
'password' => true,
|
||||
'browser' => true,
|
||||
'providers' => IdentityProvider::forLogin()->map->toLoginButton()->pluck('label')->filter()->values(),
|
||||
],
|
||||
'connect_url' => route('app.connect'),
|
||||
'api_version' => 1,
|
||||
]);
|
||||
}
|
||||
|
||||
public function exchange(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'code' => ['required', 'string', 'max:100'],
|
||||
'code_verifier' => ['required', 'string', 'min:43', 'max:128'],
|
||||
'device_name' => ['nullable', 'string', 'max:100'],
|
||||
]);
|
||||
|
||||
$entry = AppConnect::redeem($data['code'], $data['code_verifier']);
|
||||
$user = $entry ? User::query()->find($entry['user']) : null;
|
||||
|
||||
if (! $user || ! $user->is_active) {
|
||||
throw ValidationException::withMessages(['code' => 'Die Anmeldung ist abgelaufen oder ungültig. Bitte erneut verbinden.']);
|
||||
}
|
||||
|
||||
return AuthController::issue($user, $entry['device']);
|
||||
}
|
||||
|
||||
public function registerDevice(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'token' => ['required', 'string', 'max:255', 'regex:/^(ExponentPushToken|ExpoPushToken)\[[A-Za-z0-9_-]+\]$/'],
|
||||
'platform' => ['nullable', 'in:ios,android'],
|
||||
'name' => ['nullable', 'string', 'max:100'],
|
||||
]);
|
||||
|
||||
// Ein Gerät gehört immer der Person, die zuletzt damit angemeldet war
|
||||
AppDevice::query()->updateOrCreate(['push_token' => $data['token']], [
|
||||
'user_id' => $request->user()->id,
|
||||
'token_id' => $request->user()->currentAccessToken()?->getKey(),
|
||||
'platform' => $data['platform'] ?? null,
|
||||
'name' => $data['name'] ?? $request->user()->currentAccessToken()?->name,
|
||||
]);
|
||||
|
||||
return response()->json(['registered' => true], 201);
|
||||
}
|
||||
|
||||
public function removeDevice(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate(['token' => ['required', 'string', 'max:255']]);
|
||||
AppDevice::query()->where('user_id', $request->user()->id)->where('push_token', $data['token'])->delete();
|
||||
|
||||
return response()->json(['removed' => true]);
|
||||
}
|
||||
}
|
||||
|
|
@ -45,15 +45,22 @@ class AuthController extends Controller
|
|||
|
||||
RateLimiter::clear($key);
|
||||
|
||||
// Höchstens 20 Zugänge je Person – der älteste fällt weg
|
||||
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($data['device_name']));
|
||||
Audit::log('api.token_created', ['user' => $user->id, 'device' => $data['device_name']]);
|
||||
$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);
|
||||
}
|
||||
|
|
|
|||
60
app/Http/Controllers/AppConnectController.php
Normal file
60
app/Http/Controllers/AppConnectController.php
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Support\AppConnect;
|
||||
use App\Support\Audit;
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
use Symfony\Component\HttpFoundation\Response as HttpResponse;
|
||||
|
||||
/**
|
||||
* „App verbinden“: Die Inhaus-App öffnet diese Seite im Browser. So klappt die Anmeldung
|
||||
* auch mit Microsoft & Co.; die Person bestätigt, danach geht es zurück in die App.
|
||||
*/
|
||||
class AppConnectController extends Controller
|
||||
{
|
||||
public function show(Request $request): Response
|
||||
{
|
||||
$data = $this->validated($request);
|
||||
|
||||
return Inertia::render('AppConnect', [
|
||||
'deviceName' => $data['device_name'],
|
||||
'query' => $data,
|
||||
]);
|
||||
}
|
||||
|
||||
public function confirm(Request $request): HttpResponse
|
||||
{
|
||||
$data = $this->validated($request);
|
||||
$code = AppConnect::issueCode($request->user()->id, $data['code_challenge'], $data['device_name']);
|
||||
Audit::log('api.app_connect', ['user' => $request->user()->id, 'device' => $data['device_name']]);
|
||||
|
||||
$separator = str_contains($data['redirect_uri'], '?') ? '&' : '?';
|
||||
|
||||
return Inertia::location($data['redirect_uri'].$separator.http_build_query(['code' => $code, 'state' => $data['state']]));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{redirect_uri: string, device_name: string, code_challenge: string, state: string}
|
||||
*/
|
||||
private function validated(Request $request): array
|
||||
{
|
||||
$data = $request->validate([
|
||||
'redirect_uri' => ['required', 'string', 'max:500'],
|
||||
'device_name' => ['nullable', 'string', 'max:100'],
|
||||
'code_challenge' => ['required', 'string'],
|
||||
'state' => ['required', 'string', 'max:200'],
|
||||
]);
|
||||
|
||||
abort_unless(AppConnect::allowedRedirect($data['redirect_uri']) && AppConnect::validChallenge($data['code_challenge']), 400, 'Ungültige Anfrage der App.');
|
||||
|
||||
return [
|
||||
'redirect_uri' => $data['redirect_uri'],
|
||||
'device_name' => trim($data['device_name'] ?? '') ?: 'Inhaus-App',
|
||||
'code_challenge' => $data['code_challenge'],
|
||||
'state' => $data['state'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Push\AppDevice;
|
||||
use App\Push\PushSubscription;
|
||||
use App\Support\Abilities;
|
||||
use App\Support\Audit;
|
||||
|
|
@ -257,6 +258,7 @@ class User extends Authenticatable implements FilamentUser, HasAvatar
|
|||
$this->forceFill(['remember_token' => Str::random(60)])->saveQuietly();
|
||||
$this->tokens()->delete();
|
||||
PushSubscription::query()->where('user_id', $this->id)->delete();
|
||||
AppDevice::query()->where('user_id', $this->id)->delete();
|
||||
|
||||
if (Schema::hasTable(config('session.table', 'sessions'))) {
|
||||
DB::table(config('session.table', 'sessions'))->where('user_id', $this->id)
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
namespace App\Notifications;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Push\AppDevice;
|
||||
use App\Push\PushSubscription;
|
||||
use App\Push\WebPushChannel;
|
||||
use Illuminate\Bus\Queueable;
|
||||
|
|
@ -31,7 +32,8 @@ class PortalNotification extends Notification
|
|||
public function via(object $notifiable): array
|
||||
{
|
||||
// Zusätzlich als Push auf Geräte, die das eingeschaltet haben
|
||||
$push = $notifiable instanceof User && PushSubscription::query()->where('user_id', $notifiable->id)->exists();
|
||||
$push = $notifiable instanceof User && (PushSubscription::query()->where('user_id', $notifiable->id)->exists()
|
||||
|| AppDevice::query()->where('user_id', $notifiable->id)->exists());
|
||||
|
||||
return $push ? ['database', WebPushChannel::class] : ['database'];
|
||||
}
|
||||
|
|
|
|||
25
app/Push/AppDevice.php
Normal file
25
app/Push/AppDevice.php
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
namespace App\Push;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
#[Fillable(['user_id', 'token_id', 'push_token', 'platform', 'name', 'last_used_at'])]
|
||||
class AppDevice extends Model
|
||||
{
|
||||
protected function casts(): array
|
||||
{
|
||||
return ['last_used_at' => 'datetime'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<User, $this>
|
||||
*/
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
52
app/Push/ExpoPush.php
Normal file
52
app/Push/ExpoPush.php
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
namespace App\Push;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
/**
|
||||
* Push an die Inhaus-App über den Push-Dienst von Expo (leitet an Apple und Google weiter).
|
||||
* Übertragen werden nur Titel, Kurztext und der Link – wie bei Web-Push.
|
||||
*/
|
||||
class ExpoPush
|
||||
{
|
||||
public const ENDPOINT = 'https://exp.host/--/api/v2/push/send';
|
||||
|
||||
/**
|
||||
* @param Collection<int, AppDevice> $devices
|
||||
* @param array<string, mixed> $payload
|
||||
*/
|
||||
public function send(Collection $devices, array $payload): void
|
||||
{
|
||||
foreach ($devices->chunk(100) as $chunk) {
|
||||
$chunk = $chunk->values();
|
||||
$response = Http::timeout(15)->acceptJson()->post(self::ENDPOINT, $chunk->map(fn (AppDevice $device) => [
|
||||
'to' => $device->push_token,
|
||||
'title' => $payload['title'] ?? '',
|
||||
'body' => $payload['body'] ?? '',
|
||||
'data' => ['url' => $payload['url'] ?? '/'],
|
||||
'sound' => 'default',
|
||||
])->all());
|
||||
|
||||
if (! $response->successful()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Antwort in derselben Reihenfolge: abgemeldete Geräte entfernen
|
||||
foreach ((array) $response->json('data') as $i => $ticket) {
|
||||
$device = $chunk[$i] ?? null;
|
||||
|
||||
if (! $device) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (($ticket['status'] ?? null) === 'error' && ($ticket['details']['error'] ?? null) === 'DeviceNotRegistered') {
|
||||
$device->delete();
|
||||
} elseif (($ticket['status'] ?? null) === 'ok') {
|
||||
$device->forceFill(['last_used_at' => now()])->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -9,7 +9,7 @@ use Illuminate\Queue\InteractsWithQueue;
|
|||
use Throwable;
|
||||
|
||||
/**
|
||||
* Push-Nachricht an alle Geräte einer Person – im Hintergrund, damit
|
||||
* Push-Nachricht an alle Geräte einer Person (Browser und App) – im Hintergrund, damit
|
||||
* langsame Push-Dienste keine Seite aufhalten.
|
||||
*/
|
||||
class SendWebPush implements ShouldQueue
|
||||
|
|
@ -23,8 +23,18 @@ class SendWebPush implements ShouldQueue
|
|||
*/
|
||||
public function __construct(public readonly int $userId, public readonly array $payload) {}
|
||||
|
||||
public function handle(WebPush $push): void
|
||||
public function handle(WebPush $push, ExpoPush $expo): void
|
||||
{
|
||||
$devices = AppDevice::query()->where('user_id', $this->userId)->get();
|
||||
|
||||
if ($devices->isNotEmpty()) {
|
||||
try {
|
||||
$expo->send($devices, $this->payload);
|
||||
} catch (Throwable $e) {
|
||||
report($e);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (PushSubscription::query()->where('user_id', $this->userId)->get() as $subscription) {
|
||||
try {
|
||||
$push->send($subscription, $this->payload)
|
||||
|
|
|
|||
59
app/Support/AppConnect.php
Normal file
59
app/Support/AppConnect.php
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* Anmeldung der App über den Browser (wie OAuth mit PKCE): Das Portal gibt nach der
|
||||
* Bestätigung nur einen Einmal-Code an die App zurück. Den Zugang bekommt nur, wer den
|
||||
* passenden geheimen Prüfwert kennt – also die App, die die Anmeldung gestartet hat.
|
||||
*/
|
||||
class AppConnect
|
||||
{
|
||||
public const TTL = 300;
|
||||
|
||||
public static function allowedRedirect(string $uri): bool
|
||||
{
|
||||
$scheme = strtolower((string) parse_url($uri, PHP_URL_SCHEME));
|
||||
|
||||
return $scheme !== '' && in_array($scheme, array_map('strtolower', (array) config('inhaus.app_schemes')), true)
|
||||
&& ! in_array($scheme, ['http', 'https', 'javascript', 'data', 'file'], true)
|
||||
&& ! preg_match('/[\s<>"]/', $uri);
|
||||
}
|
||||
|
||||
public static function validChallenge(string $challenge): bool
|
||||
{
|
||||
return (bool) preg_match('/^[A-Za-z0-9_-]{43}$/', $challenge);
|
||||
}
|
||||
|
||||
public static function issueCode(int $userId, string $challenge, string $deviceName): string
|
||||
{
|
||||
$code = Str::random(48);
|
||||
Cache::put(self::key($code), ['user' => $userId, 'challenge' => $challenge, 'device' => $deviceName], self::TTL);
|
||||
|
||||
return $code;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{user: int, device: string}|null nur einmal einlösbar
|
||||
*/
|
||||
public static function redeem(string $code, string $verifier): ?array
|
||||
{
|
||||
$entry = Cache::pull(self::key($code));
|
||||
|
||||
if (! is_array($entry)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$expected = rtrim(strtr(base64_encode(hash('sha256', $verifier, true)), '+/', '-_'), '=');
|
||||
|
||||
return hash_equals($entry['challenge'], $expected) ? ['user' => $entry['user'], 'device' => $entry['device']] : null;
|
||||
}
|
||||
|
||||
private static function key(string $code): string
|
||||
{
|
||||
return 'app-connect:'.hash('sha256', $code);
|
||||
}
|
||||
}
|
||||
|
|
@ -9,5 +9,5 @@ namespace App;
|
|||
*/
|
||||
final class Version
|
||||
{
|
||||
public const VERSION = '0.29.0';
|
||||
public const VERSION = '0.30.0';
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue