BlueGit

projects-timfalken-com meteors

Files

meteors / server / auth.php

auth.php

auth.php 1.5 KiB Download Raw
1<?php
2declare(strict_types=1);
3require __DIR__ . '/lib/bootstrap.php';
4require __DIR__ . '/lib/I18n.php';
5
6try {
7 if (localDev() && isset($_GET['test'])) {
8 $who = $_GET['test'] === '2' ? 'test_user_2' : 'test_user_1';
9 loginUser('local:' . $who, $who);
10 $next = isset($_GET['next']) && is_string($_GET['next']) ? $_GET['next'] : 'index.html';
11 header('Location: ' . $next);
12 exit;
13 }
14 if (!authConfigured()) {
15 header('Location: index.html?auth=unconfigured');
16 exit;
17 }
18 $cfg = config();
19 // t1193: remember where the login started (menu vs. Online screen).
20 $_SESSION['login_return'] = loginReturnTarget($_GET['return'] ?? null);
21 $_SESSION['oauth_state'] = bin2hex(random_bytes(32));
22 $_SESSION['oauth_started'] = time();
23 header('Location: ' . authUrl() . '/oauth/authorize?' . http_build_query([
24 'client_id' => $cfg['client_id'],
25 'redirect_uri' => baseUrl() . '/callback.php',
26 'response_type' => 'code',
27 'state' => $_SESSION['oauth_state'],
28 ]));
29} catch (Throwable $e) {
30 http_response_code(503);
31 header('Content-Type: text/html; charset=utf-8');
32 $lang = I18n::htmlLang();
33 echo '<!DOCTYPE html><html lang="' . htmlspecialchars($lang) . '"><meta charset="utf-8"><title>Meteors</title>';
34 echo '<p>' . htmlspecialchars(I18n::t('auth.unavailable')) . ' ' . htmlspecialchars($e->getMessage()) . '</p>';
35 echo '<p><a href="index.html">' . htmlspecialchars(I18n::t('auth.backToGame')) . '</a></p>';
36}