37 lines
622 B
Go
37 lines
622 B
Go
package middleware
|
|
|
|
import (
|
|
"basic-sensor-receiver/app"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func LoginAuthMiddleware(server *app.Server) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
_, err := server.Services.Auth.FromContext(c)
|
|
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusUnauthorized, err)
|
|
|
|
return
|
|
}
|
|
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
func KeyAuthMiddleware(server *app.Server) gin.HandlerFunc {
|
|
keyWithBearer := "Bearer " + server.Config.AuthKey
|
|
|
|
return func(c *gin.Context) {
|
|
if c.GetHeader("authorization") != keyWithBearer {
|
|
c.AbortWithStatus(http.StatusUnauthorized)
|
|
|
|
return
|
|
}
|
|
|
|
c.Next()
|
|
}
|
|
}
|