1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-22 09:12:59 +00:00

update encryptcookie documentation

This commit is contained in:
wernerr 2021-08-24 08:52:32 +02:00
parent ca3696624d
commit 426520b671

View File

@ -2,6 +2,12 @@
Encrypt middleware for [Fiber](https://github.com/gofiber/fiber) which encrypts cookie values. Note: this middleware does not encrypt cookie names. Encrypt middleware for [Fiber](https://github.com/gofiber/fiber) which encrypts cookie values. Note: this middleware does not encrypt cookie names.
## Table of Contents
* [Signatures](encryptcookie.md#signatures)
* [Setup](encryptcookie.md#setup)
* [Config](encryptcookie.md#config)
* [Default Config](encryptcookie.md#default-config)
## Signaures ## Signaures
@ -13,26 +19,38 @@ func New(config ...Config) fiber.Handler
func GenerateKey() string func GenerateKey() string
``` ```
## Setup ## Examples
First import the middleware from Fiber, Import the middleware package that is part of the Fiber web framework
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cache"
)
Then create a Fiber app with `app := fiber.New()`.
## Minimum Config
```go ```go
// `Key` must be a 32 character string. It's used to encrpyt the values, so make sure it is random and keep it secret. import (
// You can call `encryptcookie.GenerateKey()` to create a random key for you. "github.com/gofiber/fiber/v2"
// Make sure not to set `Key` to `encryptcookie.GenerateKey()` because that will create a new key every run. "github.com/gofiber/fiber/v2/middleware/encryptcookie"
)
```
After you initiate your Fiber app, you can use the following possibilities:
```go
// Default middleware config
app.Use(encryptcookie.New(encryptcookie.Config{ app.Use(encryptcookie.New(encryptcookie.Config{
Key: "secret-thirty-2-character-string", Key: "secret-thirty-2-character-string",
})) }))
// Get / reading out the encrypted cookie
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("value=" + c.Cookies("test"))
})
// Post / create the encrypted cookie
app.Post("/", func(c *fiber.Ctx) error {
c.Cookie(&fiber.Cookie{
Name: "test",
Value: "SomeThing",
})
return nil
})
``` ```
## Config ## Config
@ -66,3 +84,14 @@ type Config struct {
Decryptor func(encryptedString, key string) (string, error) Decryptor func(encryptedString, key string) (string, error)
} }
``` ```
## Default Config
```go
// `Key` must be a 32 character string. It's used to encrpyt the values, so make sure it is random and keep it secret.
// You can call `encryptcookie.GenerateKey()` to create a random key for you.
// Make sure not to set `Key` to `encryptcookie.GenerateKey()` because that will create a new key every run.
app.Use(encryptcookie.New(encryptcookie.Config{
Key: "secret-thirty-2-character-string",
}))
```