use of com.fanxb.bookmark.common.exception.CustomException in project bookmark by FleyX.
the class OauthServiceImpl method oAuthCheck.
@Override
@Transactional(rollbackFor = Exception.class)
public String oAuthCheck(OauthBody body) {
User current, other = new User();
if (StrUtil.equals(body.getType(), OauthBody.GITHUB)) {
Map<String, String> header = new HashMap<>(2);
header.put("accept", "application/json");
String url = "https://github.com/login/oauth/access_token?client_id=" + githubClientId + "&client_secret=" + githubSecret + "&code=" + body.getCode();
JSONObject obj = HttpUtil.getObj(url, header, true);
String accessToken = obj.getString("access_token");
if (StrUtil.isEmpty(accessToken)) {
throw new CustomException("github登陆失败,请稍后重试");
}
header.put("Authorization", "token " + accessToken);
JSONObject userInfo = HttpUtil.getObj("https://api.github.com/user", header, true);
other.setGithubId(userInfo.getLong("id"));
if (other.getGithubId() == null) {
log.error("github返回异常:{}", userInfo);
throw new CustomException("登陆异常,请稍后重试");
}
other.setEmail(userInfo.getString("email"));
other.setIcon(userInfo.getString("avatar_url"));
other.setUsername(userInfo.getString("login"));
current = userDao.selectByUserIdOrGithubId(null, other.getGithubId());
if (current == null) {
current = userDao.selectByUsernameOrEmail(null, other.getEmail());
}
} else {
throw new CustomException("不支持的登陆方式" + body.getType());
}
User newest = dealOauth(current, other);
return JwtUtil.encode(Collections.singletonMap("userId", String.valueOf(newest.getUserId())), CommonConstant.jwtSecret, body.isRememberMe() ? LONG_EXPIRE_TIME : SHORT_EXPIRE_TIME);
}
Aggregations