From a8b21cd919b76a548b77ef0ec2d8aee28c8e6b0e Mon Sep 17 00:00:00 2001
From: wayne <wayne8692wayne8692@gmail.com>
Date: 星期二, 23 十一月 2021 10:41:56 +0800
Subject: [PATCH] [update] 修正發送OTP api,將兩個發送方式並成同一個

---
 /dev/null                                                           |   15 -----
 pamapi/src/main/java/com/pollex/pam/web/rest/TestLoginResource.java |   32 +++++-----
 pamapi/src/main/java/com/pollex/pam/web/rest/OtpResource.java       |   40 ++++---------
 pamapi/src/main/java/com/pollex/pam/service/OtpWebService.java      |   17 ++---
 pamapi/src/doc/登入API/客戶發OTP.txt                                     |   16 +++++
 pamapi/src/main/java/com/pollex/pam/web/rest/vm/OtpLoginVM.java     |   24 ++++++++
 pamapi/src/main/java/com/pollex/pam/enums/OtpLoginTypeEnum.java     |    6 ++
 7 files changed, 82 insertions(+), 68 deletions(-)

diff --git "a/pamapi/src/doc/\347\231\273\345\205\245API/\345\256\242\346\210\266\347\231\274EmailOTP.txt" "b/pamapi/src/doc/\347\231\273\345\205\245API/\345\256\242\346\210\266\347\231\274EmailOTP.txt"
deleted file mode 100644
index d1fba1e..0000000
--- "a/pamapi/src/doc/\347\231\273\345\205\245API/\345\256\242\346\210\266\347\231\274EmailOTP.txt"
+++ /dev/null
@@ -1,15 +0,0 @@
-http post:
-http://localhost:8080/api/otp/byEmail
-
-request body:
-{
-    "email":"test@pollex.com.tw"
-}
-
-response body:
-{
-    "indexKey": "7830d17b",
-    "success": true,
-    "failCode": "",
-    "failReason": ""
-}
diff --git "a/pamapi/src/doc/\347\231\273\345\205\245API/\345\256\242\346\210\266\347\231\274OTP.txt" "b/pamapi/src/doc/\347\231\273\345\205\245API/\345\256\242\346\210\266\347\231\274OTP.txt"
new file mode 100644
index 0000000..732f42a
--- /dev/null
+++ "b/pamapi/src/doc/\347\231\273\345\205\245API/\345\256\242\346\210\266\347\231\274OTP.txt"
@@ -0,0 +1,16 @@
+http post:
+http://localhost:8080/api/otp/sendOtp
+
+request body:
+{
+    "loginType":"SMS",          // "SMS"嚗tp������"EMAIL":Otp�email
+    "account": "0912345678",    // �loginType憛俟MS��府甈葆�����MAIL��葆��隞嗡縑蝞�
+}
+
+response body:
+{
+    "indexKey": "7c8c38a2",     // ��撣嗅otp隤���
+    "success": true,            // Otp��������
+    "failCode": "",
+    "failReason": ""
+}
diff --git "a/pamapi/src/doc/\347\231\273\345\205\245API/\345\256\242\346\210\266\347\231\274\346\211\213\346\251\237OTP.txt" "b/pamapi/src/doc/\347\231\273\345\205\245API/\345\256\242\346\210\266\347\231\274\346\211\213\346\251\237OTP.txt"
deleted file mode 100644
index 6072e63..0000000
--- "a/pamapi/src/doc/\347\231\273\345\205\245API/\345\256\242\346\210\266\347\231\274\346\211\213\346\251\237OTP.txt"
+++ /dev/null
@@ -1,15 +0,0 @@
-http post:
-http://localhost:8080/api/otp/byPhone
-
-request body:
-{
-    "phone":"0912345678"
-}
-
-response body:
-{
-    "indexKey": "5913a8cd",
-    "success": true,
-    "failCode": "",
-    "failReason": ""
-}
diff --git a/pamapi/src/main/java/com/pollex/pam/enums/OtpLoginTypeEnum.java b/pamapi/src/main/java/com/pollex/pam/enums/OtpLoginTypeEnum.java
new file mode 100644
index 0000000..43635cd
--- /dev/null
+++ b/pamapi/src/main/java/com/pollex/pam/enums/OtpLoginTypeEnum.java
@@ -0,0 +1,6 @@
+package com.pollex.pam.enums;
+
+public enum OtpLoginTypeEnum {
+    SMS,
+    EMAIL
+}
diff --git a/pamapi/src/main/java/com/pollex/pam/service/OtpWebService.java b/pamapi/src/main/java/com/pollex/pam/service/OtpWebService.java
index 35c9a63..144e202 100644
--- a/pamapi/src/main/java/com/pollex/pam/service/OtpWebService.java
+++ b/pamapi/src/main/java/com/pollex/pam/service/OtpWebService.java
@@ -22,7 +22,8 @@
 
     public OtpResponseDTO sendByPhone(String phone) throws ServiceException, RemoteException {
         OtpWebServicePortBindingStub stub = getOtpWebServicePortBindingStub();
-        log.info("call OtpService snedOtpBySMS, ");
+        log.info("call OtpService sendOtpBySMS, url = {}, systemType = {}, service password = {}, phone = {}",
+            applicationProperty.getOtpWebServiceUrl(), applicationProperty.getOtpWebServiceSystemType(), applicationProperty.getOtpWebServicePassword(), phone);
 
         String[] result =
             stub.sendOtpBySMS(applicationProperty.getOtpWebServicePassword(), applicationProperty.getOtpWebServiceSystemType(), phone);
@@ -32,21 +33,19 @@
 
     public OtpResponseDTO sendByEmail(String email) throws ServiceException, RemoteException {
         OtpWebServicePortBindingStub stub = getOtpWebServicePortBindingStub();
+        log.info("call OtpService sendByEmail, url = {}, systemType = {}, service password = {}, email = {}",
+            applicationProperty.getOtpWebServiceUrl(), applicationProperty.getOtpWebServiceSystemType(), applicationProperty.getOtpWebServicePassword(), email);
 
         String[] result =
             stub.sendOtpByEmail(applicationProperty.getOtpWebServicePassword(), applicationProperty.getOtpWebServiceSystemType(), email);
 
-        final OtpResponseDTO otpResponseDTO = new OtpResponseDTO(result);
-        if(otpResponseDTO.isSuccess()) {
-            return otpResponseDTO;
-        }
-        else {
-            throw new RuntimeException("error code = " + otpResponseDTO.getFailCode() + ", error reason = " + otpResponseDTO.getFailReason());
-        }
+        return new OtpResponseDTO(result);
     }
 
     public OtpResponseDTO verifyOTP(String indexKey, String otpCode) throws ServiceException, RemoteException {
         OtpWebServicePortBindingStub stub = getOtpWebServicePortBindingStub();
+        log.info("call OtpService verifyOTP, url = {}, systemType = {}, service password = {}, indexKey = {}, otpCode = {}",
+            applicationProperty.getOtpWebServiceUrl(), applicationProperty.getOtpWebServiceSystemType(), applicationProperty.getOtpWebServicePassword(), indexKey, otpCode);
 
         String[] result =
             stub.verifyOtp(applicationProperty.getOtpWebServicePassword(), applicationProperty.getOtpWebServiceSystemType(), indexKey, otpCode);
@@ -54,7 +53,7 @@
         return new OtpResponseDTO(result);
     }
 
-    private OtpWebServicePortBindingStub getOtpWebServicePortBindingStub() throws ServiceException {
+    public OtpWebServicePortBindingStub getOtpWebServicePortBindingStub() throws ServiceException {
         OtpWebServiceLocator locator = new OtpWebServiceLocator();
         locator.setOtpWebServicePortEndpointAddress(applicationProperty.getOtpWebServiceUrl());
 
diff --git a/pamapi/src/main/java/com/pollex/pam/web/rest/OtpResource.java b/pamapi/src/main/java/com/pollex/pam/web/rest/OtpResource.java
index bb55739..6308dac 100644
--- a/pamapi/src/main/java/com/pollex/pam/web/rest/OtpResource.java
+++ b/pamapi/src/main/java/com/pollex/pam/web/rest/OtpResource.java
@@ -1,15 +1,13 @@
 package com.pollex.pam.web.rest;
 
 import com.pollex.pam.config.ApplicationProperties;
+import com.pollex.pam.enums.OtpLoginTypeEnum;
 import com.pollex.pam.security.jwt.JWTFilter;
 import com.pollex.pam.security.jwt.TokenProvider;
 import com.pollex.pam.security.token.OtpAuthenticationToken;
 import com.pollex.pam.service.OtpWebService;
 import com.pollex.pam.service.dto.OtpResponseDTO;
-import com.pollex.pam.web.rest.vm.OtpAccount;
-import com.pollex.pam.web.rest.vm.OtpEmailLoginVM;
-import com.pollex.pam.web.rest.vm.OtpSMSLoginVM;
-import com.pollex.pam.web.rest.vm.VerifyOtpVM;
+import com.pollex.pam.web.rest.vm.*;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -22,14 +20,8 @@
 import org.springframework.web.bind.annotation.*;
 
 import javax.xml.rpc.ServiceException;
-import java.nio.charset.Charset;
-import java.nio.charset.StandardCharsets;
 import java.rmi.RemoteException;
-import java.util.Arrays;
-import java.util.Random;
 import java.util.UUID;
-
-import static java.nio.charset.StandardCharsets.UTF_8;
 
 @RestController
 @RequestMapping("/api/otp")
@@ -49,29 +41,21 @@
     @Autowired
     TokenProvider tokenProvider;
 
-    @PostMapping("/byPhone")
-    public ResponseEntity<Object> sendOtpByPhone(@RequestBody OtpSMSLoginVM login) {
+    @PostMapping("/sendOtp")
+    public ResponseEntity<Object> sendOtp(@RequestBody OtpLoginVM login) {
         try {
             if(applicationProperty.isMockLogin()) {
-                return new ResponseEntity<>(getMockOtpResponse(), HttpStatus.OK);
+                return new ResponseEntity<>(getMockSendOtpResponse(), HttpStatus.OK);
             }
 
-            OtpResponseDTO otpResponseDTO = otpWebService.sendByPhone(login.getPhone());
-            return new ResponseEntity<>(otpResponseDTO, HttpStatus.OK);
-        } catch (ServiceException | RemoteException e) {
-            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("connecting otp web service error");
-        }
-    }
-
-    @PostMapping("/byEmail")
-    public ResponseEntity<Object> sendOtpByEmail(@RequestBody OtpEmailLoginVM login) {
-        try {
-            if(applicationProperty.isMockLogin()) {
-                return new ResponseEntity<>(getMockOtpResponse(), HttpStatus.OK);
+            if(login.getLoginType() == OtpLoginTypeEnum.SMS) {
+                return new ResponseEntity<>(otpWebService.sendByPhone(login.getAccount()), HttpStatus.OK);
+            }
+            else if(login.getLoginType() == OtpLoginTypeEnum.EMAIL) {
+                return new ResponseEntity<>(otpWebService.sendByEmail(login.getAccount()), HttpStatus.OK);
             }
 
-            OtpResponseDTO otpResponseDTO = otpWebService.sendByEmail(login.getEmail());
-            return new ResponseEntity<>(otpResponseDTO, HttpStatus.OK);
+            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("can not support this login type, loginType = " + login.getLoginType().name());
         } catch (ServiceException | RemoteException e) {
             return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("connecting otp web service error");
         }
@@ -93,7 +77,7 @@
         return new ResponseEntity<>(new UserJWTController.JWTToken(jwt), httpHeaders, HttpStatus.OK);
     }
 
-    private OtpResponseDTO getMockOtpResponse() {
+    private OtpResponseDTO getMockSendOtpResponse() {
         String indexKey = UUID.randomUUID().toString().substring(0, 8);
         return new OtpResponseDTO(new String[]{indexKey, "0", "", ""});
     }
diff --git a/pamapi/src/main/java/com/pollex/pam/web/rest/TestLoginResource.java b/pamapi/src/main/java/com/pollex/pam/web/rest/TestLoginResource.java
index 07691a3..ae4d394 100644
--- a/pamapi/src/main/java/com/pollex/pam/web/rest/TestLoginResource.java
+++ b/pamapi/src/main/java/com/pollex/pam/web/rest/TestLoginResource.java
@@ -19,10 +19,14 @@
 import org.springframework.security.core.Authentication;
 import org.springframework.security.core.context.SecurityContextHolder;
 import org.springframework.web.bind.annotation.*;
+import tw.com.softleader.otp.ws.OtpWebServicePortBindingStub;
 
 import javax.xml.rpc.ServiceException;
 import java.rmi.RemoteException;
 
+
+// todo嚗����login�靘蹂蝙��get��撘�嚗��歇��OtpResource��ServiceResource嚗蜓閬�����
+@Deprecated
 @RestController
 @RequestMapping("/api/testLogin")
 public class TestLoginResource {
@@ -46,30 +50,26 @@
 
     @GetMapping("/bySMS")
     public ResponseEntity<OtpResponseDTO> sendOtpBySMS(@RequestParam("phone") String phone) throws ServiceException, RemoteException {
-        otpWebService.sendByPhone(phone);
-        return new ResponseEntity<>(HttpStatus.OK);
+        final OtpResponseDTO otpResponseDTO = otpWebService.sendByPhone(phone);
+        return new ResponseEntity<>(otpResponseDTO, HttpStatus.OK);
     }
 
     @GetMapping("/byEmail")
     public ResponseEntity<OtpResponseDTO> sendOtpByEmail(@RequestParam("email") String email) throws RemoteException, ServiceException {
-        otpWebService.sendByEmail(email);
-        return new ResponseEntity<>(HttpStatus.OK);
+        final OtpResponseDTO otpResponseDTO = otpWebService.sendByEmail(email);
+        return new ResponseEntity<>(otpResponseDTO, HttpStatus.OK);
     }
 
     @GetMapping("/verifyOtp")
-    public ResponseEntity<UserJWTController.JWTToken> verifyOtp(@RequestParam("account") String account, @RequestParam("indexKey") String indexKey, @RequestParam("otpCode") String otpCode) throws ServiceException, RemoteException {
-        OtpAccount otpAccount = new OtpAccount(account, indexKey);
-        OtpAuthenticationToken authenticationToken = new OtpAuthenticationToken(
-            otpAccount,
-            otpCode
-        );
+    public ResponseEntity<OtpResponseDTO> verifyOtp(@RequestParam("account") String account, @RequestParam("indexKey") String indexKey, @RequestParam("otpCode") String otpCode) throws ServiceException, RemoteException {
+        OtpWebServicePortBindingStub stub = otpWebService.getOtpWebServicePortBindingStub();
+        log.info("call OtpService verifyOTP, systemType = {}, service password = {}, indexKey = {}, paxxword = {}",
+            applicationProperty.getOtpWebServiceSystemType(), applicationProperty.getOtpWebServicePassword(), indexKey, otpCode);
 
-        Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken);
-        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
-        String jwt = tokenProvider.createToken(authentication, false);
-        HttpHeaders httpHeaders = new HttpHeaders();
-        httpHeaders.add(JWTFilter.AUTHORIZATION_HEADER, "Bearer" + jwt);
-        return new ResponseEntity<>(new UserJWTController.JWTToken(jwt), httpHeaders, HttpStatus.OK);
+        String[] result =
+            stub.verifyOtp(applicationProperty.getOtpWebServicePassword(), applicationProperty.getOtpWebServiceSystemType(), indexKey, otpCode);
+
+        return new ResponseEntity<>(new OtpResponseDTO(result), HttpStatus.OK);
     }
 
     @GetMapping("/byEService")
diff --git a/pamapi/src/main/java/com/pollex/pam/web/rest/vm/OtpLoginVM.java b/pamapi/src/main/java/com/pollex/pam/web/rest/vm/OtpLoginVM.java
new file mode 100644
index 0000000..e376624
--- /dev/null
+++ b/pamapi/src/main/java/com/pollex/pam/web/rest/vm/OtpLoginVM.java
@@ -0,0 +1,24 @@
+package com.pollex.pam.web.rest.vm;
+
+import com.pollex.pam.enums.OtpLoginTypeEnum;
+
+public class OtpLoginVM {
+    private OtpLoginTypeEnum loginType;
+    private String account;
+
+    public OtpLoginTypeEnum getLoginType() {
+        return loginType;
+    }
+
+    public void setLoginType(OtpLoginTypeEnum loginType) {
+        this.loginType = loginType;
+    }
+
+    public String getAccount() {
+        return account;
+    }
+
+    public void setAccount(String account) {
+        this.account = account;
+    }
+}

--
Gitblit v1.8.0