github.com/gofiber/fiber/v2@v2.47.0/middleware/encryptcookie/config.go (about)

     1  package encryptcookie
     2  
     3  import (
     4  	"github.com/gofiber/fiber/v2"
     5  )
     6  
     7  // Config defines the config for middleware.
     8  type Config struct {
     9  	// Next defines a function to skip this middleware when returned true.
    10  	//
    11  	// Optional. Default: nil
    12  	Next func(c *fiber.Ctx) bool
    13  
    14  	// Array of cookie keys that should not be encrypted.
    15  	//
    16  	// Optional. Default: []
    17  	Except []string
    18  
    19  	// Base64 encoded unique key to encode & decode cookies.
    20  	//
    21  	// Required. Key length should be 32 characters.
    22  	// You may use `encryptcookie.GenerateKey()` to generate a new key.
    23  	Key string
    24  
    25  	// Custom function to encrypt cookies.
    26  	//
    27  	// Optional. Default: EncryptCookie
    28  	Encryptor func(decryptedString, key string) (string, error)
    29  
    30  	// Custom function to decrypt cookies.
    31  	//
    32  	// Optional. Default: DecryptCookie
    33  	Decryptor func(encryptedString, key string) (string, error)
    34  }
    35  
    36  // ConfigDefault is the default config
    37  var ConfigDefault = Config{
    38  	Next:      nil,
    39  	Except:    []string{"csrf_"},
    40  	Key:       "",
    41  	Encryptor: EncryptCookie,
    42  	Decryptor: DecryptCookie,
    43  }
    44  
    45  // Helper function to set default values
    46  func configDefault(config ...Config) Config {
    47  	// Set default config
    48  	cfg := ConfigDefault
    49  
    50  	// Override config if provided
    51  	if len(config) > 0 {
    52  		cfg = config[0]
    53  
    54  		// Set default values
    55  
    56  		if cfg.Next == nil {
    57  			cfg.Next = ConfigDefault.Next
    58  		}
    59  
    60  		if cfg.Except == nil {
    61  			cfg.Except = ConfigDefault.Except
    62  		}
    63  
    64  		if cfg.Encryptor == nil {
    65  			cfg.Encryptor = ConfigDefault.Encryptor
    66  		}
    67  
    68  		if cfg.Decryptor == nil {
    69  			cfg.Decryptor = ConfigDefault.Decryptor
    70  		}
    71  	}
    72  
    73  	if cfg.Key == "" {
    74  		panic("fiber: encrypt cookie middleware requires key")
    75  	}
    76  
    77  	return cfg
    78  }