1818use CodeIgniter \I18n \Time ;
1919use CodeIgniter \Security \Exceptions \SecurityException ;
2020use CodeIgniter \Session \Session ;
21- use Config \App ;
2221use Config \Cookie as CookieConfig ;
2322use Config \Security as SecurityConfig ;
2423use Config \Services ;
@@ -44,13 +43,17 @@ class Security implements SecurityInterface
4443 * Protection Method for Cross Site Request Forgery protection.
4544 *
4645 * @var string 'cookie' or 'session'
46+ *
47+ * @deprecated 4.4.0 Use $this->config->csrfProtection.
4748 */
4849 protected $ csrfProtection = self ::CSRF_PROTECTION_COOKIE ;
4950
5051 /**
5152 * CSRF Token Randomization
5253 *
5354 * @var bool
55+ *
56+ * @deprecated 4.4.0 Use $this->config->tokenRandomize.
5457 */
5558 protected $ tokenRandomize = false ;
5659
@@ -69,6 +72,8 @@ class Security implements SecurityInterface
6972 * Token name for Cross Site Request Forgery protection.
7073 *
7174 * @var string
75+ *
76+ * @deprecated 4.4.0 Use $this->config->tokenName.
7277 */
7378 protected $ tokenName = 'csrf_token_name ' ;
7479
@@ -78,6 +83,8 @@ class Security implements SecurityInterface
7883 * Header name for Cross Site Request Forgery protection.
7984 *
8085 * @var string
86+ *
87+ * @deprecated 4.4.0 Use $this->config->headerName.
8188 */
8289 protected $ headerName = 'X-CSRF-TOKEN ' ;
8390
@@ -105,6 +112,8 @@ class Security implements SecurityInterface
105112 * Defaults to two hours (in seconds).
106113 *
107114 * @var int
115+ *
116+ * @deprecated 4.4.0 Use $this->config->expires.
108117 */
109118 protected $ expires = 7200 ;
110119
@@ -114,6 +123,8 @@ class Security implements SecurityInterface
114123 * Regenerate CSRF Token on every request.
115124 *
116125 * @var bool
126+ *
127+ * @deprecated 4.4.0 Use $this->config->regenerate.
117128 */
118129 protected $ regenerate = true ;
119130
@@ -123,6 +134,8 @@ class Security implements SecurityInterface
123134 * Redirect to previous page with error on failure.
124135 *
125136 * @var bool
137+ *
138+ * @deprecated 4.4.0 Use $this->config->redirect.
126139 */
127140 protected $ redirect = false ;
128141
@@ -163,35 +176,22 @@ class Security implements SecurityInterface
163176 */
164177 private ?string $ hashInCookie = null ;
165178
179+ /**
180+ * Security Config
181+ */
182+ protected SecurityConfig $ config ;
183+
166184 /**
167185 * Constructor.
168186 *
169187 * Stores our configuration and fires off the init() method to setup
170188 * initial state.
171189 */
172- public function __construct (App $ config )
190+ public function __construct (SecurityConfig $ config )
173191 {
174- $ security = config (SecurityConfig::class);
175-
176- // Store CSRF-related configurations
177- if ($ security instanceof SecurityConfig) {
178- $ this ->csrfProtection = $ security ->csrfProtection ?? $ this ->csrfProtection ;
179- $ this ->tokenName = $ security ->tokenName ?? $ this ->tokenName ;
180- $ this ->headerName = $ security ->headerName ?? $ this ->headerName ;
181- $ this ->regenerate = $ security ->regenerate ?? $ this ->regenerate ;
182- $ this ->redirect = $ security ->redirect ?? $ this ->redirect ;
183- $ this ->rawCookieName = $ security ->cookieName ?? $ this ->rawCookieName ;
184- $ this ->expires = $ security ->expires ?? $ this ->expires ;
185- $ this ->tokenRandomize = $ security ->tokenRandomize ?? $ this ->tokenRandomize ;
186- } else {
187- // `Config/Security.php` is absence
188- $ this ->tokenName = $ config ->CSRFTokenName ?? $ this ->tokenName ;
189- $ this ->headerName = $ config ->CSRFHeaderName ?? $ this ->headerName ;
190- $ this ->regenerate = $ config ->CSRFRegenerate ?? $ this ->regenerate ;
191- $ this ->rawCookieName = $ config ->CSRFCookieName ?? $ this ->rawCookieName ;
192- $ this ->expires = $ config ->CSRFExpire ?? $ this ->expires ;
193- $ this ->redirect = $ config ->CSRFRedirect ?? $ this ->redirect ;
194- }
192+ $ this ->config = $ config ;
193+
194+ $ this ->rawCookieName = $ config ->cookieName ;
195195
196196 if ($ this ->isCSRFCookie ()) {
197197 $ cookie = config (CookieConfig::class);
@@ -213,7 +213,7 @@ public function __construct(App $config)
213213
214214 private function isCSRFCookie (): bool
215215 {
216- return $ this ->csrfProtection === self ::CSRF_PROTECTION_COOKIE ;
216+ return $ this ->config -> csrfProtection === self ::CSRF_PROTECTION_COOKIE ;
217217 }
218218
219219 private function configureSession (): void
@@ -287,7 +287,7 @@ public function verify(RequestInterface $request)
287287 $ postedToken = $ this ->getPostedToken ($ request );
288288
289289 try {
290- $ token = ($ postedToken !== null && $ this ->tokenRandomize )
290+ $ token = ($ postedToken !== null && $ this ->config -> tokenRandomize )
291291 ? $ this ->derandomize ($ postedToken ) : $ postedToken ;
292292 } catch (InvalidArgumentException $ e ) {
293293 $ token = null ;
@@ -300,7 +300,7 @@ public function verify(RequestInterface $request)
300300
301301 $ this ->removeTokenInRequest ($ request );
302302
303- if ($ this ->regenerate ) {
303+ if ($ this ->config -> regenerate ) {
304304 $ this ->generateHash ();
305305 }
306306
@@ -318,13 +318,13 @@ private function removeTokenInRequest(RequestInterface $request): void
318318
319319 $ json = json_decode ($ request ->getBody () ?? '' );
320320
321- if (isset ($ _POST [$ this ->tokenName ])) {
321+ if (isset ($ _POST [$ this ->config -> tokenName ])) {
322322 // We kill this since we're done and we don't want to pollute the POST array.
323- unset($ _POST [$ this ->tokenName ]);
323+ unset($ _POST [$ this ->config -> tokenName ]);
324324 $ request ->setGlobal ('post ' , $ _POST );
325- } elseif (isset ($ json ->{$ this ->tokenName })) {
325+ } elseif (isset ($ json ->{$ this ->config -> tokenName })) {
326326 // We kill this since we're done and we don't want to pollute the JSON data.
327- unset($ json ->{$ this ->tokenName });
327+ unset($ json ->{$ this ->config -> tokenName });
328328 $ request ->setBody (json_encode ($ json ));
329329 }
330330 }
@@ -335,19 +335,19 @@ private function getPostedToken(RequestInterface $request): ?string
335335
336336 // Does the token exist in POST, HEADER or optionally php:://input - json data.
337337
338- if ($ tokenValue = $ request ->getPost ($ this ->tokenName )) {
338+ if ($ tokenValue = $ request ->getPost ($ this ->config -> tokenName )) {
339339 return $ tokenValue ;
340340 }
341341
342- if ($ request ->hasHeader ($ this ->headerName ) && ! empty ($ request ->header ($ this ->headerName )->getValue ())) {
343- return $ request ->header ($ this ->headerName )->getValue ();
342+ if ($ request ->hasHeader ($ this ->config -> headerName ) && ! empty ($ request ->header ($ this -> config ->headerName )->getValue ())) {
343+ return $ request ->header ($ this ->config -> headerName )->getValue ();
344344 }
345345
346346 $ body = (string ) $ request ->getBody ();
347347 $ json = json_decode ($ body );
348348
349349 if ($ body !== '' && ! empty ($ json ) && json_last_error () === JSON_ERROR_NONE ) {
350- return $ json ->{$ this ->tokenName } ?? null ;
350+ return $ json ->{$ this ->config -> tokenName } ?? null ;
351351 }
352352
353353 return null ;
@@ -358,7 +358,7 @@ private function getPostedToken(RequestInterface $request): ?string
358358 */
359359 public function getHash (): ?string
360360 {
361- return $ this ->tokenRandomize ? $ this ->randomize ($ this ->hash ) : $ this ->hash ;
361+ return $ this ->config -> tokenRandomize ? $ this ->randomize ($ this ->hash ) : $ this ->hash ;
362362 }
363363
364364 /**
@@ -407,23 +407,23 @@ protected function derandomize(string $token): string
407407 */
408408 public function getTokenName (): string
409409 {
410- return $ this ->tokenName ;
410+ return $ this ->config -> tokenName ;
411411 }
412412
413413 /**
414414 * Returns the CSRF Header Name.
415415 */
416416 public function getHeaderName (): string
417417 {
418- return $ this ->headerName ;
418+ return $ this ->config -> headerName ;
419419 }
420420
421421 /**
422422 * Returns the CSRF Cookie Name.
423423 */
424424 public function getCookieName (): string
425425 {
426- return $ this ->cookieName ;
426+ return $ this ->config -> cookieName ;
427427 }
428428
429429 /**
@@ -443,7 +443,7 @@ public function isExpired(): bool
443443 */
444444 public function shouldRedirect (): bool
445445 {
446- return $ this ->redirect ;
446+ return $ this ->config -> redirect ;
447447 }
448448
449449 /**
@@ -521,9 +521,9 @@ private function restoreHash(): void
521521 if ($ this ->isHashInCookie ()) {
522522 $ this ->hash = $ this ->hashInCookie ;
523523 }
524- } elseif ($ this ->session ->has ($ this ->tokenName )) {
524+ } elseif ($ this ->session ->has ($ this ->config -> tokenName )) {
525525 // Session based CSRF protection
526- $ this ->hash = $ this ->session ->get ($ this ->tokenName );
526+ $ this ->hash = $ this ->session ->get ($ this ->config -> tokenName );
527527 }
528528 }
529529
@@ -562,7 +562,7 @@ private function saveHashInCookie(): void
562562 $ this ->rawCookieName ,
563563 $ this ->hash ,
564564 [
565- 'expires ' => $ this ->expires === 0 ? 0 : Time::now ()->getTimestamp () + $ this ->expires ,
565+ 'expires ' => $ this ->config -> expires === 0 ? 0 : Time::now ()->getTimestamp () + $ this -> config ->expires ,
566566 ]
567567 );
568568
@@ -606,6 +606,6 @@ protected function doSendCookie(): void
606606
607607 private function saveHashInSession (): void
608608 {
609- $ this ->session ->set ($ this ->tokenName , $ this ->hash );
609+ $ this ->session ->set ($ this ->config -> tokenName , $ this ->hash );
610610 }
611611}
0 commit comments