From b832880e1fcf3307387d53106078498f68a95853 Mon Sep 17 00:00:00 2001
From: jack <jack.su@pollex.com.tw>
Date: 星期二, 05 三月 2024 16:59:02 +0800
Subject: [PATCH] 移除TestLoginResource

---
 pamapi/src/main/java/com/pollex/pam/service/CustomerAuthService.java |   45 +++++++++++++++++++++++++++++++++++++++------
 1 files changed, 39 insertions(+), 6 deletions(-)

diff --git a/pamapi/src/main/java/com/pollex/pam/service/CustomerAuthService.java b/pamapi/src/main/java/com/pollex/pam/service/CustomerAuthService.java
index 6960ee6..e7d394c 100644
--- a/pamapi/src/main/java/com/pollex/pam/service/CustomerAuthService.java
+++ b/pamapi/src/main/java/com/pollex/pam/service/CustomerAuthService.java
@@ -1,34 +1,67 @@
 package com.pollex.pam.service;
 
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
 import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
 import org.springframework.security.core.Authentication;
+import org.springframework.security.core.GrantedAuthority;
+import org.springframework.security.core.authority.SimpleGrantedAuthority;
 import org.springframework.security.core.context.SecurityContextHolder;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
+import com.pollex.pam.business.domain.Customer;
+import com.pollex.pam.business.enums.CustomerDetailEnum;
+import com.pollex.pam.business.repository.CustomerRepository;
 import com.pollex.pam.security.jwt.TokenProvider;
 import com.pollex.pam.security.token.OtpAuthenticationToken;
-import com.pollex.pam.web.rest.vm.OtpAccount;
+import com.pollex.pam.business.web.vm.OtpAccount;
 
 @Service
+@Transactional
 public class CustomerAuthService {
-	
+
 	@Autowired
     AuthenticationManagerBuilder authenticationManagerBuilder;
 
+	@Autowired
+    CustomerRepository customerRepository;
+
     @Autowired
     TokenProvider tokenProvider;
-	
-	public String authorize(String account, String indexKey, String otpCode) {
-		OtpAccount otpAccount = new OtpAccount(account, indexKey);
+
+	public String authorize(Customer account, String indexKey, String otpCode) {
+		OtpAccount otpAccount = OtpAccount.createOtpAccount(account, indexKey);
         OtpAuthenticationToken authenticationToken = new OtpAuthenticationToken(
             otpAccount,
             otpCode
         );
 
-        Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken);
         SecurityContextHolder.getContext().setAuthentication(authenticationToken);
+
+        Authentication authentication = buildCustomerAuthToken(account, otpCode, indexKey);
         String jwt = tokenProvider.createToken(authentication, false);
         return jwt;
 	}
+
+	public UsernamePasswordAuthenticationToken buildCustomerAuthToken(Customer customer
+    		, String otpCode, String indexKey) {
+
+        List<GrantedAuthority> grantedAuths = Arrays.asList(new SimpleGrantedAuthority("ROLE_USER"));
+
+        final String account = customer.toAccountString();
+        UsernamePasswordAuthenticationToken authenticationToken =
+        		new UsernamePasswordAuthenticationToken(account, otpCode, grantedAuths);
+
+        Map<String, String> details = new HashMap<>();
+        details.put(CustomerDetailEnum.DB_ID.getValue(), customer.getId().toString());
+        authenticationToken.setDetails(details);
+
+        return authenticationToken;
+    }
 }

--
Gitblit v1.8.0