// 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/service/AuthService.java
// @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) {
//         User user = null;
//         try {
//             user = userDao.getUser(loginName, UserEntity.Status.enable.getStatus());
//             if (null != user && user.getPassword().equals(HashStrategyUtil.computeHash(password, user.getPasswordSalt()))) {
//                 return Optional.of(user);
//             }
//         } catch (DBIException e) {
//             log.error("user auth: get user {} database error: {}", loginName, ExceptionUtils.getMessage(e));
//         } catch (Exception e) {
//             log.error("user auth: validate user {} computeHash error: {}", loginName, ExceptionUtils.getMessage(e));
//         }
//         return Optional.empty();
//     }
// 
//     public String getTokenByAccountId(String accountId) throws NiPingException {
//         String token = null;
//         try {
//             token = accessCredentialsDao.getTokenByAccountId(accountId);
//         } catch (DBIException e) {
//             log.error("user auth: get token by account id {} error: {}", accountId, ExceptionUtils.getMessage(e));
//             throw new NiPingException(DBError);
//         }
//         return token;
//     }
// }


import com.cloudwise.sap.niping.common.entity.AccessCredentials;
import com.cloudwise.sap.niping.service.AuthService;
import io.dropwizard.auth.AuthenticationException;
import io.dropwizard.auth.Authenticator;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jvnet.hk2.annotations.Service;
import javax.inject.Inject;
import java.util.Optional;
package com.cloudwise.sap.niping.auth;



@Data
@Service
@Slf4j
public class SapOAuthenticator implements Authenticator<String, OAuthUser> {

    @Inject
// 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/service/AuthService.java
// @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) {
//         User user = null;
//         try {
//             user = userDao.getUser(loginName, UserEntity.Status.enable.getStatus());
//             if (null != user && user.getPassword().equals(HashStrategyUtil.computeHash(password, user.getPasswordSalt()))) {
//                 return Optional.of(user);
//             }
//         } catch (DBIException e) {
//             log.error("user auth: get user {} database error: {}", loginName, ExceptionUtils.getMessage(e));
//         } catch (Exception e) {
//             log.error("user auth: validate user {} computeHash error: {}", loginName, ExceptionUtils.getMessage(e));
//         }
//         return Optional.empty();
//     }
// 
//     public String getTokenByAccountId(String accountId) throws NiPingException {
//         String token = null;
//         try {
//             token = accessCredentialsDao.getTokenByAccountId(accountId);
//         } catch (DBIException e) {
//             log.error("user auth: get token by account id {} error: {}", accountId, ExceptionUtils.getMessage(e));
//             throw new NiPingException(DBError);
//         }
//         return token;
//     }
// }


// Path: server/src/main/java/com/cloudwise/sap/niping/auth/SapOAuthenticator.java
import com.cloudwise.sap.niping.common.entity.AccessCredentials;
import com.cloudwise.sap.niping.service.AuthService;
import io.dropwizard.auth.AuthenticationException;
import io.dropwizard.auth.Authenticator;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jvnet.hk2.annotations.Service;
import javax.inject.Inject;
import java.util.Optional;

package com.cloudwise.sap.niping.auth;



@Data
@Service
@Slf4j
public class SapOAuthenticator implements Authenticator<String, OAuthUser> {

    @Inject
    AuthService authService;