Created
May 19, 2016 15:40
-
-
Save Allsimon/84f4930e211211dfd064d40e7ddb1e56 to your computer and use it in GitHub Desktop.
This file contains 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 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