aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/http/www/TwitterAuth.java
blob: 5c5581d1ca695c378c400fc18c44601f454cdffa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.juick.http.www;

import com.github.scribejava.apis.TwitterApi;
import com.github.scribejava.core.builder.ServiceBuilder;
import com.github.scribejava.core.model.*;
import com.github.scribejava.core.oauth.OAuthService;
import com.juick.server.UserQueries;
import org.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;

/**
 * Created by vt on 01.12.2015.
 */
public class TwitterAuth {

    private final static String VERIFY_URL = "https://api.twitter.com/1.1/account/verify_credentials.json";

    private String consumerKey, consumerSecret;

    public TwitterAuth(String consumerKey, String consumerSecret) {
        this.consumerKey = consumerKey;
        this.consumerSecret = consumerSecret;
    }

    protected void doGet(Connection sql, HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String hash = "", request_token = "", request_token_secret = "";
        String verifier = request.getParameter("oauth_verifier");
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("hash")) {
                hash = cookie.getValue();
            }
            if (cookie.getName().equals("request_token")) {
                request_token = cookie.getValue();
            }
            if (cookie.getName().equals("request_token_secret")) {
                request_token_secret = cookie.getValue();
            }
        }
        com.juick.User user = UserQueries.getUserByHash(sql, hash);
        if ( user == null || user.UID == 0) {
            response.sendError(403);
            return;
        }
        OAuthService oAuthService = new ServiceBuilder()
                .provider(TwitterApi.class)
                .apiKey(consumerKey)
                .apiSecret(consumerSecret)
                .callback("http://juick.com/_twitter")
                .build();

        if (request_token.isEmpty() && request_token_secret.isEmpty()
                && (verifier == null || verifier.isEmpty())) {
            Token requestToken = oAuthService.getRequestToken();
            String authUrl = oAuthService.getAuthorizationUrl(requestToken);
            response.addCookie(new Cookie("request_token", requestToken.getToken()));
            response.addCookie(new Cookie("request_token_secret", requestToken.getSecret()));
            response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
            response.setHeader("Location", authUrl);
        } else {
            if (verifier != null && verifier.length() > 0) {
                Token requestToken = new Token(request_token, request_token_secret);
                Token accessToken = oAuthService.getAccessToken(requestToken, new Verifier(verifier));
                OAuthRequest oAuthRequest = new OAuthRequest(Verb.GET, VERIFY_URL, oAuthService);
                oAuthService.signRequest(accessToken, oAuthRequest);
                JSONObject jsonResponse = new JSONObject(oAuthRequest.send().getBody());
                String screenName = jsonResponse.getString("screen_name");
                if (UserQueries.linkTwitterAccount(sql, user, accessToken.getToken(), accessToken.getSecret(), screenName)) {
                    response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
                    response.setHeader("Location", "http://juick.com/settings");
                } else {
                    response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                }
            }
        }
    }
}