Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
package edu.harvard.iq.dataverse.persistence.user;

import static java.time.Instant.now;
import static java.time.temporal.ChronoUnit.MINUTES;
import static java.util.UUID.randomUUID;
import static javax.persistence.GenerationType.IDENTITY;

import java.io.Serializable;
import java.sql.Timestamp;
import java.time.Instant;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;
import java.util.UUID;

@SuppressWarnings("serial")
@Table(indexes = {@Index(columnList = "token")
Expand All @@ -43,7 +47,7 @@ public enum Reason {
// make the token lookup much faster.

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@GeneratedValue(strategy = IDENTITY)
private Long id;

@Column(nullable = true)
Expand All @@ -66,65 +70,57 @@ public enum Reason {
@Enumerated(EnumType.STRING)
private Reason reason;

/**
* This is only here because it has to be: "The class should have a no-arg,
* public or protected constructor." Please use the constructor that takes
* arguments.
*/
@Deprecated
public PasswordResetData() {
}

public PasswordResetData(BuiltinUser aBuiltinUser) {
builtinUser = aBuiltinUser;
token = UUID.randomUUID().toString();
long nowInMilliseconds = new Date().getTime();
created = new Timestamp(nowInMilliseconds);
reason = Reason.FORGOT_PASSWORD;
public PasswordResetData(final BuiltinUser user, final Reason reason,
final long minutesUntilPasswordResetDataExpires) {
this.builtinUser = user;
this.reason = reason;
this.token = randomUUID().toString();
final Instant now = now();
this.created = Timestamp.from(now);
this.expires = Timestamp.from(now.plus(minutesUntilPasswordResetDataExpires, MINUTES));
}

public boolean isExpired() {
if (this.expires == null) {
return true;
}
long expiresInMilliseconds = this.expires.getTime();
long nowInMilliseconds = new Date().getTime();
return nowInMilliseconds > expiresInMilliseconds;
return this.expires.before(Timestamp.from(now()));
}

public String getToken() {
return token;
return this.token;
}

public BuiltinUser getBuiltinUser() {
return builtinUser;
return this.builtinUser;
}

public Timestamp getCreated() {
return created;
return this.created;
}

public Timestamp getExpires() {
return expires;
return this.expires;
}

public Long getId() {
return id;
return this.id;
}

public void setId(Long id) {
public void setId(final Long id) {
this.id = id;
}

public Reason getReason() {
return reason;
return this.reason;
}

public void setReason(Reason reason) {
public void setReason(final Reason reason) {
this.reason = reason;
}

public void setExpires(Timestamp expires) {
public void setExpires(final Timestamp expires) {
this.expires = expires;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package edu.harvard.iq.dataverse.persistence.user;


import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

public class PasswordResetDataTest {

@Test
void isExpired() {



assertFalse(new PasswordResetData(null, null, 60*24).isExpired());
assertTrue(new PasswordResetData(null, null, -60*24).isExpired());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
import javax.persistence.NonUniqueResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.sql.Timestamp;
import java.text.MessageFormat;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -86,11 +84,8 @@ public PasswordResetInitResponse requestPasswordReset(BuiltinUser aUser, boolean
}

// create a fresh token for the user
PasswordResetData passwordResetData = new PasswordResetData(aUser);
passwordResetData.setExpires(new Timestamp(
passwordResetData.getCreated().getTime() +
TimeUnit.MINUTES.toMillis(systemConfig.getMinutesUntilPasswordResetTokenExpires())));
passwordResetData.setReason(reason);
PasswordResetData passwordResetData = new PasswordResetData(aUser, reason,
systemConfig.getMinutesUntilPasswordResetTokenExpires());
try {
em.persist(passwordResetData);
PasswordResetInitResponse passwordResetInitResponse =
Expand Down

This file was deleted.