aboutsummaryrefslogtreecommitdiff
path: root/juick-www/src/main/java/com/juick/www/controllers/VKontakteLogin.java
diff options
context:
space:
mode:
Diffstat (limited to 'juick-www/src/main/java/com/juick/www/controllers/VKontakteLogin.java')
-rw-r--r--juick-www/src/main/java/com/juick/www/controllers/VKontakteLogin.java130
1 files changed, 130 insertions, 0 deletions
diff --git a/juick-www/src/main/java/com/juick/www/controllers/VKontakteLogin.java b/juick-www/src/main/java/com/juick/www/controllers/VKontakteLogin.java
new file mode 100644
index 00000000..d860a7bc
--- /dev/null
+++ b/juick-www/src/main/java/com/juick/www/controllers/VKontakteLogin.java
@@ -0,0 +1,130 @@
+/*
+ * 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.vk.Token;
+import com.juick.www.vk.UsersResponse;
+import org.apache.commons.lang3.CharEncoding;
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+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.IOException;
+import java.net.URLEncoder;
+import java.util.UUID;
+
+/**
+ * @author Ugnich Anton
+ */
+@Controller
+public class VKontakteLogin {
+ private static final Logger logger = LoggerFactory.getLogger(VKontakteLogin.class);
+ private static final String VK_APPID = "3544101";
+ private static final String VK_SECRET = "z2afNI8jA5lIpZ2jsTm1";
+ private static final String VK_REDIRECT = "http://juick.com/_vklogin";
+
+ @Inject
+ CrosspostService crosspostService;
+ @Inject
+ UserService userService;
+
+ public VKontakteLogin() {
+ mapper = new ObjectMapper();
+ mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
+ mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
+ mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
+ }
+
+ private final ObjectMapper mapper;
+
+ @RequestMapping(value = "/_vklogin", method = RequestMethod.GET)
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
+ String code = request.getParameter("code");
+ if (StringUtils.isBlank(code)) {
+ response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
+ response.setHeader("Location", "https://oauth.vk.com/authorize?client_id=" + VK_APPID + "&redirect_uri=" + URLEncoder.encode(VK_REDIRECT, CharEncoding.UTF_8) + "&scope=friends,wall,offline&response_type=code");
+ return;
+ }
+
+
+ String tokenjson = Utils.fetchURL("https://oauth.vk.com/access_token?client_id=" + VK_APPID + "&redirect_uri=" + URLEncoder.encode(VK_REDIRECT, CharEncoding.UTF_8) + "&client_secret=" + VK_SECRET + "&code=" + URLEncoder.encode(code, CharEncoding.UTF_8));
+ if (tokenjson == null || tokenjson.isEmpty()) {
+ logger.error("VK TOKEN EMPTY");
+ response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+ return;
+ }
+ String token = null;
+ long vkID = 0;
+ Token json = mapper.readValue(tokenjson, Token.class);
+ token = json.getAccessToken();
+ vkID = json.getUserId();
+ if (token == null || vkID == 0) {
+ logger.error("VK TOKEN EMPTY: {}", tokenjson);
+ response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+ return;
+ }
+
+ String graph = Utils.fetchURL("https://api.vk.com/method/users.get?uids=" + vkID + "&fields=screen_name&access_token=" + token);
+ if (graph == null || graph.isEmpty()) {
+ logger.error("VK GRAPH ERROR");
+ response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+ return;
+ }
+
+ try {
+ com.juick.www.vk.User jsonUser = mapper.readValue(graph, UsersResponse.class).getUsers().get(0);
+ String vkName = jsonUser.getFirstName() + " " + jsonUser.getLastName();
+ String vkLink = jsonUser.getScreenName();
+
+ if (vkName == null || vkLink == null || vkName.isEmpty() || vkName.length() == 1 || vkLink.isEmpty()) {
+ throw new Exception();
+ }
+
+ int uid = crosspostService.getUIDbyVKID(vkID);
+ if (uid > 0) {
+ 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 {
+ String loginhash = UUID.randomUUID().toString();
+ if (!crosspostService.createVKUser(vkID, loginhash, token, vkName, vkLink)) {
+ throw new Exception();
+ }
+ response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
+ response.setHeader("Location", "/signup?type=vk&hash=" + loginhash);
+ }
+ } catch (Exception e) {
+ logger.error("JSON ERROR", e);
+ response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+ }
+ }
+}