mirror of
https://github.com/Afilmory/afilmory
synced 2026-04-25 07:15:36 +00:00
@@ -13,7 +13,9 @@ import { SystemSettingService } from 'core/modules/configuration/system-setting/
|
|||||||
import type { Context } from 'hono'
|
import type { Context } from 'hono'
|
||||||
import { injectable } from 'tsyringe'
|
import { injectable } from 'tsyringe'
|
||||||
|
|
||||||
|
import { PLACEHOLDER_TENANT_SLUG } from '../tenant/tenant.constants'
|
||||||
import { TenantService } from '../tenant/tenant.service'
|
import { TenantService } from '../tenant/tenant.service'
|
||||||
|
import { extractTenantSlugFromHost } from '../tenant/tenant-host.utils'
|
||||||
import type { AuthModuleOptions, SocialProviderOptions, SocialProvidersConfig } from './auth.config'
|
import type { AuthModuleOptions, SocialProviderOptions, SocialProvidersConfig } from './auth.config'
|
||||||
import { AuthConfig } from './auth.config'
|
import { AuthConfig } from './auth.config'
|
||||||
|
|
||||||
@@ -320,7 +322,11 @@ export class AuthProvider implements OnModuleInit {
|
|||||||
const endpoint = this.resolveRequestEndpoint()
|
const endpoint = this.resolveRequestEndpoint()
|
||||||
const fallbackHost = options.baseDomain.trim().toLowerCase()
|
const fallbackHost = options.baseDomain.trim().toLowerCase()
|
||||||
const requestedHost = (endpoint.host ?? fallbackHost).trim().toLowerCase()
|
const requestedHost = (endpoint.host ?? fallbackHost).trim().toLowerCase()
|
||||||
const tenantSlug = this.resolveTenantSlugFromContext()
|
const tenantSlugFromContext = this.resolveTenantSlugFromContext()
|
||||||
|
const tenantSlug =
|
||||||
|
tenantSlugFromContext && tenantSlugFromContext !== PLACEHOLDER_TENANT_SLUG
|
||||||
|
? tenantSlugFromContext
|
||||||
|
: (extractTenantSlugFromHost(requestedHost, options.baseDomain) ?? tenantSlugFromContext)
|
||||||
const host = this.applyTenantSlugToHost(requestedHost || fallbackHost, fallbackHost, tenantSlug)
|
const host = this.applyTenantSlugToHost(requestedHost || fallbackHost, fallbackHost, tenantSlug)
|
||||||
const protocol = this.determineProtocol(host, endpoint.protocol)
|
const protocol = this.determineProtocol(host, endpoint.protocol)
|
||||||
const slugKey = tenantSlug ?? 'global'
|
const slugKey = tenantSlug ?? 'global'
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import { injectable } from 'tsyringe'
|
|||||||
import { PLACEHOLDER_TENANT_SLUG, ROOT_TENANT_SLUG } from './tenant.constants'
|
import { PLACEHOLDER_TENANT_SLUG, ROOT_TENANT_SLUG } from './tenant.constants'
|
||||||
import { TenantService } from './tenant.service'
|
import { TenantService } from './tenant.service'
|
||||||
import type { TenantAggregate, TenantContext } from './tenant.types'
|
import type { TenantAggregate, TenantContext } from './tenant.types'
|
||||||
|
import { extractTenantSlugFromHost } from './tenant-host.utils'
|
||||||
|
|
||||||
const HEADER_TENANT_ID = 'x-tenant-id'
|
const HEADER_TENANT_ID = 'x-tenant-id'
|
||||||
const HEADER_TENANT_SLUG = 'x-tenant-slug'
|
const HEADER_TENANT_SLUG = 'x-tenant-slug'
|
||||||
@@ -66,7 +67,7 @@ export class TenantContextResolver {
|
|||||||
|
|
||||||
const baseDomain = await this.getBaseDomain()
|
const baseDomain = await this.getBaseDomain()
|
||||||
|
|
||||||
let derivedSlug = host ? this.extractSlugFromHost(host, baseDomain) : undefined
|
let derivedSlug = host ? (extractTenantSlugFromHost(host, baseDomain) ?? undefined) : undefined
|
||||||
if (!derivedSlug && host && this.isBaseDomainHost(host, baseDomain)) {
|
if (!derivedSlug && host && this.isBaseDomainHost(host, baseDomain)) {
|
||||||
derivedSlug = ROOT_TENANT_SLUG
|
derivedSlug = ROOT_TENANT_SLUG
|
||||||
}
|
}
|
||||||
@@ -232,34 +233,6 @@ export class TenantContextResolver {
|
|||||||
return normalized ? normalized.toLowerCase() : undefined
|
return normalized ? normalized.toLowerCase() : undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
private extractSlugFromHost(host: string, baseDomain: string): string | undefined {
|
|
||||||
const hostname = host.split(':')[0]
|
|
||||||
|
|
||||||
if (!hostname) {
|
|
||||||
return undefined
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hostname.endsWith('.localhost')) {
|
|
||||||
const parts = hostname.split('.localhost')[0]
|
|
||||||
return parts ? parts.trim().toLowerCase() : undefined
|
|
||||||
}
|
|
||||||
|
|
||||||
const normalizedBase = baseDomain.toLowerCase()
|
|
||||||
if (hostname === normalizedBase) {
|
|
||||||
return undefined
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hostname.endsWith(`.${normalizedBase}`)) {
|
|
||||||
const candidate = hostname.slice(0, hostname.length - normalizedBase.length - 1)
|
|
||||||
if (!candidate || candidate.includes('.')) {
|
|
||||||
return undefined
|
|
||||||
}
|
|
||||||
return candidate.toLowerCase()
|
|
||||||
}
|
|
||||||
|
|
||||||
return undefined
|
|
||||||
}
|
|
||||||
|
|
||||||
private shouldFallbackToPlaceholder(tenantId?: string, slug?: string): boolean {
|
private shouldFallbackToPlaceholder(tenantId?: string, slug?: string): boolean {
|
||||||
return !(tenantId && tenantId.length > 0) && !(slug && slug.length > 0)
|
return !(tenantId && tenantId.length > 0) && !(slug && slug.length > 0)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
export function extractTenantSlugFromHost(host: string | null | undefined, baseDomain: string): string | null {
|
||||||
|
if (!host) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
const hostname = host.split(':', 1)[0]
|
||||||
|
if (!hostname) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
const normalizedHostname = hostname.trim().toLowerCase()
|
||||||
|
if (!normalizedHostname) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
if (normalizedHostname.endsWith('.localhost')) {
|
||||||
|
const candidate = normalizedHostname.slice(0, normalizedHostname.length - '.localhost'.length)
|
||||||
|
return candidate ?? null
|
||||||
|
}
|
||||||
|
|
||||||
|
const normalizedBase = baseDomain.trim().toLowerCase()
|
||||||
|
if (!normalizedBase || normalizedHostname === normalizedBase) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
if (normalizedHostname.endsWith(`.${normalizedBase}`)) {
|
||||||
|
const candidate = normalizedHostname.slice(0, normalizedHostname.length - normalizedBase.length - 1)
|
||||||
|
if (!candidate || candidate.includes('.')) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
return candidate
|
||||||
|
}
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user