// Path: server/src/main/java/com/cloudwise/sap/niping/common/entity/AccessCredentials.java
// @Data
// @Builder
// @EqualsAndHashCode(callSuper = true)
// public class AccessCredentials extends AbstractEntity {
// 	private static final long serialVersionUID = 8305605853259461011L;
// 	private String token;
// 	private String comment;
// 	private Date expiryDate;
// }
// 
// Path: server/src/main/java/com/cloudwise/sap/niping/common/entity/UserEntity.java
// @Data
// @EqualsAndHashCode(callSuper = true)
// public class UserEntity extends AbstractEntity {
// 
// 	private static final long serialVersionUID = -4131084870810239557L;
// 	private String userId;
// 	private String loginName;
// 	private String name;
// 	private String password;
// 	private String passwordSalt;
// 	private int status;
// 
// 	public enum Status {
// 		enable(1), disable(0), deleted(-1);
// 
// 		@Getter
// 		private int status;
// 		Status(int status){this.status = status;}
// 	}
// }
// 
// Path: server/src/main/java/com/cloudwise/sap/niping/common/utils/HashStrategyUtil.java
// public class HashStrategyUtil {
// 
// 	private HashStrategyUtil(){};
// 
// 	private static final char[] HEX_CHARS = "0123456789ABCDEF".toCharArray();
// 
// 	public static String computeHash(String password, String salt) throws Exception {
// 		String algo = "SHA-512";
// 		MessageDigest md = MessageDigest.getInstance(algo);
// 		String concat = (salt == null ? "" : salt) + password;
// 		byte[] bHash = md.digest(concat.getBytes(StandardCharsets.UTF_8));
// 		return bytesToHex(bHash);
// 	}
// 
// 	public static String bytesToHex(byte[] bytes) {
// 		char[] chars = new char[bytes.length * 2];
// 		for (int i = 0; i < bytes.length; i++) {
// 			int x = 0xFF & bytes[i];
// 			chars[i * 2] = HEX_CHARS[x >>> 4];
// 			chars[1 + i * 2] = HEX_CHARS[0x0F & x];
// 		}
// 		return new String(chars);
// 	}
// 
// 	public static void main(String[] args) throws Exception {
// 		String pwd = "sap118";
// 		String salt = "sap998";
// 		if (args != null && args.length == 2) {
// 			pwd = args[0];
// 			salt = args[1];
// 		}
// 		String encPassowrd = HashStrategyUtil.computeHash(pwd, salt);
// 		System.out.println("Password=" + pwd + "\nEncodePassword=" + encPassowrd + "\nSalt=" + salt);
// 	}
// }
// 
// Path: server/src/main/java/com/cloudwise/sap/niping/common/vo/User.java
// @Data
// @Builder
// @AllArgsConstructor
// public class User {
// 	protected String userId;
// 	protected String accountId;
// 	protected String name;
// 	protected String loginName;
// 	private String password;
// 	private String passwordSalt;
// }
// 
// Path: server/src/main/java/com/cloudwise/sap/niping/dao/AccessCredentialsDao.java
// public interface AccessCredentialsDao {
// 
//     @SqlQuery("SELECT TOKEN, ACCOUNT_ID, COMMENT, EXPIRY_DATE, CREATION_TIME, MODIFIED_TIME FROM SNM_ACCESS_CREDENTIALS " +
//             " WHERE TOKEN = :token")
//     @RegisterMapper(AccessCredentialsMapper.class)
//     public AccessCredentials getCredentialsByToken(@Bind("token") String token);
// 
//     @SqlQuery("SELECT TOKEN FROM SNM_ACCESS_CREDENTIALS WHERE ACCOUNT_ID = :accountId")
//     String getTokenByAccountId(@Bind("accountId") String accountId);
// }
// 
// Path: server/src/main/java/com/cloudwise/sap/niping/dao/UserDao.java
// public interface UserDao {
// 
//     @SqlQuery("SELECT USER_ID, ACCOUNT_ID, NAME, PASSWORD, PASSWORD_SALT, LOGIN_NAME FROM SNM_USER " +
//             " WHERE LOGIN_NAME = :loginName AND STATUS = :status")
//     @RegisterMapper(UserMapper.class)
//     public User getUser(@Bind("loginName") String loginName, @Bind("status") int status);
// }
// 
// Path: server/src/main/java/com/cloudwise/sap/niping/exception/NiPingException.java
// @Data
// public class NiPingException extends Exception {
//     private int code;
//     private String errorMessage;
// 
//     public NiPingException(Result errorResult) {
//         this.code = errorResult.getCode();
//         this.errorMessage = errorResult.getMessage();
//     }
// }


import com.cloudwise.sap.niping.common.entity.AccessCredentials;
import com.cloudwise.sap.niping.common.entity.UserEntity;
import com.cloudwise.sap.niping.common.utils.HashStrategyUtil;
import com.cloudwise.sap.niping.common.vo.User;
import com.cloudwise.sap.niping.dao.AccessCredentialsDao;
import com.cloudwise.sap.niping.dao.UserDao;
import com.cloudwise.sap.niping.exception.NiPingException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.jvnet.hk2.annotations.Service;
import org.skife.jdbi.v2.exceptions.DBIException;
import javax.inject.Inject;
import java.util.Optional;
import static com.cloudwise.sap.niping.common.constant.Result.DBError;
package com.cloudwise.sap.niping.service;




@Slf4j
@Service
public class AuthService {

    @Inject
    private AccessCredentialsDao accessCredentialsDao;

    @Inject
    private UserDao userDao;

    public Optional<AccessCredentials> getCredentialsByToken(String credentials) {
        AccessCredentials accessCredentials = null;
        try {
            accessCredentials = accessCredentialsDao.getCredentialsByToken(credentials);
        } catch (DBIException e) {
            log.error("user auth: get credentials by token {} database error: {}", credentials, ExceptionUtils.getMessage(e));
        }
        return Optional.ofNullable(accessCredentials);
    }

// Path: server/src/main/java/com/cloudwise/sap/niping/common/entity/AccessCredentials.java
// @Data
// @Builder
// @EqualsAndHashCode(callSuper = true)
// public class AccessCredentials extends AbstractEntity {
// 	private static final long serialVersionUID = 8305605853259461011L;
// 	private String token;
// 	private String comment;
// 	private Date expiryDate;
// }
// 
// Path: server/src/main/java/com/cloudwise/sap/niping/common/entity/UserEntity.java
// @Data
// @EqualsAndHashCode(callSuper = true)
// public class UserEntity extends AbstractEntity {
// 
// 	private static final long serialVersionUID = -4131084870810239557L;
// 	private String userId;
// 	private String loginName;
// 	private String name;
// 	private String password;
// 	private String passwordSalt;
// 	private int status;
// 
// 	public enum Status {
// 		enable(1), disable(0), deleted(-1);
// 
// 		@Getter
// 		private int status;
// 		Status(int status){this.status = status;}
// 	}
// }
// 
// Path: server/src/main/java/com/cloudwise/sap/niping/common/utils/HashStrategyUtil.java
// public class HashStrategyUtil {
// 
// 	private HashStrategyUtil(){};
// 
// 	private static final char[] HEX_CHARS = "0123456789ABCDEF".toCharArray();
// 
// 	public static String computeHash(String password, String salt) throws Exception {
// 		String algo = "SHA-512";
// 		MessageDigest md = MessageDigest.getInstance(algo);
// 		String concat = (salt == null ? "" : salt) + password;
// 		byte[] bHash = md.digest(concat.getBytes(StandardCharsets.UTF_8));
// 		return bytesToHex(bHash);
// 	}
// 
// 	public static String bytesToHex(byte[] bytes) {
// 		char[] chars = new char[bytes.length * 2];
// 		for (int i = 0; i < bytes.length; i++) {
// 			int x = 0xFF & bytes[i];
// 			chars[i * 2] = HEX_CHARS[x >>> 4];
// 			chars[1 + i * 2] = HEX_CHARS[0x0F & x];
// 		}
// 		return new String(chars);
// 	}
// 
// 	public static void main(String[] args) throws Exception {
// 		String pwd = "sap118";
// 		String salt = "sap998";
// 		if (args != null && args.length == 2) {
// 			pwd = args[0];
// 			salt = args[1];
// 		}
// 		String encPassowrd = HashStrategyUtil.computeHash(pwd, salt);
// 		System.out.println("Password=" + pwd + "\nEncodePassword=" + encPassowrd + "\nSalt=" + salt);
// 	}
// }
// 
// Path: server/src/main/java/com/cloudwise/sap/niping/common/vo/User.java
// @Data
// @Builder
// @AllArgsConstructor
// public class User {
// 	protected String userId;
// 	protected String accountId;
// 	protected String name;
// 	protected String loginName;
// 	private String password;
// 	private String passwordSalt;
// }
// 
// Path: server/src/main/java/com/cloudwise/sap/niping/dao/AccessCredentialsDao.java
// public interface AccessCredentialsDao {
// 
//     @SqlQuery("SELECT TOKEN, ACCOUNT_ID, COMMENT, EXPIRY_DATE, CREATION_TIME, MODIFIED_TIME FROM SNM_ACCESS_CREDENTIALS " +
//             " WHERE TOKEN = :token")
//     @RegisterMapper(AccessCredentialsMapper.class)
//     public AccessCredentials getCredentialsByToken(@Bind("token") String token);
// 
//     @SqlQuery("SELECT TOKEN FROM SNM_ACCESS_CREDENTIALS WHERE ACCOUNT_ID = :accountId")
//     String getTokenByAccountId(@Bind("accountId") String accountId);
// }
// 
// Path: server/src/main/java/com/cloudwise/sap/niping/dao/UserDao.java
// public interface UserDao {
// 
//     @SqlQuery("SELECT USER_ID, ACCOUNT_ID, NAME, PASSWORD, PASSWORD_SALT, LOGIN_NAME FROM SNM_USER " +
//             " WHERE LOGIN_NAME = :loginName AND STATUS = :status")
//     @RegisterMapper(UserMapper.class)
//     public User getUser(@Bind("loginName") String loginName, @Bind("status") int status);
// }
// 
// Path: server/src/main/java/com/cloudwise/sap/niping/exception/NiPingException.java
// @Data
// public class NiPingException extends Exception {
//     private int code;
//     private String errorMessage;
// 
//     public NiPingException(Result errorResult) {
//         this.code = errorResult.getCode();
//         this.errorMessage = errorResult.getMessage();
//     }
// }


// Path: server/src/main/java/com/cloudwise/sap/niping/service/AuthService.java
import com.cloudwise.sap.niping.common.entity.AccessCredentials;
import com.cloudwise.sap.niping.common.entity.UserEntity;
import com.cloudwise.sap.niping.common.utils.HashStrategyUtil;
import com.cloudwise.sap.niping.common.vo.User;
import com.cloudwise.sap.niping.dao.AccessCredentialsDao;
import com.cloudwise.sap.niping.dao.UserDao;
import com.cloudwise.sap.niping.exception.NiPingException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.jvnet.hk2.annotations.Service;
import org.skife.jdbi.v2.exceptions.DBIException;
import javax.inject.Inject;
import java.util.Optional;
import static com.cloudwise.sap.niping.common.constant.Result.DBError;

package com.cloudwise.sap.niping.service;




@Slf4j
@Service
public class AuthService {

    @Inject
    private AccessCredentialsDao accessCredentialsDao;

    @Inject
    private UserDao userDao;

    public Optional<AccessCredentials> getCredentialsByToken(String credentials) {
        AccessCredentials accessCredentials = null;
        try {
            accessCredentials = accessCredentialsDao.getCredentialsByToken(credentials);
        } catch (DBIException e) {
            log.error("user auth: get credentials by token {} database error: {}", credentials, ExceptionUtils.getMessage(e));
        }
        return Optional.ofNullable(accessCredentials);
    }

    public Optional<User> validateUser(String loginName, String password) {