use of javax.persistence.NoResultException in project activityinfo by bedatadriven.
the class BasicAuthentication method authenticate.
// This method checks the user information sent in the Authorization
// header against the database of users maintained in the users Hashtable.
public User authenticate(String auth) throws IOException {
if (Strings.isNullOrEmpty(auth)) {
// no auth
return null;
}
if (!auth.toUpperCase().startsWith("BASIC ")) {
LOGGER.severe("Unsupported authorization header [" + auth + "]");
// we only do BASIC
return null;
}
// Get encoded user and password, comes after "BASIC "
String emailPasswordEncoded = auth.substring(6);
// Decode it, using any base 64 decoder
byte[] emailPassDecodedBytes = BaseEncoding.base64().decode(emailPasswordEncoded);
String emailPassDecoded = new String(emailPassDecodedBytes, Charsets.UTF_8);
String[] emailPass = emailPassDecoded.split(":");
if (emailPass.length != 2) {
return null;
}
// look up the user in the database
User user = null;
try {
user = userDAO.get().findUserByEmail(emailPass[0]);
} catch (NoResultException e) {
return null;
}
if (!authenticator.get().check(user, emailPass[1])) {
return null;
}
return user;
}
use of javax.persistence.NoResultException in project activityinfo by bedatadriven.
the class ChangePasswordController method changePassword.
@POST
public Response changePassword(@Context UriInfo uri, @FormParam("key") String key, @FormParam("password") String password, @FormParam("password2") String password2) throws IOException, ServletException {
User user = null;
try {
user = userDAO.get().findUserByChangePasswordKey(key);
} catch (NoResultException e) {
return ok(new InvalidInvitePageModel());
}
if (password == null || password.length() < MINIMUM_PASSWORD_LENGTH) {
return ok(new ChangePasswordPageModel(user).setPasswordLengthInvalid(true));
}
if (!password.equals(password2)) {
return ok(new ChangePasswordPageModel(user).setPasswordsNotMatched(true));
}
changePassword(user, password);
return Response.seeOther(uri.getAbsolutePathBuilder().replacePath("/").build()).cookie(authTokenProvider.createNewAuthCookies(user)).build();
}
use of javax.persistence.NoResultException in project activityinfo by bedatadriven.
the class ResetPasswordController method resetPassword.
@POST
@Produces(MediaType.TEXT_HTML)
@Transactional
public Viewable resetPassword(@FormParam("email") String email) {
try {
User user = userDAO.get().findUserByEmail(email);
user.setChangePasswordKey(SecureTokenGenerator.generate());
user.setDateChangePasswordKeyIssued(new Date());
mailer.send(new ResetPasswordMessage(user));
ResetPasswordPageModel model = new ResetPasswordPageModel();
model.setEmailSent(true);
return model.asViewable();
} catch (NoResultException e) {
ResetPasswordPageModel model = new ResetPasswordPageModel();
model.setLoginError(true);
return model.asViewable();
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Failed to send password reset email", e);
ResetPasswordPageModel model = new ResetPasswordPageModel();
model.setEmailError(true);
return model.asViewable();
}
}
use of javax.persistence.NoResultException in project activityinfo by bedatadriven.
the class SignUpAddressExistsController method resetPassword.
@POST
@Produces(MediaType.TEXT_HTML)
@Transactional
public Viewable resetPassword(@FormParam("email") String email) {
try {
User user = userDAO.get().findUserByEmail(email);
user.setChangePasswordKey(SecureTokenGenerator.generate());
user.setDateChangePasswordKeyIssued(new Date());
mailer.send(new ResetPasswordMessage(user));
return new SignUpAddressExistsPageModel(email).asEmailSent();
} catch (NoResultException e) {
return new SignUpAddressExistsPageModel().asLoginError();
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Failed to send password reset email", e);
return new SignUpAddressExistsPageModel().asEmailError();
}
}
use of javax.persistence.NoResultException in project activityinfo by bedatadriven.
the class ConfirmInviteControllerTest method setup.
@Before
public final void setup() {
user = new User();
userDAO = createMock(UserDAO.class);
expect(userDAO.findUserByChangePasswordKey(eq(VALID_KEY))).andReturn(user);
expect(userDAO.findUserByChangePasswordKey(EasyMock.not(eq(VALID_KEY)))).andThrow(new NoResultException());
replay(userDAO);
AuthenticationDAO authDAO = createMock(AuthenticationDAO.class);
authDAO.persist(isA(Authentication.class));
expectLastCall().anyTimes();
MailingListClient mailingListClient = createNiceMock(MailingListClient.class);
replay(mailingListClient);
AuthTokenProvider authTokenProvider = new AuthTokenProvider(Providers.of(authDAO));
resource = new ConfirmInviteController(Providers.of(userDAO), authTokenProvider, mailingListClient);
}
Aggregations