Example #1
0
        private async Task <IActionResult> HandleDomainAsync(string domain)
        {
            domain = domain?.Trim();

            if (string.IsNullOrEmpty(domain))
            {
                throw new RequestFailedApiException(RequestFailedApiException.DomainMissing, "The domain is missing");
            }

            domain = domain.ToLower();

            if (!Tenant.IsMatch(domain))
            {
                throw new RequestFailedApiException(RequestFailedApiException.DomainInvalid, "The domain is invalid");
            }

            var(_, tenantInfo) = await _tenantDataProvider.GetTenantByHostAsync($"{domain}.smint.io").ConfigureAwait(false);

            if (tenantInfo == null)
            {
                throw new RequestFailedApiException(RequestFailedApiException.DomainNotFound, "The domain was not found");
            }

            return(Redirect(tenantInfo.WebUrl));
        }
Example #2
0
        public async Task InvokeAsync(HttpContext context)
        {
            if (!context.Items.ContainsKey(TenantConstants.TenantInfoContextKey))
            {
                string      matchedSubDomain = null;
                ITenantInfo tenantInfo       = null;

                var hostString = context.Request.Host;

                string host = null;

                if (hostString.HasValue)
                {
                    host = hostString.Host;

                    (matchedSubDomain, tenantInfo) = await _tenantDataProvider.GetTenantByHostAsync(host, context).ConfigureAwait(false);

                    if (tenantInfo != null && tenantInfo.RequiresDevAdminSsoReplacement)
                    {
                        if (_devAdminSsoReplacementWhitelistIpAddresses != null &&
                            _devAdminSsoReplacementWhitelistIpAddresses.Count > 0)
                        {
                            var ipAddress = context.Connection?.RemoteIpAddress?.ToString();

                            if (_devAdminSsoReplacementWhitelistIpAddresses.Contains(ipAddress))
                            {
                                tenantInfo = tenantInfo.Clone();

                                if (!string.IsNullOrEmpty(tenantInfo.DevAdminSsoReplacementSamlPeerEntityId))
                                {
                                    tenantInfo.SamlPeerEntityId = tenantInfo.DevAdminSsoReplacementSamlPeerEntityId;
                                }

                                if (!string.IsNullOrEmpty(tenantInfo.DevAdminSsoReplacementSamlPeerIdpMetadataLocation))
                                {
                                    tenantInfo.SamlPeerIdpMetadataLocation = tenantInfo.DevAdminSsoReplacementSamlPeerIdpMetadataLocation;
                                    tenantInfo.SamlPeerIdpMetadata         = null;
                                }

                                tenantInfo.AdditionalCacheKey = "devAdminSsoReplacement";
                            }
                        }
                    }
                }

                if (tenantInfo == null)
                {
                    // we could not find any tenant

                    // check if we have a health check running here

                    var healthCheckPort = _tenantDataProvider.HealthCheckPort;

                    if (healthCheckPort != null &&
                        context.Request.Host.Port == healthCheckPort)
                    {
                        var healthCheckTenantHost = _tenantDataProvider.HealthCheckTenantHost;

                        if (!string.IsNullOrEmpty(healthCheckTenantHost))
                        {
                            (matchedSubDomain, tenantInfo) = await _tenantDataProvider.GetTenantByHostAsync(healthCheckTenantHost).ConfigureAwait(false);
                        }
                    }

                    if (tenantInfo == null)
                    {
                        _logger.LogInformation($"No tenant found for host {hostString}");

                        throw new NotFoundApiException(NotFoundApiException.TenantNotFound, $"The tenant for host {host} was not found", host);
                    }
                }

                context.Items.Add(TenantConstants.TenantInfoContextKey, tenantInfo);
                context.Items.Add(TenantConstants.MatchedSubDomainContextKey, matchedSubDomain);
            }

            await _next.Invoke(context).ConfigureAwait(false);
        }