aboutsummaryrefslogtreecommitdiff
path: root/juick-www/src/main/java/com/juick/www/controllers/FacebookLogin.java
blob: cc11f99a8cd0366459280652e7e1db946d45310f (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * Juick
 * Copyright (C) 2008-2013, Ugnich Anton
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.juick.www.controllers;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.juick.service.CrosspostService;
import com.juick.service.UserService;
import com.juick.www.Utils;
import com.juick.www.facebook.Graph;
import org.apache.commons.lang3.CharEncoding;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.inject.Inject;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.UUID;

/**
 *
 * @author Ugnich Anton
 */
@Controller
public class FacebookLogin {

    private static final Logger logger = LoggerFactory.getLogger(FacebookLogin.class);

    private final String FACEBOOK_APPID;
    private final String FACEBOOK_SECRET;
    private final String FACEBOOK_REDIRECT = "http://juick.com/_fblogin";
    private final ObjectMapper mapper;

    @Inject
    CrosspostService crosspostService;
    @Inject
    UserService userService;

    @Inject
    public FacebookLogin(Environment env) {
        FACEBOOK_APPID = env.getProperty("facebook_appid");
        FACEBOOK_SECRET = env.getProperty("facebook_secret");

        mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
    }

    @RequestMapping(value = "/_fblogin", method = RequestMethod.GET)
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        String fbstate;

        String code = request.getParameter("code");
        if (StringUtils.isBlank(code)) {
            fbstate = UUID.randomUUID().toString();

            Cookie c = new Cookie("fbstate", fbstate);
            response.addCookie(c);

            response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
            response.setHeader("Location", "https://www.facebook.com/dialog/oauth?scope=publish_stream&client_id=" + FACEBOOK_APPID + "&redirect_uri=" + URLEncoder.encode(FACEBOOK_REDIRECT, CharEncoding.UTF_8) + "&state=" + fbstate);
            return;
        }

        fbstate = Utils.getCookie(request, "fbstate");
        if (fbstate == null || fbstate.isEmpty() || !fbstate.equals(request.getParameter("state"))) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            return;
        } else {
            Cookie c = new Cookie("fbstate", "-");
            c.setMaxAge(0);
            response.addCookie(c);
        }

        String token = Utils.fetchURL("https://graph.facebook.com/oauth/access_token?client_id=" + FACEBOOK_APPID + "&redirect_uri=" + URLEncoder.encode(FACEBOOK_REDIRECT, CharEncoding.UTF_8) + "&client_secret=" + FACEBOOK_SECRET + "&code=" + URLEncoder.encode(code, CharEncoding.UTF_8));
        if (token == null || token.isEmpty() || !token.startsWith("access_token=")) {
            logger.error("FACEBOOK TOKEN ERROR: {}", token);
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        }
        token = token.substring(13); // access_token=...
        int tokenamp = token.indexOf('&'); // &expires=
        if (tokenamp > 0) {
            token = token.substring(0, tokenamp);
        }

        String graph = Utils.fetchURL("https://graph.facebook.com/me?access_token=" + token);
        if (graph == null || graph.isEmpty()) {
            logger.error("FACEBOOK GRAPH ERROR");
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        }

        try {
            Graph fb = mapper.readValue(graph, Graph.class);

            long fbID = NumberUtils.toLong(fb.getId(), 0);
            if (fbID == 0 || StringUtils.isBlank(fb.getName()) || StringUtils.isBlank(fb.getLink())) {
                throw new Exception();
            }

            int uid = crosspostService.getUIDbyFBID(fbID);
            if (uid > 0) {
                if (!crosspostService.updateFacebookUser(fbID, token, fb.getName(), fb.getLink())) {
                    throw new Exception();
                }
                Cookie c = new Cookie("hash", userService.getHashByUID(uid));
                c.setMaxAge(50 * 24 * 60 * 60);
                response.addCookie(c);
                response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
                response.setHeader("Location", "/");
            } else if (fb.getVerified()) {
                String loginhash = UUID.randomUUID().toString();
                if (!crosspostService.createFacebookUser(fbID, loginhash, token, fb.getName(), fb.getLink())) {
                    throw new Exception();
                }
                response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
                response.setHeader("Location", "/signup?type=fb&hash=" + loginhash);
            } else {
                throw new Exception();
            }
        } catch (Exception e) {
            logger.error("fb error", e);
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        }
    }
}