aboutsummaryrefslogtreecommitdiff
path: root/juick-common/src/main/java/com/juick/service
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-03-16 10:46:23 +0300
committerGravatar Vitaly Takmazov2018-03-16 10:46:23 +0300
commit9ef168a03b75aeca0c2f7dda9ce87d4014c703a9 (patch)
tree8ace0ada0f576d97592f8bc84803a2dcf3e7a244 /juick-common/src/main/java/com/juick/service
parent7cadae166b103182d7d1daaafe602cd8fb145c53 (diff)
merge common projects
Diffstat (limited to 'juick-common/src/main/java/com/juick/service')
-rw-r--r--juick-common/src/main/java/com/juick/service/BaseRestService.java35
-rw-r--r--juick-common/src/main/java/com/juick/service/CrosspostService.java78
-rw-r--r--juick-common/src/main/java/com/juick/service/EmailService.java33
-rw-r--r--juick-common/src/main/java/com/juick/service/ImagesService.java7
-rw-r--r--juick-common/src/main/java/com/juick/service/ImagesServiceImpl.java71
-rw-r--r--juick-common/src/main/java/com/juick/service/MessagesService.java107
-rw-r--r--juick-common/src/main/java/com/juick/service/MessengerService.java14
-rw-r--r--juick-common/src/main/java/com/juick/service/PMQueriesService.java45
-rw-r--r--juick-common/src/main/java/com/juick/service/PrivacyQueriesService.java34
-rw-r--r--juick-common/src/main/java/com/juick/service/PushQueriesService.java50
-rw-r--r--juick-common/src/main/java/com/juick/service/ShowQueriesService.java31
-rw-r--r--juick-common/src/main/java/com/juick/service/SubscriptionService.java55
-rw-r--r--juick-common/src/main/java/com/juick/service/TagService.java63
-rw-r--r--juick-common/src/main/java/com/juick/service/TelegramService.java41
-rw-r--r--juick-common/src/main/java/com/juick/service/UserService.java138
-rw-r--r--juick-common/src/main/java/com/juick/service/search/SearchService.java31
-rw-r--r--juick-common/src/main/java/com/juick/service/security/HashParamAuthenticationFilter.java103
-rw-r--r--juick-common/src/main/java/com/juick/service/security/JuickUserDetailsService.java53
-rw-r--r--juick-common/src/main/java/com/juick/service/security/NotAuthorizedAuthenticationEntryPoint.java36
-rw-r--r--juick-common/src/main/java/com/juick/service/security/NullUserDetailsService.java33
-rw-r--r--juick-common/src/main/java/com/juick/service/security/deprecated/CookieSimpleHashRememberMeServices.java130
-rw-r--r--juick-common/src/main/java/com/juick/service/security/deprecated/RequestParamHashRememberMeServices.java88
-rw-r--r--juick-common/src/main/java/com/juick/service/security/entities/JuickUser.java92
23 files changed, 1368 insertions, 0 deletions
diff --git a/juick-common/src/main/java/com/juick/service/BaseRestService.java b/juick-common/src/main/java/com/juick/service/BaseRestService.java
new file mode 100644
index 00000000..13604a89
--- /dev/null
+++ b/juick-common/src/main/java/com/juick/service/BaseRestService.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2008-2017, Juick
+ *
+ * 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.service;
+
+import org.springframework.web.client.RestTemplate;
+
+/**
+ * Created by vitalyster on 15.12.2016.
+ */
+public abstract class BaseRestService {
+ private RestTemplate rest;
+
+ public BaseRestService(RestTemplate rest) {
+ this.rest = rest;
+ }
+
+ public RestTemplate getRest() {
+ return rest;
+ }
+}
diff --git a/juick-common/src/main/java/com/juick/service/CrosspostService.java b/juick-common/src/main/java/com/juick/service/CrosspostService.java
new file mode 100644
index 00000000..b82621e5
--- /dev/null
+++ b/juick-common/src/main/java/com/juick/service/CrosspostService.java
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2008-2017, Juick
+ *
+ * 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.service;
+
+import com.juick.ExternalToken;
+import com.juick.server.helpers.ApplicationStatus;
+import org.apache.commons.lang3.tuple.Pair;
+
+import javax.annotation.Nonnull;
+import java.util.Optional;
+
+/**
+ * Created by aalexeev on 11/13/16.
+ */
+public interface CrosspostService {
+
+ Optional<ExternalToken> getTwitterToken(int uid);
+
+ boolean deleteTwitterToken(Integer uid);
+
+ Optional<Pair<String, String>> getFacebookTokens(int uid);
+
+ ApplicationStatus getFbCrossPostStatus(int uid);
+
+ boolean enableFBCrosspost(Integer uid);
+
+ void disableFBCrosspost(Integer uid);
+
+ @Nonnull
+ String getTwitterName(int uid);
+
+ String getTelegramName(int uid);
+
+ Optional<Pair<String, String>> getVkTokens(int uid);
+
+ void deleteVKUser(Integer uid);
+
+ int getUIDbyFBID(long fbID);
+
+ boolean createFacebookUser(long fbID, String loginhash, String token, String fbName, String fbLink);
+
+ boolean updateFacebookUser(long fbID, String token, String fbName, String fbLink);
+
+ int getUIDbyVKID(long vkID);
+
+ boolean createVKUser(long vkID, String loginhash, String token, String vkName, String vkLink);
+
+ String getFacebookNameByHash(String hash);
+
+ String getTelegramNameByHash(String hash);
+
+ boolean setFacebookUser(String hash, int uid);
+
+ String getVKNameByHash(String hash);
+
+ boolean setVKUser(String hash, int uid);
+
+ boolean setTelegramUser(String hash, int uid);
+
+ String getJIDByHash(String hash);
+
+ boolean setJIDUser(String hash, int uid);
+}
diff --git a/juick-common/src/main/java/com/juick/service/EmailService.java b/juick-common/src/main/java/com/juick/service/EmailService.java
new file mode 100644
index 00000000..2440bcb4
--- /dev/null
+++ b/juick-common/src/main/java/com/juick/service/EmailService.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2008-2017, Juick
+ *
+ * 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.service;
+
+import java.util.List;
+
+/**
+ * Created by vitalyster on 09.12.2016.
+ */
+public interface EmailService {
+ boolean verifyAddressByCode(Integer userId, String code);
+ boolean addVerificationCode(Integer userId, String account, String code);
+ boolean addEmail(Integer userId, String email);
+ boolean deleteEmail(Integer userId, String account);
+ String getNotificationsEmail(Integer userId);
+ boolean setNotificationsEmail(Integer userId, String account);
+ List<String> getEmails(Integer userId, boolean active);
+}
diff --git a/juick-common/src/main/java/com/juick/service/ImagesService.java b/juick-common/src/main/java/com/juick/service/ImagesService.java
new file mode 100644
index 00000000..b5cff16e
--- /dev/null
+++ b/juick-common/src/main/java/com/juick/service/ImagesService.java
@@ -0,0 +1,7 @@
+package com.juick.service;
+
+import com.juick.Message;
+
+public interface ImagesService {
+ void setAttachmentMetadata(String imgDir, String baseUrl, Message msg) throws Exception;
+}
diff --git a/juick-common/src/main/java/com/juick/service/ImagesServiceImpl.java b/juick-common/src/main/java/com/juick/service/ImagesServiceImpl.java
new file mode 100644
index 00000000..3a5c77dd
--- /dev/null
+++ b/juick-common/src/main/java/com/juick/service/ImagesServiceImpl.java
@@ -0,0 +1,71 @@
+package com.juick.service;
+
+import com.juick.Attachment;
+import com.juick.Message;
+import com.juick.Photo;
+import com.juick.server.util.ImageUtils;
+import org.springframework.util.StringUtils;
+
+import java.io.File;
+import java.nio.file.Paths;
+
+public class ImagesServiceImpl implements ImagesService {
+ @Override
+ public void setAttachmentMetadata(String imgDir, String baseUrl, Message msg) throws Exception {
+ if (!StringUtils.isEmpty(msg.getAttachmentType())) {
+ Photo photo = new Photo();
+ if (msg.getRid()> 0) {
+ photo.setSmall(String.format("%sphotos-512/%d-%d.%s", baseUrl, msg.getMid(), msg.getRid(), msg.getAttachmentType()));
+ photo.setMedium(String.format("%sphotos-1024/%d-%d.%s", baseUrl, msg.getMid(), msg.getRid(), msg.getAttachmentType()));
+ photo.setThumbnail(String.format("%sps/%d-%d.%s", baseUrl, msg.getMid(), msg.getRid(), msg.getAttachmentType()));
+ } else {
+ photo.setSmall(String.format("%sphotos-512/%d.%s", baseUrl, msg.getMid(), msg.getAttachmentType()));
+ photo.setMedium(String.format("%sphotos-1024/%d.%s", baseUrl, msg.getMid(), msg.getAttachmentType()));
+ photo.setThumbnail(String.format("%sps/%d.%s", baseUrl, msg.getMid(), msg.getAttachmentType()));
+ }
+ msg.setPhoto(photo);
+ String imageName = String.format("%s.%s", msg.getMid(), msg.getAttachmentType());
+ if (msg.getRid() > 0) {
+ imageName = String.format("%s-%s.%s", msg.getMid(), msg.getRid(), msg.getAttachmentType());
+ }
+ File fullImage = Paths.get(imgDir, "p", imageName).toFile();
+ File mediumImage = Paths.get(imgDir, "photos-1024", imageName).toFile();
+ File smallImage = Paths.get(imgDir, "photos-512", imageName).toFile();
+ File thumbnailImage = Paths.get(imgDir, "ps", imageName).toFile();
+ StringBuilder builder = new StringBuilder();
+ builder.append(baseUrl);
+ builder.append(msg.getAttachmentType().equals("mp4") ? "video" : "p");
+ builder.append("/").append(msg.getMid());
+ if (msg.getRid() > 0) {
+ builder.append("-").append(msg.getRid());
+ }
+ builder.append(".").append(msg.getAttachmentType());
+ String originalUrl = builder.toString();
+
+ Attachment original = new Attachment();
+ original.setUrl(originalUrl);
+ original.setHeight(ImageUtils.getImageHeight(fullImage));
+ original.setWidth(ImageUtils.getImageWidth(fullImage));
+
+ Attachment medium = new Attachment();
+ medium.setUrl(photo.getMedium());
+ medium.setWidth(ImageUtils.getImageWidth(mediumImage));
+ medium.setHeight(ImageUtils.getImageHeight(mediumImage));
+ original.setMedium(medium);
+
+ Attachment small = new Attachment();
+ small.setUrl(photo.getSmall());
+ small.setWidth(ImageUtils.getImageWidth(smallImage));
+ small.setHeight(ImageUtils.getImageHeight(smallImage));
+ original.setSmall(small);
+
+ Attachment thumb = new Attachment();
+ thumb.setUrl(photo.getMedium());
+ thumb.setWidth(ImageUtils.getImageWidth(thumbnailImage));
+ thumb.setHeight(ImageUtils.getImageHeight(thumbnailImage));
+ original.setThumbnail(thumb);
+
+ msg.setAttachment(original);
+ }
+ }
+}
diff --git a/juick-common/src/main/java/com/juick/service/MessagesService.java b/juick-common/src/main/java/com/juick/service/MessagesService.java
new file mode 100644
index 00000000..341175dd
--- /dev/null
+++ b/juick-common/src/main/java/com/juick/service/MessagesService.java
@@ -0,0 +1,107 @@
+/*
+ * Copyright (C) 2008-2017, Juick
+ *
+ * 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.service;
+
+import com.juick.User;
+import com.juick.server.helpers.ResponseReply;
+
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * Created by aalexeev on 11/13/16.
+ */
+public interface MessagesService {
+ int createMessage(int uid, String txt, String attachment, Collection<com.juick.Tag> tags);
+
+ int createReply(int mid, int rid, int uid, String txt, String attachment);
+
+ int getReplyIDIncrement(int mid);
+
+ boolean recommendMessage(int mid, int vuid);
+
+ boolean canViewThread(int mid, int uid);
+
+ boolean isReadOnly(int mid);
+
+ boolean isSubscribed(int uid, int mid);
+
+ int getMessagePrivacy(int mid);
+
+ com.juick.Message getMessage(int mid);
+
+ com.juick.Message getReply(int mid, int rid);
+
+ User getMessageAuthor(int mid);
+
+ List<String> getMessageRecommendations(int mid);
+
+ List<Integer> getAll(int visitorUid, int before);
+
+ List<Integer> getTag(int tid, int visitorUid, int before, int cnt);
+
+ List<Integer> getTags(String tids, int visitorUid, int before, int cnt);
+
+ List<Integer> getPlace(int placeId, int visitorUid, int before);
+
+ List<Integer> getMyFeed(int uid, int before, boolean recommended);
+
+ List<Integer> getPrivate(int uid, int before);
+
+ List<Integer> getDiscussions(int uid, Long to);
+
+ List<Integer> getRecommended(int uid, int before);
+
+ List<Integer> getPopular(int visitorUid, int before);
+
+ List<Integer> getPhotos(int visitorUid, int before);
+
+ List<Integer> getSearch(String search, int before);
+
+ List<Integer> getUserBlog(int uid, int privacy, int before);
+
+ List<Integer> getUserTag(int uid, int tid, int privacy, int before);
+
+ List<Integer> getUserBlogAtDay(int uid, int privacy, int daysback);
+
+ List<Integer> getUserBlogWithRecommendations(int uid, int privacy, int before);
+
+ List<Integer> getUserRecommendations(int uid, int before);
+
+ List<Integer> getUserPhotos(int uid, int privacy, int before);
+
+ List<Integer> getUserSearch(int UID, String search, int privacy, int before);
+
+ List<com.juick.Message> getMessages(List<Integer> mids);
+
+ List<com.juick.Message> getReplies(int mid);
+
+ boolean setMessagePopular(int mid, int popular);
+
+ boolean setMessagePrivacy(int mid);
+
+ boolean deleteMessage(int uid, int mid);
+
+ boolean deleteReply(int uid, int mid, int rid);
+
+ List<Integer> getLastMessages(int hours);
+
+ List<ResponseReply> getLastReplies(int hours);
+
+ List<Integer> getPopularCandidates();
+}
diff --git a/juick-common/src/main/java/com/juick/service/MessengerService.java b/juick-common/src/main/java/com/juick/service/MessengerService.java
new file mode 100644
index 00000000..e07c73fe
--- /dev/null
+++ b/juick-common/src/main/java/com/juick/service/MessengerService.java
@@ -0,0 +1,14 @@
+package com.juick.service;
+
+import com.juick.User;
+
+import java.util.Optional;
+
+public interface MessengerService {
+ Integer getUserId(String senderId);
+ Optional<String> getSenderId(User user);
+ boolean createMessengerUser(String senderId, String displayName);
+ String getDisplayName(String hash);
+ String getSignUpHash(String senderId, String username);
+ boolean linkMessengerUser(String hash, int uid);
+}
diff --git a/juick-common/src/main/java/com/juick/service/PMQueriesService.java b/juick-common/src/main/java/com/juick/service/PMQueriesService.java
new file mode 100644
index 00000000..4c70eece
--- /dev/null
+++ b/juick-common/src/main/java/com/juick/service/PMQueriesService.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2008-2017, Juick
+ *
+ * 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.service;
+
+import com.juick.User;
+
+import java.util.List;
+
+/**
+ * Created by aalexeev on 11/13/16.
+ */
+public interface PMQueriesService {
+ boolean createPM(int uidFrom, int uid_to, String body);
+
+ boolean addPMinRoster(int uid, String jid);
+
+ boolean removePMinRoster(int uid, String jid);
+
+ boolean havePMinRoster(int uid, String jid);
+
+ String getLastView(int uidFrom, int uidTo);
+
+ List<User> getPMLastConversationsUsers(int uid, int cnt);
+
+ List<com.juick.Message> getPMMessages(int uid, int uidTo);
+
+ List<com.juick.Message> getLastPMInbox(int uid);
+
+ List<com.juick.Message> getLastPMSent(int uid);
+}
diff --git a/juick-common/src/main/java/com/juick/service/PrivacyQueriesService.java b/juick-common/src/main/java/com/juick/service/PrivacyQueriesService.java
new file mode 100644
index 00000000..17dd6a9b
--- /dev/null
+++ b/juick-common/src/main/java/com/juick/service/PrivacyQueriesService.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2008-2017, Juick
+ *
+ * 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.service;
+
+import com.juick.Tag;
+import com.juick.User;
+
+/**
+ * Created by aalexeev on 11/13/16.
+ */
+public interface PrivacyQueriesService {
+ enum PrivacyResult {
+ Removed, Added
+ }
+
+ PrivacyResult blacklistUser(User user, User target);
+
+ PrivacyResult blacklistTag(User user, Tag tag);
+}
diff --git a/juick-common/src/main/java/com/juick/service/PushQueriesService.java b/juick-common/src/main/java/com/juick/service/PushQueriesService.java
new file mode 100644
index 00000000..f84a83e4
--- /dev/null
+++ b/juick-common/src/main/java/com/juick/service/PushQueriesService.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2008-2017, Juick
+ *
+ * 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.service;
+
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * Created by aalexeev on 11/13/16.
+ */
+public interface PushQueriesService {
+ List<String> getGCMRegID(int uid);
+
+ List<String> getGCMTokens(Collection<Integer> uids);
+
+ boolean addGCMToken(Integer uid, String token);
+
+ boolean deleteGCMToken(String token);
+
+ List<String> getMPNSURL(int uid);
+
+ List<String> getMPNSTokens(Collection<Integer> uids);
+
+ boolean addMPNSToken(Integer uid, String token);
+
+ boolean deleteMPNSToken(String token);
+
+ List<String> getAPNSToken(int uid);
+
+ List<String> getAPNSTokens(Collection<Integer> uids);
+
+ boolean addAPNSToken(Integer uid, String token);
+
+ boolean deleteAPNSToken(String token);
+}
diff --git a/juick-common/src/main/java/com/juick/service/ShowQueriesService.java b/juick-common/src/main/java/com/juick/service/ShowQueriesService.java
new file mode 100644
index 00000000..32b34b4e
--- /dev/null
+++ b/juick-common/src/main/java/com/juick/service/ShowQueriesService.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2008-2017, Juick
+ *
+ * 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.service;
+
+import com.juick.User;
+
+import java.util.List;
+
+/**
+ * Created by aalexeev on 11/13/16.
+ */
+public interface ShowQueriesService {
+ List<String> getRecommendedUsers(User forUser);
+
+ List<String> getTopUsers();
+}
diff --git a/juick-common/src/main/java/com/juick/service/SubscriptionService.java b/juick-common/src/main/java/com/juick/service/SubscriptionService.java
new file mode 100644
index 00000000..47f81415
--- /dev/null
+++ b/juick-common/src/main/java/com/juick/service/SubscriptionService.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2008-2017, Juick
+ *
+ * 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.service;
+
+import com.juick.Tag;
+import com.juick.User;
+import com.juick.server.helpers.NotifyOpts;
+
+import java.util.List;
+
+/**
+ * Created by aalexeev on 11/13/16.
+ */
+public interface SubscriptionService {
+ List<String> getJIDSubscribedToUser(int uid, boolean friendsonly);
+
+ List<User> getSubscribedUsers(int uid, int mid);
+
+ List<User> getUsersSubscribedToComments(int mid, int ignore_uid);
+
+ List<User> getUsersSubscribedToUserRecommendations(int uid, int mid, int muid);
+
+ boolean subscribeMessage(int mid, int vuid);
+
+ boolean unSubscribeMessage(int mid, int vuid);
+
+ boolean subscribeUser(User user, User toUser);
+
+ boolean unSubscribeUser(User user, User fromUser);
+
+ boolean subscribeTag(User user, Tag toTag);
+
+ boolean unSubscribeTag(User user, Tag toTag);
+
+ List<String> getSubscribedTags(User user);
+
+ NotifyOpts getNotifyOptions(User user);
+
+ boolean setNotifyOptions(User user, NotifyOpts options);
+}
diff --git a/juick-common/src/main/java/com/juick/service/TagService.java b/juick-common/src/main/java/com/juick/service/TagService.java
new file mode 100644
index 00000000..7cd7768f
--- /dev/null
+++ b/juick-common/src/main/java/com/juick/service/TagService.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2008-2017, Juick
+ *
+ * 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.service;
+
+import com.juick.Tag;
+import com.juick.User;
+import com.juick.server.helpers.TagStats;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.stream.Stream;
+
+/**
+ * Created by aalexeev on 11/13/16.
+ */
+public interface TagService {
+ Tag getTag(int tid);
+
+ Tag getTag(String tag, boolean autoCreate);
+
+ List<Tag> getTags(Stream<String> tags, boolean autoCreate);
+
+ boolean getTagNoIndex(int tagId);
+
+ int createTag(String name);
+
+ List<TagStats> getUserTagStats(int uid);
+
+ List<String> getUserBLTags(int uid);
+
+ List<String> getPopularTags();
+
+ List<TagStats> getTagStats();
+
+ List<Tag> updateTags(int mid, Collection<Tag> newTags);
+
+ List<Tag> fromString(String txt, boolean tagsOnly);
+
+ List<TagStats> getMessageTags(int mid);
+
+ List<Integer> getMessageTagsIDs(int mid);
+
+ boolean blacklistTag(User user, Tag tag);
+
+ boolean isInBL(User user, Tag tag);
+
+ boolean isSubscribed(User user, Tag tag);
+}
diff --git a/juick-common/src/main/java/com/juick/service/TelegramService.java b/juick-common/src/main/java/com/juick/service/TelegramService.java
new file mode 100644
index 00000000..7786ca9f
--- /dev/null
+++ b/juick-common/src/main/java/com/juick/service/TelegramService.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2008-2017, Juick
+ *
+ * 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.service;
+
+import com.juick.User;
+
+import java.util.List;
+
+/**
+ * Created by vt on 24/11/2016.
+ */
+public interface TelegramService {
+ boolean addChat(Long id);
+
+ boolean deleteChat(Long id);
+
+ List<Long> getChats();
+
+ int getUser(long tgId);
+
+ boolean createTelegramUser(long tgID, String tgName);
+
+ boolean deleteTelegramUser(Integer uid);
+
+ List<Long> getTelegramIdentifiers(List<User> users);
+}
diff --git a/juick-common/src/main/java/com/juick/service/UserService.java b/juick-common/src/main/java/com/juick/service/UserService.java
new file mode 100644
index 00000000..115c7dfc
--- /dev/null
+++ b/juick-common/src/main/java/com/juick/service/UserService.java
@@ -0,0 +1,138 @@
+/*
+ * Copyright (C) 2008-2017, Juick
+ *
+ * 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.service;
+
+import com.juick.User;
+import com.juick.server.helpers.Auth;
+import com.juick.server.helpers.UserInfo;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * Created by aalexeev on 11/13/16.
+ */
+public interface UserService {
+ enum ActiveStatus {
+ Inactive,
+ Active
+ }
+
+ String getSignUpHashByJID(String jid);
+
+ String getSignUpHashByTelegramID(Long telegramId, String username);
+
+ int createUser(String username, String password);
+
+ Optional<User> getUserByUID(int uid);
+
+ User getUserByName(String username);
+
+ User getFullyUserByName(String username);
+
+ User getUserByEmail(String email);
+
+ List<User> getFullyUsersByNames(Collection<String> usernames);
+
+ User getUserByJID(String jid);
+
+ List<User> getUsersByName(Collection<String> unames);
+
+ List<User> getUsersByID(Collection<Integer> uids);
+
+ List<String> getJIDsbyUID(int uid);
+
+ int getUIDbyJID(String jid);
+
+ int getUIDbyName(String uname);
+
+ int getUIDbyHash(String hash);
+
+ com.juick.User getUserByHash(String hash);
+
+ String getHashByUID(int uid);
+
+ int getUIDByHttpAuth(String header);
+
+ int checkPassword(String username, String password);
+
+ boolean updatePassword(User user, String newPassword);
+
+ int getUserOptionInt(int uid, String option, int defaultValue);
+
+ int setUserOptionInt(int uid, String option, int value);
+
+ UserInfo getUserInfo(User user);
+
+ boolean updateUserInfo(User user, UserInfo info);
+
+ boolean getCanMedia(int uid);
+
+ boolean isInWL(int uid, int check);
+
+ boolean isInBL(int uid, int check);
+
+ boolean isInBLAny(int uid, int uid2);
+
+ List<Integer> checkBL(int visitor, Collection<Integer> uids);
+
+ boolean isSubscribed(int uid, int check);
+
+ List<Integer> getUserRead(int uid);
+
+ List<com.juick.User> getUserReadLeastPopular(int uid, int cnt);
+
+ List<User> getUserReaders(int uid);
+
+ List<User> getUserFriends(int uid);
+
+ List<com.juick.User> getUserBLUsers(int uid);
+
+ boolean linkTwitterAccount(User user, String accessToken, String accessTokenSecret, String screenName);
+
+ int getStatsIRead(int uid);
+
+ int getStatsMyReaders(int uid);
+
+ int getStatsMessages(int uid);
+
+ int getStatsReplies(int uid);
+
+ boolean setActiveStatusForJID(String JID, ActiveStatus jidStatus);
+
+ List<String> getAllJIDs(User user);
+
+ List<Auth> getAuthCodes(User user);
+
+ List<String> getEmails(User user);
+
+ String getEmailHash(User user);
+
+ int deleteLoginForUser(String name);
+
+ int setLoginForUser(int uid, String loginHash);
+
+ void logout(int uid);
+
+ boolean deleteJID(int uid, String jid);
+
+ boolean unauthJID(int uid, String jid);
+
+ List<String> getActiveJIDs();
+}
diff --git a/juick-common/src/main/java/com/juick/service/search/SearchService.java b/juick-common/src/main/java/com/juick/service/search/SearchService.java
new file mode 100644
index 00000000..b1ea9374
--- /dev/null
+++ b/juick-common/src/main/java/com/juick/service/search/SearchService.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2008-2017, Juick
+ *
+ * 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.service.search;
+
+import java.util.List;
+
+/**
+ * Created by aalexeev on 11/18/16.
+ */
+public interface SearchService {
+ void setMaxResult(int maxResult);
+
+ List<Integer> searchInAllMessages(String searchString, int messageIdBefore);
+
+ List<Integer> searchByStringAndUser(String searchString, final int userId, int messageIdBefore);
+}
diff --git a/juick-common/src/main/java/com/juick/service/security/HashParamAuthenticationFilter.java b/juick-common/src/main/java/com/juick/service/security/HashParamAuthenticationFilter.java
new file mode 100644
index 00000000..b56b98c8
--- /dev/null
+++ b/juick-common/src/main/java/com/juick/service/security/HashParamAuthenticationFilter.java
@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) 2008-2017, Juick
+ *
+ * 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.service.security;
+
+import com.juick.User;
+import com.juick.service.security.entities.JuickUser;
+import com.juick.service.UserService;
+import org.springframework.security.authentication.AnonymousAuthenticationToken;
+import org.springframework.security.authentication.RememberMeAuthenticationToken;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.security.web.authentication.RememberMeServices;
+import org.springframework.security.web.authentication.rememberme.AbstractRememberMeServices;
+import org.springframework.util.Assert;
+import org.springframework.web.filter.OncePerRequestFilter;
+import org.springframework.web.util.WebUtils;
+
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+/**
+ * Created by aalexeev on 4/5/17.
+ */
+public class HashParamAuthenticationFilter extends OncePerRequestFilter {
+ public static final String PARAM_NAME = "hash";
+
+ private final UserService userService;
+ private final RememberMeServices rememberMeServices;
+
+
+ public HashParamAuthenticationFilter(
+ final UserService userService,
+ final RememberMeServices rememberMeServices) {
+ Assert.notNull(userService, "userService should not be null");
+ Assert.notNull(rememberMeServices, "rememberMeServices should not be null");
+
+ this.userService = userService;
+ this.rememberMeServices = rememberMeServices;
+ }
+
+ @Override
+ protected void doFilterInternal(
+ HttpServletRequest request,
+ HttpServletResponse response,
+ FilterChain filterChain) throws ServletException, IOException {
+
+ String hash = getHashFromRequest(request);
+
+ if (hash != null && authenticationIsRequired()) {
+ User user = userService.getUserByHash(hash);
+
+ if (!user.isAnonymous()) {
+ User userWithPassword = userService.getFullyUserByName(user.getName());
+ userWithPassword.setAuthHash(userService.getHashByUID(userWithPassword.getUid()));
+ Authentication authentication = new RememberMeAuthenticationToken(
+ ((AbstractRememberMeServices)rememberMeServices).getKey(), new JuickUser(userWithPassword), JuickUser.USER_AUTHORITY);
+
+ SecurityContextHolder.getContext().setAuthentication(authentication);
+
+ rememberMeServices.loginSuccess(request, response, authentication);
+ }
+ }
+
+ filterChain.doFilter(request, response);
+ }
+
+ private boolean authenticationIsRequired() {
+ Authentication existingAuth = SecurityContextHolder.getContext().getAuthentication();
+
+ return existingAuth == null ||
+ !existingAuth.isAuthenticated() ||
+ existingAuth instanceof AnonymousAuthenticationToken;
+ }
+
+ private String getHashFromRequest(HttpServletRequest request) {
+ String paramHash = request.getParameter(PARAM_NAME);
+ Cookie cookieHash = WebUtils.getCookie(request, PARAM_NAME);
+
+ if (paramHash == null && cookieHash != null) {
+ return cookieHash.getValue();
+ }
+ return paramHash;
+ }
+}
diff --git a/juick-common/src/main/java/com/juick/service/security/JuickUserDetailsService.java b/juick-common/src/main/java/com/juick/service/security/JuickUserDetailsService.java
new file mode 100644
index 00000000..f6ae8909
--- /dev/null
+++ b/juick-common/src/main/java/com/juick/service/security/JuickUserDetailsService.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2008-2017, Juick
+ *
+ * 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.service.security;
+
+import com.juick.service.UserService;
+import com.juick.service.security.entities.JuickUser;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.security.core.userdetails.UserDetails;
+import org.springframework.security.core.userdetails.UserDetailsService;
+import org.springframework.security.core.userdetails.UsernameNotFoundException;
+import org.springframework.util.Assert;
+
+/**
+ * Created by aalexeev on 11/28/16.
+ */
+public class JuickUserDetailsService implements UserDetailsService {
+ private final UserService userService;
+
+ public JuickUserDetailsService(final UserService userService) {
+ Assert.notNull(userService, "UserService must be initialized");
+ this.userService = userService;
+ }
+
+ @Override
+ public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException {
+ if (StringUtils.isBlank(username))
+ throw new UsernameNotFoundException("Invalid user name " + username);
+
+ com.juick.User user = userService.getFullyUserByName(username);
+
+ if (user != null) {
+ user.setAuthHash(userService.getHashByUID(user.getUid()));
+ return new JuickUser(user);
+ }
+
+ throw new UsernameNotFoundException("The username " + username + " is not found");
+ }
+}
diff --git a/juick-common/src/main/java/com/juick/service/security/NotAuthorizedAuthenticationEntryPoint.java b/juick-common/src/main/java/com/juick/service/security/NotAuthorizedAuthenticationEntryPoint.java
new file mode 100644
index 00000000..b9bdcaa9
--- /dev/null
+++ b/juick-common/src/main/java/com/juick/service/security/NotAuthorizedAuthenticationEntryPoint.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2008-2017, Juick
+ *
+ * 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.service.security;
+
+import org.springframework.security.core.AuthenticationException;
+import org.springframework.security.web.AuthenticationEntryPoint;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+/**
+ * Created by vitalyster on 25.11.2016.
+ */
+public class NotAuthorizedAuthenticationEntryPoint implements AuthenticationEntryPoint {
+ @Override
+ public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) {
+ response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
+ }
+}
diff --git a/juick-common/src/main/java/com/juick/service/security/NullUserDetailsService.java b/juick-common/src/main/java/com/juick/service/security/NullUserDetailsService.java
new file mode 100644
index 00000000..91acefa3
--- /dev/null
+++ b/juick-common/src/main/java/com/juick/service/security/NullUserDetailsService.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2008-2017, Juick
+ *
+ * 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.service.security;
+
+import org.springframework.security.core.userdetails.UserDetails;
+import org.springframework.security.core.userdetails.UserDetailsService;
+import org.springframework.security.core.userdetails.UsernameNotFoundException;
+
+/**
+ * Created by aalexeev on 11/28/16.
+ */
+public class NullUserDetailsService implements UserDetailsService {
+ @Override
+ public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
+ throw new UsernameNotFoundException(
+ "loadUserByUsername called for NullUserDetailsService, user " + username + "can not be found");
+ }
+}
diff --git a/juick-common/src/main/java/com/juick/service/security/deprecated/CookieSimpleHashRememberMeServices.java b/juick-common/src/main/java/com/juick/service/security/deprecated/CookieSimpleHashRememberMeServices.java
new file mode 100644
index 00000000..a8b956c1
--- /dev/null
+++ b/juick-common/src/main/java/com/juick/service/security/deprecated/CookieSimpleHashRememberMeServices.java
@@ -0,0 +1,130 @@
+/*
+ * Copyright (C) 2008-2017, Juick
+ *
+ * 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.service.security.deprecated;
+
+import com.juick.User;
+import com.juick.server.util.HashUtils;
+import com.juick.service.security.entities.JuickUser;
+import com.juick.service.UserService;
+import com.juick.service.security.NullUserDetailsService;
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.core.env.Environment;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.userdetails.UserDetails;
+import org.springframework.security.core.userdetails.UsernameNotFoundException;
+import org.springframework.security.web.authentication.RememberMeServices;
+import org.springframework.security.web.authentication.rememberme.AbstractRememberMeServices;
+import org.springframework.security.web.authentication.rememberme.InvalidCookieException;
+import org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationException;
+import org.springframework.util.Assert;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.util.Optional;
+
+/**
+ * Created by aalexeev on 11/28/16.
+ *
+ * @deprecated not recommended use for secure reasons
+ */
+@Deprecated
+public class CookieSimpleHashRememberMeServices extends AbstractRememberMeServices implements RememberMeServices {
+ private static final Logger logger = LoggerFactory.getLogger(CookieSimpleHashRememberMeServices.class);
+
+ private static final String COOKIE_PARAM_NAME = "hash";
+
+ private final UserService userService;
+
+ public CookieSimpleHashRememberMeServices(
+ final String key, final UserService userService, final Environment environment) {
+ super(key, new NullUserDetailsService());
+
+ Assert.notNull(userService);
+ Assert.notNull(environment);
+
+ this.userService = userService;
+
+ setCookieName(COOKIE_PARAM_NAME);
+ setCookieDomain(environment.getProperty("web_domain", "localhost"));
+ setAlwaysRemember(true);
+ }
+
+ @Override
+ public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
+ super.logout(request, response, authentication);
+ userService.deleteLoginForUser(authentication.getName());
+ }
+
+ @Override
+ protected void onLoginSuccess(
+ HttpServletRequest request, HttpServletResponse response, Authentication successfulAuthentication) {
+ String username = successfulAuthentication.getName();
+
+ logger.debug("Creating new persistent login for user {}", username);
+
+ try {
+ int uid = userService.getUIDbyName(username);
+
+ Assert.isTrue(uid > 0);
+
+ String hash = HashUtils.generateHash(16);
+
+ userService.setLoginForUser(uid, hash);
+
+ setCookie(new String[]{hash}, getTokenValiditySeconds(), request, response);
+ } catch (Exception e) {
+ logger.error("Failed to save cookies", e);
+ }
+ }
+
+ @Override
+ protected UserDetails processAutoLoginCookie(
+ String[] cookieTokens, HttpServletRequest request, HttpServletResponse response)
+ throws RememberMeAuthenticationException, UsernameNotFoundException {
+ String hash = cookieTokens[0];
+
+ if (StringUtils.isBlank(hash)) {
+ hash = request.getParameter("hash");
+ }
+ if (StringUtils.isBlank(hash)) {
+ throw new InvalidCookieException("Cookie is invalid and hash parameter not found");
+ }
+
+ int uid = userService.getUIDbyHash(hash);
+ if (uid <= 0)
+ throw new UsernameNotFoundException("User not found by hash, cookies" + cookieTokens);
+
+ Optional<User> userOptional = userService.getUserByUID(uid);
+
+ Assert.isTrue(userOptional.isPresent());
+
+ return new JuickUser(userOptional.get());
+ }
+
+ @Override
+ protected String[] decodeCookie(String cookieValue) throws InvalidCookieException {
+ return new String[]{cookieValue};
+ }
+
+ @Override
+ protected String encodeCookie(String[] cookieTokens) {
+ return cookieTokens != null && cookieTokens.length > 0 ? cookieTokens[0] : StringUtils.EMPTY;
+ }
+}
diff --git a/juick-common/src/main/java/com/juick/service/security/deprecated/RequestParamHashRememberMeServices.java b/juick-common/src/main/java/com/juick/service/security/deprecated/RequestParamHashRememberMeServices.java
new file mode 100644
index 00000000..04794d07
--- /dev/null
+++ b/juick-common/src/main/java/com/juick/service/security/deprecated/RequestParamHashRememberMeServices.java
@@ -0,0 +1,88 @@
+/*
+ * Copyright (C) 2008-2017, Juick
+ *
+ * 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.service.security.deprecated;
+
+import com.juick.User;
+import com.juick.service.security.entities.JuickUser;
+import com.juick.service.UserService;
+import com.juick.service.security.NullUserDetailsService;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.userdetails.UserDetails;
+import org.springframework.security.core.userdetails.UsernameNotFoundException;
+import org.springframework.security.web.authentication.RememberMeServices;
+import org.springframework.security.web.authentication.rememberme.AbstractRememberMeServices;
+import org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationException;
+import org.springframework.util.Assert;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * Created by aalexeev on 11/30/16.
+ *
+ * @deprecated for security reasons
+ */
+@Deprecated
+public class RequestParamHashRememberMeServices extends AbstractRememberMeServices implements RememberMeServices {
+ private static final String PARAM_NAME = "hash";
+
+ private final UserService userService;
+
+ public RequestParamHashRememberMeServices(String key, UserService userService) {
+ super(key, new NullUserDetailsService());
+
+ Assert.notNull(userService);
+ this.userService = userService;
+ setAlwaysRemember(false);
+ }
+
+ @Override
+ protected void onLoginSuccess(HttpServletRequest request, HttpServletResponse response, Authentication successfulAuthentication) {
+ // do nothing
+ }
+
+ @Override
+ protected boolean rememberMeRequested(HttpServletRequest request, String parameter) {
+ return false; // always false
+ }
+
+ @Override
+ protected void cancelCookie(HttpServletRequest request, HttpServletResponse response) {
+ // do nothing
+ }
+
+ @Override
+ protected String extractRememberMeCookie(HttpServletRequest request) {
+ return PARAM_NAME; // return any not blank value
+ }
+
+ @Override
+ protected UserDetails processAutoLoginCookie(
+ String[] cookieTokens, HttpServletRequest request, HttpServletResponse response)
+ throws RememberMeAuthenticationException, UsernameNotFoundException {
+ String hash = request.getParameter(PARAM_NAME);
+
+ if (StringUtils.isNotBlank(hash)) {
+ User user = userService.getUserByHash(hash);
+ if (user.getUid() > 0)
+ return new JuickUser(user);
+ }
+ throw new UsernameNotFoundException("User not found by hash " + hash);
+ }
+}
diff --git a/juick-common/src/main/java/com/juick/service/security/entities/JuickUser.java b/juick-common/src/main/java/com/juick/service/security/entities/JuickUser.java
new file mode 100644
index 00000000..6e72117e
--- /dev/null
+++ b/juick-common/src/main/java/com/juick/service/security/entities/JuickUser.java
@@ -0,0 +1,92 @@
+/*
+ * Copyright (C) 2008-2017, Juick
+ *
+ * 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.service.security.entities;
+
+import com.juick.User;
+import com.juick.server.helpers.AnonymousUser;
+import org.springframework.security.core.GrantedAuthority;
+import org.springframework.security.core.authority.SimpleGrantedAuthority;
+import org.springframework.security.core.userdetails.UserDetails;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Created by aalexeev on 11/21/16.
+ */
+public class JuickUser implements UserDetails {
+ static final GrantedAuthority ROLE_USER = new SimpleGrantedAuthority("ROLE_USER");
+ static final GrantedAuthority ROLE_ANONYMOUS = new SimpleGrantedAuthority("ROLE_ANONYMOUS");
+
+ public static final List<GrantedAuthority> USER_AUTHORITY = Collections.singletonList(ROLE_USER);
+ public static final List<GrantedAuthority> ANONYMOUS_AUTHORITY = Collections.singletonList(ROLE_ANONYMOUS);
+
+ public static final JuickUser ANONYMOUS_USER = new JuickUser(AnonymousUser.INSTANCE, ANONYMOUS_AUTHORITY);
+
+ private final com.juick.User user;
+ private final Collection<? extends GrantedAuthority> authorities;
+
+ public JuickUser(com.juick.User user) {
+ this(user, USER_AUTHORITY);
+ }
+
+ public JuickUser(com.juick.User user, Collection<? extends GrantedAuthority> authorities) {
+ this.user = user;
+ this.authorities = authorities;
+ }
+
+ @Override
+ public Collection<? extends GrantedAuthority> getAuthorities() {
+ return authorities;
+ }
+
+ @Override
+ public String getPassword() {
+ return "{noop}" + user.getCredentials();
+ }
+
+ @Override
+ public String getUsername() {
+ return user.getName();
+ }
+
+ @Override
+ public boolean isAccountNonExpired() {
+ return true;
+ }
+
+ @Override
+ public boolean isAccountNonLocked() {
+ return true;
+ }
+
+ @Override
+ public boolean isCredentialsNonExpired() {
+ return true;
+ }
+
+ @Override
+ public boolean isEnabled() {
+ return !user.isBanned();
+ }
+
+ public User getUser() {
+ return user;
+ }
+}