BlueGit

projects-timfalken-com allianties

Files

allianties / server / auth.php

auth.php

auth.php 1.7 KiB Download Raw
1<?php
2declare(strict_types=1);
3require __DIR__ . '/lib/bootstrap.php';
4require __DIR__ . '/lib/I18n.php';
5
6try {
7 $next = safeNext($_GET['next'] ?? null);
8 if ($next) {
9 $_SESSION['after_login'] = $next;
10 }
11 if (localDev() && isset($_GET['test'])) {
12 $n = (int) $_GET['test'];
13 if ($n < 1 || $n > ALLIANTIES_TEST_USERS) {
14 throw new RuntimeException('Onbekende testgebruiker.');
15 }
16 $who = 'test_user_' . $n;
17 loginUser('local:' . $who, $who, 'images/avatars/' . $who . '.svg');
18 $dest = $_SESSION['after_login'] ?? (is_string($next) ? $next : 'index.html');
19 unset($_SESSION['after_login']);
20 header('Location: ' . $dest);
21 exit;
22 }
23 if (!authConfigured()) {
24 header('Location: index.html?auth=unconfigured');
25 exit;
26 }
27 $cfg = config();
28 $_SESSION['oauth_state'] = bin2hex(random_bytes(32));
29 $_SESSION['oauth_started'] = time();
30 header('Location: ' . authUrl() . '/oauth/authorize?' . http_build_query([
31 'client_id' => $cfg['client_id'],
32 'redirect_uri' => baseUrl() . '/callback.php',
33 'response_type' => 'code',
34 'state' => $_SESSION['oauth_state'],
35 ]));
36} catch (Throwable $e) {
37 http_response_code(503);
38 header('Content-Type: text/html; charset=utf-8');
39 $lang = I18n::htmlLang();
40 echo '<!DOCTYPE html><html lang="' . htmlspecialchars($lang) . '"><meta charset="utf-8"><title>Allianties</title>';
41 echo '<p>' . htmlspecialchars(I18n::t('auth.unavailable')) . ' ' . htmlspecialchars($e->getMessage()) . '</p>';
42 echo '<p><a href="index.html">' . htmlspecialchars(I18n::t('auth.backToGame')) . '</a></p>';
43}