Skip to content

Commit

Permalink
update encryptcookie documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
ReneWerner87 committed Aug 24, 2021
1 parent ca36966 commit 426520b
Showing 1 changed file with 38 additions and 9 deletions.
47 changes: 38 additions & 9 deletions middleware/encryptcookie/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

## Table of Contents

* [Signatures](encryptcookie.md#signatures)
* [Setup](encryptcookie.md#setup)
* [Config](encryptcookie.md#config)
* [Default Config](encryptcookie.md#default-config)

## Signaures

Expand All @@ -13,26 +19,38 @@ func New(config ...Config) fiber.Handler
func GenerateKey() string
```

## Setup
## Examples

First import the middleware from Fiber,
Import the middleware package that is part of the Fiber web framework

```go
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cache"
"github.com/gofiber/fiber/v2/middleware/encryptcookie"
)
```

Then create a Fiber app with `app := fiber.New()`.

## Minimum Config
After you initiate your Fiber app, you can use the following possibilities:

```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.
// Default middleware config
app.Use(encryptcookie.New(encryptcookie.Config{
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
Expand Down Expand Up @@ -66,3 +84,14 @@ type Config struct {
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",
}))
```

0 comments on commit 426520b

Please sign in to comment.