]> git.mxchange.org Git - juser-core.git/commitdiff
Contined:
authorRoland Häder <roland@mxchange.org>
Thu, 21 Apr 2016 10:17:46 +0000 (12:17 +0200)
committerRoland Häder <roland@mxchange.org>
Thu, 21 Apr 2016 10:17:46 +0000 (12:17 +0200)
- added method for checking user instance password with given, both from container
- ignored strings for i18n
- used MessageFormat.format()

src/org/mxchange/jusercore/model/user/UserUtils.java

index 740e7e3927ba2729c58905b4559ca34b21823cc8..82a041b74892f175f4d72d71af48cde69e9f9871 100644 (file)
@@ -22,6 +22,7 @@ import java.util.Random;
 import org.apache.commons.codec.digest.Crypt;
 import org.mxchange.jcore.BaseFrameworkSystem;
 import org.mxchange.jusercore.container.login.LoginContainer;
+import org.mxchange.jusercore.container.login.UserLoginContainer;
 
 /**
  * An utilities class for users
@@ -33,7 +34,7 @@ public class UserUtils extends BaseFrameworkSystem {
        /**
         * Password alphabet
         */
-       private static final String PASSWORD_ALPHABET = "abcdefghijklmnopqrstuvwxzyABCDEFGHIJKLMNOPQRSTUVWXZY0123456789-/?!_+#@";
+       private static final String PASSWORD_ALPHABET = "abcdefghijklmnopqrstuvwxzyABCDEFGHIJKLMNOPQRSTUVWXZY0123456789-/?!_+#@"; //NOI18N
 
        /**
         * Minimum password length
@@ -64,10 +65,10 @@ public class UserUtils extends BaseFrameworkSystem {
                // Parameter should be valid
                if (null == length) {
                        // Throw NPE
-                       throw new NullPointerException("length is null");
+                       throw new NullPointerException("length is null"); //NOI18N
                } else if (length < PASSWORD_MINIMUM_LENGTH) {
                        // To weak passwords
-                       throw new IllegalArgumentException(MessageFormat.format("Password length {0} is to short, minimum: {1}", length, PASSWORD_MINIMUM_LENGTH));
+                       throw new IllegalArgumentException(MessageFormat.format("Password length {0} is to short, minimum: {1}", length, PASSWORD_MINIMUM_LENGTH)); //NOI18N
                }
 
                // Init variable
@@ -83,7 +84,7 @@ public class UserUtils extends BaseFrameworkSystem {
                }
 
                // Should have the wanted length
-               assert (password.length() == length) : "Password length " + password.length() + " doesn't match requested: " + length;
+               assert (password.length() == length) : MessageFormat.format("Password length {0} doesn't match requested: {1}", password.length(), length); //NOI18N
 
                // Return it
                return password.toString();
@@ -100,10 +101,10 @@ public class UserUtils extends BaseFrameworkSystem {
                // Is it null or empty?
                if (null == userPassword) {
                        // Throw NPE
-                       throw new NullPointerException("userPassword is null");
+                       throw new NullPointerException("userPassword is null"); //NOI18N
                } else if (userPassword.isEmpty()) {
                        // Empty passwords are hardcoded not allowed due to security risks
-                       throw new IllegalArgumentException("userPassword is empty");
+                       throw new IllegalArgumentException("userPassword is empty"); //NOI18N
                }
 
                // Generate large number
@@ -122,8 +123,7 @@ public class UserUtils extends BaseFrameworkSystem {
        /**
         * Checks if password from container matches the updatedUser's password
         * <p>
-        * @param container   Container holding user instance and unencrypted
-        *                    password
+        * @param container   Container holding user instance and unencrypted password
         * @param updatedUser Updated user instance from database
         * <p>
         * @return Whether the password matches
@@ -132,19 +132,19 @@ public class UserUtils extends BaseFrameworkSystem {
                // Validate parameters
                if (null == container) {
                        // Throw NPE
-                       throw new NullPointerException("container is null");
+                       throw new NullPointerException("container is null"); //NOI18N
                } else if (null == updatedUser) {
                        // And again NPE ...
-                       throw new NullPointerException("updatedUser is null");
+                       throw new NullPointerException("updatedUser is null"); //NOI18N
                } else if (container.getUser() == null) {
                        // NPE for user in container
-                       throw new NullPointerException("container.user is null");
+                       throw new NullPointerException("container.user is null"); //NOI18N
                } else if (container.getUserPassword() == null) {
                        // NPE for user password in container
-                       throw new NullPointerException("container.userPassword is null");
+                       throw new NullPointerException("container.userPassword is null"); //NOI18N
                } else if (container.getUserPassword().isEmpty()) {
                        // Empty password in container
-                       throw new IllegalArgumentException("container.userPassword is empty");
+                       throw new IllegalArgumentException("container.userPassword is empty"); //NOI18N
                }
 
                // First encrypt password
@@ -154,6 +154,36 @@ public class UserUtils extends BaseFrameworkSystem {
                return encryptedPassword.equals(updatedUser.getUserEncryptedPassword());
        }
 
+       /**
+        * Checks if password from container matches with from user instance.
+        * <p>
+        * @param container   Container holding user instance and unencrypted password
+        * <p>
+        * @return Whether it maches
+        */
+       public static boolean ifPasswordMatches (final UserLoginContainer container) {
+               // Validate parameters
+               if (null == container) {
+                       // Throw NPE
+                       throw new NullPointerException("container is null"); //NOI18N
+               } else if (container.getUser() == null) {
+                       // NPE for user in container
+                       throw new NullPointerException("container.user is null"); //NOI18N
+               } else if (container.getUserPassword() == null) {
+                       // NPE for user password in container
+                       throw new NullPointerException("container.userPassword is null"); //NOI18N
+               } else if (container.getUserPassword().isEmpty()) {
+                       // Empty password in container
+                       throw new IllegalArgumentException("container.userPassword is empty"); //NOI18N
+               }
+
+               // First encrypt password
+               String encryptedPassword = Crypt.crypt(container.getUserPassword(), container.getUser().getUserEncryptedPassword());
+
+               // Is it matching?
+               return encryptedPassword.equals(container.getUser().getUserEncryptedPassword());
+       }
+
        /**
         * No instance from this class
         */