예제 #1
0
  // Access 토큰을 refresh함.
  private TokenVO refreshToken(String clientRefreshToken) throws OAuth2Exception {
    TokenVO tempVO = new TokenVO();
    tempVO.setRefresh_token(clientRefreshToken);
    TokenVO tVO = null;
    try {
      tVO = dao.selectRefreshToken(tempVO);
    } catch (Exception e) {
      e.printStackTrace();
      throw new OAuth2Exception(500, OAuth2ErrorConstant.SERVER_ERROR);
    }

    if (tVO == null) {
      throw new OAuth2Exception(401, OAuth2ErrorConstant.INVALID_TOKEN);
    }

    tVO.setAccess_token(OAuth2Util.generateToken());
    tVO.setCreated_at(OAuth2Util.getCurrentTimeStamp());

    try {
      dao.updateAccessToken(tVO);
    } catch (Exception e) {
      e.printStackTrace();
      throw new OAuth2Exception(401, OAuth2ErrorConstant.INVALID_TOKEN);
    }

    return tVO;
  }
예제 #2
0
  // ****Utility 메서드 시작
  // Token 값을 비롯한 timestamp 정보를 생성하여 테이블에 추가함.
  private TokenVO createTokenToTable(RequestAuthVO rVO, UserVO uVO, ClientVO cVO)
      throws OAuth2Exception {
    TokenVO tVO;
    try {
      // 5.1 Access Token 랜덤하게 생성하여 TokenVO 생성하고 저장
      // TODO 목요일 : TOKEN 값 설정하고 DB에 저장 (sqlmap, dao, controller)
      tVO = new TokenVO();
      tVO.setClient_id(rVO.getClient_id());

      // 승인한 사용자
      tVO.setUserid(uVO.getUserid());
      tVO.setToken_type(OAuth2Constant.TOKEN_TYPE_BEARER);
      tVO.setScope(rVO.getScope());

      // expires_in과 refresh_token의 사용 여부는 각 조직이 결정한다.
      tVO.setExpires_in(OAuth2Constant.EXPIRES_IN_VALUE);
      tVO.setRefresh_token(OAuth2Util.generateToken());
      tVO.setCode(OAuth2Util.generateToken());
      tVO.setClient_type(cVO.getClient_type());

      tVO.setAccess_token(OAuth2Util.generateToken());
      // 생성 시간을 계산을 용이하게 하기 위해 타임스탬프 형태로 저장
      long currentTimeStamp = OAuth2Util.getCurrentTimeStamp();
      tVO.setCreated_at(currentTimeStamp);
      tVO.setCreated_rt(currentTimeStamp);

      System.out.println(tVO);
      // tbl_Token테이블에 추가
      dao.createToken(tVO);
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      throw new OAuth2Exception(401, OAuth2ErrorConstant.UNAUTHORIZED_CLIENT);
    }
    return tVO;
  }
예제 #3
0
  // grant_type이 authorization_code일 때
  private ResponseAccessTokenVO refreshTokenFlow(
      RequestAccessTokenVO ratVO, HttpServletRequest request) throws OAuth2Exception {
    // 1. 전달된 refresh Token과
    // GET 방식일 때는 Client ID와 Client Secret은 Authorization Header를 통해 전달되어야 함.
    if (request.getMethod().equalsIgnoreCase("GET")) {
      String authHeader = (String) request.getHeader("Authorization");
      if (authHeader == null || authHeader.equals("")) {
        throw new OAuth2Exception(400, OAuth2ErrorConstant.INVALID_PARAMETER);
      }
      // Basic 인증 헤더 파싱
      OAuth2Util.parseBasicAuthHeader(authHeader, ratVO);
    }

    // 2. ClientID, Secret 모두 전달되었는지 여부 --> 존재 여부 확인
    if (ratVO.getClient_id() == null || ratVO.getClient_secret() == null) {
      throw new OAuth2Exception(400, OAuth2ErrorConstant.INVALID_PARAMETER);
    }

    // 3. clientID 와 client_secret의 일치여부
    ClientVO cVOTemp = new ClientVO();
    cVOTemp.setClient_id(ratVO.getClient_id());
    ClientVO cVO = null;
    try {
      cVO = dao.getClientOne(cVOTemp);
    } catch (Exception e) {
      throw new OAuth2Exception(500, OAuth2ErrorConstant.SERVER_ERROR);
    }

    if (cVO == null) {
      throw new OAuth2Exception(500, OAuth2ErrorConstant.UNAUTHORIZED_CLIENT);
    }

    if (ratVO.getClient_secret() != null
        && !cVO.getClient_secret().equals(ratVO.getClient_secret())) {
      throw new OAuth2Exception(500, OAuth2ErrorConstant.UNAUTHORIZED_CLIENT);
    }

    // 4. refresh token의 일치 여부
    if (ratVO.getRefresh_token() == null) {
      throw new OAuth2Exception(400, OAuth2ErrorConstant.INVALID_PARAMETER);
    }

    TokenVO tVOTemp = new TokenVO();
    tVOTemp.setRefresh_token(ratVO.getRefresh_token());
    TokenVO tVO = null;
    try {
      tVO = dao.selectRefreshToken(tVOTemp);
    } catch (Exception e) {
      e.printStackTrace();
      throw new OAuth2Exception(500, OAuth2ErrorConstant.SERVER_ERROR);
    }

    if (tVO == null) {
      throw new OAuth2Exception(400, OAuth2ErrorConstant.INVALID_TOKEN);
    }

    // 5. TokenVO의 accessToken 갱신 --> DB 업데이트 -->
    // --> refreshToken 값 없이  ResponseAccessTokenVO객체 생성 --> JSON 포맷으로 응답
    tVO.setAccess_token(OAuth2Util.generateToken());
    tVO.setCreated_at(OAuth2Util.getCurrentTimeStamp());
    try {
      dao.updateAccessToken(tVO);
    } catch (Exception e) {
      e.printStackTrace();
      throw new OAuth2Exception(500, OAuth2ErrorConstant.SERVER_ERROR);
    }

    ResponseAccessTokenVO resVO =
        new ResponseAccessTokenVO(
            tVO.getAccess_token(),
            tVO.getToken_type(),
            tVO.getExpires_in(),
            null,
            ratVO.getState(),
            tVO.getCreated_at());

    return resVO;
  }