starsz commented on a change in pull request #1296:
URL: https://github.com/apache/apisix-dashboard/pull/1296#discussion_r557915888



##########
File path: api/internal/filter/authentication.go
##########
@@ -17,62 +17,79 @@
 package filter
 
 import (
+       "errors"
        "net/http"
        "strings"
 
        "github.com/dgrijalva/jwt-go"
-       "github.com/gin-gonic/gin"
+       "github.com/shiningrush/droplet"
+       "github.com/shiningrush/droplet/data"
+       "github.com/shiningrush/droplet/middleware"
 
        "github.com/apisix/manager-api/internal/conf"
        "github.com/apisix/manager-api/internal/log"
 )
 
-func Authentication() gin.HandlerFunc {
-       return func(c *gin.Context) {
-               if c.Request.URL.Path != "/apisix/admin/user/login" && 
strings.HasPrefix(c.Request.URL.Path, "/apisix") {
-                       tokenStr := c.GetHeader("Authorization")
-
-                       // verify token
-                       token, err := jwt.ParseWithClaims(tokenStr, 
&jwt.StandardClaims{}, func(token *jwt.Token) (interface{}, error) {
-                               return []byte(conf.AuthConf.Secret), nil
-                       })
-
-                       errResp := gin.H{
-                               "code":    010013,
-                               "message": "Request Unauthorized",
-                       }
-
-                       if err != nil || token == nil || !token.Valid {
-                               log.Warnf("token validate failed: %s", err)
-                               c.AbortWithStatusJSON(http.StatusUnauthorized, 
errResp)
-                               return
-                       }
-
-                       claims, ok := token.Claims.(*jwt.StandardClaims)
-                       if !ok {
-                               log.Warnf("token validate failed: %s, %v", err, 
token.Valid)
-                               c.AbortWithStatusJSON(http.StatusUnauthorized, 
errResp)
-                               return
-                       }
-
-                       if err := token.Claims.Valid(); err != nil {
-                               log.Warnf("token claims validate failed: %s", 
err)
-                               c.AbortWithStatusJSON(http.StatusUnauthorized, 
errResp)
-                               return
-                       }
-
-                       if claims.Subject == "" {
-                               log.Warn("token claims subject empty")
-                               c.AbortWithStatusJSON(http.StatusUnauthorized, 
errResp)
-                               return
-                       }
-
-                       if _, ok := conf.UserList[claims.Subject]; !ok {
-                               log.Warnf("user not exists by token claims 
subject %s", claims.Subject)
-                               c.AbortWithStatusJSON(http.StatusUnauthorized, 
errResp)
-                               return
-                       }
+type AuthenticationMiddleware struct {
+       middleware.BaseMiddleware
+}
+
+func (mw *AuthenticationMiddleware) Handle(ctx droplet.Context) error {
+       httpReq := ctx.Get(middleware.KeyHttpRequest)
+       if httpReq == nil {
+               err := errors.New("input middleware cannot get http request, " +
+                       "please check if HttpInfoInjectorMiddleware middlle 
work well")

Review comment:
       OK, let me change it.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to