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
Expand Up @@ -113,10 +113,8 @@ private GdprExportDTO.UserData buildUserData(User user) {
.email(user.getEmail())
.firstName(user.getFirstName())
.lastName(user.getLastName())
.registrationDate(user.getRegistrationDate() != null
? user.getRegistrationDate().toInstant() : null)
.lastActivityDate(user.getLastActivityDate() != null
? user.getLastActivityDate().toInstant() : null)
.registrationDate(user.getRegistrationDate())
.lastActivityDate(user.getLastActivityDate())
.enabled(user.isEnabled())
.locked(user.isLocked())
.provider(user.getProvider() != null ? user.getProvider().name() : null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.digitalsanctuary.spring.user.persistence.model;

import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -81,21 +81,18 @@ public enum Provider {

/** The registration date. */
@CreatedDate
@Temporal(TemporalType.TIMESTAMP)
private Date registrationDate;
private Instant registrationDate;

/** The last activity date. */
@LastModifiedDate
@Temporal(TemporalType.TIMESTAMP)
private Date lastActivityDate;
private Instant lastActivityDate;

private int failedLoginAttempts;

/** The locked. */
private boolean locked;

@Temporal(TemporalType.TIMESTAMP)
private Date lockedDate;
private Instant lockedDate;

/** The roles - stored as Set to avoid Hibernate immutable collection issues */
@ToString.Exclude
Expand Down Expand Up @@ -124,7 +121,7 @@ public User() {
*/
@PreUpdate
public void setLastActivityDate() {
setLastActivityDate(new Date());
setLastActivityDate(Instant.now());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.digitalsanctuary.spring.user.service;

import java.util.Date;
import java.time.Duration;
import java.time.Instant;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -88,7 +89,7 @@ private void incrementFailedAttempts(User user) {
user.setFailedLoginAttempts(++currentAttempts);
if (currentAttempts >= maxFailedLoginAttempts) {
user.setLocked(true);
user.setLockedDate(new Date());
user.setLockedDate(Instant.now());
}
userRepository.save(user);
}
Expand Down Expand Up @@ -124,10 +125,7 @@ public boolean isLocked(final String email) {
public User checkIfUserShouldBeUnlocked(User user) {
log.debug("Checking if user should be unlocked: {}", user.getEmail());
if (user.isLocked() && user.getLockedDate() != null && accountLockoutDuration >= 0) {
Date lockedDate = user.getLockedDate();
Date now = new Date();
long diff = now.getTime() - lockedDate.getTime();
long diffMinutes = diff / (60 * 1000);
long diffMinutes = Duration.between(user.getLockedDate(), Instant.now()).toMinutes();
if (diffMinutes >= accountLockoutDuration) {
log.debug("User should be unlocked: {}", user.getEmail());
user.setLocked(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.digitalsanctuary.spring.user.service;

import java.time.Instant;
import java.util.Collection;
import java.util.Date;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.core.oidc.OidcIdToken;
import org.springframework.security.oauth2.core.oidc.OidcUserInfo;
Expand Down Expand Up @@ -42,7 +42,7 @@ public class LoginHelperService {
*/
public DSUserDetails userLoginHelper(User dbUser) {
// Updating lastActivity date for this login
dbUser.setLastActivityDate(new Date());
dbUser.setLastActivityDate(Instant.now());

// Check if the user account is locked, but should be unlocked now, and unlock it
dbUser = loginAttemptService.checkIfUserShouldBeUnlocked(dbUser);
Expand All @@ -63,7 +63,7 @@ public DSUserDetails userLoginHelper(User dbUser) {
*/
public DSUserDetails userLoginHelper(User dbUser, OidcUserInfo oidcUserInfo, OidcIdToken oidcIdToken) {
// Updating lastActivity date for this login
dbUser.setLastActivityDate(new Date());
dbUser.setLastActivityDate(Instant.now());

// Check if the user account is locked, but should be unlocked now, and unlock it
dbUser = loginAttemptService.checkIfUserShouldBeUnlocked(dbUser);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.time.Instant;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -169,7 +169,7 @@ void shouldHandleLockedUserThatRemainsLocked() {
// Given
testUser.setLocked(true);
testUser.setFailedLoginAttempts(5);
Date lockedDate = new Date(System.currentTimeMillis() - 60000); // 1 minute ago
Instant lockedDate = Instant.now().minusSeconds(60); // 1 minute ago
testUser.setLockedDate(lockedDate);

when(loginAttemptService.checkIfUserShouldBeUnlocked(testUser)).thenReturn(testUser); // User remains locked
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.digitalsanctuary.spring.user.test.builders;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
Expand Down Expand Up @@ -34,11 +34,11 @@ public class UserTestDataBuilder {
private String password = "password123";
private boolean passwordEncoded = false;
private boolean enabled = false;
private Date registrationDate = new Date();
private Date lastActivityDate = new Date();
private Instant registrationDate = Instant.now();
private Instant lastActivityDate = Instant.now();
private int failedLoginAttempts = 0;
private boolean locked = false;
private Date lockedDate = null;
private Instant lockedDate = null;
private List<Role> roles = new ArrayList<>();

private UserTestDataBuilder() {
Expand Down Expand Up @@ -139,27 +139,23 @@ public UserTestDataBuilder unverified() {
return this;
}

public UserTestDataBuilder withRegistrationDate(Date registrationDate) {
public UserTestDataBuilder withRegistrationDate(Instant registrationDate) {
this.registrationDate = registrationDate;
return this;
}

public UserTestDataBuilder registeredDaysAgo(int days) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR, -days);
this.registrationDate = cal.getTime();
this.registrationDate = Instant.now().minus(days, ChronoUnit.DAYS);
return this;
}

public UserTestDataBuilder withLastActivityDate(Date lastActivityDate) {
public UserTestDataBuilder withLastActivityDate(Instant lastActivityDate) {
this.lastActivityDate = lastActivityDate;
return this;
}

public UserTestDataBuilder lastActiveDaysAgo(int days) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR, -days);
this.lastActivityDate = cal.getTime();
this.lastActivityDate = Instant.now().minus(days, ChronoUnit.DAYS);
return this;
}

Expand All @@ -170,7 +166,7 @@ public UserTestDataBuilder withFailedLoginAttempts(int attempts) {

public UserTestDataBuilder locked() {
this.locked = true;
this.lockedDate = new Date();
this.lockedDate = Instant.now();
return this;
}

Expand All @@ -181,7 +177,7 @@ public UserTestDataBuilder unlocked() {
return this;
}

public UserTestDataBuilder withLockedDate(Date lockedDate) {
public UserTestDataBuilder withLockedDate(Instant lockedDate) {
this.lockedDate = lockedDate;
this.locked = (lockedDate != null);
return this;
Expand Down
Loading