From e00cba6279aaff8976d9a5606de7aa75f189f982 Mon Sep 17 00:00:00 2001 From: geek Date: Tue, 13 Mar 2018 19:19:23 +0900 Subject: [PATCH] totp service modify --- .../member/service/MemberTotpService.java | 41 ++++++++++++------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/loafle/overflow/module/member/service/MemberTotpService.java b/src/main/java/com/loafle/overflow/module/member/service/MemberTotpService.java index 635700b..fc8602e 100644 --- a/src/main/java/com/loafle/overflow/module/member/service/MemberTotpService.java +++ b/src/main/java/com/loafle/overflow/module/member/service/MemberTotpService.java @@ -10,6 +10,9 @@ import org.apache.http.client.utils.URIBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.HashMap; +import java.util.Map; + /** * Created by geek on 18. 3. 8. */ @@ -18,15 +21,21 @@ public class MemberTotpService { @Autowired private MemberTotpDAO totpDAO; - public MemberTotp regist(MemberTotp totp) throws Exception { - if ( null == totp.getSecretCode() || totp.getSecretCode().equals("")) { - throw new Exception("Not Null SecretCode"); - } - if (null == totp.getMember() || 0 < totp.getMember().getId()) { + public void regist(Member member, String secretCode, String code) throws Exception { + + if (null == member || 0 < member.getId()) { throw new Exception("Not Null Member ID"); } - return this.totpDAO.save(totp); + if (!this.checkCode(secretCode, code) ) { + throw new Exception("Not Equal"); + } + + MemberTotp totp = new MemberTotp(); + totp.setMember(member); + totp.setSecretCode(secretCode); + + this.totpDAO.save(totp); } public MemberTotp modify(MemberTotp totp) throws Exception { @@ -48,9 +57,9 @@ public class MemberTotpService { return this.totpDAO.findOne(id); } - public boolean checkCode(MemberTotp totp, String code) throws Exception { + public boolean checkCode(String secretCode, String code) throws Exception { GoogleAuthenticator googleAuthenticator = new GoogleAuthenticator(); - boolean isCheck = googleAuthenticator.authorize(totp.getSecretCode(), Integer.valueOf(code)); + boolean isCheck = googleAuthenticator.authorize(secretCode, Integer.valueOf(code)); if (!isCheck) { throw new Exception("Invalid Code"); @@ -59,8 +68,9 @@ public class MemberTotpService { return isCheck; } - public MemberTotp createTotp(Member member) { - MemberTotp totp = new MemberTotp(); + public Map createTotp(Member member) { +// MemberTotp totp = new MemberTotp(); + Map returnMap = new HashMap<>(); GoogleAuthenticator googleAuthenticator = new GoogleAuthenticator(); final GoogleAuthenticatorKey key = googleAuthenticator.createCredentials(); @@ -73,11 +83,14 @@ public class MemberTotpService { .setPath("/" + formatLabel("overFlow", member.getEmail())) .setParameter("secret", key.getKey()); - totp.setMember(member); - totp.setSecretCode(secret); - totp.setOtpAuth(uri.toString()); +// totp.setMember(member); +// totp.setSecretCode(secret); +// totp.setOtpAuth(uri.toString()); - return totp; + returnMap.put("key", secret); + returnMap.put("uri", uri.toString()); + + return returnMap; } private String formatLabel(String issuer, String accountName) {