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

     1  package basicauth
     2  
     3  import (
     4  	"crypto/subtle"
     5  
     6  	"github.com/gofiber/fiber/v2"
     7  	"github.com/gofiber/fiber/v2/utils"
     8  )
     9  
    10  // Config defines the config for middleware.
    11  type Config struct {
    12  	// Next defines a function to skip this middleware when returned true.
    13  	//
    14  	// Optional. Default: nil
    15  	Next func(c *fiber.Ctx) bool
    16  
    17  	// Users defines the allowed credentials
    18  	//
    19  	// Required. Default: map[string]string{}
    20  	Users map[string]string
    21  
    22  	// Realm is a string to define realm attribute of BasicAuth.
    23  	// the realm identifies the system to authenticate against
    24  	// and can be used by clients to save credentials
    25  	//
    26  	// Optional. Default: "Restricted".
    27  	Realm string
    28  
    29  	// Authorizer defines a function you can pass
    30  	// to check the credentials however you want.
    31  	// It will be called with a username and password
    32  	// and is expected to return true or false to indicate
    33  	// that the credentials were approved or not.
    34  	//
    35  	// Optional. Default: nil.
    36  	Authorizer func(string, string) bool
    37  
    38  	// Unauthorized defines the response body for unauthorized responses.
    39  	// By default it will return with a 401 Unauthorized and the correct WWW-Auth header
    40  	//
    41  	// Optional. Default: nil
    42  	Unauthorized fiber.Handler
    43  
    44  	// ContextUser is the key to store the username in Locals
    45  	//
    46  	// Optional. Default: "username"
    47  	ContextUsername string
    48  
    49  	// ContextPass is the key to store the password in Locals
    50  	//
    51  	// Optional. Default: "password"
    52  	ContextPassword string
    53  }
    54  
    55  // ConfigDefault is the default config
    56  var ConfigDefault = Config{
    57  	Next:            nil,
    58  	Users:           map[string]string{},
    59  	Realm:           "Restricted",
    60  	Authorizer:      nil,
    61  	Unauthorized:    nil,
    62  	ContextUsername: "username",
    63  	ContextPassword: "password",
    64  }
    65  
    66  // Helper function to set default values
    67  func configDefault(config ...Config) Config {
    68  	// Return default config if nothing provided
    69  	if len(config) < 1 {
    70  		return ConfigDefault
    71  	}
    72  
    73  	// Override default config
    74  	cfg := config[0]
    75  
    76  	// Set default values
    77  	if cfg.Next == nil {
    78  		cfg.Next = ConfigDefault.Next
    79  	}
    80  	if cfg.Users == nil {
    81  		cfg.Users = ConfigDefault.Users
    82  	}
    83  	if cfg.Realm == "" {
    84  		cfg.Realm = ConfigDefault.Realm
    85  	}
    86  	if cfg.Authorizer == nil {
    87  		cfg.Authorizer = func(user, pass string) bool {
    88  			userPwd, exist := cfg.Users[user]
    89  			return exist && subtle.ConstantTimeCompare(utils.UnsafeBytes(userPwd), utils.UnsafeBytes(pass)) == 1
    90  		}
    91  	}
    92  	if cfg.Unauthorized == nil {
    93  		cfg.Unauthorized = func(c *fiber.Ctx) error {
    94  			c.Set(fiber.HeaderWWWAuthenticate, "basic realm="+cfg.Realm)
    95  			return c.SendStatus(fiber.StatusUnauthorized)
    96  		}
    97  	}
    98  	if cfg.ContextUsername == "" {
    99  		cfg.ContextUsername = ConfigDefault.ContextUsername
   100  	}
   101  	if cfg.ContextPassword == "" {
   102  		cfg.ContextPassword = ConfigDefault.ContextPassword
   103  	}
   104  	return cfg
   105  }