aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--build.gradle1
m---------deps/com.juick.server12
-rw-r--r--src/main/java/com/juick/CrosspostComponent.java2
-rw-r--r--src/main/java/com/juick/http/www/Main.java6
-rw-r--r--src/main/java/com/juick/http/www/TwitterAuth.java84
5 files changed, 96 insertions, 9 deletions
diff --git a/build.gradle b/build.gradle
index eb3b6113..de9db814 100644
--- a/build.gradle
+++ b/build.gradle
@@ -57,6 +57,7 @@ dependencies {
compile json
compile 'com.ganyo:gcm-server:1.0.+'
compile 'com.notnoop.apns:apns:1.0.0.Beta6'
+ compile 'com.github.scribejava:scribejava-apis:2.0'
providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
runtime 'mysql:mysql-connector-java:5.1.37'
def tomcatVersion = '7.0.+'
diff --git a/deps/com.juick.server b/deps/com.juick.server
-Subproject 0a00a5e5625ea0241c0effb3ed6f715fd737a04
+Subproject 79e48598873f6a2b4ec1aa850e310a689d862af
diff --git a/src/main/java/com/juick/CrosspostComponent.java b/src/main/java/com/juick/CrosspostComponent.java
index b05dd1be..73791c5e 100644
--- a/src/main/java/com/juick/CrosspostComponent.java
+++ b/src/main/java/com/juick/CrosspostComponent.java
@@ -77,7 +77,7 @@ public class CrosspostComponent implements ServletContextListener, Stream.Strea
public void run() {
try {
Properties conf = new Properties();
- conf.load(new FileInputStream("/etc/juick/crosspost.conf"));
+ conf.load(sce.getServletContext().getResourceAsStream("WEB-INF/juick.conf"));
twitter_consumer_key = conf.getProperty("twitter_consumer_key", "");
twitter_consumer_secret = conf.getProperty("twitter_consumer_secret", "");
diff --git a/src/main/java/com/juick/http/www/Main.java b/src/main/java/com/juick/http/www/Main.java
index ced7fb98..9a22c172 100644
--- a/src/main/java/com/juick/http/www/Main.java
+++ b/src/main/java/com/juick/http/www/Main.java
@@ -59,6 +59,7 @@ public class Main extends HttpServlet implements Stream.StreamListener {
NewMessage pagesNewMessage = new NewMessage();
FacebookLogin loginFacebook = new FacebookLogin();
VKontakteLogin loginVK = new VKontakteLogin();
+ TwitterAuth twitterAuth;
SignUp signup = new SignUp();
Settings settings = new Settings();
RSS rss = new RSS();
@@ -76,7 +77,8 @@ public class Main extends HttpServlet implements Stream.StreamListener {
sqlSearch = null; // init this on search, timeout is too low
setupXmppComponent(conf.getProperty("xmpp_password"));
-
+ twitterAuth = new TwitterAuth(conf.getProperty("twitter_consumer_key"),
+ conf.getProperty("twitter_consumer_secret"));
PageTemplates.sape = new Sape(conf.getProperty("sape_user"), "juick.com", 2000, 3600);
} catch (Exception e) {
log(null, e);
@@ -217,6 +219,8 @@ public class Main extends HttpServlet implements Stream.StreamListener {
loginFacebook.doGet(sql, request, response);
} else if (uri.equals("/_vklogin")) {
loginVK.doGet(sql, request, response);
+ } else if (uri.equals("/_twitter")) {
+ twitterAuth.doGet(sql, request, response);
} else if (uri.equals("/signup")) {
signup.doGet(sql, request, response);
} else if (uri.equals("/help") || uri.equals("/help/")) {
diff --git a/src/main/java/com/juick/http/www/TwitterAuth.java b/src/main/java/com/juick/http/www/TwitterAuth.java
new file mode 100644
index 00000000..5c5581d1
--- /dev/null
+++ b/src/main/java/com/juick/http/www/TwitterAuth.java
@@ -0,0 +1,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);
+ }
+ }
+ }
+ }
+}