Innovenergy_trunk/csharp/App/Backend/Model/Relations/Session.cs

35 lines
953 B
C#
Raw Normal View History

2023-02-16 12:57:06 +00:00
using SQLite;
2023-03-08 12:20:33 +00:00
namespace InnovEnergy.App.Backend.Model.Relations;
2023-02-16 12:57:06 +00:00
public class Session : Relation<String, Int64>
{
[Indexed] public String Token { get => Left ; set => Left = value;}
[Indexed] public Int64 UserId { get => Right; set => Right = value;}
[Indexed] public DateTime ExpiresAt { get; set; }
[Ignore] public Boolean Valid => ExpiresAt > DateTime.Now;
[Ignore] public Boolean Expired => !Valid;
[Obsolete("To be used only by serializer")]
public Session()
{}
public Session(User user) : this(user, TimeSpan.FromDays(7))
{
}
public Session(User user, TimeSpan validFor)
{
Token = CreateToken();
UserId = user.Id;
ExpiresAt = DateTime.Now + validFor;
}
private static String CreateToken()
{
var token = new Byte[16]; // 128 bit
Random.Shared.NextBytes(token);
return Convert.ToBase64String(token);
}
}