From 6768e86327549a61bd38b3d22f0988454c3ea6e2 Mon Sep 17 00:00:00 2001 From: Innei Date: Wed, 12 Nov 2025 19:45:37 +0800 Subject: [PATCH] fix: update oauth Signed-off-by: Innei --- .../modules/platform/auth/auth.provider.ts | 8 ++++- .../tenant/tenant-context-resolver.service.ts | 31 ++-------------- .../platform/tenant/tenant-host.utils.ts | 35 +++++++++++++++++++ 3 files changed, 44 insertions(+), 30 deletions(-) create mode 100644 be/apps/core/src/modules/platform/tenant/tenant-host.utils.ts diff --git a/be/apps/core/src/modules/platform/auth/auth.provider.ts b/be/apps/core/src/modules/platform/auth/auth.provider.ts index 02c2b438..ef59e365 100644 --- a/be/apps/core/src/modules/platform/auth/auth.provider.ts +++ b/be/apps/core/src/modules/platform/auth/auth.provider.ts @@ -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' diff --git a/be/apps/core/src/modules/platform/tenant/tenant-context-resolver.service.ts b/be/apps/core/src/modules/platform/tenant/tenant-context-resolver.service.ts index 04b10fd3..fe3522ff 100644 --- a/be/apps/core/src/modules/platform/tenant/tenant-context-resolver.service.ts +++ b/be/apps/core/src/modules/platform/tenant/tenant-context-resolver.service.ts @@ -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) } diff --git a/be/apps/core/src/modules/platform/tenant/tenant-host.utils.ts b/be/apps/core/src/modules/platform/tenant/tenant-host.utils.ts new file mode 100644 index 00000000..70bc6e03 --- /dev/null +++ b/be/apps/core/src/modules/platform/tenant/tenant-host.utils.ts @@ -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 +}