BlueGit

projects-timfalken-com allianties

Files

allianties / server / auth.php

auth.php

auth.php 2.1 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 // Local only: ?avatar=none (no picture) / ?avatar=broken (URL that 404s,
18 // like the auth server's /avatar/<id> for users without a picture).
19 $avatar = 'images/avatars/' . $who . '.svg';
20 $mode = (string) ($_GET['avatar'] ?? '');
21 if ($mode === 'none') {
22 $avatar = null;
23 } elseif ($mode === 'broken') {
24 $avatar = 'images/avatars/missing-' . $who . '.png';
25 }
26 loginUser('local:' . $who, $who, $avatar);
27 $dest = $_SESSION['after_login'] ?? (is_string($next) ? $next : 'index.html');
28 unset($_SESSION['after_login']);
29 header('Location: ' . $dest);
30 exit;
31 }
32 if (!authConfigured()) {
33 header('Location: index.html?auth=unconfigured');
34 exit;
35 }
36 $cfg = config();
37 $_SESSION['oauth_state'] = bin2hex(random_bytes(32));
38 $_SESSION['oauth_started'] = time();
39 header('Location: ' . authUrl() . '/oauth/authorize?' . http_build_query([
40 'client_id' => $cfg['client_id'],
41 'redirect_uri' => baseUrl() . '/callback.php',
42 'response_type' => 'code',
43 'state' => $_SESSION['oauth_state'],
44 ]));
45} catch (Throwable $e) {
46 http_response_code(503);
47 header('Content-Type: text/html; charset=utf-8');
48 $lang = I18n::htmlLang();
49 echo '<!DOCTYPE html><html lang="' . htmlspecialchars($lang) . '"><meta charset="utf-8"><title>Allianties</title>';
50 echo '<p>' . htmlspecialchars(I18n::t('auth.unavailable')) . ' ' . htmlspecialchars($e->getMessage()) . '</p>';
51 echo '<p><a href="index.html">' . htmlspecialchars(I18n::t('auth.backToGame')) . '</a></p>';
52}