You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@streampark.apache.org by GitBox <gi...@apache.org> on 2022/10/14 13:39:22 UTC

[GitHub] [incubator-streampark] 1996fanrui commented on a diff in pull request #1836: [Improve]Splitting ldap logic

1996fanrui commented on code in PR #1836:
URL: https://github.com/apache/incubator-streampark/pull/1836#discussion_r995772420


##########
streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/system/security/impl/AuthenticatorImpl.java:
##########
@@ -0,0 +1,80 @@
+/*
+ * 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 org.apache.streampark.console.system.security.impl;
+
+import org.apache.streampark.console.base.util.ShaHashUtils;
+import org.apache.streampark.console.core.enums.UserType;
+import org.apache.streampark.console.system.entity.User;
+import org.apache.streampark.console.system.security.Authenticator;
+import org.apache.streampark.console.system.security.impl.ldap.LdapService;
+import org.apache.streampark.console.system.service.UserService;
+
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.Date;
+
+@Component
+public class AuthenticatorImpl implements Authenticator {
+    @Autowired
+    private UserService usersService;
+    @Autowired
+    private LdapService ldapService;
+
+    @Override
+    public User authenticate(String username, String password) {
+        User user = usersService.findByName(username);
+        if (user == null) {
+            return null;
+        }
+        String salt = user.getSalt();
+        password = ShaHashUtils.encrypt(salt, password);
+        if (!StringUtils.equals(user.getPassword(), password)) {
+            return null;
+        }
+        return user;
+    }
+
+    @Override
+    public User ldapAuthenticate(String username, String password) throws Exception {
+        User user = null;
+        String ldapEmail = ldapService.ldapLogin(username, password);
+        if (ldapEmail != null) {
+            //check if user exist
+            user = usersService.findByName(username);
+            if (user == null && ldapService.createIfUserNotExists()) {
+                User newUser = new User();
+                newUser.setCreateTime(new Date());
+                newUser.setUsername(username);
+                newUser.setNickName(username);
+                newUser.setUserType(UserType.USER);
+                newUser.setStatus("1");
+                newUser.setSex("1");
+
+                String salt = ShaHashUtils.getRandomSalt();
+                String saltPass = ShaHashUtils.encrypt(salt, password);
+                newUser.setSalt(salt);
+                newUser.setPassword(saltPass);
+                usersService.createUser(newUser);
+                return newUser;
+            }
+        }
+        return user;

Review Comment:
   ```suggestion
           String ldapEmail = ldapService.ldapLogin(username, password);
           if (ldapEmail == null) {
               return null;
           }
   
           //check if user exist
           User user = usersService.findByName(username);
           if (user != null || !ldapService.createIfUserNotExists()) {
               return user;
           }
           User newUser = new User();
           newUser.setCreateTime(new Date());
           newUser.setUsername(username);
           newUser.setNickName(username);
           newUser.setUserType(UserType.USER);
           newUser.setStatus("1");
           newUser.setSex("1");
   
           String salt = ShaHashUtils.getRandomSalt();
           String saltPass = ShaHashUtils.encrypt(salt, password);
           newUser.setSalt(salt);
           newUser.setPassword(saltPass);
           usersService.createUser(newUser);
           return newUser;
   ```
   
   Minor comment, it can reduce this tab.



-- 
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: issues-unsubscribe@streampark.apache.org

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