97 lines
2.4 KiB
PHP
97 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Support;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
use Symfony\Component\HttpFoundation\Cookie;
|
|
|
|
class AuthCookie
|
|
{
|
|
public static function name(): string
|
|
{
|
|
return config('auth_cookie.name', 'auth_token');
|
|
}
|
|
|
|
public static function originalUserCookieName(): string
|
|
{
|
|
return config('auth_cookie.original_user_cookie', 'original_user_id');
|
|
}
|
|
|
|
public static function make(string $plainTextToken): Cookie
|
|
{
|
|
return cookie(
|
|
self::name(),
|
|
$plainTextToken,
|
|
config('auth_cookie.lifetime_minutes', 720),
|
|
'/',
|
|
null,
|
|
(bool) config('auth_cookie.secure', true),
|
|
true,
|
|
false,
|
|
config('auth_cookie.same_site', 'lax')
|
|
);
|
|
}
|
|
|
|
public static function forget(): Cookie
|
|
{
|
|
return cookie(
|
|
self::name(),
|
|
null,
|
|
-1,
|
|
'/',
|
|
null,
|
|
(bool) config('auth_cookie.secure', true),
|
|
true,
|
|
false,
|
|
config('auth_cookie.same_site', 'lax')
|
|
);
|
|
}
|
|
|
|
public static function makeOriginalUserId(string $userId): Cookie
|
|
{
|
|
return cookie(
|
|
self::originalUserCookieName(),
|
|
(string) $userId,
|
|
config('auth_cookie.lifetime_minutes', 720),
|
|
'/',
|
|
null,
|
|
(bool) config('auth_cookie.secure', true),
|
|
true,
|
|
false,
|
|
config('auth_cookie.same_site', 'lax')
|
|
);
|
|
}
|
|
|
|
public static function forgetOriginalUserId(): Cookie
|
|
{
|
|
return cookie(
|
|
self::originalUserCookieName(),
|
|
null,
|
|
-1,
|
|
'/',
|
|
null,
|
|
(bool) config('auth_cookie.secure', true),
|
|
true,
|
|
false,
|
|
config('auth_cookie.same_site', 'lax')
|
|
);
|
|
}
|
|
|
|
public static function attachAuthToken(JsonResponse $response, string $plainTextToken): JsonResponse
|
|
{
|
|
return $response->withCookie(self::make($plainTextToken));
|
|
}
|
|
|
|
public static function clearAuthCookies(JsonResponse $response): JsonResponse
|
|
{
|
|
return $response
|
|
->withCookie(self::forget())
|
|
->withCookie(self::forgetOriginalUserId());
|
|
}
|
|
|
|
public static function shouldExposeTokenInResponse(): bool
|
|
{
|
|
return (bool) config('auth_cookie.expose_token_in_response', false);
|
|
}
|
|
}
|