BlueGit

projects-timfalken-com meteors

Files

meteors / server / auth.php

auth.php

auth.php 1.4 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 $_SESSION['oauth_state'] = bin2hex(random_bytes(32));
20 $_SESSION['oauth_started'] = time();
21 header('Location: ' . authUrl() . '/oauth/authorize?' . http_build_query([
22 'client_id' => $cfg['client_id'],
23 'redirect_uri' => baseUrl() . '/callback.php',
24 'response_type' => 'code',
25 'state' => $_SESSION['oauth_state'],
26 ]));
27} catch (Throwable $e) {
28 http_response_code(503);
29 header('Content-Type: text/html; charset=utf-8');
30 $lang = I18n::htmlLang();
31 echo '<!DOCTYPE html><html lang="' . htmlspecialchars($lang) . '"><meta charset="utf-8"><title>Meteors</title>';
32 echo '<p>' . htmlspecialchars(I18n::t('auth.unavailable')) . ' ' . htmlspecialchars($e->getMessage()) . '</p>';
33 echo '<p><a href="index.html">' . htmlspecialchars(I18n::t('auth.backToGame')) . '</a></p>';
34}