2023-08-03 01:35:08 +00:00
|
|
|
<?php
|
|
|
|
namespace Misuzu\Auth;
|
|
|
|
|
2024-01-24 22:14:42 +00:00
|
|
|
use DateTimeImmutable;
|
2023-08-03 01:35:08 +00:00
|
|
|
|
2024-01-24 22:14:42 +00:00
|
|
|
// is this the right way to do this?
|
2023-08-03 01:35:08 +00:00
|
|
|
final class AuthTokenCookie {
|
|
|
|
public static function domain(): string {
|
|
|
|
$url = parse_url($_SERVER['HTTP_HOST'], PHP_URL_HOST);
|
|
|
|
if(empty($url))
|
|
|
|
$url = $_SERVER['HTTP_HOST'];
|
|
|
|
|
|
|
|
if(!filter_var($url, FILTER_VALIDATE_IP))
|
|
|
|
$url = '.' . $url;
|
|
|
|
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function apply(string $packed): void {
|
2024-01-24 22:14:42 +00:00
|
|
|
$now = new DateTimeImmutable('now');
|
|
|
|
$threeMonths = $now->modify('+3 months');
|
|
|
|
|
|
|
|
header(sprintf(
|
|
|
|
'Set-Cookie: msz_auth=%s; Expires=%s; Max-Age=%d; Domain=%s; Path=/; SameSite=Lax; HttpOnly;%s',
|
|
|
|
$packed,
|
|
|
|
$threeMonths->format('D, d M Y H:i:s e'),
|
|
|
|
$threeMonths->getTimestamp() - $now->getTimestamp(),
|
|
|
|
self::domain(),
|
|
|
|
filter_has_var(INPUT_SERVER, 'HTTPS') ? ' Secure' : ''
|
|
|
|
));
|
2023-08-03 01:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function nuke(): void {
|
2024-01-24 22:14:42 +00:00
|
|
|
header(sprintf(
|
|
|
|
'Set-Cookie: msz_auth=; Expires=Wed, 31 Dec 1969 21:29:59 UTC; Max-Age=-9001; Domain=%s; Path=/; SameSite=Lax; HttpOnly;%s',
|
|
|
|
self::domain(),
|
|
|
|
filter_has_var(INPUT_SERVER, 'HTTPS') ? ' Secure' : ''
|
|
|
|
));
|
2023-08-03 01:35:08 +00:00
|
|
|
}
|
|
|
|
}
|