github.com/gofiber/fiber/v2@v2.47.0/docs/api/middleware/basicauth.md (about) 1 --- 2 id: basicauth 3 title: BasicAuth 4 --- 5 6 Basic Authentication middleware for [Fiber](https://github.com/gofiber/fiber) that provides an HTTP basic authentication. It calls the next handler for valid credentials and [401 Unauthorized](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/401) or a custom response for missing or invalid credentials. 7 8 ## Signatures 9 10 ```go 11 func New(config Config) fiber.Handler 12 ``` 13 14 ## Examples 15 16 Import the middleware package that is part of the Fiber web framework 17 18 ```go 19 import ( 20 "github.com/gofiber/fiber/v2" 21 "github.com/gofiber/fiber/v2/middleware/basicauth" 22 ) 23 ``` 24 25 After you initiate your Fiber app, you can use the following possibilities: 26 27 ```go 28 // Provide a minimal config 29 app.Use(basicauth.New(basicauth.Config{ 30 Users: map[string]string{ 31 "john": "doe", 32 "admin": "123456", 33 }, 34 })) 35 36 // Or extend your config for customization 37 app.Use(basicauth.New(basicauth.Config{ 38 Users: map[string]string{ 39 "john": "doe", 40 "admin": "123456", 41 }, 42 Realm: "Forbidden", 43 Authorizer: func(user, pass string) bool { 44 if user == "john" && pass == "doe" { 45 return true 46 } 47 if user == "admin" && pass == "123456" { 48 return true 49 } 50 return false 51 }, 52 Unauthorized: func(c *fiber.Ctx) error { 53 return c.SendFile("./unauthorized.html") 54 }, 55 ContextUsername: "_user", 56 ContextPassword: "_pass", 57 })) 58 ``` 59 60 ## Config 61 62 ```go 63 // Config defines the config for middleware. 64 type Config struct { 65 // Next defines a function to skip this middleware when returned true. 66 // 67 // Optional. Default: nil 68 Next func(c *fiber.Ctx) bool 69 70 // Users defines the allowed credentials 71 // 72 // Required. Default: map[string]string{} 73 Users map[string]string 74 75 // Realm is a string to define realm attribute of BasicAuth. 76 // the realm identifies the system to authenticate against 77 // and can be used by clients to save credentials 78 // 79 // Optional. Default: "Restricted". 80 Realm string 81 82 // Authorizer defines a function you can pass 83 // to check the credentials however you want. 84 // It will be called with a username and password 85 // and is expected to return true or false to indicate 86 // that the credentials were approved or not. 87 // 88 // Optional. Default: nil. 89 Authorizer func(string, string) bool 90 91 // Unauthorized defines the response body for unauthorized responses. 92 // By default it will return with a 401 Unauthorized and the correct WWW-Auth header 93 // 94 // Optional. Default: nil 95 Unauthorized fiber.Handler 96 97 // ContextUser is the key to store the username in Locals 98 // 99 // Optional. Default: "username" 100 ContextUsername string 101 102 // ContextPass is the key to store the password in Locals 103 // 104 // Optional. Default: "password" 105 ContextPassword string 106 } 107 ``` 108 109 ## Default Config 110 111 ```go 112 var ConfigDefault = Config{ 113 Next: nil, 114 Users: map[string]string{}, 115 Realm: "Restricted", 116 Authorizer: nil, 117 Unauthorized: nil, 118 ContextUsername: "username", 119 ContextPassword: "password", 120 } 121 ```