| | |
| | | |
| | | 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; |
| | |
| | | |
| | | @Autowired |
| | | CustomerRepository customerRepository; |
| | | |
| | | @Autowired |
| | | OtpTmpService otpTmpService; |
| | | |
| | | public Authentication authenticate(OtpAuthenticationToken otpAuthenticationToken) throws AuthenticationException { |
| | | OtpAccount otpAccount = otpAuthenticationToken.getPrincipal(); |
| | |
| | | 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); |
| | |
| | | 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); |
| | |
| | | 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; |