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



##########
File path: api/internal/conf/authentication.go
##########
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package conf
+
+import (
+       "strconv"
+
+       "github.com/apisix/manager-api/internal/utils"
+       "github.com/apisix/manager-api/internal/utils/consts"
+)
+
+var (
+       // UserList is a mapping of user type and users
+       // like  local  => [User1, User2] google => [User3, User4] github => 
[User5, User6]
+       UserList   = make([]DashboardUserLocal, 0)

Review comment:
       Why not use map instead ?

##########
File path: api/internal/handler/authentication/authentication.go
##########
@@ -87,22 +94,61 @@ func (h *Handler) userLogin(c droplet.Context) 
(interface{}, error) {
        username := input.Username
        password := input.Password
 
-       user := conf.UserList[username]
-       if username != user.Username || password != user.Password {
-               return nil, consts.ErrUsernamePassword
-       }
+       loginSuccess := false
+       switch conf.DataSource {
+       case conf.DataSourceTypeLocal:
+               users := conf.UserList
+               for _, user := range users {
+                       if username == user.Username {
+                               ok, err := user.Valid(password)
+                               if !ok && err != nil {
+                                       return nil, err
+                               }
+
+                               loginSuccess = true
+                       }
+               }
+       case conf.DataSourceTypeEtcd:
+               ret, err := h.dashboardUserStore.List(c.Context(), 
store.ListInput{
+                       Predicate: func(obj interface{}) bool {
+                               return obj.(*entity.DashboardUser).Username == 
username
+                       },
+                       Format: func(obj interface{}) interface{} {
+                               return obj.(*entity.DashboardUser)
+                       },
+                       PageSize:   math.MaxInt32,
+                       PageNumber: 1,
+               })
+               if err != nil {
+                       return nil, err
+               }
 
-       // create JWT for session
-       claims := jwt.StandardClaims{
-               Subject:   username,
-               IssuedAt:  time.Now().Unix(),
-               ExpiresAt: time.Now().Add(time.Second * 
time.Duration(conf.AuthConf.ExpireTime)).Unix(),
+               if ret.TotalSize == 1 {
+                       user := ret.Rows[0].(*entity.DashboardUser)
+                       err := 
bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))

Review comment:
       I would prefer the way that uses salt.

##########
File path: api/internal/filter/authentication.go
##########
@@ -90,7 +93,40 @@ func (mw *AuthenticationMiddleware) Handle(ctx 
droplet.Context) error {
                return nil
        }
 
-       if _, ok := conf.UserList[claims.Subject]; !ok {
+       userExist := false
+       switch conf.DataSourceType(claims.Audience) {
+       case conf.DataSourceTypeEtcd:
+               dashboardUserStore := store.GetStore(store.HubKeyDashboardUser)
+               ret, err := dashboardUserStore.List(ctx.Context(), 
store.ListInput{
+                       Predicate: func(obj interface{}) bool {
+                               return claims.Subject == 
obj.(*entity.DashboardUser).Username
+                       },
+                       Format: func(obj interface{}) interface{} {
+                               return obj.(*entity.DashboardUser)
+                       },
+                       PageSize:   math.MaxInt32,
+                       PageNumber: 1,
+               })
+               if err != nil {
+                       log.Warn("user not exist in etcd")
+                       ctx.SetOutput(&data.SpecCodeResponse{StatusCode: 
http.StatusUnauthorized, Response: response})
+                       return nil
+               }
+
+               if ret.TotalSize > 0 {
+                       userExist = true
+               }
+       case conf.DataSourceTypeLocal:
+               fallthrough
+       default:
+               for _, item := range conf.UserList {

Review comment:
       IMO, use map would be better?




-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to