From: David ‘Bombe’ Roden Date: Wed, 25 Jul 2012 18:43:40 +0000 (+0200) Subject: Add password capability to user. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=552a91b0c8a42edf5e9288f333af9c2d1bbb8ec6;p=demoscenemusic.git Add password capability to user. --- diff --git a/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java b/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java index 48c5ace..f936ff2 100644 --- a/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java +++ b/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java @@ -654,7 +654,7 @@ public class DataManager { */ @Override public User createObject(ResultSet resultSet) throws SQLException { - return new LazyUser(resultSet.getString("USERS.ID")).setName(resultSet.getString("USERS.NAME")).setLevel(resultSet.getInt("USERS.LEVEL")); + return new LazyUser(resultSet.getString("USERS.ID")).setName(resultSet.getString("USERS.NAME")).setPasswordHash(resultSet.getString("USERS.PASSWORD")).setLevel(resultSet.getInt("USERS.LEVEL")); } } diff --git a/src/main/java/net/pterodactylus/demoscenemusic/data/DefaultUser.java b/src/main/java/net/pterodactylus/demoscenemusic/data/DefaultUser.java index f8fe1b6..6867ef3 100644 --- a/src/main/java/net/pterodactylus/demoscenemusic/data/DefaultUser.java +++ b/src/main/java/net/pterodactylus/demoscenemusic/data/DefaultUser.java @@ -17,8 +17,11 @@ package net.pterodactylus.demoscenemusic.data; +import java.io.UnsupportedEncodingException; import java.util.Collection; +import net.pterodactylus.util.crypto.MessageDigestHasher; +import net.pterodactylus.util.number.Hex; import net.pterodactylus.util.object.Default; /** @@ -59,6 +62,40 @@ public class DefaultUser extends DefaultBase implements User { * {@inheritDoc} */ @Override + public User setPassword(String password) { + try { + getValue("password", String.class).set(Hex.toHex(MessageDigestHasher.getSHA512Hasher().hash(password.getBytes("UTF-8")))); + return this; + } catch (UnsupportedEncodingException uee1) { + throw new RuntimeException("VM does not support UTF-8.", uee1); + } + } + + /** + * {@inheritDoc} + */ + @Override + public User setPasswordHash(String passwordHash) { + getValue("password", String.class).set(passwordHash); + return this; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean verifyPassword(String password) { + try { + return getValue("password", String.class).get().equalsIgnoreCase(Hex.toHex(MessageDigestHasher.getSHA512Hasher().hash(password.getBytes("UTF-8")))); + } catch (UnsupportedEncodingException uee1) { + throw new RuntimeException("VM does not support UTF-8.", uee1); + } + } + + /** + * {@inheritDoc} + */ + @Override public int getLevel() { return Default.forNull(getValue("level", Integer.class).get(), 0); } diff --git a/src/main/java/net/pterodactylus/demoscenemusic/data/User.java b/src/main/java/net/pterodactylus/demoscenemusic/data/User.java index 0ca665c..d33daf5 100644 --- a/src/main/java/net/pterodactylus/demoscenemusic/data/User.java +++ b/src/main/java/net/pterodactylus/demoscenemusic/data/User.java @@ -49,6 +49,34 @@ public interface User extends Base { public User setName(String name); /** + * Sets the password of this user. + * + * @param password + * The new password of this user + * @return This user + */ + public User setPassword(String password); + + /** + * Sets the hash of the password of this user. + * + * @param passwordHash + * The hash of the user’s password + * @return This user + */ + public User setPasswordHash(String passwordHash); + + /** + * Verifies the given password. + * + * @param password + * The password to verify + * @return {@code true} if the given password matches the user’s password, + * {@code false} otherwise + */ + public boolean verifyPassword(String password); + + /** * Returns the privilege level of this user. 0 is the level of a normal user * without any special privileges. *