You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/modules/anchor/csrf/index.md
+33-31Lines changed: 33 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@
10
10
11
11
</div>
12
12
13
-
This package is leaf's implementation of CSRF default protection with leaf anchor. It comes separated from leaf anchor because it is not needed in every project you may build.
13
+
A CSRF (Cross-Site Request Forgery) attack tricks a user into performing unwanted actions on your website without their knowledge. This can be done by sending a request to your website from another website the user is logged into. To prevent this, Leaf provides a powerful CSRF protection module that handles all the funny business for you.
14
14
15
15
## Installation
16
16
@@ -28,7 +28,13 @@ leaf install csrf
28
28
29
29
## Basic Usage
30
30
31
-
After installing leaf CSRF, leaf automatically loads the CSRF package for you, so you don't need to do anything unless you want to configure the CSRF module to match your application requirements.
31
+
Once installed, you can enable CSRF protection in your app by passing CSRF config to your app instance. Since CSRF is a Leaf module, it comes with first-class support for Leaf apps.
32
+
33
+
```php
34
+
app()->csrf();
35
+
36
+
// ... your app
37
+
```
32
38
33
39
### Using CSRF outside of leaf
34
40
@@ -46,20 +52,20 @@ Just like every other leaf module, this module also allows you to customize it t
46
52
47
53
**Available config:**
48
54
49
-
-**SECRET_KEY** - This is the key with which the token is saved and used in your leaf app. If this is not specified, leaf uses the name `_token` as done in other frameworks like Laravel.
55
+
-**secretKey** - This is the key with which the token is saved and used in your leaf app. If this is not specified, leaf uses the name `_token` as done in other frameworks like Laravel.
50
56
51
-
-**SECRET** - This is the secret key used to encrypt the token. Leaf also has a default secret key set for you. Note that the secret key is attached to a set of unique numbers that not even leaf knows.
57
+
-**secret** - This is the secret key used to encrypt the token. Leaf also has a default secret key set for you. Note that the secret key is attached to a set of unique numbers that not even leaf knows.
52
58
53
-
-**EXCEPT** - This is an array of routes that you want to exclude from the CSRF protection.
59
+
-**except** - This is an array of routes that you want to exclude from the CSRF protection.
54
60
55
-
-**METHODS** - This is an array of HTTP methods to apply CSRF protection to. By default, leaf uses `["POST", "PUT", "PATCH", "DELETE"]`
61
+
-**methods** - This is an array of HTTP methods to apply CSRF protection to. By default, leaf uses `["POST", "PUT", "PATCH", "DELETE"]`
56
62
57
63
```php
58
64
use Leaf\Anchor\CSRF;
59
65
60
66
CSRF::config([
61
-
"METHODS" => ["GET"],
62
-
"EXCEPT" => ["/"],
67
+
'methods' => ['GET'],
68
+
'except' => ['/'],
63
69
]);
64
70
```
65
71
@@ -70,13 +76,7 @@ A token is generated under the hood for your application, you can get this token
70
76
```php
71
77
$csrfToken = Leaf\Anchor\CSRF::token();
72
78
73
-
>> ["_token" => "TOKEN VALUE"]
74
-
```
75
-
76
-
To make things a bit easier, `token` returns associative array holding the token key name and the token itself. This is an example JSON represenation.
77
-
78
-
```json
79
-
{"_token": "TOKEN VALUE"}
79
+
>> "TOKEN VALUE"
80
80
```
81
81
82
82
## Form
@@ -90,27 +90,29 @@ You would usually want to append a hidden input field holding the token to a for
90
90
</form>
91
91
```
92
92
93
-
## Functional Mode
94
-
95
-
Just as with other modules, leaf csrf also ships with global functions that make development a lot easier.
96
-
97
-
### _token
93
+
## Error Handling
98
94
99
-
This method returns the CSRF token just as done with the `token` method above.
95
+
By default, Leaf will output a built-in error page when a CSRF token is invalid. You can customize the messages shown on this page by updating your `config` object.
100
96
101
97
```php
102
-
$csrfToken = _token();
103
-
104
-
>> ["_token" => "TOKEN VALUE"]
98
+
app()->csrf([
99
+
'messages.tokenNotFound' => 'Token not found',
100
+
'messages.tokenInvalid' => 'Invalid token.',
101
+
]);
105
102
```
106
103
107
-
### _csrfField
108
-
109
-
This directly renders the form field for the CSRF token generated.
104
+
This will update the messages shown when a token is not found or invalid. If you want to handle the error yourself, you can pass an error handler to the `csrf` method.
110
105
111
106
```php
112
-
<form...>
113
-
<?php _csrfField(); ?>
114
-
...
115
-
</form>
107
+
app()->csrf([
108
+
'onError' => function($error) {
109
+
if ($error === "tokenNotFound") {
110
+
// handle token not found error
111
+
} else {
112
+
// handle invalid token error
113
+
}
114
+
}
115
+
]);
116
116
```
117
+
118
+
You can use this to handle the error in any way you want.
0 commit comments