Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 2 commits
  • 17 files changed
  • 0 commit comments
  • 1 contributor
View
33 src/com/chiasma/writey/domain/social/User.java
@@ -0,0 +1,33 @@
+package com.chiasma.writey.domain.social;
+
+public class User {
+
+ private final String id;
+ private String username;
+ private String nickname;
+
+ public User(String id){
+ this.id=id;
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ public String getUsername() {
+ return username;
+ }
+
+ public void setUsername(String username) {
+ this.username = username;
+ }
+
+ public String getNickname() {
+ return nickname;
+ }
+
+ public void setNickname(String nickname) {
+ this.nickname = nickname;
+ }
+
+}
View
5 src/com/chiasma/writey/domain/social/UserRanking.java
@@ -0,0 +1,5 @@
+package com.chiasma.writey.domain.social;
+
+public enum UserRanking {
+
+}
View
147 src/com/chiasma/writey/domain/social/impl/SocialUser.java
@@ -0,0 +1,147 @@
+package com.chiasma.writey.domain.social.impl;
+
+import java.util.Date;
+
+public class SocialUser {
+ /**
+ * This is user's unique username in the writey application
+ */
+ private String userId;
+
+ /**
+ * This is the provider identifier for the social application e.g. Facebook
+ * or Twitter
+ */
+ private String providerId;
+
+ /**
+ * This is the user's unique id in the provider system
+ */
+ private String providerUserId;
+
+ /**
+ * Spring Social allows for 1-n accounts per provider per user e.g. Multiple
+ * Facebook account associated with one user in the application. This value
+ * determines the order of importance of those accounts This value would
+ * usually be 1 indicating one to one mapping from writey to provider system
+ */
+ private int rank;
+
+ /*
+ *
+ */
+ private String displayName;
+ private String profileUrl;
+ private String imageUrl;
+
+ /*
+ * OAuth Credentials and related information accessToken, secret,
+ * refreshToken and expireTime
+ */
+ private String accessToken;
+ private String secret;
+ private String refreshToken;
+ private Long expireTime;
+
+ private Date createDate;
+
+ public String getUserId() {
+ return userId;
+ }
+
+ public void setUserId(String userId) {
+ this.userId = userId;
+ }
+
+ public String getProviderId() {
+ return providerId;
+ }
+
+ public void setProviderId(String providerId) {
+ this.providerId = providerId;
+ }
+
+ public int getRank() {
+ return rank;
+ }
+
+ public void setRank(int rank) {
+ this.rank = rank;
+ }
+
+ public String getDisplayName() {
+ return displayName;
+ }
+
+ public void setDisplayName(String displayName) {
+ this.displayName = displayName;
+ }
+
+ public String getProfileUrl() {
+ return profileUrl;
+ }
+
+ public void setProfileUrl(String profileUrl) {
+ this.profileUrl = profileUrl;
+ }
+
+ public String getImageUrl() {
+ return imageUrl;
+ }
+
+ public void setImageUrl(String imageUrl) {
+ this.imageUrl = imageUrl;
+ }
+
+ public String getAccessToken() {
+ return accessToken;
+ }
+
+ public void setAccessToken(String accessToken) {
+ this.accessToken = accessToken;
+ }
+
+ public String getSecret() {
+ return secret;
+ }
+
+ public void setSecret(String secret) {
+ this.secret = secret;
+ }
+
+ public String getRefreshToken() {
+ return refreshToken;
+ }
+
+ public void setRefreshToken(String refreshToken) {
+ this.refreshToken = refreshToken;
+ }
+
+ public Long getExpireTime() {
+ return expireTime;
+ }
+
+ public void setExpireTime(Long expireTime) {
+ this.expireTime = expireTime;
+ }
+
+ public Date getCreateDate() {
+ if (createDate == null) {
+ createDate = new Date();
+ }
+ return createDate;
+ }
+
+ public void setCreateDate(Date createDate) {
+ this.createDate = createDate;
+ }
+
+ public String getProviderUserId() {
+ return providerUserId;
+ }
+
+ public void setProviderUserId(String providerUserId) {
+ this.providerUserId = providerUserId;
+ }
+
+}
View
26 src/com/chiasma/writey/persistance/datastore/social/SocialUserDao.java
@@ -0,0 +1,26 @@
+package com.chiasma.writey.persistance.datastore.social;
+
+import java.util.List;
+import java.util.Set;
+
+import org.springframework.util.MultiValueMap;
+
+import com.chiasma.writey.domain.social.impl.SocialUser;
+
+public interface SocialUserDao {
+ List<SocialUser> findByUserId(String userId);
+
+ List<SocialUser> findByUserIdAndProviderId(String userId, String providerId);
+
+ List<SocialUser> findByUserIdAndProviderUserIds(String userId, MultiValueMap<String, String> providerUserIds);
+
+ SocialUser get(String userId, String providerId, String providerUserId);
+
+ List<SocialUser> findPrimaryByUserIdAndProviderId(String userId, String providerId);
+
+ Integer selectMaxRankByUserIdAndProviderId(String userId, String providerId);
+
+ List<String> findUserIdsByProviderIdAndProviderUserId(String providerId, String providerUserId);
+
+ List<String> findUserIdsByProviderIdAndProviderUserIds(String providerId, Set<String> providerUserIds);
+}
View
61 src/com/chiasma/writey/persistance/datastore/social/impl/DatastoreSocialUserDao.java
@@ -0,0 +1,61 @@
+package com.chiasma.writey.persistance.datastore.social.impl;
+
+import java.util.List;
+import java.util.Set;
+
+import org.springframework.util.MultiValueMap;
+
+import com.chiasma.writey.domain.social.impl.SocialUser;
+import com.chiasma.writey.persistance.datastore.social.SocialUserDao;
+
+public class DatastoreSocialUserDao implements SocialUserDao {
+
+ @Override
+ public List<SocialUser> findByUserId(String userId) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public List<SocialUser> findByUserIdAndProviderId(String userId, String providerId) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public List<SocialUser> findByUserIdAndProviderUserIds(String userId, MultiValueMap<String, String> providerUserIds) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public SocialUser get(String userId, String providerId, String providerUserId) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public List<SocialUser> findPrimaryByUserIdAndProviderId(String userId, String providerId) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public Integer selectMaxRankByUserIdAndProviderId(String userId, String providerId) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public List<String> findUserIdsByProviderIdAndProviderUserId(String providerId, String providerUserId) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public List<String> findUserIdsByProviderIdAndProviderUserIds(String providerId, Set<String> providerUserIds) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+}
View
19 src/com/chiasma/writey/security/SimpleConnectionSignup.java
@@ -0,0 +1,19 @@
+package com.chiasma.writey.security;
+
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.springframework.social.connect.Connection;
+import org.springframework.social.connect.ConnectionSignUp;
+
+
+public class SimpleConnectionSignup implements ConnectionSignUp{
+
+ private final AtomicLong userIdSequence = new AtomicLong();
+
+ @Override
+ public String execute(Connection<?> arg0) {
+
+ return Long.toString(userIdSequence.incrementAndGet());
+ }
+
+}
View
23 src/com/chiasma/writey/security/SimpleSignInAdapter.java
@@ -0,0 +1,23 @@
+package com.chiasma.writey.security;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.springframework.social.connect.Connection;
+import org.springframework.social.connect.web.SignInAdapter;
+import org.springframework.web.context.request.NativeWebRequest;
+
+import com.chiasma.writey.domain.social.User;
+import com.chiasma.writey.security.config.SecurityContext;
+import com.chiasma.writey.security.web.UserCookieGenerator;
+
+public class SimpleSignInAdapter implements SignInAdapter {
+
+ private final UserCookieGenerator userCookieGenerator = new UserCookieGenerator();
+
+ public String signIn(String userId, Connection<?> connection, NativeWebRequest request) {
+ SecurityContext.setCurrentUser(new User(userId));
+ userCookieGenerator.addCookie(userId, request.getNativeResponse(HttpServletResponse.class));
+ return null;
+ }
+
+}
View
28 src/com/chiasma/writey/security/config/SecurityContext.java
@@ -0,0 +1,28 @@
+package com.chiasma.writey.security.config;
+
+import com.chiasma.writey.domain.social.User;
+
+public class SecurityContext {
+
+ private static final ThreadLocal<User> currentUser = new ThreadLocal<User>();
+
+ public static User getCurrentUser() {
+ User user = currentUser.get();
+ if (user == null) {
+ throw new IllegalStateException("No user is currently signed in");
+ }
+ return user;
+ }
+
+ public static void setCurrentUser(User user) {
+ currentUser.set(user);
+ }
+
+ public static boolean userSignedIn() {
+ return currentUser.get() != null;
+ }
+
+ public static void remove() {
+ currentUser.remove();
+ }
+}
View
32 src/com/chiasma/writey/security/config/SocialConfig.java
@@ -0,0 +1,32 @@
+package com.chiasma.writey.security.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.social.connect.ConnectionFactoryLocator;
+import org.springframework.social.connect.support.ConnectionFactoryRegistry;
+import org.springframework.social.facebook.connect.FacebookConnectionFactory;
+
+@Configuration
+public class SocialConfig {
+
+ private final static String facebookClientId = "";
+ private final static String facebookClientSecret = "";
+
+ @Bean
+ public ConnectionFactoryLocator connectionFactoryLocator() {
+
+ ConnectionFactoryRegistry registry = new ConnectionFactoryRegistry();
+ registry.addConnectionFactory(new FacebookConnectionFactory(facebookClientId, facebookClientSecret));
+ return registry;
+ }
+
+ // @Bean
+ // public UsersConnectionRepository usersConnectionRepository() {
+ // JdbcUsersConnectionRepository repository =
+ // new JdbcUsersConnectionRepository(dataSource, connectionFactoryLocator(),
+ // Encryptors.noOpText());
+ // repository.setConnectionSignUp(new SimpleConnectionSignup());
+ // return repository;
+ // }
+
+}
View
147 src/com/chiasma/writey/security/repository/DatastoreUserConnectionRepository.java
@@ -0,0 +1,147 @@
+package com.chiasma.writey.security.repository;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
+
+import org.springframework.social.connect.Connection;
+import org.springframework.social.connect.ConnectionKey;
+import org.springframework.social.connect.ConnectionRepository;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
+
+public class DatastoreUserConnectionRepository implements ConnectionRepository {
+
+ @Override
+ public void addConnection(Connection<?> arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public MultiValueMap<String, Connection<?>> findAllConnections() {
+ // List<Connection<?>> resultList =
+ // jdbcTemplate.query(selectFromUserConnection() +
+ // " where userId = ? order by providerId, rank", connectionMapper,
+ // userId);
+
+ List<Connection<?>> resultList = new ArrayList<Connection<?>>();
+ MultiValueMap<String, Connection<?>> connections = new LinkedMultiValueMap<String, Connection<?>>();
+ Set<String> registeredProviderIds = connectionFactoryLocator.registeredProviderIds();
+ for (String registeredProviderId : registeredProviderIds) {
+ connections.put(registeredProviderId, Collections.<Connection<?>> emptyList());
+ }
+ for (Connection<?> connection : resultList) {
+ String providerId = connection.getKey().getProviderId();
+ if (connections.get(providerId).size() == 0) {
+ connections.put(providerId, new LinkedList<Connection<?>>());
+ }
+ connections.add(providerId, connection);
+ }
+ return connections;
+ }
+
+ @Override
+ public List<Connection<?>> findConnections(String arg0) {
+ // TODO Auto-generated method stub
+ // return jdbcTemplate.query(selectFromUserConnection() +
+ // " where userId = ? and providerId = ? order by rank",
+ // connectionMapper, userId, providerId);
+ return null;
+ }
+
+ @Override
+ public <A> List<Connection<A>> findConnections(Class<A> apiType) {
+ List<?> connections = findConnections(getProviderId(apiType));
+ return (List<Connection<A>>) connections;
+ }
+
+ @Override
+ public MultiValueMap<String, Connection<?>> findConnectionsToUsers(MultiValueMap<String, String> providerUsers) {
+ if (providerUsers == null || providerUsers.isEmpty()) {
+ throw new IllegalArgumentException("Unable to execute find: no providerUsers provided");
+ }
+ StringBuilder providerUsersCriteriaSql = new StringBuilder();
+ /*
+ * MapSqlParameterSource parameters = new MapSqlParameterSource();
+ * parameters.addValue("userId", userId); for (Iterator<Entry<String,
+ * List<String>>> it = providerUsers.entrySet().iterator();
+ * it.hasNext();) { Entry<String, List<String>> entry = it.next();
+ * String providerId = entry.getKey();
+ * providerUsersCriteriaSql.append("providerId = :providerId_"
+ * ).append(providerId)
+ * .append(" and providerUserId in (:providerUserIds_"
+ * ).append(providerId).append(")"); parameters.addValue("providerId_" +
+ * providerId, providerId); parameters.addValue("providerUserIds_" +
+ * providerId, entry.getValue()); if (it.hasNext()) {
+ * providerUsersCriteriaSql.append(" or "); } }
+ */
+
+ /*
+ * List<Connection<?>> resultList = new
+ * NamedParameterJdbcTemplate(jdbcTemplate
+ * ).query(selectFromUserConnection() + " where userId = :userId and " +
+ * providerUsersCriteriaSql + " order by providerId, rank", parameters,
+ * connectionMapper); MultiValueMap<String, Connection<?>>
+ * connectionsForUsers = new LinkedMultiValueMap<String,
+ * Connection<?>>(); for (Connection<?> connection : resultList) {
+ * String providerId = connection.getKey().getProviderId(); List<String>
+ * userIds = providerUsers.get(providerId); List<Connection<?>>
+ * connections = connectionsForUsers.get(providerId); if (connections ==
+ * null) { connections = new ArrayList<Connection<?>>(userIds.size());
+ * for (int i = 0; i < userIds.size(); i++) { connections.add(null); }
+ * connectionsForUsers.put(providerId, connections); } String
+ * providerUserId = connection.getKey().getProviderUserId(); int
+ * connectionIndex = userIds.indexOf(providerUserId);
+ * connections.set(connectionIndex, connection); }
+ */
+
+ MultiValueMap<String, Connection<?>> connectionsForUsers = new LinkedMultiValueMap<String, Connection<?>>();
+ return connectionsForUsers;
+ }
+
+ @Override
+ public <A> Connection<A> findPrimaryConnection(Class<A> arg0) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public Connection<?> getConnection(ConnectionKey arg0) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public <A> Connection<A> getConnection(Class<A> arg0, String arg1) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public <A> Connection<A> getPrimaryConnection(Class<A> arg0) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public void removeConnection(ConnectionKey arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void removeConnections(String arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void updateConnection(Connection<?> arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+}
View
36 src/com/chiasma/writey/security/web/UserCookieGenerator.java
@@ -0,0 +1,36 @@
+package com.chiasma.writey.security.web;
+
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.springframework.web.util.CookieGenerator;
+
+public class UserCookieGenerator {
+ private final CookieGenerator userCookieGenerator = new CookieGenerator();
+
+ public UserCookieGenerator() {
+ userCookieGenerator.setCookieName("writeyuser");
+ }
+
+ public void addCookie(String userId, HttpServletResponse response) {
+ userCookieGenerator.addCookie(response, userId);
+ }
+
+ public void removeCookie(HttpServletResponse response) {
+ userCookieGenerator.addCookie(response, "");
+ }
+
+ public String readCookieValue(HttpServletRequest request) {
+ Cookie[] cookies = request.getCookies();
+ if (cookies == null) {
+ return null;
+ }
+ for (Cookie cookie : cookies) {
+ if (cookie.getName().equals(userCookieGenerator.getCookieName())) {
+ return cookie.getValue();
+ }
+ }
+ return null;
+ }
+}
View
75 src/com/chiasma/writey/security/web/inteceptor/UserInterceptor.java
@@ -0,0 +1,75 @@
+package com.chiasma.writey.security.web.inteceptor;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.springframework.social.connect.UsersConnectionRepository;
+import org.springframework.social.facebook.api.Facebook;
+import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
+import org.springframework.web.servlet.view.RedirectView;
+
+import com.chiasma.writey.domain.social.User;
+import com.chiasma.writey.security.config.SecurityContext;
+import com.chiasma.writey.security.web.UserCookieGenerator;
+
+public class UserInterceptor extends HandlerInterceptorAdapter {
+
+ private final UsersConnectionRepository connectionRepository;
+
+ private final UserCookieGenerator userCookieGenerator = new UserCookieGenerator();
+
+ public UserInterceptor(UsersConnectionRepository connectionRepository) {
+ this.connectionRepository = connectionRepository;
+ }
+
+ public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
+ rememberUser(request, response);
+ handleSignOut(request, response);
+ if (SecurityContext.userSignedIn() || requestForSignIn(request)) {
+ return true;
+ } else {
+ return requireSignIn(request, response);
+ }
+ }
+
+ public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
+ SecurityContext.remove();
+ }
+
+ // internal helpers
+
+ private void rememberUser(HttpServletRequest request, HttpServletResponse response) {
+ String userId = userCookieGenerator.readCookieValue(request);
+ if (userId == null) {
+ return;
+ }
+ if (!userNotFound(userId)) {
+ userCookieGenerator.removeCookie(response);
+ return;
+ }
+ SecurityContext.setCurrentUser(new User(userId));
+ }
+
+ private void handleSignOut(HttpServletRequest request, HttpServletResponse response) {
+ if (SecurityContext.userSignedIn() && request.getServletPath().startsWith("/signout")) {
+ connectionRepository.createConnectionRepository(SecurityContext.getCurrentUser().getId()).removeConnections("facebook");
+ userCookieGenerator.removeCookie(response);
+ SecurityContext.remove();
+ }
+ }
+
+ private boolean requestForSignIn(HttpServletRequest request) {
+ return request.getServletPath().startsWith("/signin");
+ }
+
+ private boolean requireSignIn(HttpServletRequest request, HttpServletResponse response) throws Exception {
+ new RedirectView("/signin", true).render(null, request, response);
+ return false;
+ }
+
+ private boolean userNotFound(String userId) {
+ // doesn't bother checking a local user database: simply checks if the
+ // userId is connected to Facebook
+ return connectionRepository.createConnectionRepository(userId).findPrimaryConnection(Facebook.class) != null;
+ }
+}
View
BIN war/WEB-INF/lib/spring-security-crypto-3.1.0.RELEASE.jar
Binary file not shown.
View
BIN war/WEB-INF/lib/spring-social-core-1.0.1.RELEASE.jar
Binary file not shown.
View
BIN war/WEB-INF/lib/spring-social-facebook-1.0.1.RELEASE.jar
Binary file not shown.
View
BIN war/WEB-INF/lib/spring-social-web-1.0.1.RELEASE.jar
Binary file not shown.
View
2 war/index.html
@@ -14,7 +14,7 @@
collaborative <br />
writing project
</h2>
- <h3><a href="/story">start playing around</a></h3>
+
<span class="footer">copyright chiasma media services</span>
</body>

No commit comments for this range

Something went wrong with that request. Please try again.