fix: update oauth

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei
2025-11-12 19:45:37 +08:00
parent a5ff9363af
commit 6768e86327
3 changed files with 44 additions and 30 deletions

View File

@@ -13,7 +13,9 @@ import { SystemSettingService } from 'core/modules/configuration/system-setting/
import type { Context } from 'hono'
import { injectable } from 'tsyringe'
import { PLACEHOLDER_TENANT_SLUG } from '../tenant/tenant.constants'
import { TenantService } from '../tenant/tenant.service'
import { extractTenantSlugFromHost } from '../tenant/tenant-host.utils'
import type { AuthModuleOptions, SocialProviderOptions, SocialProvidersConfig } from './auth.config'
import { AuthConfig } from './auth.config'
@@ -320,7 +322,11 @@ export class AuthProvider implements OnModuleInit {
const endpoint = this.resolveRequestEndpoint()
const fallbackHost = options.baseDomain.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 protocol = this.determineProtocol(host, endpoint.protocol)
const slugKey = tenantSlug ?? 'global'

View File

@@ -11,6 +11,7 @@ import { injectable } from 'tsyringe'
import { PLACEHOLDER_TENANT_SLUG, ROOT_TENANT_SLUG } from './tenant.constants'
import { TenantService } from './tenant.service'
import type { TenantAggregate, TenantContext } from './tenant.types'
import { extractTenantSlugFromHost } from './tenant-host.utils'
const HEADER_TENANT_ID = 'x-tenant-id'
const HEADER_TENANT_SLUG = 'x-tenant-slug'
@@ -66,7 +67,7 @@ export class TenantContextResolver {
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)) {
derivedSlug = ROOT_TENANT_SLUG
}
@@ -232,34 +233,6 @@ export class TenantContextResolver {
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 {
return !(tenantId && tenantId.length > 0) && !(slug && slug.length > 0)
}

View File

@@ -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
}