| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | require __DIR__ . '/lib/bootstrap.php'; |
| 4 | |
| 5 | try { |
| 6 | $state = $_GET['state'] ?? null; |
| 7 | if (!is_string($state) || !isset($_SESSION['oauth_state']) |
| 8 | || !hash_equals((string) $_SESSION['oauth_state'], $state) |
| 9 | || time() - (int) ($_SESSION['oauth_started'] ?? 0) > 600) { |
| 10 | throw new RuntimeException('Ongeldige of verlopen login.'); |
| 11 | } |
| 12 | unset($_SESSION['oauth_state'], $_SESSION['oauth_started']); |
| 13 | $code = $_GET['code'] ?? null; |
| 14 | if (!is_string($code) || $code === '') { |
| 15 | throw new RuntimeException('De login is niet afgerond.'); |
| 16 | } |
| 17 | $cfg = config(); |
| 18 | $token = authRequest('/oauth/token', [ |
| 19 | 'grant_type' => 'authorization_code', |
| 20 | 'code' => $code, |
| 21 | 'client_id' => $cfg['client_id'], |
| 22 | 'client_secret' => $cfg['client_secret'], |
| 23 | 'redirect_uri' => baseUrl() . '/callback.php', |
| 24 | ]); |
| 25 | if (!isset($token['access_token'], $token['expires_in']) || !is_string($token['access_token'])) { |
| 26 | throw new RuntimeException('Geen toegangstoken ontvangen.'); |
| 27 | } |
| 28 | $me = authRequest('/api/me', null, $token['access_token']); |
| 29 | if (!isset($me['user']) || !is_array($me['user'])) { |
| 30 | throw new RuntimeException('Het profiel ontbreekt.'); |
| 31 | } |
| 32 | [$id, $name] = authIdentity($me['user']); |
| 33 | loginUser($id, $name); |
| 34 | $_SESSION['token'] = $token['access_token']; |
| 35 | $_SESSION['expires'] = time() + (int) $token['expires_in']; |
| 36 | header('Location: ' . baseUrl() . '/index.html?online=1'); |
| 37 | } catch (Throwable $e) { |
| 38 | http_response_code(401); |
| 39 | header('Content-Type: text/html; charset=utf-8'); |
| 40 | echo '<!DOCTYPE html><meta charset="utf-8"><title>Meteors</title>'; |
| 41 | echo 'Inloggen is mislukt. ' . htmlspecialchars($e->getMessage()); |
| 42 | echo ' <a href="auth.php">Opnieuw proberen</a> · <a href="index.html">Terug</a>'; |
| 43 | } |
projects-timfalken-com meteors