using Raven.Client.Documents; using Raven.Client.Documents.Session; using System; using System.Linq; using System.Threading.Tasks; using zero.Core.Database; using zero.Core.Entities; using zero.Core.Extensions; using zero.Core.Options; namespace zero.Core.Api { public class Token : IToken { protected IZeroStore Store { get; private set; } protected IZeroOptions Options { get; private set; } private const string PREFIX = "changeTokens"; public Token(IZeroStore store, IZeroOptions options) { Store = store; Options = options; } /// public bool Verify(IZeroIdEntity entity, string token) { return Verify(entity?.Id, token); } /// public bool Verify(string entityId, string token) { if (token.IsNullOrWhiteSpace() && entityId.IsNullOrEmpty()) { return true; } if (token.IsNullOrWhiteSpace() || entityId.IsNullOrEmpty()) { return false; } using (IDocumentSession session = Store.OpenSession()) { return session.Query().Any(x => x.Id == token && x.ReferenceId == entityId); } } /// public string Get(IZeroIdEntity entity) { return Get(entity?.Id); } /// public string Get(string entityId) { 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(), ReferenceId = entityId }; using (IDocumentSession session = Store.OpenSession()) { session.Store(token); session.Advanced.GetMetadataFor(token)[Constants.Database.Expires] = DateTime.UtcNow.AddMinutes(Options.TokenExpiration); session.SaveChanges(); } return token.Id; } } public interface IToken { /// /// Verifies if the change token is valid for the entity /// bool Verify(IZeroIdEntity entity, string token); /// /// Verifies if the change token is valid for the entity /// bool Verify(string entityId, string token); /// /// Get a new change token for the entity /// string Get(IZeroIdEntity entity); /// /// Get a new change token for the entity /// string Get(string entityId); } }