|
| 1 | +import { db, member } from '@sim/db' |
1 | 2 | import { createLogger } from '@sim/logger' |
| 3 | +import { and, eq } from 'drizzle-orm' |
2 | 4 | import { type NextRequest, NextResponse } from 'next/server' |
3 | 5 | import { z } from 'zod' |
4 | 6 | import { auth, getSession } from '@/lib/auth' |
@@ -112,6 +114,17 @@ export async function POST(request: NextRequest) { |
112 | 114 | const body = parseResult.data |
113 | 115 | const { providerId, issuer, domain, providerType, mapping, orgId } = body |
114 | 116 |
|
| 117 | + if (orgId) { |
| 118 | + const [membership] = await db |
| 119 | + .select({ organizationId: member.organizationId }) |
| 120 | + .from(member) |
| 121 | + .where(and(eq(member.userId, session.user.id), eq(member.organizationId, orgId))) |
| 122 | + .limit(1) |
| 123 | + if (!membership) { |
| 124 | + return NextResponse.json({ error: 'Forbidden' }, { status: 403 }) |
| 125 | + } |
| 126 | + } |
| 127 | + |
115 | 128 | const headers: Record<string, string> = {} |
116 | 129 | request.headers.forEach((value, key) => { |
117 | 130 | headers[key] = value |
@@ -349,7 +362,7 @@ export async function POST(request: NextRequest) { |
349 | 362 | }) |
350 | 363 |
|
351 | 364 | const spMetadataXml = `<?xml version="1.0" encoding="UTF-8"?> |
352 | | -<md:EntityDescriptor xmlns:md="urn:oasis:names:tc:SAML:2.0:metadata" entityID="${escapeXml(issuer)}"> |
| 365 | +<md:EntityDescriptor xmlns:md="urn:oasis:names:tc:SAML:2.0:metadata" entityID="${escapeXml(getBaseUrl())}"> |
353 | 366 | <md:SPSSODescriptor AuthnRequestsSigned="false" WantAssertionsSigned="false" protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol"> |
354 | 367 | <md:AssertionConsumerService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" Location="${escapeXml(computedCallbackUrl)}" index="1"/> |
355 | 368 | </md:SPSSODescriptor> |
@@ -436,7 +449,6 @@ export async function POST(request: NextRequest) { |
436 | 449 | { |
437 | 450 | error: 'Failed to register SSO provider', |
438 | 451 | details: error instanceof Error ? error.message : 'Unknown error', |
439 | | - fullError: JSON.stringify(error), |
440 | 452 | }, |
441 | 453 | { status: 500 } |
442 | 454 | ) |
|
0 commit comments