109 lines
3.2 KiB
C#
109 lines
3.2 KiB
C#
|
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>
|
||
|
public sealed class SessionDataService : IHostedService, ISessionCache
|
||
|
{
|
||
|
private readonly RedisConnectionProvider _provider;
|
||
|
private RedisCollection<SessionToken> _sessionTokens;
|
||
|
/// <summary>
|
||
|
/// Index Creation Device
|
||
|
/// </summary>
|
||
|
/// <param name="provider">Redis connection</param>
|
||
|
public SessionDataService(
|
||
|
RedisConnectionProvider provider)
|
||
|
{
|
||
|
_provider = provider;
|
||
|
_sessionTokens = (RedisCollection<SessionToken>)provider.RedisCollection<SessionToken>();
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Empty constructor
|
||
|
/// </summary>
|
||
|
public SessionDataService() { }
|
||
|
|
||
|
/// <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();
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Save session data
|
||
|
/// </summary>
|
||
|
public void Save()
|
||
|
{
|
||
|
_sessionTokens.Save();
|
||
|
}
|
||
|
|
||
|
|
||
|
/// <summary>
|
||
|
/// Gets a Bearer By Access Token Id
|
||
|
/// </summary>
|
||
|
/// <param name="sessionTokenId"></param>
|
||
|
public async Task<string> GetBearerByAccessTokenId(string sessionTokenId)
|
||
|
{
|
||
|
var accessToken = await _sessionTokens.Where(t => t.Id == sessionTokenId)
|
||
|
.FirstOrDefaultAsync();
|
||
|
|
||
|
if (accessToken == null) return string.Empty;
|
||
|
|
||
|
if (accessToken.IsValid)
|
||
|
{
|
||
|
var createdDate = DateTime.UnixEpoch.AddMilliseconds(accessToken.CreatedDate);
|
||
|
if (createdDate.AddMilliseconds(accessToken.ValidFor) < DateTime.Now)
|
||
|
{
|
||
|
accessToken.IsValid = false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
await _sessionTokens.SaveAsync();
|
||
|
|
||
|
return accessToken.IsValid ? accessToken.AccessToken : string.Empty;
|
||
|
}
|
||
|
|
||
|
/// <inheritdoc />
|
||
|
public async Task StartAsync(CancellationToken cancellationToken)
|
||
|
{
|
||
|
await _provider.Connection.CreateIndexAsync(typeof(SessionToken));
|
||
|
}
|
||
|
|
||
|
/// <inheritdoc />
|
||
|
public Task StopAsync(CancellationToken cancellationToken)
|
||
|
{
|
||
|
return Task.CompletedTask;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|