|
@@ -10,7 +10,6 @@ import java.util.Base64.Encoder;
|
|
|
import java.util.List;
|
|
|
import java.util.Random;
|
|
|
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
import javax.servlet.http.HttpSession;
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
@@ -43,6 +42,7 @@ import com.icontrols.oauth.model.WallpadCode;
|
|
|
import com.icontrols.oauth.repo.ClientInfoRepository;
|
|
|
import com.icontrols.oauth.repo.ComplexInfoRepository;
|
|
|
import com.icontrols.oauth.utils.AES256Util;
|
|
|
+import com.icontrols.oauth.utils.DanziAuthUtils;
|
|
|
import com.icontrols.oauth.utils.JWTUtils;
|
|
|
|
|
|
@RestController
|
|
@@ -76,8 +76,14 @@ public class OAuthController {
|
|
|
logger.info("*1. response_type: " + responseType);
|
|
|
logger.info("*2. client_id: " + clientId);
|
|
|
logger.info("*3. state: " + state);
|
|
|
- logger.info("*4. scope: " + scope);
|
|
|
+// logger.info("*4. scope: " + scope);
|
|
|
logger.info("*5. redirect_uri: " + redirectUri);
|
|
|
+ // 월패드 인증코드 시간이 만료되어 history.go(-1)로 해당 페이지에 들어오는 경우에
|
|
|
+ // 세션에 저장되었던 사용자 정보를 지움.
|
|
|
+ if (httpSession.getAttribute("wallpadCode") != null)
|
|
|
+ httpSession.removeAttribute("wallpadCode");
|
|
|
+ if (httpSession.getAttribute("homeInfo") != null)
|
|
|
+ httpSession.removeAttribute("homeInfo");
|
|
|
|
|
|
ModelAndView mav = new ModelAndView();
|
|
|
|
|
@@ -92,11 +98,15 @@ public class OAuthController {
|
|
|
if (!responseType.equals("code")) {
|
|
|
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, ERROR.UNSUPPORTED_GRANT_TYPE);
|
|
|
} else {
|
|
|
-
|
|
|
+ // 클라이언트에 따라 endpoint를 변경해야해서 세션에 클라이언트 정보를 저장.
|
|
|
+ httpSession.setAttribute("clientId", clientId);
|
|
|
httpSession.setAttribute("redirectUri", redirectUri);
|
|
|
httpSession.setAttribute("state", state);
|
|
|
|
|
|
- // TODO scope != null -> httpSession.setAttribute("scope", scope);
|
|
|
+ // TODO
|
|
|
+// if (scope != null)
|
|
|
+// httpSession.setAttribute("scope", scope);
|
|
|
+
|
|
|
List<ComplexInfo> complexInfos = complexInfoRepo.findAll();
|
|
|
model.addAttribute("complexInfos", complexInfos);
|
|
|
|
|
@@ -110,7 +120,7 @@ public class OAuthController {
|
|
|
}
|
|
|
|
|
|
@RequestMapping(value = "/user/info/submit", method = RequestMethod.GET)
|
|
|
- public ModelAndView sendCode(HttpSession httpSession, HttpServletResponse httpResponse,
|
|
|
+ public ModelAndView sendCode(HttpSession httpSession,
|
|
|
@RequestParam(value = "complex", required = true) String complex,
|
|
|
@RequestParam(value = "dong", required = true) String dong,
|
|
|
@RequestParam(value = "ho", required = true) String ho)
|
|
@@ -126,7 +136,7 @@ public class OAuthController {
|
|
|
logger.info("*2. dong: " + dong);
|
|
|
logger.info("*3. ho: " + ho);
|
|
|
|
|
|
- // TODO 단지서버로 인증번호 전송해야함.
|
|
|
+ String clientId = httpSession.getAttribute("clientId").toString();
|
|
|
|
|
|
// wallpad 코드 생성
|
|
|
Random generator = new Random();
|
|
@@ -134,22 +144,16 @@ public class OAuthController {
|
|
|
for (int i = 0; i < 6; i++) {
|
|
|
wallpadCode += Integer.toString(generator.nextInt(10));
|
|
|
}
|
|
|
+
|
|
|
logger.info("*[wallpadCode]");
|
|
|
logger.info("*code: " + wallpadCode);
|
|
|
|
|
|
// 세대정보 인코딩
|
|
|
- String homeInfo = complex + "/" + dong + "/" + ho;
|
|
|
- // db에 저장
|
|
|
-
|
|
|
- ValueOperations<String, Object> vop = redisTemplate.opsForValue();
|
|
|
- Duration d = Duration.ofSeconds(180);
|
|
|
+ String homeInfo = complex + "/" + dong + "/" + ho + "/" + clientId;
|
|
|
|
|
|
- while (!vop.setIfAbsent(wallpadCode, homeInfo, d)) {
|
|
|
- wallpadCode = "";
|
|
|
- for (int i = 0; i < 6; i++) {
|
|
|
- wallpadCode += Integer.toString(generator.nextInt(10));
|
|
|
- }
|
|
|
- }
|
|
|
+ // 2019.05.02 DB저장에서 세션 저장 방식으로 변경
|
|
|
+ httpSession.setAttribute("wallpadCode", wallpadCode);
|
|
|
+ httpSession.setAttribute("homeInfo", homeInfo);
|
|
|
|
|
|
logger.info("*[redis]");
|
|
|
logger.info("*(key,value): (" + wallpadCode + "," + homeInfo + ")");
|
|
@@ -157,7 +161,13 @@ public class OAuthController {
|
|
|
// 단지서버로 월패드인증번호 전송
|
|
|
WallpadCode body = new WallpadCode(dong, ho, wallpadCode);
|
|
|
logger.info(body.toString());
|
|
|
- String url = "http://" + complex + ":8002/kakao/auth";
|
|
|
+
|
|
|
+ ClientInfo clientInfo = clientInfoRepo.findByClientId(clientId);
|
|
|
+ String endPoint = clientInfo.getEndPoint();
|
|
|
+
|
|
|
+// String url = "http://" + complex + ":8002/kakao/auth";
|
|
|
+ String url = "http://" + complex + endPoint+"/auth";
|
|
|
+
|
|
|
logger.info(url);
|
|
|
|
|
|
// TODO 예외처리 단지통신
|
|
@@ -191,12 +201,25 @@ public class OAuthController {
|
|
|
logger.info("*[parameters]");
|
|
|
logger.info("*1. Input code: " + wallpadCode);
|
|
|
|
|
|
- ValueOperations<String, Object> vop = redisTemplate.opsForValue();
|
|
|
- if (vop.get(wallpadCode) == null) { // 없으면 null
|
|
|
-
|
|
|
+// ValueOperations<String, Object> vop = redisTemplate.opsForValue();
|
|
|
+// if (vop.get(wallpadCode) == null) { // 없으면 null
|
|
|
+//
|
|
|
+// return false;
|
|
|
+// }
|
|
|
+// httpSession.setAttribute("homeId", vop.get(wallpadCode));
|
|
|
+
|
|
|
+ // 2019.05.02 DB저장에서 세션 저장 방식으로 변경
|
|
|
+ logger.info(httpSession.getAttributeNames().toString());
|
|
|
+ if (httpSession.getAttribute("wallpadCode") == null) {
|
|
|
+ logger.info("null");
|
|
|
return false;
|
|
|
+ } else {
|
|
|
+ if (!httpSession.getAttribute("wallpadCode").toString().equals(wallpadCode)) {
|
|
|
+ logger.info(httpSession.getAttribute("wallpadCode").toString());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
}
|
|
|
- httpSession.setAttribute("homeId", vop.get(wallpadCode));
|
|
|
+ logger.info(httpSession.getAttribute("wallpadCode").toString());
|
|
|
return true;
|
|
|
}
|
|
|
|
|
@@ -209,14 +232,14 @@ public class OAuthController {
|
|
|
logger.info("**************************************************************");
|
|
|
logger.info("**************************************************************");
|
|
|
logger.info("*[parameters]");
|
|
|
- logger.info("*1. home id: " + httpSession.getAttribute("homeId"));
|
|
|
+ logger.info("*1. home id: " + httpSession.getAttribute("homeInfo"));
|
|
|
|
|
|
String code = "";
|
|
|
String homeInfo = "";
|
|
|
ValueOperations<String, Object> vop = redisTemplate.opsForValue();
|
|
|
|
|
|
// 코드 발급
|
|
|
- homeInfo = httpSession.getAttribute("homeId").toString();
|
|
|
+ homeInfo = httpSession.getAttribute("homeInfo").toString();
|
|
|
logger.info("*[home info]");
|
|
|
logger.info("*1. homeInfo: " + homeInfo);
|
|
|
code = "";
|
|
@@ -281,38 +304,62 @@ public class OAuthController {
|
|
|
String complexIp = homeInfo.split("/")[0];
|
|
|
Encoder encoder = Base64.getEncoder();
|
|
|
complexIp = new String(encoder.encode(complexIp.getBytes()));
|
|
|
- JWTInfo jwtinfo = new JWTInfo(complexIp, encodedHomeInfo);
|
|
|
+ JWTInfo jwtinfo = new JWTInfo(complexIp, encodedHomeInfo, homeInfo.split("/")[3]);
|
|
|
newAccessToken = JWTUtils.generateToken(jwtinfo, "accessToken");
|
|
|
newRefreshToken = JWTUtils.generateToken(jwtinfo, "refreshToken");
|
|
|
|
|
|
+ token.setAccess_token(newAccessToken);
|
|
|
+ token.setRefresh_token(newRefreshToken);
|
|
|
+
|
|
|
+ String clientId = JWTUtils.getClientInfoFromToken(newAccessToken);
|
|
|
+ ClientInfo clientInfo = clientInfoRepo.findByClientId(clientId);
|
|
|
+ String url = clientInfo.getEndPoint();
|
|
|
+ if (!DanziAuthUtils.create(token, url)) {
|
|
|
+ throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR);
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
} else if (grantType.equals("refresh_token")) {
|
|
|
if (refreshToken != null) {
|
|
|
- Boolean bool = JWTUtils.validateToken(refreshToken, "refreshToken");
|
|
|
- if (bool) {
|
|
|
- // TODO token의 payload에서 compelxCd, homeId 가져오고 새로 토큰 생성한다
|
|
|
- if (headers.get("Authorization").get(0) == null)
|
|
|
- throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, ERROR.INVALID_CLIENT);
|
|
|
-
|
|
|
- if (!validateHeaderAuth(headers.get("Authorization").get(0)))
|
|
|
- throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, ERROR.INVALID_CLIENT);
|
|
|
-
|
|
|
- JWTInfo jwtinfo = JWTUtils.getJWTInfoFromToken(refreshToken);
|
|
|
- newAccessToken = JWTUtils.generateToken(jwtinfo, "accessToken");
|
|
|
- newRefreshToken = JWTUtils.generateToken(jwtinfo, "refreshToken");
|
|
|
- } else {
|
|
|
+ // 1. 정상적으로 생성되었는지 확인
|
|
|
+ if (!JWTUtils.validateToken(refreshToken, "refreshToken"))
|
|
|
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, ERROR.INVALID_REFRESHTOKEN);
|
|
|
- }
|
|
|
+
|
|
|
+ // client정보검사
|
|
|
+ if (headers.get("Authorization").get(0) == null)
|
|
|
+ throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, ERROR.INVALID_CLIENT);
|
|
|
+
|
|
|
+ if (!validateHeaderAuth(headers.get("Authorization").get(0)))
|
|
|
+ throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, ERROR.INVALID_CLIENT);
|
|
|
+
|
|
|
+ // 단지에 갱신토큰 유효성 확인
|
|
|
+ String clientId = JWTUtils.getClientInfoFromToken(refreshToken);
|
|
|
+ ClientInfo clientInfo = clientInfoRepo.findByClientId(clientId);
|
|
|
+ String url = clientInfo.getEndPoint();
|
|
|
+
|
|
|
+ if (!DanziAuthUtils.get(url, refreshToken))
|
|
|
+ throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR); // 에러를 invalid ? internal?
|
|
|
+
|
|
|
+ JWTInfo jwtinfo = JWTUtils.getJWTInfoFromToken(refreshToken);
|
|
|
+ newAccessToken = JWTUtils.generateToken(jwtinfo, "accessToken");
|
|
|
+ newRefreshToken = JWTUtils.generateToken(jwtinfo, "refreshToken");
|
|
|
+ token.setAccess_token(newAccessToken);
|
|
|
+ token.setRefresh_token(newRefreshToken);
|
|
|
+
|
|
|
+ if (!DanziAuthUtils.refresh(url, token, refreshToken))
|
|
|
+ throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR);
|
|
|
+
|
|
|
}
|
|
|
} else {
|
|
|
throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
|
|
|
}
|
|
|
|
|
|
- token.setAccess_token(newAccessToken);
|
|
|
- token.setRefresh_token(newRefreshToken);
|
|
|
// class 만들어서 사용하는 것으로 변경해야함.
|
|
|
logger.info(token.toString());
|
|
|
|
|
|
+ // TODO 단지서버로 전송하고 정상 응답 받은 경우에만 정상리턴 -> 아닌경우에는 500
|
|
|
+ // 토큰 생성인 경우와 갱신인 경우 구분해서 처리 create, refresh
|
|
|
+
|
|
|
return token;
|
|
|
|
|
|
}
|