github.com/gofiber/fiber/v2@v2.47.0/docs/api/middleware/encryptcookie.md (about)

     1  ---
     2  id: encryptcookie
     3  title: Encrypt Cookie
     4  ---
     5  
     6  Encrypt middleware for [Fiber](https://github.com/gofiber/fiber) which encrypts cookie values. Note: this middleware does not encrypt cookie names.
     7  
     8  ## Signatures
     9  
    10  ```go
    11  // Intitializes the middleware
    12  func New(config ...Config) fiber.Handler
    13  
    14  // Returns a random 32 character long string
    15  func GenerateKey() string
    16  ```
    17  
    18  ## Examples
    19  
    20  Import the middleware package that is part of the Fiber web framework
    21  
    22  ```go
    23  import (
    24    "github.com/gofiber/fiber/v2"
    25    "github.com/gofiber/fiber/v2/middleware/encryptcookie"
    26  )
    27  ```
    28  
    29  After you initiate your Fiber app, you can use the following possibilities:
    30  
    31  ```go
    32  // Provide a minimal config
    33  // `Key` must be a 32 character string. It's used to encrypt the values, so make sure it is random and keep it secret.
    34  // You can run `openssl rand -base64 32` or call `encryptcookie.GenerateKey()` to create a random key for you.
    35  // Make sure not to set `Key` to `encryptcookie.GenerateKey()` because that will create a new key every run.
    36  app.Use(encryptcookie.New(encryptcookie.Config{
    37      Key: "secret-thirty-2-character-string",
    38  }))
    39  
    40  // Get / reading out the encrypted cookie
    41  app.Get("/", func(c *fiber.Ctx) error {
    42      return c.SendString("value=" + c.Cookies("test"))
    43  })
    44  
    45  // Post / create the encrypted cookie
    46  app.Post("/", func(c *fiber.Ctx) error {
    47      c.Cookie(&fiber.Cookie{
    48          Name:  "test",
    49          Value: "SomeThing",
    50      })
    51      return nil
    52  })
    53  ```
    54  
    55  ## Config
    56  
    57  ```go
    58  // Config defines the config for middleware.
    59  type Config struct {
    60      // Next defines a function to skip this middleware when returned true.
    61      //
    62      // Optional. Default: nil
    63      Next func(c *fiber.Ctx) bool
    64  
    65      // Array of cookie keys that should not be encrypted.
    66      //
    67      // Optional. Default: ["csrf_"]
    68      Except []string
    69  
    70  	// Base64 encoded unique key to encode & decode cookies.
    71  	//
    72  	// Required. The key should be 32 bytes of random data in base64-encoded form.
    73  	// You may run `openssl rand -base64 32` or use `encryptcookie.GenerateKey()` to generate a new key.
    74  	Key string
    75  
    76  	// Custom function to encrypt cookies.
    77  	//
    78  	// Optional. Default: EncryptCookie
    79  	Encryptor func(decryptedString, key string) (string, error)
    80  
    81  	// Custom function to decrypt cookies.
    82  	//
    83  	// Optional. Default: DecryptCookie
    84  	Decryptor func(encryptedString, key string) (string, error)
    85  }
    86  ```
    87  
    88  ## Default Config
    89  
    90  ```go
    91  var ConfigDefault = Config{
    92  	Next:      nil,
    93  	Except:    []string{"csrf_"},
    94  	Key:       "",
    95  	Encryptor: EncryptCookie,
    96  	Decryptor: DecryptCookie,
    97  }
    98  ```
    99  
   100  ## Usage of CSRF and Encryptcookie Middlewares with Custom Cookie Names
   101  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:
   102  
   103  ```go
   104  app.Use(encryptcookie.New(encryptcookie.Config{
   105  	Key: "secret-thirty-2-character-string",
   106  	Except: []string{"csrf_1"}, // exclude CSRF cookie
   107  }))
   108  app.Use(csrf.New(csrf.Config{
   109  	KeyLookup:      "form:test",
   110  	CookieName:     "csrf_1", 
   111  	CookieHTTPOnly: true,
   112  }))
   113  ```