//using FluentValidation.Results; //using Raven.Client.Documents; //using System; //using System.Threading; //using System.Threading.Tasks; //using zero.Core.Entities; //using zero.Core.Validation; //using zero.Core.Extensions; //using System.Security.Claims; //using Microsoft.AspNetCore.Http; //using Microsoft.AspNetCore.Authentication; //using Microsoft.AspNetCore.Authentication.Cookies; //using Raven.Client.Documents.Session; //using Microsoft.AspNetCore.Identity; //namespace zero.Core.Auth //{ // public class ZeroAuth // where TUser : class, IUser, new() // where TRole : class, IUserRole, new() // { // /// // /// Gets the database context for this store. // /// // protected IDocumentStore Raven { get; set; } // /// // /// Configuration // /// // protected IZeroConfiguration Config { get; private set; } // /// // /// Currently logged-in user // /// // protected IUser Current { get; private set; } // protected IHttpContextAccessor HttpContextAccessor { get; set; } // protected SignInManager SignInManager { get; set; } // public ZeroAuth(IDocumentStore raven, IZeroConfiguration config, IUser currentUser, IHttpContextAccessor httpContextAccessor, SignInManager signInManager) // { // Raven = raven; // Config = config; // HttpContextAccessor = httpContextAccessor; // SignInManager = signInManager; // } // /// // /// Tries to log in a user // /// // public async Task Login(string username, string password, bool rememberMe = true) // { // ClaimsPrincipal principal = await ResolveClaimsPrincipalFromLogin(username, password); // if (principal != null && principal.Identity.IsAuthenticated) // { // await SignInManager.SignInAsync() // await HttpContextAccessor.HttpContext.SignInAsync(Constants.Auth.Scheme, principal, new AuthenticationProperties() // { // IsPersistent = rememberMe, // AllowRefresh = true, // ExpiresUtc = DateTime.UtcNow.AddMinutes(30) // }); // } // // error signing in // //if (principal == null || !principal.Identity.IsAuthenticated) // //{ // // return null; // //} // //return await ResolveUserFromClaimsPrincipal(principal); // } // /// // /// Try to create a claims principal from a login // /// // async Task ResolveClaimsPrincipalFromLogin(string email, string password) // { // TUser user = default; // using (IAsyncDocumentSession session = Raven.OpenAsyncSession()) // { // user = await session.Query().FirstOrDefaultAsync(query => query.Email == email); // } // // wrong username // if (user == null) // { // return null; // } // // generate password hash // //string passwordHash = BCrypt.Net.BCrypt.HashPassword(password, user.PasswordSalt); // //// wrong password // //if (!user.PasswordHash.Equals(passwordHash)) // //{ // // return null; // //} // // create claims identity // //ClaimsIdentity identity = new ClaimsIdentity(new List() // //{ // // new Claim(ClaimTypes.Name, user.Username), // // new Claim(ClaimTypeDbId, user.Id) // //}, "SecureLogin"); // //return new ClaimsPrincipal(identity); // } // /// // /// Try to get an associated user for a claims principal // /// // async Task ResolveUserFromClaimsPrincipal(ClaimsPrincipal principal) // { // if (principal == null || !principal.Identity.IsAuthenticated) // { // return null; // } // Claim idClaim = principal.Claims.FirstOrDefault(x => x.Type == ClaimTypeDbId); // if (idClaim == null) // { // return null; // } // return await StoreApi.GetByIdAsync(idClaim.Value); // } // /// // /// Try to get an associated user ID for a claims principal // /// // string ResolveIdFromClaimsPrincipal(ClaimsPrincipal principal) // { // if (principal == null || !principal.Identity.IsAuthenticated) // { // return null; // } // Claim idClaim = principal.Claims.FirstOrDefault(x => x.Type == ClaimTypeDbId); // if (idClaim == null) // { // return null; // } // return idClaim.Value; // } // /// // /// Adds a new backoffice user // /// // public async Task> Add(TUser model, CancellationToken token = default) // { // // set default language // if (model.LanguageId.IsNullOrEmpty()) // { // model.LanguageId = Config.DefaultLanguage; // } // // validate model // ValidationResult validation = await new BackofficeUserValidator(isCreate: true).ValidateAsync(model); // if (!validation.IsValid) // { // return EntityChangeResult.Fail(validation); // } // return EntityChangeResult.Success(); // } // } //}