t1193: login returns to the main menu unless started from Online; ?online=1 is one-shot
@@ -16,6 +16,8 @@ try {
16
16
exit;
17
17
}
18
18
$cfg = config();
19
// t1193: remember where the login started (menu vs. Online screen).
20
$_SESSION['login_return'] = loginReturnTarget($_GET['return'] ?? null);
19
21
$_SESSION['oauth_state'] = bin2hex(random_bytes(32));
20
22
$_SESSION['oauth_started'] = time();
21
23
header('Location: ' . authUrl() . '/oauth/authorize?' . http_build_query([
@@ -16,6 +16,8 @@ try {16
exit;16
exit;17
}17
}18
$cfg = config();18
$cfg = config();19
// t1193: remember where the login started (menu vs. Online screen).20
$_SESSION['login_return'] = loginReturnTarget($_GET['return'] ?? null);19
$_SESSION['oauth_state'] = bin2hex(random_bytes(32));21
$_SESSION['oauth_state'] = bin2hex(random_bytes(32));20
$_SESSION['oauth_started'] = time();22
$_SESSION['oauth_started'] = time();21
header('Location: ' . authUrl() . '/oauth/authorize?' . http_build_query([23
header('Location: ' . authUrl() . '/oauth/authorize?' . http_build_query([@@ -10,7 +10,8 @@ try {
10
10
|| time() - (int) ($_SESSION['oauth_started'] ?? 0) > 600) {
11
11
throw new RuntimeException('Ongeldige of verlopen login.');
12
12
}
13
unset($_SESSION['oauth_state'], $_SESSION['oauth_started']);
13
$loginReturn = loginReturnTarget($_SESSION['login_return'] ?? null);
14
unset($_SESSION['oauth_state'], $_SESSION['oauth_started'], $_SESSION['login_return']);
14
15
$code = $_GET['code'] ?? null;
15
16
if (!is_string($code) || $code === '') {
16
17
throw new RuntimeException('De login is niet afgerond.');
@@ -34,7 +35,8 @@ try {
34
35
loginUser($id, $name);
35
36
$_SESSION['token'] = $token['access_token'];
36
37
$_SESSION['expires'] = time() + (int) $token['expires_in'];
37
header('Location: ' . baseUrl() . '/index.html?online=1');
38
// t1193: back to the main menu unless the login started from Online.
39
header('Location: ' . baseUrl() . '/' . loginReturnPath($loginReturn));
38
40
} catch (Throwable $e) {
39
41
http_response_code(401);
40
42
header('Content-Type: text/html; charset=utf-8');
@@ -10,7 +10,8 @@ try {10
|| time() - (int) ($_SESSION['oauth_started'] ?? 0) > 600) {10
|| time() - (int) ($_SESSION['oauth_started'] ?? 0) > 600) {11
throw new RuntimeException('Ongeldige of verlopen login.');11
throw new RuntimeException('Ongeldige of verlopen login.');12
}12
}13
unset($_SESSION['oauth_state'], $_SESSION['oauth_started']);13
$loginReturn = loginReturnTarget($_SESSION['login_return'] ?? null);14
unset($_SESSION['oauth_state'], $_SESSION['oauth_started'], $_SESSION['login_return']);14
$code = $_GET['code'] ?? null;15
$code = $_GET['code'] ?? null;15
if (!is_string($code) || $code === '') {16
if (!is_string($code) || $code === '') {16
throw new RuntimeException('De login is niet afgerond.');17
throw new RuntimeException('De login is niet afgerond.');@@ -34,7 +35,8 @@ try {34
loginUser($id, $name);35
loginUser($id, $name);35
$_SESSION['token'] = $token['access_token'];36
$_SESSION['token'] = $token['access_token'];36
$_SESSION['expires'] = time() + (int) $token['expires_in'];37
$_SESSION['expires'] = time() + (int) $token['expires_in'];37
header('Location: ' . baseUrl() . '/index.html?online=1');38
// t1193: back to the main menu unless the login started from Online.39
header('Location: ' . baseUrl() . '/' . loginReturnPath($loginReturn));38
} catch (Throwable $e) {40
} catch (Throwable $e) {39
http_response_code(401);41
http_response_code(401);40
header('Content-Type: text/html; charset=utf-8');42
header('Content-Type: text/html; charset=utf-8');@@ -179,6 +179,22 @@ function authIdentity(array $profile): array
179
179
return ['auth:' . (int) $raw, $profile['username']];
180
180
}
181
181
182
/**
183
* Where to go after a login (t1193). Only an explicit 'online' (login
184
* started from the Online screen) returns to online play; everything else
185
* (menu login, missing or unknown value) returns to the main menu.
186
*/
187
function loginReturnTarget($param): string
188
{
189
return $param === 'online' ? 'online' : 'menu';
190
}
191
192
/** Relative page to land on after a login for a loginReturnTarget() value. */
193
function loginReturnPath(?string $target): string
194
{
195
return $target === 'online' ? 'index.html?online=1' : 'index.html';
196
}
197
182
198
function loginUser(string $id, string $name): void
183
199
{
184
200
if (!preg_match('/^(?:local:test_user_[12]|auth:[1-9][0-9]{0,17})$/', $id)) {
@@ -179,6 +179,22 @@ function authIdentity(array $profile): array179
return ['auth:' . (int) $raw, $profile['username']];179
return ['auth:' . (int) $raw, $profile['username']];180
}180
}181
181
182
/**183
* Where to go after a login (t1193). Only an explicit 'online' (login184
* started from the Online screen) returns to online play; everything else185
* (menu login, missing or unknown value) returns to the main menu.186
*/187
function loginReturnTarget($param): string188
{189
return $param === 'online' ? 'online' : 'menu';190
}191
192
/** Relative page to land on after a login for a loginReturnTarget() value. */193
function loginReturnPath(?string $target): string194
{195
return $target === 'online' ? 'index.html?online=1' : 'index.html';196
}197
182
function loginUser(string $id, string $name): void198
function loginUser(string $id, string $name): void183
{199
{184
if (!preg_match('/^(?:local:test_user_[12]|auth:[1-9][0-9]{0,17})$/', $id)) {200
if (!preg_match('/^(?:local:test_user_[12]|auth:[1-9][0-9]{0,17})$/', $id)) {@@ -29,6 +29,21 @@ $tmp = sys_get_temp_dir() . '/meteors-test-' . bin2hex(random_bytes(6));
29
29
mkdir($tmp, 0775, true);
30
30
putenv('METEORS_DATA_DIR=' . $tmp);
31
31
32
echo "Login return target (t1193)\n";
33
expect(loginReturnTarget(null) === 'menu', 'no return param → main menu');
34
expect(loginReturnTarget('') === 'menu', 'empty return → main menu');
35
expect(loginReturnTarget('online') === 'online', 'return=online → online play');
36
expect(loginReturnTarget('https://evil.example/') === 'menu', 'unknown/URL value → main menu (no open redirect)');
37
expect(loginReturnTarget(['online']) === 'menu', 'array value → main menu');
38
expect(loginReturnPath('menu') === 'index.html', 'menu path has no online flag');
39
expect(loginReturnPath(null) === 'index.html', 'missing session value → menu');
40
expect(loginReturnPath('online') === 'index.html?online=1', 'online path opens the online panel');
41
$cbSrc = (string) file_get_contents(dirname(__DIR__) . '/callback.php');
42
expect(strpos($cbSrc, "index.html?online=1'") === false, 'callback no longer hard-codes ?online=1');
43
expect(strpos($cbSrc, 'loginReturnPath(') !== false, 'callback uses loginReturnPath');
44
$authSrc = (string) file_get_contents(dirname(__DIR__) . '/auth.php');
45
expect(strpos($authSrc, "loginReturnTarget(\$_GET['return']") !== false, 'auth.php stores the return target');
46
32
47
echo "Store: lobby and game\n";
33
48
$store = Store::open($tmp);
34
49
@@ -29,6 +29,21 @@ $tmp = sys_get_temp_dir() . '/meteors-test-' . bin2hex(random_bytes(6));29
mkdir($tmp, 0775, true);29
mkdir($tmp, 0775, true);30
putenv('METEORS_DATA_DIR=' . $tmp);30
putenv('METEORS_DATA_DIR=' . $tmp);31
31
32
echo "Login return target (t1193)\n";33
expect(loginReturnTarget(null) === 'menu', 'no return param → main menu');34
expect(loginReturnTarget('') === 'menu', 'empty return → main menu');35
expect(loginReturnTarget('online') === 'online', 'return=online → online play');36
expect(loginReturnTarget('https://evil.example/') === 'menu', 'unknown/URL value → main menu (no open redirect)');37
expect(loginReturnTarget(['online']) === 'menu', 'array value → main menu');38
expect(loginReturnPath('menu') === 'index.html', 'menu path has no online flag');39
expect(loginReturnPath(null) === 'index.html', 'missing session value → menu');40
expect(loginReturnPath('online') === 'index.html?online=1', 'online path opens the online panel');41
$cbSrc = (string) file_get_contents(dirname(__DIR__) . '/callback.php');42
expect(strpos($cbSrc, "index.html?online=1'") === false, 'callback no longer hard-codes ?online=1');43
expect(strpos($cbSrc, 'loginReturnPath(') !== false, 'callback uses loginReturnPath');44
$authSrc = (string) file_get_contents(dirname(__DIR__) . '/auth.php');45
expect(strpos($authSrc, "loginReturnTarget(\$_GET['return']") !== false, 'auth.php stores the return target');46
32
echo "Store: lobby and game\n";47
echo "Store: lobby and game\n";33
$store = Store::open($tmp);48
$store = Store::open($tmp);34
49
@@ -362,9 +362,10 @@ Online.stopPolling = function ()
362
362
}
363
363
};
364
364
365
/** Login from the Online screen: come back to online play (t1193). */
365
366
Online.loginUrl = function ()
366
367
{
367
return 'auth.php';
368
return 'auth.php?return=online';
368
369
};
369
370
370
371
Online.logoutUrl = function ()
@@ -362,9 +362,10 @@ Online.stopPolling = function ()362
}362
}363
};363
};364
364
365
/** Login from the Online screen: come back to online play (t1193). */365
Online.loginUrl = function ()366
Online.loginUrl = function ()366
{367
{367
return 'auth.php';368
return 'auth.php?return=online';368
};369
};369
370
370
Online.logoutUrl = function ()371
Online.logoutUrl = function ()