Final Class Yiisoft\Cookies\CookieEncryptor
| Inheritance | Yiisoft\Cookies\CookieEncryptor |
|---|
A CookieEncryptor encrypts the cookie value and validates whether the encrypted cookie value has been tampered with.
See also Yiisoft\Cookies\Cookie.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Cookies\CookieEncryptor | |
| decrypt() | Returns a new cookie instance with the decrypted cookie value. | Yiisoft\Cookies\CookieEncryptor |
| encrypt() | Returns a new cookie instance with the encrypted cookie value. | Yiisoft\Cookies\CookieEncryptor |
| isEncrypted() | Checks whether the cookie value is validly encrypted. | Yiisoft\Cookies\CookieEncryptor |
Method Details
| public mixed __construct ( string $key ) | ||
| $key | string |
The secret key used to encrypt and decrypt cookie values. |
public function __construct(string $key)
{
$this->crypt = new Crypt();
$this->key = $key;
}
Returns a new cookie instance with the decrypted cookie value.
| public Yiisoft\Cookies\Cookie decrypt ( Yiisoft\Cookies\Cookie $cookie ) | ||
| $cookie | Yiisoft\Cookies\Cookie |
The cookie with encrypted value. |
| return | Yiisoft\Cookies\Cookie |
The cookie with decrypted value. |
|---|---|---|
| throws | RuntimeException |
If the cookie value is tampered with or not validly encrypted. If you are not sure that the value of the cookie file was encrypted earlier, then first use the {@see \Yiisoft\Cookies\isEncrypted()}. |
public function decrypt(Cookie $cookie): Cookie
{
if (!$this->isEncrypted($cookie)) {
throw new RuntimeException("The \"{$cookie->getName()}\" cookie value is not validly encrypted.");
}
try {
/**
* @psalm-suppress PossiblyFalseArgument Length of the cookie value is checked in the {@see isEncrypted()}
* method and it is greater than 32, so `substr()` never returns false. This is actual for PHP 7.4 only.
*/
$value = rawurldecode(substr($cookie->getValue(), 32));
return $cookie->withValue($this->crypt->decryptByKey($value, $this->key, $cookie->getName()));
} catch (AuthenticationException $e) {
throw new RuntimeException("The \"{$cookie->getName()}\" cookie value was tampered with.");
}
}
Returns a new cookie instance with the encrypted cookie value.
| public Yiisoft\Cookies\Cookie encrypt ( Yiisoft\Cookies\Cookie $cookie ) | ||
| $cookie | Yiisoft\Cookies\Cookie |
The cookie with clean value. |
| return | Yiisoft\Cookies\Cookie |
The cookie with encrypted value. |
|---|---|---|
| throws | RuntimeException |
If the cookie value is already encrypted. |
public function encrypt(Cookie $cookie): Cookie
{
if ($this->isEncrypted($cookie)) {
throw new RuntimeException("The \"{$cookie->getName()}\" cookie value is already encrypted.");
}
$value = $this->crypt->encryptByKey($cookie->getValue(), $this->key, $cookie->getName());
return $cookie->withValue($this->prefix($cookie) . rawurlencode($value));
}
Checks whether the cookie value is validly encrypted.
| public boolean isEncrypted ( Yiisoft\Cookies\Cookie $cookie ) | ||
| $cookie | Yiisoft\Cookies\Cookie |
The cookie to check. |
| return | boolean |
Whether the cookie value is validly encrypted. |
|---|---|---|
public function isEncrypted(Cookie $cookie): bool
{
return strlen($cookie->getValue()) > 32 && strpos($cookie->getValue(), $this->prefix($cookie)) === 0;
}
Signup or Login in order to comment.