1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
<?php
namespace SimpleSAML\Module\consent\Consent\Store;
/**
* Cookie storage for consent
*
* This class implements a consent store which stores the consent information in cookies on the users computer.
*
* Example - Consent module with cookie store:
*
* <code>
* 'authproc' => array(
* array(
* 'consent:Consent',
* 'store' => 'consent:Cookie',
* ),
* ),
* </code>
*
* @author Olav Morken <olav.morken@uninett.no>
* @package SimpleSAMLphp
*/
class Cookie extends \SimpleSAML\Module\consent\Store
{
/**
* @var string Cookie name prefix
*/
private $name;
/**
* @var int Cookie lifetime
*/
private $lifetime;
/**
* @var string Cookie path
*/
private $path;
/**
* @var string Cookie domain
*/
private $domain = '';
/**
* @var bool Cookie secure flag
*/
private $secure;
/**
* @var string|null Cookie samesite flag
*/
private $samesite = null;
/**
* Parse configuration.
*
* This constructor parses the configuration.
*
* @param array $config Configuration for database consent store.
*
* @throws \Exception in case of a configuration error.
*/
public function __construct(array $config)
{
parent::__construct($config);
if (array_key_exists('name', $config)) {
$this->name = $config['name'];
} else {
$this->name = '\SimpleSAML\Module\consent';
}
if (array_key_exists('lifetime', $config)) {
$this->lifetime = (int) $config['lifetime'];
} else {
$this->lifetime = 7776000; // (90*24*60*60)
}
if (array_key_exists('path', $config)) {
$this->path = $config['path'];
} else {
$globalConfig = \SimpleSAML\Configuration::getInstance();
$this->path = $globalConfig->getBasePath();
}
if (array_key_exists('domain', $config)) {
$this->domain = $config['domain'];
}
if (array_key_exists('secure', $config)) {
$this->secure = (bool) $config['secure'];
} else {
$this->secure = \SimpleSAML\Utils\HTTP::isHTTPS();
}
if (array_key_exists('samesite', $config)) {
$this->samesite = $config['samesite'];
}
}
/**
* Check for consent.
*
* This function checks whether a given user has authorized the release of the attributes identified by
* $attributeSet from $source to $destination.
*
* @param string $userId The hash identifying the user at an IdP.
* @param string $destinationId A string which identifies the destination.
* @param string $attributeSet A hash which identifies the attributes.
*
* @return bool True if the user has given consent earlier, false if not (or on error).
*/
public function hasConsent($userId, $destinationId, $attributeSet)
{
assert(is_string($userId));
assert(is_string($destinationId));
assert(is_string($attributeSet));
$cookieName = $this->getCookieName($userId, $destinationId);
$data = $userId.':'.$attributeSet.':'.$destinationId;
\SimpleSAML\Logger::debug('Consent cookie - Get ['.$data.']');
if (!array_key_exists($cookieName, $_COOKIE)) {
\SimpleSAML\Logger::debug(
'Consent cookie - no cookie with name \''.$cookieName.'\'.'
);
return false;
}
if (!is_string($_COOKIE[$cookieName])) {
\SimpleSAML\Logger::warning(
'Value of consent cookie wasn\'t a string. Was: '.
var_export($_COOKIE[$cookieName], true)
);
return false;
}
$data = self::sign($data);
if ($_COOKIE[$cookieName] !== $data) {
\SimpleSAML\Logger::info(
'Attribute set changed from the last time consent was given.'
);
return false;
}
\SimpleSAML\Logger::debug(
'Consent cookie - found cookie with correct name and value.'
);
return true;
}
/**
* Save consent.
*
* Called when the user asks for the consent to be saved. If consent information for the given user and destination
* already exists, it should be overwritten.
*
* @param string $userId The hash identifying the user at an IdP.
* @param string $destinationId A string which identifies the destination.
* @param string $attributeSet A hash which identifies the attributes.
*
* @return bool
*/
public function saveConsent($userId, $destinationId, $attributeSet)
{
assert(is_string($userId));
assert(is_string($destinationId));
assert(is_string($attributeSet));
$name = $this->getCookieName($userId, $destinationId);
$value = $userId.':'.$attributeSet.':'.$destinationId;
\SimpleSAML\Logger::debug('Consent cookie - Set ['.$value.']');
$value = self::sign($value);
return $this->setConsentCookie($name, $value);
}
/**
* Delete consent.
*
* Called when a user revokes consent for a given destination.
*
* @param string $userId The hash identifying the user at an IdP.
* @param string $destinationId A string which identifies the destination.
*
* @return void
*/
public function deleteConsent($userId, $destinationId)
{
assert(is_string($userId));
assert(is_string($destinationId));
$name = $this->getCookieName($userId, $destinationId);
$this->setConsentCookie($name, null);
}
/**
* Delete consent.
*
* @param string $userId The hash identifying the user at an IdP.
*
* @return void This method does not return.
*
* @throws \Exception This method always throws an exception indicating that it is not possible to delete all given
* consents with this handler.
*/
public function deleteAllConsents($userId)
{
assert(is_string($userId));
throw new \Exception(
'The cookie consent handler does not support delete of all consents...'
);
}
/**
* Retrieve consents.
*
* This function should return a list of consents the user has saved.
*
* @param string $userId The hash identifying the user at an IdP.
*
* @return array Array of all destination ids the user has given consent for.
*/
public function getConsents($userId)
{
assert(is_string($userId));
$ret = [];
$cookieNameStart = $this->name . ':';
$cookieNameStartLen = strlen($cookieNameStart);
foreach ($_COOKIE as $name => $value) {
if (substr($name, 0, $cookieNameStartLen) !== $cookieNameStart) {
continue;
}
$value = self::verify($value);
if ($value === false) {
continue;
}
$tmp = explode(':', $value, 3);
if (count($tmp) !== 3) {
\SimpleSAML\Logger::warning(
'Consent cookie with invalid value: '.$value
);
continue;
}
if ($userId !== $tmp[0]) {
// Wrong user
continue;
}
$destination = $tmp[2];
$ret[] = $destination;
}
return $ret;
}
/**
* Calculate a signature of some data.
*
* This function calculates a signature of the data.
*
* @param string $data The data which should be signed.
*
* @return string The signed data.
*/
private static function sign($data)
{
assert(is_string($data));
$secretSalt = \SimpleSAML\Utils\Config::getSecretSalt();
return sha1($secretSalt.$data.$secretSalt).':'.$data;
}
/**
* Verify signed data.
*
* This function verifies signed data.
*
* @param string $signedData The data which is signed.
*
* @return string|false The data, or false if the signature is invalid.
*/
private static function verify($signedData)
{
assert(is_string($signedData));
$data = explode(':', $signedData, 2);
if (count($data) !== 2) {
\SimpleSAML\Logger::warning('Consent cookie: Missing signature.');
return false;
}
$data = $data[1];
$newSignedData = self::sign($data);
if ($newSignedData !== $signedData) {
\SimpleSAML\Logger::warning('Consent cookie: Invalid signature.');
return false;
}
return $data;
}
/**
* Get cookie name.
*
* This function gets the cookie name for the given user & destination.
*
* @param string $userId The hash identifying the user at an IdP.
* @param string $destinationId A string which identifies the destination.
*
* @return string The cookie name
*/
private function getCookieName($userId, $destinationId)
{
assert(is_string($userId));
assert(is_string($destinationId));
return $this->name . ':' . sha1($userId . ':' . $destinationId);
}
/**
* Helper function for setting a cookie.
*
* @param string $name Name of the cookie.
* @param string|null $value Value of the cookie. Set this to null to delete the cookie.
*
* @return bool
*/
private function setConsentCookie($name, $value)
{
assert(is_string($name));
assert(is_string($value) || is_null($value));
$globalConfig = \SimpleSAML\Configuration::getInstance();
$params = [
'lifetime' => $this->lifetime,
'path' => $this->path,
'domain' => $this->domain,
'httponly' => true,
'secure' => $this->secure,
'samesite' => $this->samesite,
];
try {
\SimpleSAML\Utils\HTTP::setCookie($name, $value, $params, false);
return true;
} catch (\SimpleSAML\Error\CannotSetCookie $e) {
return false;
}
}
}
|