/*
 * Copyright 2015-2025 the original author or authors.
 * 
 * Licensed 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 fucksocks.server;

import fucksocks.common.AuthenticationException;
import fucksocks.common.Credentials;
import fucksocks.common.UsernamePasswordCredentials;
import fucksocks.common.methods.UsernamePasswordMethod;
import fucksocks.server.manager.MemoryBasedUserManager;
import fucksocks.server.manager.User;
import fucksocks.server.manager.UserManager;

/**
 * The class <code>UsernamePasswordAuthenticator</code> represents a username password
 * authenticator. It will be used by {@link UsernamePasswordMethod}.
 *
 * @author Youchao Feng
 * @version 1.0
 * @date Apr 16, 2015 11:30:46 AM
 */
public class UsernamePasswordAuthenticator implements Authenticator {

  public static final String USER_KEY = "USER";
  /**
   * {@link MemoryBasedUserManager} is default.
   */
  private UserManager userManager = new MemoryBasedUserManager();

  public UsernamePasswordAuthenticator() {
  }

  public UsernamePasswordAuthenticator(UserManager userManager) {
    this.userManager = userManager;
  }

  @Override
  public void doAuthenticate(Credentials credentials, Session session) throws AuthenticationException {
    if (credentials instanceof UsernamePasswordCredentials) {
      String username = credentials.getUserPrincipal().getName();
      String password = credentials.getPassword();
      User user = userManager.check(username, password);
      if (user == null) {
        throw new AuthenticationException(
            "Authentication failed, client from " + session.getRemoteAddress());
      }
      authenticateSuccess(session, user);

    } else {
      throw new AuthenticationException("Only support Username/Password Authentication");
    }

  }

  /**
   * This method will save user in session.
   *
   * @param session Current session.
   * @param user    user.
   */
  protected void authenticateSuccess(Session session, User user) {
    session.setAttribute(USER_KEY, user);
  }

  public UserManager getUserManager() {
    return userManager;
  }

  public void setUserManager(UserManager userManager) {
    this.userManager = userManager;
  }

  public void addUser(String username, String password) {
    userManager.addUser(username, password);
  }

  public void deleteUser(String username) {
    userManager.delete(username);
  }

}

@@ -2001,57 +2001,9 @@
     
-throw new AuthenticationException(%0A            %22A
+a
 uthe
@@ -2015,59 +2015,22 @@
 tion
- f
+F
 ailed
-, client from %22 + session.getRemoteAddress()
+(session
 );%0A 
@@ -2045,33 +2045,35 @@
      authenticat
-e
+ion
 Success(session,
@@ -2190,17 +2190,16 @@
 ;%0A    %7D%0A
-%0A
   %7D%0A%0A  /
@@ -2347,17 +2347,19 @@
 henticat
-e
+ion
 Success(
@@ -2427,32 +2427,428 @@
 EY, user);%0A  %7D%0A%0A
+  /**%0A   * This method will throw a %7B@link AuthenticationException%7D%0A   *%0A   * @param session Current session%0A   * @throws AuthenticationException %7B@link AuthenticationException%7D%0A   */%0A  protected void authenticationFailed(Session session) throws AuthenticationException%7B%0A    throw new AuthenticationException(%0A            %22Authentication failed, client from %22 + session.getRemoteAddress());%0A  %7D%0A%0A
   public UserMan

Add #authenticationFailed(Session) method