Created
July 5, 2013 16:31
-
-
Save volgar1x/5935692 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.photon.core.login.db; | |
import com.google.inject.Inject; | |
import org.atomium.DatabaseException; | |
import org.atomium.SessionInterface; | |
import org.atomium.metadata.Metadata; | |
/** | |
* @author Blackrush | |
*/ | |
public class AtomiumUserRepository implements UserRepository { | |
private final SessionInterface session; | |
private final LoginCryptoService crypto; | |
private final Metadata<User> meta; | |
@Inject | |
AtomiumUserRepository(SessionInterface session, LoginCryptoService crypto) { | |
this.session = session; | |
this.crypto = crypto; | |
this.meta = session.getDatabase().getRegistry().register(User.class); | |
} | |
@Override | |
public User createEmpty() { | |
return meta.createEmpty(); | |
} | |
@Override | |
public User findById(Long id) { | |
return session.findOne(meta.getPrimaryKey().getRef(id)); | |
} | |
@Override | |
public User findByUsername(String username) { | |
return session.findOne(meta, "username", username); | |
} | |
@Override | |
public User authenticate(String username, String hashpass, String key) { | |
String password = crypto.cipher(crypto.decrypt(hashpass, key), key); | |
try { | |
User user = findByUsername(username); | |
if (!password.equals(user.getPasswordHash())) { | |
throw new AuthenticationException.InvalidCredentials(); | |
} else if (user.isBanned()) { | |
throw new AuthenticationException.Banned(); | |
} else { | |
return user; | |
} | |
} catch (DatabaseException.NonUnique | DatabaseException.NotFound e) { | |
throw new AuthenticationException.InvalidCredentials(e); | |
} | |
} | |
@Override | |
public void persist(User user) { | |
session.persist(user); | |
} | |
@Override | |
public void remove(User user) { | |
session.remove(user); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment