totp service modify

This commit is contained in:
geek 2018-03-13 19:19:23 +09:00
parent ab8c44efca
commit e00cba6279

View File

@ -10,6 +10,9 @@ import org.apache.http.client.utils.URIBuilder;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
/** /**
* Created by geek on 18. 3. 8. * Created by geek on 18. 3. 8.
*/ */
@ -18,15 +21,21 @@ public class MemberTotpService {
@Autowired @Autowired
private MemberTotpDAO totpDAO; private MemberTotpDAO totpDAO;
public MemberTotp regist(MemberTotp totp) throws Exception { public void regist(Member member, String secretCode, String code) throws Exception {
if ( null == totp.getSecretCode() || totp.getSecretCode().equals("")) {
throw new Exception("Not Null SecretCode"); if (null == member || 0 < member.getId()) {
}
if (null == totp.getMember() || 0 < totp.getMember().getId()) {
throw new Exception("Not Null Member ID"); 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 { public MemberTotp modify(MemberTotp totp) throws Exception {
@ -48,9 +57,9 @@ public class MemberTotpService {
return this.totpDAO.findOne(id); 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(); GoogleAuthenticator googleAuthenticator = new GoogleAuthenticator();
boolean isCheck = googleAuthenticator.authorize(totp.getSecretCode(), Integer.valueOf(code)); boolean isCheck = googleAuthenticator.authorize(secretCode, Integer.valueOf(code));
if (!isCheck) { if (!isCheck) {
throw new Exception("Invalid Code"); throw new Exception("Invalid Code");
@ -59,8 +68,9 @@ public class MemberTotpService {
return isCheck; return isCheck;
} }
public MemberTotp createTotp(Member member) { public Map<String, String> createTotp(Member member) {
MemberTotp totp = new MemberTotp(); // MemberTotp totp = new MemberTotp();
Map<String, String> returnMap = new HashMap<>();
GoogleAuthenticator googleAuthenticator = new GoogleAuthenticator(); GoogleAuthenticator googleAuthenticator = new GoogleAuthenticator();
final GoogleAuthenticatorKey key = googleAuthenticator.createCredentials(); final GoogleAuthenticatorKey key = googleAuthenticator.createCredentials();
@ -73,11 +83,14 @@ public class MemberTotpService {
.setPath("/" + formatLabel("overFlow", member.getEmail())) .setPath("/" + formatLabel("overFlow", member.getEmail()))
.setParameter("secret", key.getKey()); .setParameter("secret", key.getKey());
totp.setMember(member); // totp.setMember(member);
totp.setSecretCode(secret); // totp.setSecretCode(secret);
totp.setOtpAuth(uri.toString()); // totp.setOtpAuth(uri.toString());
return totp; returnMap.put("key", secret);
returnMap.put("uri", uri.toString());
return returnMap;
} }
private String formatLabel(String issuer, String accountName) { private String formatLabel(String issuer, String accountName) {