use of com.github.scribejava.core.model.Response in project runelite by runelite.
the class AccountService method callback.
@RequestMapping("/callback")
public Object callback(HttpServletRequest request, HttpServletResponse response, @RequestParam(required = false) String error, @RequestParam String code, @RequestParam("state") String stateStr) throws InterruptedException, ExecutionException, IOException {
if (error != null) {
logger.info("Error in oauth callback: {}", error);
return null;
}
State state = gson.fromJson(stateStr, State.class);
logger.info("Got authorization code {} for uuid {}", code, state.getUuid());
OAuth20Service service = new ServiceBuilder().apiKey(oauthClientId).apiSecret(oauthClientSecret).scope(SCOPE).callback(RL_OAUTH_URL).state(gson.toJson(state)).build(GoogleApi20.instance());
OAuth2AccessToken accessToken = service.getAccessToken(code);
// Access user info
OAuthRequest orequest = new OAuthRequest(Verb.GET, USERINFO);
service.signRequest(accessToken, orequest);
Response oresponse = service.execute(orequest);
if (oresponse.getCode() / 100 != 2) {
// Could be a forged result
return null;
}
UserInfo userInfo = gson.fromJson(oresponse.getBody(), UserInfo.class);
logger.info("Got user info: {}", userInfo);
try (Connection con = sql2o.open()) {
con.createQuery("insert ignore into users (username) values (:username)").addParameter("username", userInfo.getEmail()).executeUpdate();
UserEntry user = con.createQuery("select id from users where username = :username").addParameter("username", userInfo.getEmail()).executeAndFetchFirst(UserEntry.class);
if (user == null) {
logger.warn("Unable to find newly created user session");
// that's weird
return null;
}
// insert session
con.createQuery("insert ignore into sessions (user, uuid) values (:user, :uuid)").addParameter("user", user.getId()).addParameter("uuid", state.getUuid().toString()).executeUpdate();
logger.info("Created session for user {}", userInfo.getEmail());
}
response.sendRedirect(RL_REDIR);
notifySession(state.getUuid(), userInfo.getEmail());
return "";
}
Aggregations