34 lines
668 B
Go
34 lines
668 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 != configuredToken || configuredToken == "" {
|
||
|
return c.JSONPretty(403, map[string]string{"message": "access denied"}, " ")
|
||
|
}
|
||
|
|
||
|
if err := next(c); err != nil {
|
||
|
c.Error(err)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
}
|