Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options to set secure and httpOnly cookie flags #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 91 additions & 30 deletions src/SlmLocale/Strategy/CookieStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,80 +48,93 @@
class CookieStrategy extends AbstractStrategy
{
const COOKIE_NAME = 'slm_locale';

/**
* The name of the cookie.
*
* @var string
*/
protected $cookieName;


protected $cookieHttpOnly = false;

protected $cookieSecure = false;

public function setOptions(array $options = [])
{
if (array_key_exists('cookie_name', $options)) {
$this->setCookieName($options['cookie_name']);
}
if (array_key_exists('cookie_http_only', $options)) {
$this->setCookieHttpOnly($options['cookie_http_only']);
}
if (array_key_exists('cookie_secure', $options)) {
$this->setCookieSecure($options['cookie_secure']);
}
}

public function detect(LocaleEvent $event)
{
$request = $event->getRequest();
$cookieName = $this->getCookieName();

if (! $this->isHttpRequest($request)) {
return;
}
if (! $event->hasSupported()) {
return;
}

$cookie = $request->getCookie();
if (! $cookie || ! $cookie->offsetExists($cookieName)) {
return;
}

$locale = $cookie->offsetGet($cookieName);
$supported = $event->getSupported();

if (! in_array($locale, $supported)) {
return;
}

return $locale;
}

public function found(LocaleEvent $event)
{
$locale = $event->getLocale();
$request = $event->getRequest();
$cookieName = $this->getCookieName();

if (! $this->isHttpRequest($request)) {
return;
}

$cookie = $request->getCookie();

// Omit Set-Cookie header when cookie is present
if ($cookie instanceof Cookie
&& $cookie->offsetExists($cookieName)
&& $locale === $cookie->offsetGet($cookieName)
) {
return;
}

$path = '/';

if (method_exists($request, 'getBasePath')) {
$path = rtrim($request->getBasePath(), '/') . '/';
}

$response = $event->getResponse();
$setCookie = new SetCookie($cookieName, $locale, null, $path);

$response->getHeaders()->addHeader($setCookie);
) {
return;
}

$path = '/';

if (method_exists($request, 'getBasePath')) {
$path = rtrim($request->getBasePath(), '/') . '/';
}

$secure = $this->getCookieSecure();
$httpOnly = $this->getCookieHttpOnly();

$response = $event->getResponse();
$setCookie = new SetCookie($cookieName, $locale, null, $path, null, $secure, $httpOnly);

$response->getHeaders()->addHeader($setCookie);
}

/**
* @return string
*/
Expand All @@ -130,20 +143,68 @@ public function getCookieName()
if (null === $this->cookieName) {
return self::COOKIE_NAME;
}

return (string) $this->cookieName;
}

/**
* @param string $cookieName
* @throws InvalidArgumentException
* @return self
*/
public function setCookieName($cookieName)
{
if (! preg_match('/^(?!\$)[!-~]+$/', $cookieName)) {
throw new InvalidArgumentException($cookieName . ' is not a vaild cookie name.');
}

$this->cookieName = $cookieName;
return $this;
}

/**
* @return bool
*/
public function getCookieHttpOnly()
{
return (bool) $this->cookieHttpOnly;
}

/**
* @param bool $cookieHttpOnly
* @throws InvalidArgumentException
* @return self
*/
public function setCookieHttpOnly($cookieHttpOnly)
{
if (! is_bool($cookieHttpOnly)) {
throw new InvalidArgumentException($cookieHttpOnly . ' is not a vaild cookie http only setting.');
}

$this->cookieHttpOnly = $cookieHttpOnly;
return $this;
}

/**
* @return bool
*/
public function getCookieSecure()
{
return (bool) $this->cookieSecure;
}

/**
* @param bool $cookieSecure
* @throws InvalidArgumentException
* @return self
*/
public function setCookieSecure($cookieSecure)
{
if (! is_bool($cookieSecure)) {
throw new InvalidArgumentException($cookieSecure . ' is not a vaild cookie secure setting.');
}

$this->cookieSecure = $cookieSecure;
return $this;
}
}