node-api/auth.go
Adam Štrauch a4e2bac0ff
All checks were successful
Unittests / unittests (push) Successful in 16s
Unittests / deploy-dev (push) Successful in 47s
Adding Sentry
2024-01-26 18:46:19 +01:00

40 lines
799 B
Go

package main
import (
"strings"
"github.com/labstack/echo/v4"
)
var skipPaths []string = []string{"/metrics"}
// TokenMiddleware handles authentication
func TokenMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
// Skip selected paths
var skip bool
for _, path := range skipPaths {
if path == c.Request().URL.Path {
skip = true
}
}
tokenHeader := c.Request().Header.Get("Authorization")
token := strings.Replace(tokenHeader, "Token ", "", -1)
if token == "" && !skip {
token = c.QueryParam("token")
}
if (token != config.Token || config.Token == "") && !skip {
return c.JSONPretty(403, map[string]string{"message": "access denied"}, " ")
}
if err := next(c); err != nil {
c.Error(err)
}
return nil
}
}