Files
mixtape/zero.Core/Api/Token.cs
T

112 lines
2.6 KiB
C#
Raw Normal View History

2020-04-22 15:46:35 +02:00
using Raven.Client.Documents;
using Raven.Client.Documents.Session;
using System;
using System.Linq;
2020-04-22 15:46:35 +02:00
using System.Threading.Tasks;
2020-11-15 17:07:27 +01:00
using zero.Core.Database;
2020-04-22 15:46:35 +02:00
using zero.Core.Entities;
using zero.Core.Extensions;
using zero.Core.Options;
2020-04-22 15:46:35 +02:00
namespace zero.Core.Api
{
public class Token : IToken
{
2020-11-15 17:07:27 +01:00
protected IZeroStore Store { get; private set; }
2020-04-22 15:46:35 +02:00
protected IZeroOptions Options { get; private set; }
2020-04-22 15:46:35 +02:00
private const string PREFIX = "changeTokens";
2020-04-22 15:46:35 +02:00
2020-11-15 17:07:27 +01:00
public Token(IZeroStore store, IZeroOptions options)
2020-04-22 15:46:35 +02:00
{
2020-11-15 17:07:27 +01:00
Store = store;
Options = options;
2020-04-22 15:46:35 +02:00
}
/// <inheritdoc />
2020-08-20 01:35:08 +02:00
public bool Verify(IZeroIdEntity entity, string token)
2020-04-22 15:46:35 +02:00
{
return Verify(entity?.Id, token);
2020-04-22 15:46:35 +02:00
}
/// <inheritdoc />
public bool Verify(string entityId, string token)
2020-04-22 15:46:35 +02:00
{
2020-05-04 14:13:03 +02:00
if (token.IsNullOrWhiteSpace() && entityId.IsNullOrEmpty())
{
return true;
}
2020-04-22 15:46:35 +02:00
if (token.IsNullOrWhiteSpace() || entityId.IsNullOrEmpty())
{
return false;
}
2020-11-15 17:07:27 +01:00
using (IDocumentSession session = Store.OpenSession())
2020-04-22 15:46:35 +02:00
{
return session.Query<ChangeToken>().Any(x => x.Id == token && x.ReferenceId == entityId);
2020-04-22 15:46:35 +02:00
}
}
/// <inheritdoc />
2020-08-20 01:35:08 +02:00
public string Get(IZeroIdEntity entity)
2020-04-22 15:46:35 +02:00
{
return Get(entity?.Id);
2020-04-22 15:46:35 +02:00
}
/// <inheritdoc />
public string Get(string entityId)
2020-04-22 15:46:35 +02:00
{
if (entityId.IsNullOrEmpty())
{
return null;
}
ChangeToken token = new ChangeToken()
{
Id = Options.Raven.CollectionPrefix.EnsureEndsWith(Store.Raven.Conventions.IdentityPartsSeparator) + PREFIX.EnsureEndsWith(Store.Raven.Conventions.IdentityPartsSeparator) + Guid.NewGuid(),
2020-04-22 15:46:35 +02:00
ReferenceId = entityId
};
2020-11-15 17:07:27 +01:00
using (IDocumentSession session = Store.OpenSession())
2020-04-22 15:46:35 +02:00
{
session.Store(token);
session.Advanced.GetMetadataFor(token)[Constants.Database.Expires] = DateTime.UtcNow.AddMinutes(Options.TokenExpiration);
session.SaveChanges();
2020-04-22 15:46:35 +02:00
}
return token.Id;
}
}
public interface IToken
{
/// <summary>
/// Verifies if the change token is valid for the entity
/// </summary>
2020-08-20 01:35:08 +02:00
bool Verify(IZeroIdEntity entity, string token);
2020-04-22 15:46:35 +02:00
/// <summary>
/// Verifies if the change token is valid for the entity
/// </summary>
bool Verify(string entityId, string token);
2020-04-22 15:46:35 +02:00
/// <summary>
/// Get a new change token for the entity
/// </summary>
2020-08-20 01:35:08 +02:00
string Get(IZeroIdEntity entity);
2020-04-22 15:46:35 +02:00
/// <summary>
/// Get a new change token for the entity
/// </summary>
string Get(string entityId);
2020-04-22 15:46:35 +02:00
}
}