Skip to content

Commit 5d643d8

Browse files
committed
Merge pull request #8 from skipperbent/feature-csrf
Custom CSRF middleware support
2 parents f49fa5d + d6cf5c9 commit 5d643d8

File tree

2 files changed

+68
-8
lines changed

2 files changed

+68
-8
lines changed

README.md

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,19 +197,49 @@ function csrf_token() {
197197
}
198198
```
199199

200-
### Example for getting the url
200+
## Getting urls
201201

202-
In ```routes.php``` we have added this route:
202+
**In ```routes.php``` we have added this route:**
203203

204-
```SimpleRouter::get('/item/{id}', 'myController@show', ['as' => 'item']);```
204+
```php
205+
SimpleRouter::get('/item/{id}', 'myController@show', ['as' => 'item']);
206+
```
207+
208+
**In the template we then call:**
209+
210+
```php
211+
url('item', ['id' => 22], ['category' => 'shoes']);
212+
```
213+
214+
**Result url is:**
215+
216+
```php
217+
/item/22/?category=shoes
218+
```
219+
220+
## Custom CSRF verifier
205221

206-
In the template we then call:
222+
Create a new class and extend the ```BaseCsrfVerifier``` middleware class provided with simple-php-router.
207223

208-
```url('item', ['id' => 22], ['category' => 'shoes']);```
224+
Add the property ```except``` with an array of the urls to the routes you would like to exclude from the CSRF validation. Using ```*``` at the end for the url will match the entire url.
209225

210-
Result url is:
226+
Querystrings are ignored.
211227

212-
```/item/22?category=shoes ```
228+
```php
229+
use Pecee\Http\Middleware\BaseCsrfVerifier;
230+
231+
class CsrfVerifier extends BaseCsrfVerifier {
232+
233+
protected $except = ['/companies/*', '/user/save'];
234+
235+
}
236+
```
237+
238+
Register the new class in your ```routes.php```, custom ```Router``` class or wherever you register your routes.
239+
240+
```php
241+
SimpleRouter::csrfVerifier(new \Demo\Middleware\CsrfVerifier());
242+
```
213243

214244
## Documentation
215245
While I work on a better documentation, please refer to the Laravel 5 routing documentation here:

src/Pecee/Http/Middleware/BaseCsrfVerifier.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,39 @@ class BaseCsrfVerifier extends Middleware {
1111
const POST_KEY = 'csrf-token';
1212
const HEADER_KEY = 'X-CSRF-TOKEN';
1313

14+
protected $except;
15+
16+
/**
17+
* Check if the url matches the urls in the except property
18+
* @param Request $request
19+
* @return bool
20+
*/
21+
protected function skip(Request $request) {
22+
23+
if($this->except === null || !is_array($this->except)) {
24+
return false;
25+
}
26+
27+
foreach($this->except as $url) {
28+
$url = rtrim($url, '/');
29+
if($url[strlen($url)-1] === '*') {
30+
$url = rtrim($url, '*');
31+
$skip = (stripos($request->getUri(), $url) === 0);
32+
} else {
33+
$skip = ($url === rtrim($request->getUri(), '/'));
34+
}
35+
36+
if($skip) {
37+
return true;
38+
}
39+
}
40+
41+
return false;
42+
}
43+
1444
public function handle(Request $request) {
1545

16-
if($request->getMethod() != 'get') {
46+
if($request->getMethod() != 'get' && !$this->skip($request)) {
1747

1848
$token = (isset($_POST[self::POST_KEY])) ? $_POST[self::POST_KEY] : null;
1949

0 commit comments

Comments
 (0)