DrupalAuthInfoCommand → AuthInfoCommand (je to obecná implementace, nezávislá na Drupalu)
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 30 Oct 2011 22:13:32 +0100
changeset 112ca54040b4409
parent 111 f04253b2d6c1
child 113 a059aecd1794
DrupalAuthInfoCommand → AuthInfoCommand (je to obecná implementace, nezávislá na Drupalu)
src/org/sonews/acl/AuthInfoCommand.java
src/org/sonews/acl/DrupalAuthInfoCommand.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/org/sonews/acl/AuthInfoCommand.java	Sun Oct 30 22:13:32 2011 +0100
     1.3 @@ -0,0 +1,104 @@
     1.4 +/*
     1.5 + *   SONEWS News Server
     1.6 + *   see AUTHORS for the list of contributors
     1.7 + *
     1.8 + *   This program is free software: you can redistribute it and/or modify
     1.9 + *   it under the terms of the GNU General Public License as published by
    1.10 + *   the Free Software Foundation, either version 3 of the License, or
    1.11 + *   (at your option) any later version.
    1.12 + *
    1.13 + *   This program is distributed in the hope that it will be useful,
    1.14 + *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 + *   GNU General Public License for more details.
    1.17 + *
    1.18 + *   You should have received a copy of the GNU General Public License
    1.19 + *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +package org.sonews.acl;
    1.22 +
    1.23 +import java.io.IOException;
    1.24 +import java.util.Arrays;
    1.25 +import java.util.logging.Level;
    1.26 +import java.util.logging.Logger;
    1.27 +import java.util.regex.Matcher;
    1.28 +import java.util.regex.Pattern;
    1.29 +import org.sonews.daemon.NNTPConnection;
    1.30 +import org.sonews.daemon.command.Command;
    1.31 +import org.sonews.storage.StorageBackendException;
    1.32 +import org.sonews.storage.StorageManager;
    1.33 +
    1.34 +/**
    1.35 + *
    1.36 + * @author František Kučera (frantovo.cz)
    1.37 + */
    1.38 +public class AuthInfoCommand implements Command {
    1.39 +
    1.40 +	private static final Logger log = Logger.getLogger(AuthInfoCommand.class.getName());
    1.41 +	private static String[] SUPPORTED_COMMANDS = {"AUTHINFO"};
    1.42 +
    1.43 +	@Override
    1.44 +	public boolean hasFinished() {
    1.45 +		return true;
    1.46 +	}
    1.47 +
    1.48 +	@Override
    1.49 +	public String impliedCapability() {
    1.50 +		return "AUTHINFO";
    1.51 +	}
    1.52 +
    1.53 +	@Override
    1.54 +	public boolean isStateful() {
    1.55 +		return false;
    1.56 +	}
    1.57 +
    1.58 +	@Override
    1.59 +	public String[] getSupportedCommandStrings() {
    1.60 +		return SUPPORTED_COMMANDS;
    1.61 +	}
    1.62 +
    1.63 +	@Override
    1.64 +	public void processLine(NNTPConnection conn, String line, byte[] rawLine) throws IOException, StorageBackendException {
    1.65 +		Pattern commandPattern = Pattern.compile("AUTHINFO (USER|PASS) (.*)", Pattern.CASE_INSENSITIVE);
    1.66 +		Matcher commandMatcher = commandPattern.matcher(line);
    1.67 +
    1.68 +		if (commandMatcher.matches()) {
    1.69 +
    1.70 +			if (conn.getUser() != null && conn.getUser().isAuthenticated()) {
    1.71 +				conn.println("502 Command unavailable (you are already authenticated)");
    1.72 +			} else if ("USER".equalsIgnoreCase(commandMatcher.group(1))) {
    1.73 +				conn.setUser(new User(commandMatcher.group(2)));
    1.74 +				conn.println("381 Password required"); // ask user for his password
    1.75 +				log.log(Level.FINE, "User ''{0}'' greets us. We are waiting for his password.", conn.getUser().getUserName());
    1.76 +			} else if ("PASS".equalsIgnoreCase(commandMatcher.group(1))) {
    1.77 +				if (conn.getUser() == null) {
    1.78 +					conn.println("482 Authentication commands issued out of sequence");
    1.79 +				} else {
    1.80 +
    1.81 +					char[] password = commandMatcher.group(2).toCharArray();
    1.82 +					boolean goodPassword = StorageManager.current().authenticateUser(conn.getUser().getUserName(), password);
    1.83 +					Arrays.fill(password, '*');
    1.84 +					commandMatcher = null;
    1.85 +
    1.86 +					if (goodPassword) {
    1.87 +						conn.println("281 Authentication accepted");
    1.88 +						conn.getUser().setAuthenticated(true);
    1.89 +						log.log(Level.INFO, "User ''{0}'' has been succesfully authenticated.", conn.getUser().getUserName());
    1.90 +					} else {
    1.91 +						log.log(Level.INFO, "User ''{0}'' has provided wrong password.", conn.getUser().getUserName());
    1.92 +						conn.setUser(null);
    1.93 +						conn.println("481 Authentication failed: wrong password");
    1.94 +					}
    1.95 +
    1.96 +				}
    1.97 +			} else {
    1.98 +				// impossible, see commandPattern
    1.99 +				conn.println("500 Unknown command");
   1.100 +			}
   1.101 +
   1.102 +
   1.103 +		} else {
   1.104 +			conn.println("500 Unknown command, expecting AUTHINFO USER username or AUTHINFO PASS password ");
   1.105 +		}
   1.106 +	}
   1.107 +}
     2.1 --- a/src/org/sonews/acl/DrupalAuthInfoCommand.java	Sun Oct 30 22:10:43 2011 +0100
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,107 +0,0 @@
     2.4 -/*
     2.5 - *   SONEWS News Server
     2.6 - *   see AUTHORS for the list of contributors
     2.7 - *
     2.8 - *   This program is free software: you can redistribute it and/or modify
     2.9 - *   it under the terms of the GNU General Public License as published by
    2.10 - *   the Free Software Foundation, either version 3 of the License, or
    2.11 - *   (at your option) any later version.
    2.12 - *
    2.13 - *   This program is distributed in the hope that it will be useful,
    2.14 - *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.15 - *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2.16 - *   GNU General Public License for more details.
    2.17 - *
    2.18 - *   You should have received a copy of the GNU General Public License
    2.19 - *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    2.20 - */
    2.21 -package org.sonews.acl;
    2.22 -
    2.23 -import java.io.IOException;
    2.24 -import java.util.Arrays;
    2.25 -import java.util.logging.Level;
    2.26 -import java.util.logging.Logger;
    2.27 -import java.util.regex.Matcher;
    2.28 -import java.util.regex.Pattern;
    2.29 -import org.sonews.daemon.NNTPConnection;
    2.30 -import org.sonews.daemon.command.Command;
    2.31 -import org.sonews.storage.StorageBackendException;
    2.32 -import org.sonews.storage.StorageManager;
    2.33 -import org.sonews.storage.StorageProvider;
    2.34 -import org.sonews.storage.impl.DrupalDatabaseProvider;
    2.35 -
    2.36 -/**
    2.37 - *
    2.38 - * @author František Kučera (frantovo.cz)
    2.39 - */
    2.40 -public class DrupalAuthInfoCommand implements Command {
    2.41 -
    2.42 -	private static final Logger log = Logger.getLogger(DrupalAuthInfoCommand.class.getName());
    2.43 -	private static String[] SUPPORTED_COMMANDS = {"AUTHINFO"};
    2.44 -
    2.45 -	@Override
    2.46 -	public boolean hasFinished() {
    2.47 -		return true;
    2.48 -	}
    2.49 -
    2.50 -	@Override
    2.51 -	public String impliedCapability() {
    2.52 -		return "AUTHINFO";
    2.53 -	}
    2.54 -
    2.55 -	@Override
    2.56 -	public boolean isStateful() {
    2.57 -		return false;
    2.58 -	}
    2.59 -
    2.60 -	@Override
    2.61 -	public String[] getSupportedCommandStrings() {
    2.62 -		return SUPPORTED_COMMANDS;
    2.63 -	}
    2.64 -
    2.65 -	@Override
    2.66 -	public void processLine(NNTPConnection conn, String line, byte[] rawLine) throws IOException, StorageBackendException {
    2.67 -		Pattern commandPattern = Pattern.compile("AUTHINFO (USER|PASS) (.*)", Pattern.CASE_INSENSITIVE);
    2.68 -		Matcher commandMatcher = commandPattern.matcher(line);
    2.69 -
    2.70 -		if (commandMatcher.matches()) {
    2.71 -
    2.72 -			if (conn.isUserAuthenticated()) {
    2.73 -				conn.println("502 Command unavailable (you are already authenticated)");
    2.74 -			} else if ("USER".equalsIgnoreCase(commandMatcher.group(1))) {
    2.75 -				conn.setUsername(commandMatcher.group(2));
    2.76 -				conn.println("381 Password required");
    2.77 -				log.log(Level.FINE, "User ''{0}'' greets us. We are waiting for his password.", conn.getUsername());
    2.78 -			} else if ("PASS".equalsIgnoreCase(commandMatcher.group(1))) {
    2.79 -				if (conn.getUsername() == null) {
    2.80 -					conn.println("482 Authentication commands issued out of sequence");
    2.81 -				} else {
    2.82 -
    2.83 -					char[] password = commandMatcher.group(2).toCharArray();
    2.84 -					boolean goodPassword = StorageManager.current().authenticateUser(conn.getUsername(), password);
    2.85 -					Arrays.fill(password, '*');
    2.86 -					commandMatcher = null;
    2.87 -
    2.88 -					if (goodPassword) {
    2.89 -						conn.println("281 Authentication accepted");
    2.90 -						conn.setUserAuthenticated(true);
    2.91 -						log.log(Level.INFO, "User ''{0}'' has been succesfully authenticated.", conn.getUsername());
    2.92 -					} else {
    2.93 -						log.log(Level.INFO, "User ''{0}'' has provided wrong password.", conn.getUsername());
    2.94 -						conn.setUsername(null);
    2.95 -						conn.setUserAuthenticated(false);
    2.96 -						conn.println("481 Authentication failed: wrong password");
    2.97 -					}
    2.98 -
    2.99 -				}
   2.100 -			} else {
   2.101 -				// impossible, see commandPattern
   2.102 -				conn.println("500 Unknown command");
   2.103 -			}
   2.104 -
   2.105 -
   2.106 -		} else {
   2.107 -			conn.println("500 Unknown command, expecting AUTHINFO USER username or AUTHINFO PASS password ");
   2.108 -		}
   2.109 -	}
   2.110 -}