Skip to content

Commit a5ff936

Browse files
committed
feat(auth): enhance tenant context resolution and error logging
- Updated AuthProvider to improve tenant slug resolution by considering requestedSlug alongside tenant slug. - Enhanced error handling in the OAuth callback by logging detailed error information when encountering authentication errors. - Modified TenantContextResolver to include requestedSlug in tenant context creation, ensuring accurate context resolution. - Updated TenantContext interface to include requestedSlug for better clarity in tenant context management. Signed-off-by: Innei <tukon479@gmail.com>
1 parent 715dbf7 commit a5ff936

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

be/apps/core/src/modules/platform/auth/auth.provider.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ export class AuthProvider implements OnModuleInit {
4949

5050
private resolveTenantSlugFromContext(): string | null {
5151
try {
52-
const tenantContext = HttpContext.getValue('tenant') as { tenant?: { slug?: string | null } } | undefined
53-
const slug = tenantContext?.tenant?.slug
52+
const tenantContext = HttpContext.getValue('tenant')
53+
const slug = tenantContext?.requestedSlug ?? tenantContext?.tenant?.slug
5454
return slug ? slug.toLowerCase() : null
5555
} catch {
5656
return null
@@ -362,6 +362,20 @@ export class AuthProvider implements OnModuleInit {
362362
}
363363

364364
async handler(context: Context): Promise<Response> {
365+
const requestPath = typeof context.req.path === 'string' ? context.req.path : new URL(context.req.url).pathname
366+
if (requestPath.startsWith('/api/auth/error')) {
367+
const error = context.req.query('error')
368+
const errorDescription = context.req.query('error_description')
369+
const provider = context.req.query('provider')
370+
const debugParts = [
371+
'[AuthProvider] OAuth callback error encountered.',
372+
error ? `error=${error}` : null,
373+
errorDescription ? `description=${errorDescription}` : null,
374+
provider ? `provider=${provider}` : null,
375+
`url=${context.req.url}`,
376+
].filter(Boolean)
377+
logger.error(debugParts.join(' '))
378+
}
365379
const auth = await this.getAuth()
366380
return auth.handler(context.req.raw)
367381
}

be/apps/core/src/modules/platform/tenant/tenant-context-resolver.service.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export class TenantContextResolver {
7474
derivedSlug = ROOT_TENANT_SLUG
7575
}
7676

77+
const requestedSlug = derivedSlug ?? tenantSlugHeader ?? null
7778
if (!derivedSlug) {
7879
derivedSlug = tenantSlugHeader
7980
}
@@ -91,12 +92,16 @@ export class TenantContextResolver {
9192

9293
if (!tenantContext && this.shouldFallbackToPlaceholder(tenantId, derivedSlug)) {
9394
const placeholder = await this.tenantService.ensurePlaceholderTenant()
94-
tenantContext = this.asTenantContext(placeholder, true)
95+
tenantContext = this.asTenantContext(placeholder, true, requestedSlug)
9596
this.log.verbose(
9697
`Applied placeholder tenant context for ${context.req.method} ${context.req.path} (host=${host ?? 'n/a'})`,
9798
)
9899
} else if (tenantContext) {
99-
tenantContext = this.asTenantContext(tenantContext, tenantContext.tenant.slug === PLACEHOLDER_TENANT_SLUG)
100+
tenantContext = this.asTenantContext(
101+
tenantContext,
102+
tenantContext.tenant.slug === PLACEHOLDER_TENANT_SLUG,
103+
requestedSlug ?? tenantContext.tenant.slug ?? null,
104+
)
100105
}
101106

102107
if (!tenantContext) {
@@ -259,10 +264,15 @@ export class TenantContextResolver {
259264
return !(tenantId && tenantId.length > 0) && !(slug && slug.length > 0)
260265
}
261266

262-
private asTenantContext(source: TenantAggregate, isPlaceholder: boolean): TenantContext {
267+
private asTenantContext(
268+
source: TenantAggregate,
269+
isPlaceholder: boolean,
270+
requestedSlug: string | null,
271+
): TenantContext {
263272
return {
264273
tenant: source.tenant,
265274
isPlaceholder,
275+
requestedSlug,
266276
}
267277
}
268278
}

be/apps/core/src/modules/platform/tenant/tenant.types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface TenantAggregate {
99

1010
export interface TenantContext extends TenantAggregate {
1111
readonly isPlaceholder?: boolean
12+
readonly requestedSlug?: string | null
1213
}
1314

1415
export interface TenantResolutionInput {

0 commit comments

Comments
 (0)