47 lines
1.0 KiB
Go
47 lines
1.0 KiB
Go
package middleware
|
|
|
|
import (
|
|
"leafdev.top/Ecosystem/recommender/internal/handler/http/response"
|
|
"leafdev.top/Ecosystem/recommender/internal/schema"
|
|
"leafdev.top/Ecosystem/recommender/internal/service/auth"
|
|
"leafdev.top/Ecosystem/recommender/pkg/consts"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type AuthMiddleware struct {
|
|
authService *auth.Service
|
|
}
|
|
|
|
func NewAuthMiddleware(authService *auth.Service) *AuthMiddleware {
|
|
return &AuthMiddleware{
|
|
authService,
|
|
}
|
|
}
|
|
|
|
func (a AuthMiddleware) RequireJWTIDToken(c *gin.Context) {
|
|
user, err := a.authService.GinMiddlewareAuth(schema.JWTIDToken, c)
|
|
|
|
if err != nil {
|
|
c.Abort()
|
|
response.Ctx(c).Error(err).Status(http.StatusUnauthorized).Send()
|
|
return
|
|
}
|
|
|
|
c.Set(consts.AuthMiddlewareKey, user)
|
|
c.Next()
|
|
}
|
|
|
|
func (a AuthMiddleware) RequireJWTAccessToken(c *gin.Context) {
|
|
user, err := a.authService.GinMiddlewareAuth(schema.JWTAccessToken, c)
|
|
if err != nil {
|
|
c.Abort()
|
|
response.Ctx(c).Error(err).Status(http.StatusUnauthorized).Send()
|
|
return
|
|
}
|
|
|
|
c.Set(consts.AuthMiddlewareKey, user)
|
|
c.Next()
|
|
}
|