node-api/auth.go

34 lines
668 B
Go
Raw Normal View History

2020-07-13 22:01:42 +00:00
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 != configuredToken || configuredToken == "" {
return c.JSONPretty(403, map[string]string{"message": "access denied"}, " ")
}
if err := next(c); err != nil {
c.Error(err)
}
return nil
}
}