-
Notifications
You must be signed in to change notification settings - Fork 234
feat(web): [EE] OAuth2 authorization server with MCP support #977
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
15 commits
Select commit
Hold shift + click to select a range
871ee1b
wip on oauth2 support over mcp
brendan-kellam 0851626
tests
brendan-kellam f8e9355
wip - move things into EE and also improve authorization page
brendan-kellam a40f7fc
chore: update CHANGELOG for #977
brendan-kellam 642f244
better changelog
brendan-kellam fa561e9
claude rules
brendan-kellam 3d2ee1b
improve error messaging
brendan-kellam de8d4d5
feedback: handle case where a authorization code has already been con…
brendan-kellam 9f5a4d5
feedback
brendan-kellam 633f0e0
RFC 8707 - resource indicator
brendan-kellam 1cd5bc5
include service_documentation in .well-known/oauth-authorization route
brendan-kellam 502472e
implement refresh tokens
brendan-kellam 8ba9bb2
feedback
brendan-kellam 76c68af
posthog events
brendan-kellam 7fed80a
feedback
brendan-kellam 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
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
47 changes: 47 additions & 0 deletions
47
packages/db/prisma/migrations/20260304051539_add_oauth_tables/migration.sql
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,47 @@ | ||
| -- CreateTable | ||
| CREATE TABLE "OAuthClient" ( | ||
| "id" TEXT NOT NULL, | ||
| "name" TEXT NOT NULL, | ||
| "redirectUris" TEXT[], | ||
| "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
|
|
||
| CONSTRAINT "OAuthClient_pkey" PRIMARY KEY ("id") | ||
| ); | ||
|
|
||
| -- CreateTable | ||
| CREATE TABLE "OAuthAuthorizationCode" ( | ||
| "codeHash" TEXT NOT NULL, | ||
| "clientId" TEXT NOT NULL, | ||
| "userId" TEXT NOT NULL, | ||
| "redirectUri" TEXT NOT NULL, | ||
| "codeChallenge" TEXT NOT NULL, | ||
| "expiresAt" TIMESTAMP(3) NOT NULL, | ||
| "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
|
|
||
| CONSTRAINT "OAuthAuthorizationCode_pkey" PRIMARY KEY ("codeHash") | ||
| ); | ||
|
|
||
| -- CreateTable | ||
| CREATE TABLE "OAuthToken" ( | ||
| "hash" TEXT NOT NULL, | ||
| "clientId" TEXT NOT NULL, | ||
| "userId" TEXT NOT NULL, | ||
| "scope" TEXT NOT NULL DEFAULT '', | ||
| "expiresAt" TIMESTAMP(3) NOT NULL, | ||
| "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| "lastUsedAt" TIMESTAMP(3), | ||
|
|
||
| CONSTRAINT "OAuthToken_pkey" PRIMARY KEY ("hash") | ||
| ); | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "OAuthAuthorizationCode" ADD CONSTRAINT "OAuthAuthorizationCode_clientId_fkey" FOREIGN KEY ("clientId") REFERENCES "OAuthClient"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "OAuthAuthorizationCode" ADD CONSTRAINT "OAuthAuthorizationCode_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "OAuthToken" ADD CONSTRAINT "OAuthToken_clientId_fkey" FOREIGN KEY ("clientId") REFERENCES "OAuthClient"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "OAuthToken" ADD CONSTRAINT "OAuthToken_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; |
2 changes: 2 additions & 0 deletions
2
packages/db/prisma/migrations/20260304212808_add_oauth_client_logo_uri/migration.sql
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,2 @@ | ||
| -- AlterTable | ||
| ALTER TABLE "OAuthClient" ADD COLUMN "logoUri" TEXT; |
5 changes: 5 additions & 0 deletions
5
packages/db/prisma/migrations/20260304230353_add_oauth_resource_parameter/migration.sql
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,5 @@ | ||
| -- AlterTable | ||
| ALTER TABLE "OAuthAuthorizationCode" ADD COLUMN "resource" TEXT; | ||
|
|
||
| -- AlterTable | ||
| ALTER TABLE "OAuthToken" ADD COLUMN "resource" TEXT; |
21 changes: 21 additions & 0 deletions
21
packages/db/prisma/migrations/20260304233124_add_oauth_refresh_tokens/migration.sql
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,21 @@ | ||
| -- CreateTable | ||
| CREATE TABLE "OAuthRefreshToken" ( | ||
| "hash" TEXT NOT NULL, | ||
| "clientId" TEXT NOT NULL, | ||
| "userId" TEXT NOT NULL, | ||
| "scope" TEXT NOT NULL DEFAULT '', | ||
| "resource" TEXT, | ||
| "expiresAt" TIMESTAMP(3) NOT NULL, | ||
| "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
|
|
||
| CONSTRAINT "OAuthRefreshToken_pkey" PRIMARY KEY ("hash") | ||
| ); | ||
|
|
||
| -- CreateIndex | ||
| CREATE INDEX "OAuthRefreshToken_clientId_userId_idx" ON "OAuthRefreshToken"("clientId", "userId"); | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "OAuthRefreshToken" ADD CONSTRAINT "OAuthRefreshToken_clientId_fkey" FOREIGN KEY ("clientId") REFERENCES "OAuthClient"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "OAuthRefreshToken" ADD CONSTRAINT "OAuthRefreshToken_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; |
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
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
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
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
23 changes: 23 additions & 0 deletions
23
packages/web/src/app/api/(server)/ee/.well-known/oauth-authorization-server/route.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,23 @@ | ||
| import { apiHandler } from '@/lib/apiHandler'; | ||
| import { env } from '@sourcebot/shared'; | ||
|
|
||
| // RFC 8414: OAuth 2.0 Authorization Server Metadata | ||
| // @note: we do not gate on entitlements here. That is handled in the /register, | ||
| // /token, and /revoke routes. | ||
| // @see: https://datatracker.ietf.org/doc/html/rfc8414 | ||
| export const GET = apiHandler(async () => { | ||
| const issuer = env.AUTH_URL.replace(/\/$/, ''); | ||
|
|
||
| return Response.json({ | ||
| issuer, | ||
| authorization_endpoint: `${issuer}/oauth/authorize`, | ||
| token_endpoint: `${issuer}/api/ee/oauth/token`, | ||
| registration_endpoint: `${issuer}/api/ee/oauth/register`, | ||
| revocation_endpoint: `${issuer}/api/ee/oauth/revoke`, | ||
| response_types_supported: ['code'], | ||
| grant_types_supported: ['authorization_code', 'refresh_token'], | ||
| code_challenge_methods_supported: ['S256'], | ||
| token_endpoint_auth_methods_supported: ['none'], | ||
| service_documentation: 'https://docs.sourcebot.dev', | ||
| }); | ||
| }); |
33 changes: 33 additions & 0 deletions
33
packages/web/src/app/api/(server)/ee/.well-known/oauth-protected-resource/[...path]/route.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,33 @@ | ||
| import { apiHandler } from '@/lib/apiHandler'; | ||
| import { env } from '@sourcebot/shared'; | ||
| import { NextRequest } from 'next/server'; | ||
|
|
||
| // RFC 9728: OAuth 2.0 Protected Resource Metadata (path-specific form) | ||
| // For a resource at /api/mcp, the well-known URI is /.well-known/oauth-protected-resource/api/mcp. | ||
| // @note: we do not gate on entitlements here. That is handled in the /register, | ||
| // /token, and /revoke routes. | ||
brendan-kellam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // @see: https://datatracker.ietf.org/doc/html/rfc9728#section-3 | ||
| const PROTECTED_RESOURCES = new Set([ | ||
| 'api/mcp' | ||
| ]); | ||
|
|
||
| export const GET = apiHandler(async (_request: NextRequest, { params }: { params: Promise<{ path: string[] }> }) => { | ||
| const { path } = await params; | ||
| const resourcePath = path.join('/'); | ||
|
|
||
| if (!PROTECTED_RESOURCES.has(resourcePath)) { | ||
| return Response.json( | ||
| { error: 'not_found', error_description: `No protected resource metadata found for path: ${resourcePath}` }, | ||
| { status: 404 } | ||
| ); | ||
| } | ||
|
|
||
| const issuer = env.AUTH_URL.replace(/\/$/, ''); | ||
|
|
||
| return Response.json({ | ||
| resource: `${issuer}/${resourcePath}`, | ||
| authorization_servers: [ | ||
| issuer | ||
| ], | ||
| }); | ||
| }); | ||
16 changes: 16 additions & 0 deletions
16
packages/web/src/app/api/(server)/ee/.well-known/oauth-protected-resource/route.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,16 @@ | ||
| import { apiHandler } from '@/lib/apiHandler'; | ||
| import { env } from '@sourcebot/shared'; | ||
|
|
||
| // RFC 9728: OAuth 2.0 Protected Resource Metadata | ||
| // Tells OAuth clients which authorization server protects this resource. | ||
| // @see: https://datatracker.ietf.org/doc/html/rfc9728 | ||
| export const GET = apiHandler(async () => { | ||
| const issuer = env.AUTH_URL.replace(/\/$/, ''); | ||
|
|
||
| return Response.json({ | ||
| resource: `${issuer}/api/mcp`, | ||
| authorization_servers: [ | ||
| issuer | ||
| ], | ||
| }); | ||
| }, { track: false }); |
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.