recommender/internal/handler/http/middleware/application_auth.go
2024-11-07 20:00:06 +08:00

38 lines
888 B
Go

package middleware
import (
"github.com/gin-gonic/gin"
"leafdev.top/Ecosystem/recommender/internal/handler/http/response"
"leafdev.top/Ecosystem/recommender/internal/service/auth"
"net/http"
)
type ApplicationAuthMiddleware struct {
authService *auth.Service
}
func NewApplicationAuthMiddleware(authService *auth.Service) *ApplicationAuthMiddleware {
return &ApplicationAuthMiddleware{
authService,
}
}
func (a *ApplicationAuthMiddleware) RequireApplicationAuth(c *gin.Context) {
token, err := a.authService.GetBearerToken(c)
if err != nil {
response.Ctx(c).Status(http.StatusForbidden).Error(err).Send()
c.Abort()
return
}
applicationEntity, err := a.authService.GetAppByToken(c, token)
if err != nil {
response.Ctx(c).Status(http.StatusForbidden).Error(err).Send()
c.Abort()
return
}
a.authService.SetApplicationScope(c, applicationEntity)
c.Next()
}