node-api/auth.go
Adam Štrauch 9348504f9e
Final touches
* Possibility to pass token in query param
* More appropriate names for gathering and resource updating functions
* Shorter measuring delay
* Container status available immediatelly when needed
2020-07-26 00:34:16 +02:00

38 lines
725 B
Go

package main
import (
"log"
"strings"
"github.com/labstack/echo"
)
var configuredToken string
func init() {
configuredToken = setToken()
log.Println("Access token:", configuredToken)
}
// TokenMiddleware handles authentication
func TokenMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
tokenHeader := c.Request().Header.Get("Authorization")
token := strings.Replace(tokenHeader, "Token ", "", -1)
if token == "" {
token = c.QueryParam("token")
}
if token != configuredToken || configuredToken == "" {
return c.JSONPretty(403, map[string]string{"message": "access denied"}, " ")
}
if err := next(c); err != nil {
c.Error(err)
}
return nil
}
}