55 lines
956 B
Go
55 lines
956 B
Go
package middlewares
|
|
|
|
import (
|
|
"fmt"
|
|
// "os"
|
|
// "time"
|
|
// //"strconv"
|
|
"github.com/gin-gonic/gin"
|
|
// "github.com/golang-jwt/jwt/v5"
|
|
"net/http"
|
|
|
|
"git.dynamicdiscord.de/malobeo/portal/openapi"
|
|
"git.dynamicdiscord.de/malobeo/portal/services"
|
|
)
|
|
|
|
type AuthValidator struct {
|
|
}
|
|
|
|
func setUser(c *gin.Context) (openapi.UserResponse, error) {
|
|
user, err := services.Users.GetByAuthHeader(c)
|
|
|
|
if err != nil {
|
|
return openapi.UserResponse{}, fmt.Errorf("Error getting user by auth header: %s", err)
|
|
|
|
}
|
|
|
|
c.Set("user", user)
|
|
return user, nil
|
|
}
|
|
|
|
func (av *AuthValidator) RequireAuth(c *gin.Context) {
|
|
_, err := setUser(c)
|
|
if err != nil {
|
|
c.AbortWithStatus(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
c.Next()
|
|
}
|
|
|
|
func (av *AuthValidator) RequireAdmin(c *gin.Context) {
|
|
user, err := setUser(c)
|
|
if err != nil {
|
|
c.AbortWithStatus(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
if !*user.IsAdmin {
|
|
c.AbortWithStatus(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
c.Next()
|
|
}
|