-
-
Notifications
You must be signed in to change notification settings - Fork 541
Expand file tree
/
Copy pathroute_auth.go
More file actions
34 lines (29 loc) · 854 Bytes
/
route_auth.go
File metadata and controls
34 lines (29 loc) · 854 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package main
import (
"path"
"strings"
"github.com/gofiber/fiber/v2"
fiberbasicauth "github.com/gofiber/fiber/v2/middleware/basicauth"
"github.com/mritd/logger"
)
func routerAuth(user, passwd string, router fiber.Router, urlPrefix string) {
if user == "" && passwd == "" {
logger.Info("Bark Server Has No Basic Auth.")
return
}
logger.Info("Bark Server Has Basic Auth Enabled.")
authFreeRouters := []string{"/ping", "/register", "/healthz"}
basicAuth := fiberbasicauth.New(fiberbasicauth.Config{
Users: map[string]string{user: passwd},
Realm: "Coffee Time",
Unauthorized: func(c *fiber.Ctx) error {
for _, item := range authFreeRouters {
if strings.HasPrefix(c.Path(), path.Join(urlPrefix, item)) {
return c.Next()
}
}
return c.Status(418).SendString("I'm a teapot")
},
})
router.Use("/+", basicAuth)
}