d25d482dc2
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
73 lines
2.7 KiB
TypeScript
73 lines
2.7 KiB
TypeScript
import { createLogger } from '@sim/logger'
|
|
import { normalizeEmail } from '@sim/utils/string'
|
|
import type { NextRequest } from 'next/server'
|
|
import { NextResponse } from 'next/server'
|
|
import { publicFileSSOContract } from '@/lib/api/contracts/public-shares'
|
|
import { parseRequest } from '@/lib/api/server'
|
|
import type { TokenBucketConfig } from '@/lib/core/rate-limiter'
|
|
import { RateLimiter } from '@/lib/core/rate-limiter'
|
|
import { isEmailAllowed } from '@/lib/core/security/deployment'
|
|
import { generateRequestId, getClientIp } from '@/lib/core/utils/request'
|
|
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
|
|
import { resolveActiveShareByToken } from '@/lib/public-shares/share-manager'
|
|
|
|
export const dynamic = 'force-dynamic'
|
|
export const runtime = 'nodejs'
|
|
|
|
const logger = createLogger('PublicFileSSOAPI')
|
|
|
|
const rateLimiter = new RateLimiter()
|
|
|
|
const SSO_IP_RATE_LIMIT: TokenBucketConfig = {
|
|
maxTokens: 20,
|
|
refillRate: 20,
|
|
refillIntervalMs: 15 * 60_000,
|
|
}
|
|
|
|
/**
|
|
* POST /api/files/public/[token]/sso
|
|
* Reports whether an email is on the allow-list for an SSO-gated share. The actual
|
|
* authentication is the global Sim session (checked at the page/route gate).
|
|
*/
|
|
export const POST = withRouteHandler(
|
|
async (request: NextRequest, context: { params: Promise<{ token: string }> }) => {
|
|
const requestId = generateRequestId()
|
|
|
|
const ip = getClientIp(request)
|
|
const ipRateLimit = await rateLimiter.checkRateLimitDirect(
|
|
`file-sso:ip:${ip}`,
|
|
SSO_IP_RATE_LIMIT
|
|
)
|
|
if (!ipRateLimit.allowed) {
|
|
logger.warn(`[${requestId}] SSO eligibility rate limit exceeded from ${ip}`)
|
|
const response = NextResponse.json(
|
|
{ error: 'Too many requests. Please try again later.' },
|
|
{ status: 429 }
|
|
)
|
|
response.headers.set(
|
|
'Retry-After',
|
|
String(Math.ceil((ipRateLimit.retryAfterMs ?? SSO_IP_RATE_LIMIT.refillIntervalMs) / 1000))
|
|
)
|
|
return response
|
|
}
|
|
|
|
const parsed = await parseRequest(publicFileSSOContract, request, context)
|
|
if (!parsed.success) return parsed.response
|
|
const { token } = parsed.data.params
|
|
const email = normalizeEmail(parsed.data.body.email)
|
|
|
|
const resolved = await resolveActiveShareByToken(token)
|
|
if (!resolved) {
|
|
return NextResponse.json({ error: 'Not found' }, { status: 404 })
|
|
}
|
|
if (resolved.share.authType !== 'sso') {
|
|
return NextResponse.json({ error: 'This file is not configured for SSO' }, { status: 400 })
|
|
}
|
|
|
|
const allowedEmails = Array.isArray(resolved.share.allowedEmails)
|
|
? (resolved.share.allowedEmails as string[])
|
|
: []
|
|
return NextResponse.json({ eligible: isEmailAllowed(email, allowedEmails) })
|
|
}
|
|
)
|