mirror of
https://github.com/bitinflow/accounts.git
synced 2026-03-13 13:35:52 +00:00
82 lines
1.9 KiB
PHP
82 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace Bitinflow\Accounts;
|
|
|
|
use Carbon\Carbon;
|
|
use Firebase\JWT\JWT;
|
|
use Illuminate\Contracts\Config\Repository as Config;
|
|
use Illuminate\Contracts\Encryption\Encrypter;
|
|
use Symfony\Component\HttpFoundation\Cookie;
|
|
|
|
class ApiTokenCookieFactory
|
|
{
|
|
/**
|
|
* The configuration repository implementation.
|
|
*
|
|
* @var Config
|
|
*/
|
|
protected $config;
|
|
|
|
/**
|
|
* The encrypter implementation.
|
|
*
|
|
* @var Encrypter
|
|
*/
|
|
protected $encrypter;
|
|
|
|
/**
|
|
* Create an API token cookie factory instance.
|
|
*
|
|
* @param Config $config
|
|
* @param Encrypter $encrypter
|
|
* @return void
|
|
*/
|
|
public function __construct(Config $config, Encrypter $encrypter)
|
|
{
|
|
$this->config = $config;
|
|
$this->encrypter = $encrypter;
|
|
}
|
|
|
|
/**
|
|
* Create a new API token cookie.
|
|
*
|
|
* @param mixed $userId
|
|
* @param string $csrfToken
|
|
* @return Cookie
|
|
*/
|
|
public function make($userId, string $csrfToken): Cookie
|
|
{
|
|
$config = $this->config->get('session');
|
|
|
|
$expiration = Carbon::now()->addMinutes($config['lifetime']);
|
|
|
|
return new Cookie(
|
|
BitinflowAccounts::cookie(),
|
|
$this->createToken($userId, $csrfToken, $expiration),
|
|
$expiration,
|
|
$config['path'],
|
|
$config['domain'],
|
|
$config['secure'],
|
|
true,
|
|
false,
|
|
$config['same_site'] ?? null
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Create a new JWT token for the given user ID and CSRF token.
|
|
*
|
|
* @param mixed $userId
|
|
* @param string $csrfToken
|
|
* @param Carbon $expiration
|
|
* @return string
|
|
*/
|
|
protected function createToken($userId, string $csrfToken, Carbon $expiration): string
|
|
{
|
|
return JWT::encode([
|
|
'sub' => $userId,
|
|
'csrf' => $csrfToken,
|
|
'expiry' => $expiration->getTimestamp(),
|
|
], $this->encrypter->getKey());
|
|
}
|
|
} |