Search in sources :

Example 96 with Response

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 "";
}
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

Response (com.github.scribejava.core.model.Response)96 OAuthRequest (com.github.scribejava.core.model.OAuthRequest)86 ServiceBuilder (com.github.scribejava.core.builder.ServiceBuilder)63 Scanner (java.util.Scanner)60 OAuth2AccessToken (com.github.scribejava.core.model.OAuth2AccessToken)48 OAuth20Service (com.github.scribejava.core.oauth.OAuth20Service)46 OAuth1AccessToken (com.github.scribejava.core.model.OAuth1AccessToken)21 OAuth1RequestToken (com.github.scribejava.core.model.OAuth1RequestToken)21 OAuth10aService (com.github.scribejava.core.oauth.OAuth10aService)20 Random (java.util.Random)16 IOException (java.io.IOException)10 HashMap (java.util.HashMap)10 HttpUrl (okhttp3.HttpUrl)8 MockResponse (okhttp3.mockwebserver.MockResponse)8 Test (org.junit.Test)8 MockWebServer (okhttp3.mockwebserver.MockWebServer)7 ExecutionException (java.util.concurrent.ExecutionException)6 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)6 OAuthConfig (com.github.scribejava.core.model.OAuthConfig)3 NingHttpClientConfig (com.github.scribejava.httpclient.ning.NingHttpClientConfig)3