保誠-保戶業務員媒合平台
wayne
2021-11-25 0e8438fba91e61385093e49ffe0d7b1b01bd9e4a
pamapi/src/main/java/com/pollex/pam/security/provider/OtpAuthenticationProvider.java
@@ -2,9 +2,13 @@
import com.pollex.pam.config.ApplicationProperties;
import com.pollex.pam.domain.Customer;
import com.pollex.pam.domain.OtpTmp;
import com.pollex.pam.enums.CustomerDetailEnum;
import com.pollex.pam.enums.OtpLoginTypeEnum;
import com.pollex.pam.enums.OtpTmpStatusEnum;
import com.pollex.pam.repository.CustomerRepository;
import com.pollex.pam.security.token.OtpAuthenticationToken;
import com.pollex.pam.service.OtpTmpService;
import com.pollex.pam.service.OtpWebService;
import com.pollex.pam.service.dto.OtpResponseDTO;
import com.pollex.pam.web.rest.vm.OtpAccount;
@@ -38,6 +42,9 @@
    @Autowired
    CustomerRepository customerRepository;
    @Autowired
    OtpTmpService otpTmpService;
    public Authentication authenticate(OtpAuthenticationToken otpAuthenticationToken) throws AuthenticationException {
        OtpAccount otpAccount = otpAuthenticationToken.getPrincipal();
@@ -46,13 +53,15 @@
        String otpCode = otpAuthenticationToken.getCredentials();
        if(applicationProperty.isMockLogin()){
            return getCustomerToken(account, otpCode);
           setVerrifiedOtpTmp(account, indexKey);
            return getCustomerToken(account, otpCode, indexKey);
        }
        try {
            OtpResponseDTO otpResponseDTO = otpWebService.verifyOTP(indexKey, otpCode);
            if(otpResponseDTO.isSuccess()) {
                return getCustomerToken(account, otpCode);
               setVerrifiedOtpTmp(account, indexKey);
                return getCustomerToken(account, otpCode, indexKey);
            }
        } catch (Exception e) {
            log.error("Exception: ", e);
@@ -62,9 +71,18 @@
        throw new AuthenticationCredentialsNotFoundException("");
    }
    private UsernamePasswordAuthenticationToken getCustomerToken(String account, String otpCode) {
        // todo 未存在於DB所屬正常現象,需用特殊message告知前端可進行註冊
        Customer customer = customerRepository.findOneByEmailEqualsOrPhoneEquals(account, account).orElseThrow(() -> new UsernameNotFoundException("this customer is not in db, account = " + account));
    private void setVerrifiedOtpTmp(String account, String indexKey) {
       OtpTmp otpTmp = otpTmpService.findByAccountAndIndexKey(account, indexKey);
       otpTmp.setStatus(OtpTmpStatusEnum.VERRIFIED);
       otpTmpService.save(otpTmp);
   }
   private UsernamePasswordAuthenticationToken getCustomerToken(String account
          , String otpCode, String indexKey) {
       // todo 未存在於DB所屬正常現象,需用特殊message告知前端可進行註冊
        Customer customer = customerRepository.findOneByEmailEqualsOrPhoneEquals(account, account).orElseThrow(() -> new UsernameNotFoundException("this customer is not in register, account = " + account));
        List<GrantedAuthority> grantedAuths = Arrays.asList(new SimpleGrantedAuthority("ROLE_USER"));
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(account, otpCode, grantedAuths);
@@ -73,6 +91,7 @@
        details.put(CustomerDetailEnum.ID.getValue(), customer.getId().toString());
        details.put(CustomerDetailEnum.NAME.getValue(), customer.getName());
        details.put(CustomerDetailEnum.ACCOUNT.getValue(), account);
//        details.put(CustomerDetailEnum.CONTACT_TYPE.getValue(), customer.getContactType());
        authenticationToken.setDetails(details);
        return authenticationToken;