diff --git a/src/main/java/etunicorn/Session.java b/src/main/java/etunicorn/Session.java new file mode 100644 index 0000000..e44d66a --- /dev/null +++ b/src/main/java/etunicorn/Session.java @@ -0,0 +1,60 @@ +package etunicorn; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.ManyToOne; +import java.math.BigInteger; +import java.security.SecureRandom; +import java.util.Date; + +/** + * Created by geoffrey on 04/02/17. + */ +@Entity +public class Session { + + // Durée par défaut d'une session en secondes + private static final int SESSION_DURATION = 10 * 60; + private static SecureRandom random = new SecureRandom(); + @ManyToOne + private Personne personne; + @Id + private String token; + // TODO Vérifier si c'est bien initialisé qu'une seule fois par éxecution car c'est lourd à initialiser + private Date validity; + + + public Session() { + } + + public Session(Personne personne) { + this.personne = personne; + // From http://stackoverflow.com/a/41156 + this.token = new BigInteger(130, random).toString(32); + this.validity = new Date(new Date().getTime() + SESSION_DURATION * 1000); + } + + public Personne getPersonne() { + return personne; + } + + public void setPersonne(Personne personne) { + this.personne = personne; + } + + public String getToken() { + return token; + } + + public void setToken(String token) { + this.token = token; + } + + public Date getValidity() { + return validity; + } + + public void setValidity(Date validity) { + this.validity = validity; + } +} diff --git a/src/main/java/etunicorn/SessionRepository.java b/src/main/java/etunicorn/SessionRepository.java new file mode 100644 index 0000000..324ef0f --- /dev/null +++ b/src/main/java/etunicorn/SessionRepository.java @@ -0,0 +1,10 @@ +package etunicorn; + +import org.springframework.data.repository.CrudRepository; + +/** + * Created by geoffrey on 04/02/17. + */ +public interface SessionRepository extends CrudRepository { + Session findByToken(String token); +} -- libgit2 0.21.2