Search in sources :

Example 1 with UserEntry

use of net.runelite.http.service.account.beans.UserEntry 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 "";
}
Also used : OAuthRequest(com.github.scribejava.core.model.OAuthRequest) LoginResponse(net.runelite.http.api.ws.messages.LoginResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) OAuthResponse(net.runelite.http.api.account.OAuthResponse) Response(com.github.scribejava.core.model.Response) OAuth2AccessToken(com.github.scribejava.core.model.OAuth2AccessToken) Connection(org.sql2o.Connection) UserEntry(net.runelite.http.service.account.beans.UserEntry) OAuth20Service(com.github.scribejava.core.oauth.OAuth20Service) ServiceBuilder(com.github.scribejava.core.builder.ServiceBuilder) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ServiceBuilder (com.github.scribejava.core.builder.ServiceBuilder)1 OAuth2AccessToken (com.github.scribejava.core.model.OAuth2AccessToken)1 OAuthRequest (com.github.scribejava.core.model.OAuthRequest)1 Response (com.github.scribejava.core.model.Response)1 OAuth20Service (com.github.scribejava.core.oauth.OAuth20Service)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 OAuthResponse (net.runelite.http.api.account.OAuthResponse)1 LoginResponse (net.runelite.http.api.ws.messages.LoginResponse)1 UserEntry (net.runelite.http.service.account.beans.UserEntry)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 Connection (org.sql2o.Connection)1