-
Notifications
You must be signed in to change notification settings - Fork 3.8k
feat(sso): several identity providers per organization #7652
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b81c797
feat(sso): several identity providers per organization
waleedlatif1 569c6a6
improvement(sso): verified multi-provider flow and cleaner provider s…
waleedlatif1 b36efc0
fix(sso): make one provider per domain hold at the database and resol…
waleedlatif1 d4e62c2
fix(sso): support registered provider IDs in settings
waleedlatif1 7686476
refactor(sso): keep provider deletion in details
waleedlatif1 52f8b44
chore(db): remove SSO migration before staging update
waleedlatif1 515526a
Merge remote-tracking branch 'origin/staging' into codex/fix-sso-prov…
waleedlatif1 b66b6b0
chore(db): regenerate SSO migration after staging
waleedlatif1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
apps/sim/app/api/auth/sso/providers/[providerId]/route.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { | ||
| createMockRequest, | ||
| dbChainMock, | ||
| dbChainMockFns, | ||
| queueTableRows, | ||
| resetDbChainMock, | ||
| schemaMock, | ||
| } from '@sim/testing' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const { mockGetSession } = vi.hoisted(() => ({ mockGetSession: vi.fn() })) | ||
|
|
||
| vi.mock('@sim/db', () => ({ ...dbChainMock, ...schemaMock })) | ||
| vi.mock('@/lib/auth', () => ({ getSession: mockGetSession })) | ||
|
|
||
| import { DELETE } from '@/app/api/auth/sso/providers/[providerId]/route' | ||
|
|
||
| const context = { params: Promise.resolve({ providerId: 'acme-okta' }) } | ||
| const request = () => createMockRequest('DELETE') | ||
|
|
||
| describe('DELETE /api/auth/sso/providers/[providerId]', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| resetDbChainMock() | ||
| mockGetSession.mockResolvedValue({ user: { id: 'u1' } }) | ||
| dbChainMockFns.returning.mockResolvedValue([{ id: 'row-1' }]) | ||
| }) | ||
|
|
||
| it('requires a session', async () => { | ||
| mockGetSession.mockResolvedValue(null) | ||
| const res = await DELETE(request(), context) | ||
| expect(res.status).toBe(401) | ||
| expect(dbChainMockFns.delete).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('answers 404 for an unknown provider', async () => { | ||
| queueTableRows(schemaMock.ssoProvider, []) | ||
| const res = await DELETE(request(), context) | ||
| expect(res.status).toBe(404) | ||
| expect(dbChainMockFns.delete).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it("refuses an organization provider to a member who is not the organization's admin", async () => { | ||
| queueTableRows(schemaMock.ssoProvider, [ | ||
| { id: 'row-1', organizationId: 'org1', userId: 'u-other', domain: 'acme.com' }, | ||
| ]) | ||
| queueTableRows(schemaMock.member, [{ role: 'member' }]) | ||
| const res = await DELETE(request(), context) | ||
| expect(res.status).toBe(403) | ||
| expect(dbChainMockFns.delete).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('lets an organization admin delete a provider another admin created', async () => { | ||
| queueTableRows(schemaMock.ssoProvider, [ | ||
| { id: 'row-1', organizationId: 'org1', userId: 'u-other', domain: 'acme.com' }, | ||
| ]) | ||
| queueTableRows(schemaMock.member, [{ role: 'admin' }]) | ||
| const res = await DELETE(request(), context) | ||
| expect(res.status).toBe(200) | ||
| await expect(res.json()).resolves.toEqual({ success: true, providerId: 'acme-okta' }) | ||
| expect(dbChainMockFns.delete).toHaveBeenCalledWith(schemaMock.ssoProvider) | ||
| }) | ||
|
|
||
| it('lets only the creator delete a personal provider', async () => { | ||
| queueTableRows(schemaMock.ssoProvider, [ | ||
| { id: 'row-1', organizationId: null, userId: 'u-other', domain: 'acme.com' }, | ||
| ]) | ||
| const refused = await DELETE(request(), context) | ||
| expect(refused.status).toBe(403) | ||
|
|
||
| resetDbChainMock() | ||
| dbChainMockFns.returning.mockResolvedValue([{ id: 'row-1' }]) | ||
| queueTableRows(schemaMock.ssoProvider, [ | ||
| { id: 'row-1', organizationId: null, userId: 'u1', domain: 'acme.com' }, | ||
| ]) | ||
| const allowed = await DELETE(request(), context) | ||
| expect(allowed.status).toBe(200) | ||
| }) | ||
|
|
||
| it.each([129, 256])('deletes a provider with a %i-character ID', async (length) => { | ||
| const providerId = 'a'.repeat(length) | ||
| queueTableRows(schemaMock.ssoProvider, [ | ||
| { id: 'row-1', organizationId: 'org1', userId: 'u1', domain: 'acme.com' }, | ||
| ]) | ||
| queueTableRows(schemaMock.member, [{ role: 'owner' }]) | ||
|
|
||
| const res = await DELETE(request(), { params: Promise.resolve({ providerId }) }) | ||
|
|
||
| expect(res.status).toBe(200) | ||
| await expect(res.json()).resolves.toEqual({ success: true, providerId }) | ||
| expect(dbChainMockFns.delete).toHaveBeenCalledWith(schemaMock.ssoProvider) | ||
| }) | ||
|
|
||
| it('answers 404 when the row vanished between the check and the delete', async () => { | ||
| queueTableRows(schemaMock.ssoProvider, [ | ||
| { id: 'row-1', organizationId: 'org1', userId: 'u1', domain: 'acme.com' }, | ||
| ]) | ||
| queueTableRows(schemaMock.member, [{ role: 'owner' }]) | ||
| dbChainMockFns.returning.mockResolvedValue([]) | ||
| const res = await DELETE(request(), context) | ||
| expect(res.status).toBe(404) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import { db, member, ssoProvider } from '@sim/db' | ||
| import { createLogger } from '@sim/logger' | ||
| import { and, eq, isNull } from 'drizzle-orm' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { deleteSsoProviderContract } from '@/lib/api/contracts/auth' | ||
| import { parseRequest } from '@/lib/api/server' | ||
| import { getSession } from '@/lib/auth' | ||
| import { withRouteHandler } from '@/lib/core/utils/with-route-handler' | ||
|
|
||
| const logger = createLogger('SSOProviderRoute') | ||
|
|
||
| /** | ||
| * Removes one identity provider. | ||
| * | ||
| * Sim owns this rather than exposing the SSO plugin's `delete-provider`, which | ||
| * `/api/auth/[...all]` blocks by design: the plugin gates only on the row's | ||
| * creator, while an organization's providers belong to the organization and | ||
| * are removed by its owners and admins. Accounts and memberships the provider | ||
| * admitted are untouched; only the sign-in path goes. | ||
| */ | ||
| export const DELETE = withRouteHandler( | ||
| async (request: NextRequest, context: { params: Promise<{ providerId: string }> }) => { | ||
| const session = await getSession() | ||
| if (!session?.user?.id) { | ||
| return NextResponse.json({ error: 'Authentication required' }, { status: 401 }) | ||
| } | ||
|
|
||
| const parsed = await parseRequest(deleteSsoProviderContract, request, context) | ||
| if (!parsed.success) return parsed.response | ||
| const { providerId } = parsed.data.params | ||
|
|
||
| const [provider] = await db | ||
| .select({ | ||
| id: ssoProvider.id, | ||
| organizationId: ssoProvider.organizationId, | ||
| userId: ssoProvider.userId, | ||
| domain: ssoProvider.domain, | ||
| }) | ||
| .from(ssoProvider) | ||
| .where(eq(ssoProvider.providerId, providerId)) | ||
| .limit(1) | ||
| if (!provider) return NextResponse.json({ error: 'Provider not found' }, { status: 404 }) | ||
|
|
||
| if (provider.organizationId) { | ||
| const [membership] = await db | ||
| .select({ role: member.role }) | ||
| .from(member) | ||
| .where( | ||
| and( | ||
| eq(member.userId, session.user.id), | ||
| eq(member.organizationId, provider.organizationId) | ||
| ) | ||
| ) | ||
| .limit(1) | ||
| if (!membership || (membership.role !== 'owner' && membership.role !== 'admin')) { | ||
| return NextResponse.json({ error: 'Forbidden' }, { status: 403 }) | ||
| } | ||
| } else if (provider.userId !== session.user.id) { | ||
| return NextResponse.json({ error: 'Forbidden' }, { status: 403 }) | ||
| } | ||
|
|
||
| /** | ||
| * Deleted by primary key under the same ownership the check established, | ||
| * so a concurrent re-registration of the providerId cannot be the row | ||
| * removed. | ||
| */ | ||
| const ownerClause = provider.organizationId | ||
| ? eq(ssoProvider.organizationId, provider.organizationId) | ||
| : and(eq(ssoProvider.userId, session.user.id), isNull(ssoProvider.organizationId)) | ||
| const removed = await db | ||
| .delete(ssoProvider) | ||
| .where(and(eq(ssoProvider.id, provider.id), ownerClause)) | ||
| .returning({ id: ssoProvider.id }) | ||
| if (removed.length === 0) { | ||
| return NextResponse.json({ error: 'Provider not found' }, { status: 404 }) | ||
| } | ||
|
|
||
| logger.info('Deleted SSO provider', { | ||
| providerId, | ||
| organizationId: provider.organizationId, | ||
| domain: provider.domain, | ||
| userId: session.user.id, | ||
| }) | ||
| return NextResponse.json({ success: true, providerId }) | ||
| } | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.