]> source.dussan.org Git - gitblit.git/commitdiff
Persist cookies in ConfigUserService. Update LDAPUserService to generate cookies.
authorJames Moger <james.moger@gitblit.com>
Wed, 11 Jul 2012 21:05:32 +0000 (17:05 -0400)
committerJames Moger <james.moger@gitblit.com>
Wed, 11 Jul 2012 21:06:38 +0000 (17:06 -0400)
src/com/gitblit/ConfigUserService.java
src/com/gitblit/FileUserService.java
src/com/gitblit/GitBlit.java
src/com/gitblit/GitblitUserService.java
src/com/gitblit/IUserService.java
src/com/gitblit/LdapUserService.java
src/com/gitblit/utils/ArrayUtils.java

index b97154f40c4be50f839c9daaa2def9fe7a2c646c..66eb17606aede3c21c8d06bbea49ad79bbdc99d4 100644 (file)
@@ -64,6 +64,8 @@ public class ConfigUserService implements IUserService {
        private static final String DISPLAYNAME = "displayName";\r
        \r
        private static final String EMAILADDRESS = "emailAddress";\r
+       \r
+       private static final String COOKIE = "cookie";\r
 \r
        private static final String REPOSITORY = "repository";\r
 \r
@@ -163,11 +165,13 @@ public class ConfigUserService implements IUserService {
         * @return cookie value\r
         */\r
        @Override\r
-       public char[] getCookie(UserModel model) {\r
+       public String getCookie(UserModel model) {\r
+               if (!StringUtils.isEmpty(model.cookie)) {\r
+                       return model.cookie;\r
+               }\r
                read();\r
                UserModel storedModel = users.get(model.username.toLowerCase());\r
-               String cookie = StringUtils.getSHA1(model.username + storedModel.password);\r
-               return cookie.toCharArray();\r
+               return storedModel.cookie;\r
        }\r
 \r
        /**\r
@@ -715,6 +719,9 @@ public class ConfigUserService implements IUserService {
                        if (!StringUtils.isEmpty(model.password)) {\r
                                config.setString(USER, model.username, PASSWORD, model.password);\r
                        }\r
+                       if (!StringUtils.isEmpty(model.cookie)) {\r
+                               config.setString(USER, model.username, COOKIE, model.cookie);\r
+                       }\r
                        if (!StringUtils.isEmpty(model.displayName)) {\r
                                config.setString(USER, model.username, DISPLAYNAME, model.displayName);\r
                        }\r
@@ -820,6 +827,10 @@ public class ConfigUserService implements IUserService {
                                        user.password = config.getString(USER, username, PASSWORD);                                     \r
                                        user.displayName = config.getString(USER, username, DISPLAYNAME);\r
                                        user.emailAddress = config.getString(USER, username, EMAILADDRESS);\r
+                                       user.cookie = config.getString(USER, username, COOKIE);\r
+                                       if (StringUtils.isEmpty(user.cookie) && !StringUtils.isEmpty(user.password)) {\r
+                                               user.cookie = StringUtils.getSHA1(user.username + user.password);\r
+                                       }\r
 \r
                                        // user roles\r
                                        Set<String> roles = new HashSet<String>(Arrays.asList(config.getStringList(\r
@@ -836,7 +847,9 @@ public class ConfigUserService implements IUserService {
 \r
                                        // update cache\r
                                        users.put(user.username, user);\r
-                                       cookies.put(StringUtils.getSHA1(user.username + user.password), user);\r
+                                       if (!StringUtils.isEmpty(user.cookie)) {\r
+                                               cookies.put(user.cookie, user);\r
+                                       }\r
                                }\r
 \r
                                // load the teams\r
index fcb8eaeb10a20fd4ceaa523457d988f6fc0f5338..7705dfd8953887ee8c8b7d15edb80244fb959e65 100644 (file)
@@ -133,13 +133,16 @@ public class FileUserService extends FileSettings implements IUserService {
         * @return cookie value\r
         */\r
        @Override\r
-       public char[] getCookie(UserModel model) {\r
+       public String getCookie(UserModel model) {\r
+               if (!StringUtils.isEmpty(model.cookie)) {\r
+                       return model.cookie;\r
+               }\r
                Properties allUsers = super.read();\r
                String value = allUsers.getProperty(model.username);\r
                String[] roles = value.split(",");\r
                String password = roles[0];\r
                String cookie = StringUtils.getSHA1(model.username + password);\r
-               return cookie.toCharArray();\r
+               return cookie;\r
        }\r
 \r
        /**\r
index eab265abd513bf4fb69bc3018ea60ee36e454cbb..27d43c8ec4a50aff6d2eb738bd75e92eb7be39ed 100644 (file)
@@ -512,9 +512,15 @@ public class GitBlit implements ServletContextListener {
                                userCookie = new Cookie(Constants.NAME, "");\r
                        } else {\r
                                // set cookie for login\r
-                               char[] cookie = userService.getCookie(user);\r
-                               userCookie = new Cookie(Constants.NAME, new String(cookie));\r
-                               userCookie.setMaxAge(Integer.MAX_VALUE);\r
+                               String cookie = userService.getCookie(user);\r
+                               if (StringUtils.isEmpty(cookie)) {\r
+                                       // create empty cookie\r
+                                       userCookie = new Cookie(Constants.NAME, "");\r
+                               } else {\r
+                                       // create real cookie\r
+                                       userCookie = new Cookie(Constants.NAME, cookie);\r
+                                       userCookie.setMaxAge(Integer.MAX_VALUE);\r
+                               }\r
                        }\r
                        userCookie.setPath("/");\r
                        response.addCookie(userCookie);\r
index bb52bef4ff8555b1780c5026ab709653fc630bc4..b4640b58b76f8b4ba56d571fa0bd02a63f25dc7f 100644 (file)
@@ -138,7 +138,7 @@ public class GitblitUserService implements IUserService {
        }\r
 \r
        @Override\r
-       public char[] getCookie(UserModel model) {\r
+       public String getCookie(UserModel model) {\r
                return serviceImpl.getCookie(model);\r
        }\r
 \r
index 78b6e2f9a74e17d41153d518b8cf40afbed9894a..8822d02430fa4d83604e31ce09159e31a1836d6b 100644 (file)
@@ -84,7 +84,7 @@ public interface IUserService {
         * @param model\r
         * @return cookie value\r
         */\r
-       char[] getCookie(UserModel model);\r
+       String getCookie(UserModel model);\r
 \r
        /**\r
         * Authenticate a user based on their cookie.\r
index 78b5f99fe80721a5d5cd4411dc349ba0395a3efd..bba943d99aebd2f02f6af917055df05092b7d8d1 100644 (file)
@@ -27,6 +27,7 @@ import org.slf4j.LoggerFactory;
 \r
 import com.gitblit.models.TeamModel;\r
 import com.gitblit.models.UserModel;\r
+import com.gitblit.utils.ArrayUtils;\r
 import com.gitblit.utils.StringUtils;\r
 import com.unboundid.ldap.sdk.Attribute;\r
 import com.unboundid.ldap.sdk.LDAPConnection;\r
@@ -140,17 +141,6 @@ public class LdapUserService extends GitblitUserService {
                return !settings.getBoolean(Keys.realm.ldap.maintainTeams, false);\r
        }\r
 \r
-       /**\r
-        * Does the user service support cookie authentication?\r
-        * \r
-        * @return true or false\r
-        */\r
-       @Override\r
-       public boolean supportsCookies() {\r
-               // TODO cookies need to be reviewed\r
-               return false;\r
-       }\r
-\r
        @Override\r
        public UserModel authenticate(String username, char[] password) {\r
                String simpleUsername = getSimpleUsername(username);\r
@@ -173,8 +163,11 @@ public class LdapUserService extends GitblitUserService {
                                        UserModel user = getUserModel(simpleUsername);\r
                                        if (user == null)       // create user object for new authenticated user\r
                                                user = new UserModel(simpleUsername);\r
-                                       \r
-                                       \r
+\r
+                                       // create a user cookie\r
+                                       if (StringUtils.isEmpty(user.cookie) && !ArrayUtils.isEmpty(password)) {\r
+                                               user.cookie = StringUtils.getSHA1(user.username + new String(password));\r
+                                       }\r
                                        \r
                                        if (!supportsTeamMembershipChanges())\r
                                                getTeamsFromLdap(ldapConnection, simpleUsername, loggingInUser, user);\r
index d0322b6b7751bff3fc4296b89ab477eefbff3b31..41d110a34165ae91d5c50e1ef2d8f401768f76bc 100644 (file)
@@ -29,7 +29,11 @@ public class ArrayUtils {
        public static boolean isEmpty(byte [] array) {\r
                return array == null || array.length == 0;\r
        }\r
-       \r
+\r
+       public static boolean isEmpty(char [] array) {\r
+               return array == null || array.length == 0;\r
+       }\r
+\r
        public static boolean isEmpty(Object [] array) {\r
                return array == null || array.length == 0;\r
        }\r