2020-10-14 23:22:58 +00:00
|
|
|
package security
|
|
|
|
|
2021-12-15 04:20:43 +00:00
|
|
|
import (
|
2022-01-09 00:26:37 +00:00
|
|
|
"encoding/base64"
|
2021-12-31 05:10:54 +00:00
|
|
|
"net/http"
|
|
|
|
|
2023-04-22 16:55:30 +00:00
|
|
|
g8 "github.com/TwiN/g8/v2"
|
2024-11-14 04:54:00 +00:00
|
|
|
"github.com/TwiN/logr"
|
2023-07-09 00:37:41 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/gofiber/fiber/v2/middleware/adaptor"
|
|
|
|
"github.com/gofiber/fiber/v2/middleware/basicauth"
|
2022-01-09 00:26:37 +00:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2021-12-15 04:20:43 +00:00
|
|
|
)
|
|
|
|
|
2021-12-31 05:10:54 +00:00
|
|
|
const (
|
|
|
|
cookieNameState = "gatus_state"
|
|
|
|
cookieNameNonce = "gatus_nonce"
|
|
|
|
cookieNameSession = "gatus_session"
|
|
|
|
)
|
|
|
|
|
2020-10-23 19:58:59 +00:00
|
|
|
// Config is the security configuration for Gatus
|
2020-10-14 23:22:58 +00:00
|
|
|
type Config struct {
|
2021-12-15 04:20:43 +00:00
|
|
|
Basic *BasicConfig `yaml:"basic,omitempty"`
|
|
|
|
OIDC *OIDCConfig `yaml:"oidc,omitempty"`
|
2022-01-02 23:29:34 +00:00
|
|
|
|
|
|
|
gate *g8.Gate
|
2020-10-14 23:22:58 +00:00
|
|
|
}
|
|
|
|
|
2020-10-23 19:58:59 +00:00
|
|
|
// IsValid returns whether the security configuration is valid or not
|
2020-10-14 23:22:58 +00:00
|
|
|
func (c *Config) IsValid() bool {
|
2021-12-15 04:20:43 +00:00
|
|
|
return (c.Basic != nil && c.Basic.isValid()) || (c.OIDC != nil && c.OIDC.isValid())
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterHandlers registers all handlers required based on the security configuration
|
2023-07-09 00:37:41 +00:00
|
|
|
func (c *Config) RegisterHandlers(router fiber.Router) error {
|
2021-12-15 04:20:43 +00:00
|
|
|
if c.OIDC != nil {
|
|
|
|
if err := c.OIDC.initialize(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-09 00:37:41 +00:00
|
|
|
router.All("/oidc/login", c.OIDC.loginHandler)
|
|
|
|
router.All("/authorization-code/callback", adaptor.HTTPHandlerFunc(c.OIDC.callbackHandler))
|
2021-12-15 04:20:43 +00:00
|
|
|
}
|
|
|
|
return nil
|
2020-10-14 23:22:58 +00:00
|
|
|
}
|
|
|
|
|
2022-01-02 23:29:34 +00:00
|
|
|
// ApplySecurityMiddleware applies an authentication middleware to the router passed.
|
2023-07-09 00:37:41 +00:00
|
|
|
// The router passed should be a sub-router in charge of handlers that require authentication.
|
|
|
|
func (c *Config) ApplySecurityMiddleware(router fiber.Router) error {
|
2021-12-31 05:10:54 +00:00
|
|
|
if c.OIDC != nil {
|
|
|
|
// We're going to use g8 for session handling
|
|
|
|
clientProvider := g8.NewClientProvider(func(token string) *g8.Client {
|
|
|
|
if _, exists := sessions.Get(token); exists {
|
|
|
|
return g8.NewClient(token)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
customTokenExtractorFunc := func(request *http.Request) string {
|
|
|
|
sessionCookie, err := request.Cookie(cookieNameSession)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return sessionCookie.Value
|
|
|
|
}
|
|
|
|
// TODO: g8: Add a way to update cookie after? would need the writer
|
|
|
|
authorizationService := g8.NewAuthorizationService().WithClientProvider(clientProvider)
|
2022-01-02 23:29:34 +00:00
|
|
|
c.gate = g8.New().WithAuthorizationService(authorizationService).WithCustomTokenExtractor(customTokenExtractorFunc)
|
2023-07-09 00:37:41 +00:00
|
|
|
router.Use(adaptor.HTTPMiddleware(c.gate.Protect))
|
2021-12-31 05:10:54 +00:00
|
|
|
} else if c.Basic != nil {
|
2022-01-09 00:26:37 +00:00
|
|
|
var decodedBcryptHash []byte
|
|
|
|
if len(c.Basic.PasswordBcryptHashBase64Encoded) > 0 {
|
|
|
|
var err error
|
|
|
|
decodedBcryptHash, err = base64.URLEncoding.DecodeString(c.Basic.PasswordBcryptHashBase64Encoded)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-07-09 00:37:41 +00:00
|
|
|
router.Use(basicauth.New(basicauth.Config{
|
|
|
|
Authorizer: func(username, password string) bool {
|
2022-01-09 00:26:37 +00:00
|
|
|
if len(c.Basic.PasswordBcryptHashBase64Encoded) > 0 {
|
2023-07-09 00:37:41 +00:00
|
|
|
if username != c.Basic.Username || bcrypt.CompareHashAndPassword(decodedBcryptHash, []byte(password)) != nil {
|
|
|
|
return false
|
2022-01-09 00:26:37 +00:00
|
|
|
}
|
2021-12-31 05:10:54 +00:00
|
|
|
}
|
2023-07-09 00:37:41 +00:00
|
|
|
return true
|
|
|
|
},
|
|
|
|
Unauthorized: func(ctx *fiber.Ctx) error {
|
|
|
|
ctx.Set("WWW-Authenticate", "Basic")
|
|
|
|
return ctx.Status(401).SendString("Unauthorized")
|
|
|
|
},
|
|
|
|
}))
|
2021-12-31 05:10:54 +00:00
|
|
|
}
|
2022-01-09 00:26:37 +00:00
|
|
|
return nil
|
2020-10-14 23:22:58 +00:00
|
|
|
}
|
2022-01-02 23:29:34 +00:00
|
|
|
|
|
|
|
// IsAuthenticated checks whether the user is authenticated
|
|
|
|
// If the Config does not warrant authentication, it will always return true.
|
2023-07-09 00:37:41 +00:00
|
|
|
func (c *Config) IsAuthenticated(ctx *fiber.Ctx) bool {
|
2022-01-02 23:29:34 +00:00
|
|
|
if c.gate != nil {
|
2023-07-09 00:37:41 +00:00
|
|
|
// TODO: Update g8 to support fasthttp natively? (see g8's fasthttp branch)
|
|
|
|
request, err := adaptor.ConvertRequest(ctx, false)
|
|
|
|
if err != nil {
|
2024-11-14 04:54:00 +00:00
|
|
|
logr.Errorf("[security.IsAuthenticated] Unexpected error converting request: %v", err)
|
2023-07-09 00:37:41 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
token := c.gate.ExtractTokenFromRequest(request)
|
2022-01-02 23:29:34 +00:00
|
|
|
_, hasSession := sessions.Get(token)
|
|
|
|
return hasSession
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|