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
* chore(encryptcookie)!: update default config
docs(encryptcookie): enhance documentation and examples
BREAKING CHANGE: removed the hardcoded "csrf_" from the Except.
* docs(encryptcookie): reads or modifies cookies
* chore(encryptcookie): csrf config example
* docs(encryptcookie): md table spacing
Copy file name to clipboardExpand all lines: docs/api/middleware/encryptcookie.md
+34-24Lines changed: 34 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,11 @@ id: encryptcookie
4
4
5
5
# Encrypt Cookie
6
6
7
-
Encrypt middleware for [Fiber](https://github.com/gofiber/fiber) which encrypts cookie values. Note: this middleware does not encrypt cookie names.
7
+
Encrypt Cookie is a middleware for [Fiber](https://github.com/gofiber/fiber) that secures your cookie values through encryption.
8
+
9
+
:::note
10
+
This middleware encrypts cookie values and not the cookie names.
11
+
:::
8
12
9
13
## Signatures
10
14
@@ -18,7 +22,7 @@ func GenerateKey() string
18
22
19
23
## Examples
20
24
21
-
Import the middleware package that is part of the Fiber web framework
25
+
To use the Encrypt Cookie middleware, first, import the middleware package as part of the Fiber web framework:
22
26
23
27
```go
24
28
import (
@@ -27,23 +31,20 @@ import (
27
31
)
28
32
```
29
33
30
-
After you initiate your Fiber app, you can use the following possibilities:
34
+
Once you've imported the middleware package, you can use it inside your Fiber app:
31
35
32
36
```go
33
-
// Provide a minimal config
34
-
// `Key` must be a 32 character string. It's used to encrypt the values, so make sure it is random and keep it secret.
35
-
// You can run `openssl rand -base64 32` or call `encryptcookie.GenerateKey()` to create a random key for you.
36
-
// Make sure not to set `Key` to `encryptcookie.GenerateKey()` because that will create a new key every run.
| Next |`func(*fiber.Ctx) bool`|A function to skip this middleware when returned true.|`nil`|
68
+
| Except |`[]string`| Array of cookie keys that should not be encrypted. |`[]`|
69
+
| Key |`string`|A base64-encoded unique key to encode & decode cookies. Required. Key length should be 32 characters. | (No default, required field) |
70
+
| Encryptor |`func(decryptedString, key string) (string, error)`|A custom function to encrypt cookies. |`EncryptCookie`|
71
+
| Decryptor |`func(encryptedString, key string) (string, error)`|A custom function to decrypt cookies. |`DecryptCookie`|
65
72
66
73
## Default Config
67
74
68
75
```go
69
76
varConfigDefault = Config{
70
77
Next: nil,
71
-
Except: []string{"csrf_"},
78
+
Except: []string{},
72
79
Key: "",
73
80
Encryptor: EncryptCookie,
74
81
Decryptor: DecryptCookie,
75
82
}
76
83
```
77
84
78
-
## Usage of CSRF and Encryptcookie Middlewares with Custom Cookie Names
79
-
Normally, encryptcookie middleware skips `csrf_` cookies. However, it won't work when you use custom cookie names for CSRF. You should update `Except` config to avoid this problem. For example:
85
+
## Usage With Other Middlewares That Reads Or Modify Cookies
86
+
Place the encryptcookie middleware before any other middleware that reads or modifies cookies. For example, if you are using the CSRF middleware, ensure that the encryptcookie middleware is placed before it. Failure to do so may prevent the CSRF middleware from reading the encrypted cookie.
87
+
88
+
You may also choose to exclude certain cookies from encryption. For instance, if you are using the CSRF middleware with a frontend framework like Angular, and the framework reads the token from a cookie, you should exclude that cookie from encryption. This can be achieved by adding the cookie name to the Except array in the configuration:
0 commit comments