Token expiration added
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package com.plannaplan.entities;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
@ -11,6 +12,8 @@ import com.plannaplan.types.UserRoles;
|
||||
|
||||
@Entity
|
||||
public class User {
|
||||
private static final float TOKEN_EXPIRE_MINUTES = 15;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
@ -19,7 +22,7 @@ public class User {
|
||||
private String email;
|
||||
private UserRoles role;
|
||||
private String token;
|
||||
private Date tokenCreatedDate;
|
||||
private Timestamp tokenUsageDate;
|
||||
|
||||
public User() {
|
||||
}
|
||||
@ -39,8 +42,8 @@ public class User {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public Date getTokenCreatedDate() {
|
||||
return tokenCreatedDate;
|
||||
public Timestamp getTokenUsageDate() {
|
||||
return tokenUsageDate;
|
||||
}
|
||||
|
||||
public String getToken() {
|
||||
@ -48,7 +51,7 @@ public class User {
|
||||
}
|
||||
|
||||
public void setToken(String token) {
|
||||
this.tokenCreatedDate = new Date(System.currentTimeMillis());
|
||||
this.tokenUsageDate = new Timestamp(System.currentTimeMillis());
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
@ -79,4 +82,16 @@ public class User {
|
||||
public Long getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public boolean isCredentialsNonExpired() {
|
||||
final long diffInMilliseconds = Math
|
||||
.abs(this.tokenUsageDate.getTime() - new Timestamp(System.currentTimeMillis()).getTime());
|
||||
final long minutes = TimeUnit.MILLISECONDS.toMinutes(diffInMilliseconds);
|
||||
|
||||
if (minutes > TOKEN_EXPIRE_MINUTES) {
|
||||
return false;
|
||||
}
|
||||
this.tokenUsageDate = new Timestamp(System.currentTimeMillis());
|
||||
return true;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user