Skip to content

Instantly share code, notes, and snippets.

@Allsimon
Created May 19, 2016 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Allsimon/84f4930e211211dfd064d40e7ddb1e56 to your computer and use it in GitHub Desktop.
Save Allsimon/84f4930e211211dfd064d40e7ddb1e56 to your computer and use it in GitHub Desktop.
package com.groww.resources.auth;
import com.groww.db.model.Users;
import io.dropwizard.auth.AuthenticationException;
import io.dropwizard.auth.Authenticator;
import io.dropwizard.auth.basic.BasicCredentials;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.mindrot.jbcrypt.BCrypt;
import java.util.Optional;
public class GrowwAuthenticator implements Authenticator<BasicCredentials, Users> {
private final SessionFactory sessionFactory;
public GrowwAuthenticator(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public Optional<Users> authenticate(BasicCredentials credentials) throws AuthenticationException {
Session session = sessionFactory.getCurrentSession(); // Crash here
Query query = session.createQuery("SELECT a FROM Users a WHERE a.email = :login");
query.setParameter("login", credentials.getUsername());
Users user = (Users) query.uniqueResult();
if (user == null || !BCrypt.checkpw(credentials.getPassword(), user.getHashedPassword())) {
throw new RuntimeException("Credentials invalid.");
}
System.out.println("User: " + user);
return Optional.of(user);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment