CodeLiturgy.Dashboard/BlueWest.Api/Session/SessionManager.cs

130 lines
3.8 KiB
C#
Raw Normal View History

2022-09-18 04:00:24 +03:00
using System;
using System.Threading;
using System.Threading.Tasks;
using BlueWest.Cryptography;
using BlueWest.Data.Application;
using Microsoft.Extensions.Hosting;
using Redis.OM;
using Redis.OM.Searching;
namespace BlueWest.WebApi.Session
{
/// <summary>
/// Session Provider Context
/// </summary>
2022-09-19 05:50:15 +03:00
public sealed class SessionManager : ISessionCache
2022-09-18 04:00:24 +03:00
{
private readonly RedisConnectionProvider _provider;
2022-09-19 05:50:15 +03:00
private readonly RedisCollection<SessionToken> _sessionTokens;
2022-09-18 04:00:24 +03:00
/// <summary>
/// Index Creation Device
/// </summary>
/// <param name="provider">Redis connection</param>
2022-09-19 05:50:15 +03:00
public SessionManager(
2022-09-18 04:00:24 +03:00
RedisConnectionProvider provider)
{
_provider = provider;
_sessionTokens = (RedisCollection<SessionToken>)provider.RedisCollection<SessionToken>();
}
/// <summary>
/// Empty constructor
/// </summary>
2022-09-19 05:50:15 +03:00
public SessionManager() { }
2022-09-18 04:00:24 +03:00
/// <summary>
/// Get a session token by the respective Id.
/// </summary>
/// <param name="tokenId"></param>
/// <returns></returns>
public async Task<SessionToken> GetSessionTokenByIdAsync(string tokenId)
{
return await _sessionTokens.Where(x => x.Id == tokenId)
.FirstOrDefaultAsync();
}
/// <summary>
/// Create a new session token
/// </summary>
/// <param name="token"></param>
public async Task AddSessionToken(SessionToken token)
{
await _sessionTokens.InsertAsync(token);
}
/// <inheritdoc />
public async Task SaveAsync()
{
await _sessionTokens.SaveAsync();
}
2022-09-19 05:50:15 +03:00
2022-09-18 04:00:24 +03:00
/// <summary>
/// Save session data
/// </summary>
public void Save()
{
_sessionTokens.Save();
}
2022-09-19 05:50:15 +03:00
2022-09-18 04:00:24 +03:00
/// <summary>
/// Gets a Bearer By Access Token Id
/// </summary>
/// <param name="sessionTokenId"></param>
2022-09-19 05:50:15 +03:00
public async Task<bool> IsSessionValidAsync(string sessionTokenId)
2022-09-18 04:00:24 +03:00
{
2022-09-19 05:50:15 +03:00
var accessToken = await _sessionTokens
.Where(t => t.Id == sessionTokenId)
2022-09-18 04:00:24 +03:00
.FirstOrDefaultAsync();
2022-09-19 05:50:15 +03:00
if (accessToken == null) return false;
2022-09-18 04:00:24 +03:00
if (accessToken.IsValid)
{
2022-09-19 05:50:15 +03:00
// Check if it's not expired
var expirationDate = accessToken.CreatedDate + accessToken.ValidFor;
var timeNow = DateTimeOffset.Now.ToUnixTimeMilliseconds();
if (expirationDate >= timeNow) return accessToken.IsValid;
accessToken.IsValid = false;
await _sessionTokens.SaveAsync();
2022-09-18 04:00:24 +03:00
}
2022-09-19 05:50:15 +03:00
return accessToken.IsValid;
}
/// <summary>
/// Checks if a session is valid
/// </summary>
/// <param name="sessionTokenId"></param>
/// <returns></returns>
public bool IsSessionValid(string sessionTokenId)
{
var accessToken = _sessionTokens
.FirstOrDefault(t => t.Id == sessionTokenId);
if (accessToken == null) return false;
if (!accessToken.IsValid) return accessToken.IsValid;
accessToken.Validate();
_sessionTokens.Save();
return accessToken.IsValid;
2022-09-18 04:00:24 +03:00
}
2022-09-19 05:50:15 +03:00
2022-09-18 04:00:24 +03:00
/// <inheritdoc />
public async Task StartAsync(CancellationToken cancellationToken)
{
await _provider.Connection.CreateIndexAsync(typeof(SessionToken));
}
/// <inheritdoc />
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
}