auth MW refactor
This commit is contained in:
parent
a7530a6324
commit
0903bc55eb
7 changed files with 15 additions and 148 deletions
|
|
@ -3,42 +3,12 @@ package router
|
|||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"merch-parser-api/internal/interfaces"
|
||||
"merch-parser-api/internal/shared"
|
||||
"merch-parser-api/pkg/responses"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type mwDeps struct {
|
||||
prefix string
|
||||
excludeRoutes *map[string]shared.ExcludeRoute
|
||||
tokenProv interfaces.JWTProvider
|
||||
}
|
||||
|
||||
func authMiddleware(deps mwDeps) gin.HandlerFunc {
|
||||
func (r *router) AuthMW() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if excluded(deps.prefix, c.FullPath(), c.Request.Method, deps.excludeRoutes) {
|
||||
log.WithField("msg", "route excluded from auth check").Info("MW | Authorization")
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
|
||||
if (c.FullPath() == "/user/auth/refresh" || c.FullPath() == "/user/auth/logout") && c.Request.Method == "POST" {
|
||||
refreshUuid, err := c.Cookie("refresh_uuid")
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnauthorized, responses.ErrorResponse401{Error: "Refresh token is required"})
|
||||
log.WithField("msg", "Refresh token is required").Error("MW | Authorization")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
c.Set("refreshUuid", refreshUuid)
|
||||
|
||||
log.WithField("msg", "refresh token set to context").Debug("MW | Authorization")
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
|
||||
token := c.GetHeader("Authorization")
|
||||
if token == "" {
|
||||
c.JSON(http.StatusUnauthorized, responses.ErrorResponse401{Error: "Authorization token is required"})
|
||||
|
|
@ -47,7 +17,7 @@ func authMiddleware(deps mwDeps) gin.HandlerFunc {
|
|||
return
|
||||
}
|
||||
|
||||
userUuid, err := deps.tokenProv.Parse(token)
|
||||
userUuid, err := r.tokenProv.Parse(token)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnauthorized, responses.ErrorResponse401{Error: err.Error()})
|
||||
log.WithField("msg", "error parsing jwt").Error("MW | Authorization")
|
||||
|
|
@ -56,14 +26,8 @@ func authMiddleware(deps mwDeps) gin.HandlerFunc {
|
|||
}
|
||||
|
||||
c.Set("userUuid", userUuid)
|
||||
log.WithField("userUuid", userUuid).Debug("MW | User uuid")
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"userUuid": userUuid,
|
||||
}).Debug("MW | Parsed uuids")
|
||||
|
||||
if !c.IsAborted() {
|
||||
log.WithField("msg", "context aborted").Info("MW | Authorization")
|
||||
c.Next()
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue