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