public async Task GetAuthenticatedCustomer_RequestPath_FetchCustomerBySystemName() { var customer = new Customer() { Email = "*****@*****.**" }; var httpContext = new Mock <HttpContext>(); var req = new Mock <HttpRequest>(); req.Setup(c => c.Path).Returns(new PathString("/api/token/create")); httpContext.Setup(c => c.Request).Returns(req.Object); _httpContextAccessorMoc.Setup(c => c.HttpContext).Returns(httpContext.Object); _customerService.Setup(c => c.GetCustomerBySystemName(It.IsAny <string>())).Returns(() => Task.FromResult(customer)); var result = await _authService.GetAuthenticatedCustomer(); _customerService.Verify(c => c.GetCustomerBySystemName(It.IsAny <string>()), Times.Once); Assert.AreEqual(customer.Email, result.Email); }
public async Task GetAuthenticatedCustomer_NullAuthHeader_GetNull() { var customer = new Customer() { Email = "*****@*****.**" }; var httpContext = new Mock <HttpContext>(); var req = new Mock <HttpRequest>(); req.Setup(c => c.Path).Returns(new PathString("/api/...")); req.Setup(c => c.Headers).Returns(new HeaderDictionary()); httpContext.Setup(c => c.Request).Returns(req.Object); _httpContextAccessorMoc.Setup(c => c.HttpContext).Returns(httpContext.Object); _customerService.Setup(c => c.GetCustomerBySystemName(It.IsAny <string>())).Returns(() => Task.FromResult(customer)); var result = await _authService.GetAuthenticatedCustomer(); Assert.IsNull(result); }
/// <summary> /// Set the current customer by Middleware /// </summary> /// <returns></returns> public virtual async Task <Customer> SetCurrentCustomer() { Customer customer = null; //check whether request is made by a background (schedule) task if (_httpContextAccessor.HttpContext == null) { //in this case return built-in customer record for background task customer = await _customerService.GetCustomerBySystemName(SystemCustomerNames.BackgroundTask); //if customer comes from background task, doesn't need to create cookies if (customer != null) { //cache the found customer _cachedCustomer = customer; return(customer); } } //set customer as a background task if method setted as AllowAnonymous var endpoint = _httpContextAccessor.HttpContext?.GetEndpoint(); if (endpoint?.Metadata?.GetMetadata <IAllowAnonymous>() is object) { customer = await _customerService.GetCustomerBySystemName(SystemCustomerNames.BackgroundTask); } if (customer == null || customer.Deleted || !customer.Active) { //try to get registered user customer = await _authenticationService.GetAuthenticatedCustomer(); } if (customer == null) { //try to get api user customer = await _apiauthenticationService.GetAuthenticatedCustomer(); //if customer comes from api, doesn't need to create cookies if (customer != null) { //cache the found customer _cachedCustomer = customer; return(customer); } } if (customer != null && !customer.Deleted && customer.Active) { //get impersonate user if required var impersonatedCustomerId = customer.GetAttributeFromEntity <string>(SystemCustomerAttributeNames.ImpersonatedCustomerId); if (!string.IsNullOrEmpty(impersonatedCustomerId)) { var impersonatedCustomer = await _customerService.GetCustomerById(impersonatedCustomerId); if (impersonatedCustomer != null && !impersonatedCustomer.Deleted && impersonatedCustomer.Active) { //set impersonated customer _originalCustomerIfImpersonated = customer; customer = impersonatedCustomer; } } } if (customer == null || customer.Deleted || !customer.Active) { //get guest customer var customerguid = await _authenticationService.GetCustomerGuid(); if (!string.IsNullOrEmpty(customerguid)) { if (Guid.TryParse(customerguid, out Guid customerGuid)) { //get customer from guid (should not be registered) var customerByguid = await _customerService.GetCustomerByGuid(customerGuid); if (customerByguid != null && !customerByguid.IsRegistered()) { customer = customerByguid; } } } } if (customer == null || customer.Deleted || !customer.Active) { var crawler = _httpContextAccessor.HttpContext.Request?.Crawler(); //check whether request is made by a search engine, in this case return built-in customer record for search engines if (crawler != null) { customer = await _customerService.GetCustomerBySystemName(SystemCustomerNames.SearchEngine); } } if (customer == null || customer.Deleted || !customer.Active) { //create guest if not exists string referrer = _httpContextAccessor?.HttpContext?.Request?.Headers[HeaderNames.Referer]; customer = await _customerService.InsertGuestCustomer(_storeContext.CurrentStore, referrer); } if (!customer.Deleted && customer.Active) { //set customer cookie await _authenticationService.SetCustomerGuid(customer.CustomerGuid); } //cache the found customer return(_cachedCustomer = customer ?? throw new Exception("No customer could be loaded")); }
/// <summary> /// Set the current customer by Middleware /// </summary> /// <returns></returns> public virtual async Task <Customer> SetCurrentCustomer() { Customer customer = null; //check whether request is made by a background (schedule) task if (_httpContextAccessor.HttpContext == null) { //in this case return built-in customer record for background task customer = await _customerService.GetCustomerBySystemName(SystemCustomerNames.BackgroundTask); } if (customer == null || customer.Deleted || !customer.Active) { //try to get registered user customer = await _authenticationService.GetAuthenticatedCustomer(); } if (customer == null) { //try to get api user customer = await _apiauthenticationService.GetAuthenticatedCustomer(); //if customer comes from api, doesn't need to create cookies if (customer != null) { //cache the found customer _cachedCustomer = customer; return(customer); } } if (customer != null && !customer.Deleted && customer.Active) { //get impersonate user if required var impersonatedCustomerId = customer.GetAttributeFromEntity <string>(SystemCustomerAttributeNames.ImpersonatedCustomerId); if (!string.IsNullOrEmpty(impersonatedCustomerId)) { var impersonatedCustomer = await _customerService.GetCustomerById(impersonatedCustomerId); if (impersonatedCustomer != null && !impersonatedCustomer.Deleted && impersonatedCustomer.Active) { //set impersonated customer _originalCustomerIfImpersonated = customer; customer = impersonatedCustomer; } } } if (customer == null || customer.Deleted || !customer.Active) { //get guest customer var customerCookie = GetCustomerCookie(); if (!string.IsNullOrEmpty(customerCookie)) { if (Guid.TryParse(customerCookie, out Guid customerGuid)) { //get customer from cookie (should not be registered) var customerByCookie = await _customerService.GetCustomerByGuid(customerGuid); if (customerByCookie != null && !customerByCookie.IsRegistered()) { customer = customerByCookie; } } } } if (customer == null || customer.Deleted || !customer.Active) { var crawler = _httpContextAccessor.HttpContext.Request?.Crawler(); //check whether request is made by a search engine, in this case return built-in customer record for search engines if (crawler != null) { customer = await _customerService.GetCustomerBySystemName(SystemCustomerNames.SearchEngine); } // /* * else * { * //check whether request is static resource - assign this customer when resource is not exists (404) * var contentTypeProvider = new FileExtensionContentTypeProvider(); * var content = contentTypeProvider.TryGetContentType(_httpContextAccessor.HttpContext.Request?.Path, out string _); * if (content) * customer = await _customerService.GetCustomerBySystemName(SystemCustomerNames.SearchEngine); * } */ } if (customer == null || customer.Deleted || !customer.Active) { //create guest if not exists string referrer = _httpContextAccessor?.HttpContext?.Request?.Headers[HeaderNames.Referer]; customer = await _customerService.InsertGuestCustomer(_storeContext.CurrentStore, referrer); } if (!customer.Deleted && customer.Active) { //set customer cookie SetCustomerCookie(customer.CustomerGuid); } //cache the found customer return(_cachedCustomer = customer ?? throw new Exception("No customer could be loaded")); }
/// <summary> /// Set the current customer by Middleware /// </summary> /// <returns></returns> public virtual async Task <Customer> SetCurrentCustomer() { Customer customer = null; //check whether request is made by a background (schedule) task if (_httpContextAccessor.HttpContext == null) { //in this case return built-in customer record for background task customer = await _customerService.GetCustomerBySystemName(SystemCustomerNames.BackgroundTask); //if customer comes from background task, doesn't need to create cookies if (customer != null) { //cache the found customer _cachedCustomer = customer; return(customer); } } //set customer as a background task if method setted as AllowAnonymous var endpoint = _httpContextAccessor.HttpContext?.GetEndpoint(); if (endpoint?.Metadata?.GetMetadata <IAllowAnonymous>() is object) { customer = await _customerService.GetCustomerBySystemName(SystemCustomerNames.Anonymous); //if customer comes from Anonymous method, doesn't need to create cookies if (customer != null) { //cache the found customer _cachedCustomer = customer; return(customer); } } if (customer == null || customer.Deleted || !customer.Active) { //try to get registered user customer = await _authenticationService.GetAuthenticatedCustomer(); //if customer is authenticated if (customer != null) { //remove cookie await _authenticationService.SetCustomerGuid(Guid.Empty); //set if use impersonated session var impersonatedCustomer = await SetImpersonatedCustomer(customer); if (impersonatedCustomer != null) { customer = impersonatedCustomer; } //cache the found customer _cachedCustomer = customer; return(customer); } } if (customer == null) { //try to get api user customer = await _apiauthenticationService.GetAuthenticatedCustomer(); //if customer comes from api, doesn't need to create cookies if (customer != null) { //set if use impersonated session var impersonatedCustomer = await SetImpersonatedCustomer(customer); if (impersonatedCustomer != null) { customer = impersonatedCustomer; } //cache the found customer _cachedCustomer = customer; return(customer); } } if (customer == null || customer.Deleted || !customer.Active) { //get guest customer var customerguid = await _authenticationService.GetCustomerGuid(); if (!string.IsNullOrEmpty(customerguid)) { if (Guid.TryParse(customerguid, out Guid customerGuid)) { //get customer from guid (cannot not be registered) var customerByguid = await _customerService.GetCustomerByGuid(customerGuid); if (customerByguid != null && !await _groupService.IsRegistered(customerByguid)) { customer = customerByguid; } } } } if (customer == null || customer.Deleted || !customer.Active) { var isCrawler = _detectionService.Crawler?.IsCrawler; //check whether request is made by a search engine, in this case return built-in customer record for search engines if (isCrawler.HasValue && isCrawler.Value) { customer = await _customerService.GetCustomerBySystemName(SystemCustomerNames.SearchEngine); } } if (customer == null || customer.Deleted || !customer.Active) { //create guest if not exists customer = await _customerService.InsertGuestCustomer(CurrentStore); string referrer = _httpContextAccessor?.HttpContext?.Request?.Headers[HeaderNames.Referer]; if (!string.IsNullOrEmpty(referrer)) { await _userFieldService.SaveField(customer, SystemCustomerFieldNames.UrlReferrer, referrer); } //set customer cookie await _authenticationService.SetCustomerGuid(customer.CustomerGuid); } //cache the found customer return(_cachedCustomer = customer ?? throw new Exception("No customer could be loaded")); }