Loheide-eu-mitarbeiterplatt.../app/Support/AppConnect.php
Friederich Loheide b3e2b525e1
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
Herzen, Reichweite und App-Anbindung (0.30.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 12:24:11 +00:00

59 lines
1.9 KiB
PHP
Raw Permalink 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\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);
}
}