-
Notifications
You must be signed in to change notification settings - Fork 1
/
authentication.go
44 lines (35 loc) · 1.18 KB
/
authentication.go
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
35
36
37
38
39
40
41
42
43
44
package middleware
import (
"net/http"
"strings"
"github.com/ankorstore/yokai/config"
"github.com/ankorstore/yokai/log"
"github.com/labstack/echo/v4"
)
// AuthenticationMiddleware is the http middleware to handle authentication.
type AuthenticationMiddleware struct {
config *config.Config
}
// NewAuthenticationMiddleware returns a new [AuthenticationMiddleware].
func NewAuthenticationMiddleware(config *config.Config) *AuthenticationMiddleware {
return &AuthenticationMiddleware{
config: config,
}
}
// Handle handles the http request authentication.
func (m *AuthenticationMiddleware) Handle() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
request := c.Request()
logger := log.CtxLogger(request.Context())
if m.config.GetBool("config.authentication.enabled") {
if strings.TrimPrefix(request.Header.Get("authorization"), "Bearer ") != m.config.GetString("config.authentication.secret") {
logger.Warn().Msg("authentication failed")
return echo.NewHTTPError(http.StatusUnauthorized, "authentication failed")
}
logger.Info().Msg("authentication success")
}
return next(c)
}
}
}