change auth cookie and some related hls stuff

This commit is contained in:
Evgeny Zinoviev 2022-05-27 01:13:40 +03:00
parent cf0b9f036b
commit f1276e23d8
3 changed files with 22 additions and 9 deletions

View File

@ -4,25 +4,19 @@ class auth {
public static ?User $authorizedUser = null; public static ?User $authorizedUser = null;
const SESSION_TIMEOUT = 86400 * 365; const COOKIE_NAME = 'lws-auth';
const COOKIE_NAME = 'auth';
public static function getToken(): ?string { public static function getToken(): ?string {
return $_COOKIE[self::COOKIE_NAME] ?? null; return $_COOKIE[self::COOKIE_NAME] ?? null;
} }
public static function setToken(string $token) { public static function setToken(string $token) {
setcookie(self::COOKIE_NAME, setcookie_safe(self::COOKIE_NAME, $token);
$token,
time() + self::SESSION_TIMEOUT,
'/',
config::get('auth_cookie_host'),
true);
} }
public static function resetToken() { public static function resetToken() {
if (!headers_sent()) if (!headers_sent())
setcookie(self::COOKIE_NAME, null, -1, '/', config::get('auth_cookie_host')); unsetcookie(self::COOKIE_NAME);
} }
public static function id(bool $do_check = true): int { public static function id(bool $do_check = true): int {

View File

@ -282,4 +282,19 @@ function from_camel_case(string $s): string {
} }
} }
return $buf; return $buf;
}
function unsetcookie(string $name) {
global $config;
setcookie($name, null, -1, '/', $config['auth_cookie_host']);
}
function setcookie_safe(...$args) {
global $config;
if (!headers_sent()) {
if (count($args) == 2)
setcookie($args[0], $args[1], time()+86400+365, '/', $config['auth_cookie_host']);
else
setcookie(...$args);
}
} }

View File

@ -67,6 +67,10 @@ class MiscHandler extends RequestHandler
$hls_host = config::get('cam_hls_host'); $hls_host = config::get('cam_hls_host');
$hls_proto = config::get('cam_hls_proto'); $hls_proto = config::get('cam_hls_proto');
$hls_key = config::get('cam_hls_access_key');
if ($hls_key)
setcookie_safe('hls_key', $hls_key);
$this->tpl->set([ $this->tpl->set([
'hls_host' => $hls_host, 'hls_host' => $hls_host,
'hls_proto' => $hls_proto, 'hls_proto' => $hls_proto,