22
33namespace SimpleSAML \Module \consent \Consent \Store ;
44
5+ use Webmozart \Assert \Assert ;
6+
57/**
68 * Cookie storage for consent
79 *
@@ -38,9 +40,9 @@ class Cookie extends \SimpleSAML\Module\consent\Store
3840 */
3941 public function hasConsent ($ userId , $ destinationId , $ attributeSet )
4042 {
41- assert ( is_string ( $ userId) );
42- assert ( is_string ( $ destinationId) );
43- assert ( is_string ( $ attributeSet) );
43+ Assert:: string ( $ userId );
44+ Assert:: string ( $ destinationId );
45+ Assert:: string ( $ attributeSet );
4446
4547 $ cookieName = self ::getCookieName ($ userId , $ destinationId );
4648
@@ -89,21 +91,21 @@ public function hasConsent($userId, $destinationId, $attributeSet)
8991 * @param string $destinationId A string which identifies the destination.
9092 * @param string $attributeSet A hash which identifies the attributes.
9193 *
92- * @return void
94+ * @return bool
9395 */
9496 public function saveConsent ($ userId , $ destinationId , $ attributeSet )
9597 {
96- assert ( is_string ( $ userId) );
97- assert ( is_string ( $ destinationId) );
98- assert ( is_string ( $ attributeSet) );
98+ Assert:: string ( $ userId );
99+ Assert:: string ( $ destinationId );
100+ Assert:: string ( $ attributeSet );
99101
100102 $ name = self ::getCookieName ($ userId , $ destinationId );
101103 $ value = $ userId .': ' .$ attributeSet .': ' .$ destinationId ;
102104
103105 \SimpleSAML \Logger::debug ('Consent cookie - Set [ ' .$ value .'] ' );
104106
105107 $ value = self ::sign ($ value );
106- $ this ->setConsentCookie ($ name , $ value );
108+ return $ this ->setConsentCookie ($ name , $ value );
107109 }
108110
109111
@@ -119,8 +121,8 @@ public function saveConsent($userId, $destinationId, $attributeSet)
119121 */
120122 public function deleteConsent ($ userId , $ destinationId )
121123 {
122- assert ( is_string ( $ userId) );
123- assert ( is_string ( $ destinationId) );
124+ Assert:: string ( $ userId );
125+ Assert:: string ( $ destinationId );
124126
125127 $ name = self ::getCookieName ($ userId , $ destinationId );
126128 $ this ->setConsentCookie ($ name , null );
@@ -139,7 +141,7 @@ public function deleteConsent($userId, $destinationId)
139141 */
140142 public function deleteAllConsents ($ userId )
141143 {
142- assert ( is_string ( $ userId) );
144+ Assert:: string ( $ userId );
143145
144146 throw new \Exception (
145147 'The cookie consent handler does not support delete of all consents... '
@@ -158,7 +160,7 @@ public function deleteAllConsents($userId)
158160 */
159161 public function getConsents ($ userId )
160162 {
161- assert ( is_string ( $ userId) );
163+ Assert:: string ( $ userId );
162164
163165 $ ret = [];
164166
@@ -206,7 +208,7 @@ public function getConsents($userId)
206208 */
207209 private static function sign ($ data )
208210 {
209- assert ( is_string ( $ data) );
211+ Assert:: string ( $ data );
210212
211213 $ secretSalt = \SimpleSAML \Utils \Config::getSecretSalt ();
212214
@@ -225,7 +227,7 @@ private static function sign($data)
225227 */
226228 private static function verify ($ signedData )
227229 {
228- assert ( is_string ( $ signedData) );
230+ Assert:: string ( $ signedData );
229231
230232 $ data = explode (': ' , $ signedData , 2 );
231233 if (count ($ data ) !== 2 ) {
@@ -256,8 +258,8 @@ private static function verify($signedData)
256258 */
257259 private static function getCookieName ($ userId , $ destinationId )
258260 {
259- assert ( is_string ( $ userId) );
260- assert ( is_string ( $ destinationId) );
261+ Assert:: string ( $ userId );
262+ Assert:: string ( $ destinationId );
261263
262264 return '\SimpleSAML\Module\consent: ' .sha1 ($ userId .': ' .$ destinationId );
263265 }
@@ -269,12 +271,12 @@ private static function getCookieName($userId, $destinationId)
269271 * @param string $name Name of the cookie.
270272 * @param string|null $value Value of the cookie. Set this to null to delete the cookie.
271273 *
272- * @return void
274+ * @return bool
273275 */
274276 private function setConsentCookie ($ name , $ value )
275277 {
276- assert ( is_string ( $ name) );
277- assert ( is_string ( $ value) || $ value === null );
278+ Assert:: string ( $ name );
279+ Assert:: nullOrString ( $ value );
278280
279281 $ globalConfig = \SimpleSAML \Configuration::getInstance ();
280282 $ params = [
@@ -284,6 +286,11 @@ private function setConsentCookie($name, $value)
284286 'secure ' => \SimpleSAML \Utils \HTTP ::isHTTPS (),
285287 ];
286288
287- \SimpleSAML \Utils \HTTP ::setCookie ($ name , $ value , $ params , false );
289+ try {
290+ \SimpleSAML \Utils \HTTP ::setCookie ($ name , $ value , $ params , false );
291+ return true ;
292+ } catch (\SimpleSAML \Error \CannotSetCookie $ e ) {
293+ return false ;
294+ }
288295 }
289296}
0 commit comments