LoginResult: fix cookie login
authorNiki Roo <niki@nikiroo.be>
Wed, 13 May 2020 18:51:07 +0000 (20:51 +0200)
committerNiki Roo <niki@nikiroo.be>
Wed, 13 May 2020 18:51:07 +0000 (20:51 +0200)
src/be/nikiroo/utils/LoginResult.java

index dadd16bc9949af8f83a17f49a64b24bd8f472af6..ddc148b909c7b3cabceebcb03d407ffe5b4c972b 100644 (file)
@@ -39,16 +39,11 @@ public class LoginResult {
         * @param subkey
         *            a sub-password (can be NULL)
         * @param option
-        *            an option assigned to this user (can be NULL)
+        *            an option assigned to this login (can be NULL)
         */
        public LoginResult(String who, String key, String subkey, String option) {
-               String wookie = CookieUtils.generateCookie(who + key, 0);
-
                this.option = option;
-               this.cookie = wookie + "~"
-                               + CookieUtils.generateCookie(
-                                               wookie + (subkey == null ? "" : subkey) + option, 0)
-                               + "~" + option;
+               this.cookie = generateCookie(who, key, subkey, option);
                this.success = true;
        }
 
@@ -110,9 +105,8 @@ public class LoginResult {
                                        for (String subkey : subkeys) {
                                                if (CookieUtils.validateCookie(wookie + subkey + opts,
                                                                rehashed)) {
-                                                       wookie = CookieUtils.generateCookie(who + key, 0);
-                                                       this.cookie = CookieUtils
-                                                                       .generateCookie(wookie + subkey + opts, 0);
+                                                       this.cookie = generateCookie(who, key, subkey,
+                                                                       opts);
                                                        this.option = opts;
                                                        this.success = true;
                                                }
@@ -191,4 +185,27 @@ public class LoginResult {
 
                return "Login failed without giving a reason";
        }
+
+       /**
+        * Generate a cookie.
+        * 
+        * @param who
+        *            the user name (can be NULL)
+        * @param key
+        *            the password (can be NULL)
+        * @param subkey
+        *            a subkey (can be NULL)
+        * @param option
+        *            an option linked to the login (can be NULL)
+        * 
+        * @return a fresh cookie
+        */
+       private String generateCookie(String who, String key, String subkey,
+                       String option) {
+               String wookie = CookieUtils.generateCookie(who + key, 0);
+               return wookie + "~"
+                               + CookieUtils.generateCookie(
+                                               wookie + (subkey == null ? "" : subkey) + option, 0)
+                               + "~" + option;
+       }
 }
\ No newline at end of file