mirror of
https://github.com/flarum/framework.git
synced 2024-11-30 13:36:10 +08:00
commit
27b9c169fa
63
framework/core/src/Http/CookieFactory.php
Normal file
63
framework/core/src/Http/CookieFactory.php
Normal file
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Http;
|
||||
|
||||
use Dflydev\FigCookies\SetCookie;
|
||||
use Flarum\Foundation\Application;
|
||||
|
||||
class CookieFactory
|
||||
{
|
||||
/**
|
||||
* @var Application
|
||||
*/
|
||||
protected $app;
|
||||
|
||||
/**
|
||||
* @param Application $app
|
||||
*/
|
||||
public function __construct(Application $app)
|
||||
{
|
||||
$this->app = $app;
|
||||
}
|
||||
|
||||
/**
|
||||
* make a new cookie instance.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $value
|
||||
* @param int $maxAge
|
||||
* @param string $path
|
||||
* @param bool $secure
|
||||
* @param bool $httpOnly
|
||||
* @param string $domain
|
||||
* @return \Dflydev\FigCookies\SetCookie
|
||||
*/
|
||||
public function make($name, $value = null, $maxAge = null, $path = null, $secure = null, $httpOnly = true, $domain = null)
|
||||
{
|
||||
$url = parse_url(rtrim($this->app->url(), '/'));
|
||||
|
||||
if ($path === null) {
|
||||
$path = array_get($url, 'path') ?: '/';
|
||||
}
|
||||
|
||||
if ($secure === null && array_get($url, 'scheme') === 'https') {
|
||||
$secure = true;
|
||||
}
|
||||
|
||||
return SetCookie::create($name, $value)
|
||||
->withMaxAge($maxAge)
|
||||
->withPath($path)
|
||||
->withSecure($secure)
|
||||
->withHttpOnly($httpOnly)
|
||||
->withDomain($domain);
|
||||
}
|
||||
}
|
|
@ -12,7 +12,7 @@
|
|||
namespace Flarum\Http\Middleware;
|
||||
|
||||
use Dflydev\FigCookies\FigResponseCookies;
|
||||
use Dflydev\FigCookies\SetCookie;
|
||||
use Flarum\Http\CookieFactory;
|
||||
use Illuminate\Support\Str;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
|
@ -22,6 +22,20 @@ use Zend\Stratigility\MiddlewareInterface;
|
|||
|
||||
class StartSession implements MiddlewareInterface
|
||||
{
|
||||
/**
|
||||
* @var CookieFactory
|
||||
*/
|
||||
protected $cookie;
|
||||
|
||||
/**
|
||||
* Rememberer constructor.
|
||||
* @param CookieFactoy $cookie
|
||||
*/
|
||||
public function __construct(CookieFactory $cookie)
|
||||
{
|
||||
$this->cookie = $cookie;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
@ -65,10 +79,7 @@ class StartSession implements MiddlewareInterface
|
|||
{
|
||||
return FigResponseCookies::set(
|
||||
$response,
|
||||
SetCookie::create($session->getName(), $session->getId())
|
||||
->withPath('/')
|
||||
->withHttpOnly(true)
|
||||
->withSecure(true)
|
||||
$this->cookie->make($session->getName(), $session->getId())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,27 +12,41 @@
|
|||
namespace Flarum\Http;
|
||||
|
||||
use Dflydev\FigCookies\FigResponseCookies;
|
||||
use Dflydev\FigCookies\SetCookie;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
class Rememberer
|
||||
{
|
||||
protected $cookieName = 'flarum_remember';
|
||||
|
||||
/**
|
||||
* @var CookieFactory
|
||||
*/
|
||||
protected $cookie;
|
||||
|
||||
/**
|
||||
* Rememberer constructor.
|
||||
* @param CookieFactoy $cookie
|
||||
*/
|
||||
public function __construct(CookieFactory $cookie)
|
||||
{
|
||||
$this->cookie = $cookie;
|
||||
}
|
||||
|
||||
public function remember(ResponseInterface $response, AccessToken $token, $session = false)
|
||||
{
|
||||
$cookie = $this->createCookie()->withValue($token->id);
|
||||
$lifetime = null;
|
||||
|
||||
if (! $session) {
|
||||
$lifetime = 60 * 60 * 24 * 14;
|
||||
|
||||
$token->lifetime = $lifetime;
|
||||
$token->save();
|
||||
|
||||
$cookie = $cookie->withMaxAge($lifetime);
|
||||
}
|
||||
|
||||
return FigResponseCookies::set($response, $cookie);
|
||||
return FigResponseCookies::set(
|
||||
$response,
|
||||
$this->cookie->make($this->cookieName, $token->id, $lifetime)
|
||||
);
|
||||
}
|
||||
|
||||
public function rememberUser(ResponseInterface $response, $userId)
|
||||
|
@ -46,11 +60,4 @@ class Rememberer
|
|||
{
|
||||
return FigResponseCookies::expire($response, $this->cookieName);
|
||||
}
|
||||
|
||||
private function createCookie()
|
||||
{
|
||||
return SetCookie::create($this->cookieName)
|
||||
->withPath('/')
|
||||
->withHttpOnly(true);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user