// Path: src/main/java/org/owasp/oneliner/exceptions/AuthenticationException.java
// public class AuthenticationException extends Exception {
//     public static final AuthenticationException INTERNAL_ERROR =
//             new AuthenticationException("Could not authenticate due to an internal error");
//     public static final AuthenticationException UNKNOWN_USER_NAME_OR_PASSWORD =
//             new AuthenticationException("Wrong user name or password");
// 
//     // SecBug: Information leakage
//     public static final AuthenticationException UNKNOWN_USER_NAME =
//             new AuthenticationException("Wrong user name");
//     public static final AuthenticationException NOT_AUTHENTICATED =
//             new AuthenticationException("Authentication failed");
// 
//     private AuthenticationException (String message) {
//         super(message);
//         this.setStackTrace(new StackTraceElement[0]);  // No stack trace needed
//     }
// }
// 
// Path: src/main/java/org/owasp/oneliner/exceptions/NoSuchUserException.java
// public class NoSuchUserException extends Exception {
//     public static final NoSuchUserException NO_SUCH_USER = new NoSuchUserException("No such user");
// 
//     public NoSuchUserException(String message) {
//         super(message);
//     }
// }
// 
// Path: src/main/java/org/owasp/oneliner/password/HashedPassword.java
// public class HashedPassword {
//     private final String hashedPassword;
//     private final long salt;
// 
//     // Package default to give access to factory class only
//     HashedPassword(String hashedPassword, long salt) {
//         this.salt = salt;
//         this.hashedPassword = hashedPassword;
//     }
// 
//     public long getSalt() {
//         return salt;
//     }
// 
//     public boolean equals(Object other) {
//         if (!(other instanceof HashedPassword)) {
//             return false;
//         } else {
//             HashedPassword otherHashedPassword = (HashedPassword)other;
//             return hashedPassword.equals(otherHashedPassword.hashedPassword) &&
//                     salt == otherHashedPassword.salt;
//         }
//     }
// }
// 
// Path: src/main/java/org/owasp/oneliner/password/HashedPasswordFactory.java
// public class HashedPasswordFactory {
//     @Resource
//     private SecureRandom secureRandom;
// 
//     public HashedPassword createHashedPassword(String plainTextPassword) {
//         long salt = secureRandom.nextLong();
//         return createHashedPassword(plainTextPassword, salt);
//     }
// 
//     public HashedPassword createHashedPassword(String plainTextPassword, long salt) {
//         String hashedPasswordStr = null;
//         try {
//             hashedPasswordStr = new String(sha256.digest((plainTextPassword + salt).getBytes("UTF8")));
//         } catch (UnsupportedEncodingException e) {
//             e.printStackTrace();
//         }
//         return new HashedPassword(hashedPasswordStr, salt);
//     }
// 
//     private static MessageDigest sha256;
//     static {
//         try {
//             sha256 = MessageDigest.getInstance("SHA-256");
//         } catch (NoSuchAlgorithmException e) {
//             e.printStackTrace();
//         }
//     }
// }
// 
// Path: src/main/java/org/owasp/oneliner/user/User.java
// @XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
// @XmlRootElement
// public class User {
//     public final String userName;
//     public final String nickName;
//     private HashedPassword hashedPassword;
//     private SessionId sessionId;
// 
//     // Package default to give access to factory class only
//     User(String userName, String nickName, HashedPassword hashedPassword) {
//         this.userName = userName;
//         this.nickName = nickName;
//         this.hashedPassword = hashedPassword;
//     }
// 
//     @Override
//     public String toString() {
//         return userName + ":" + nickName;
//     }
// 
//     public String getUserName() {
//         return userName;
//     }
// 
//     public String getNickName() {
//         return nickName;
//     }
// 
//     public HashedPassword getHashedPassword() {
//         return hashedPassword;
//     }
// 
//     public SessionId getSessionId() {
//         return sessionId;
//     }
// 
//     public void setSessionId(SessionId sessionId) {
//         this.sessionId = sessionId;
//     }
// 
//     public boolean hasActiveSession() {
//         Date now = new Date();
//         return now.before(sessionId.expires()) && now.after(sessionId.starts());
//     }
// 
//     // Here for JAXB
//     public User() {
//         this.userName = null;
//         this.nickName = null;
//     }
// }


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.owasp.oneliner.exceptions.AuthenticationException;
import org.owasp.oneliner.exceptions.NoSuchUserException;
import org.owasp.oneliner.password.HashedPassword;
import org.owasp.oneliner.password.HashedPasswordFactory;
import org.owasp.oneliner.user.User;
import javax.annotation.Resource;
package org.owasp.oneliner.authentication;



/**
 * Author: @johnwilander
 * Date: 2010-dec-05
 */
public class Authenticator {
    private static final Log logger = LogFactory.getLog(Authenticator.class);

    @Resource
    private UserDataBase userDataBase;
    @Resource
    private HashedPasswordFactory hashedPasswordFactory;

    public User authenticate(String userName, String suppliedPassword) throws AuthenticationException {
        User user;
        try {
            user = userDataBase.getUserByUserName(userName);
        } catch (NoSuchUserException e) {
            logger.info("No such user: " + userName);
            // SecBug: Information leakage
            // throw AuthenticationException.UNKNOWN_USER_NAME;
// Path: src/main/java/org/owasp/oneliner/exceptions/AuthenticationException.java
// public class AuthenticationException extends Exception {
//     public static final AuthenticationException INTERNAL_ERROR =
//             new AuthenticationException("Could not authenticate due to an internal error");
//     public static final AuthenticationException UNKNOWN_USER_NAME_OR_PASSWORD =
//             new AuthenticationException("Wrong user name or password");
// 
//     // SecBug: Information leakage
//     public static final AuthenticationException UNKNOWN_USER_NAME =
//             new AuthenticationException("Wrong user name");
//     public static final AuthenticationException NOT_AUTHENTICATED =
//             new AuthenticationException("Authentication failed");
// 
//     private AuthenticationException (String message) {
//         super(message);
//         this.setStackTrace(new StackTraceElement[0]);  // No stack trace needed
//     }
// }
// 
// Path: src/main/java/org/owasp/oneliner/exceptions/NoSuchUserException.java
// public class NoSuchUserException extends Exception {
//     public static final NoSuchUserException NO_SUCH_USER = new NoSuchUserException("No such user");
// 
//     public NoSuchUserException(String message) {
//         super(message);
//     }
// }
// 
// Path: src/main/java/org/owasp/oneliner/password/HashedPassword.java
// public class HashedPassword {
//     private final String hashedPassword;
//     private final long salt;
// 
//     // Package default to give access to factory class only
//     HashedPassword(String hashedPassword, long salt) {
//         this.salt = salt;
//         this.hashedPassword = hashedPassword;
//     }
// 
//     public long getSalt() {
//         return salt;
//     }
// 
//     public boolean equals(Object other) {
//         if (!(other instanceof HashedPassword)) {
//             return false;
//         } else {
//             HashedPassword otherHashedPassword = (HashedPassword)other;
//             return hashedPassword.equals(otherHashedPassword.hashedPassword) &&
//                     salt == otherHashedPassword.salt;
//         }
//     }
// }
// 
// Path: src/main/java/org/owasp/oneliner/password/HashedPasswordFactory.java
// public class HashedPasswordFactory {
//     @Resource
//     private SecureRandom secureRandom;
// 
//     public HashedPassword createHashedPassword(String plainTextPassword) {
//         long salt = secureRandom.nextLong();
//         return createHashedPassword(plainTextPassword, salt);
//     }
// 
//     public HashedPassword createHashedPassword(String plainTextPassword, long salt) {
//         String hashedPasswordStr = null;
//         try {
//             hashedPasswordStr = new String(sha256.digest((plainTextPassword + salt).getBytes("UTF8")));
//         } catch (UnsupportedEncodingException e) {
//             e.printStackTrace();
//         }
//         return new HashedPassword(hashedPasswordStr, salt);
//     }
// 
//     private static MessageDigest sha256;
//     static {
//         try {
//             sha256 = MessageDigest.getInstance("SHA-256");
//         } catch (NoSuchAlgorithmException e) {
//             e.printStackTrace();
//         }
//     }
// }
// 
// Path: src/main/java/org/owasp/oneliner/user/User.java
// @XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
// @XmlRootElement
// public class User {
//     public final String userName;
//     public final String nickName;
//     private HashedPassword hashedPassword;
//     private SessionId sessionId;
// 
//     // Package default to give access to factory class only
//     User(String userName, String nickName, HashedPassword hashedPassword) {
//         this.userName = userName;
//         this.nickName = nickName;
//         this.hashedPassword = hashedPassword;
//     }
// 
//     @Override
//     public String toString() {
//         return userName + ":" + nickName;
//     }
// 
//     public String getUserName() {
//         return userName;
//     }
// 
//     public String getNickName() {
//         return nickName;
//     }
// 
//     public HashedPassword getHashedPassword() {
//         return hashedPassword;
//     }
// 
//     public SessionId getSessionId() {
//         return sessionId;
//     }
// 
//     public void setSessionId(SessionId sessionId) {
//         this.sessionId = sessionId;
//     }
// 
//     public boolean hasActiveSession() {
//         Date now = new Date();
//         return now.before(sessionId.expires()) && now.after(sessionId.starts());
//     }
// 
//     // Here for JAXB
//     public User() {
//         this.userName = null;
//         this.nickName = null;
//     }
// }


// Path: src/main/java/org/owasp/oneliner/authentication/Authenticator.java
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.owasp.oneliner.exceptions.AuthenticationException;
import org.owasp.oneliner.exceptions.NoSuchUserException;
import org.owasp.oneliner.password.HashedPassword;
import org.owasp.oneliner.password.HashedPasswordFactory;
import org.owasp.oneliner.user.User;
import javax.annotation.Resource;

package org.owasp.oneliner.authentication;



/**
 * Author: @johnwilander
 * Date: 2010-dec-05
 */
public class Authenticator {
    private static final Log logger = LogFactory.getLog(Authenticator.class);

    @Resource
    private UserDataBase userDataBase;
    @Resource
    private HashedPasswordFactory hashedPasswordFactory;

    public User authenticate(String userName, String suppliedPassword) throws AuthenticationException {
        User user;
        try {
            user = userDataBase.getUserByUserName(userName);
        } catch (NoSuchUserException e) {
            logger.info("No such user: " + userName);
            // SecBug: Information leakage
            // throw AuthenticationException.UNKNOWN_USER_NAME;
            throw AuthenticationException.UNKNOWN_USER_NAME_OR_PASSWORD;