-
Notifications
You must be signed in to change notification settings - Fork 0
Add OAuth 2.1 for Cowork connector support #2
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
Open
sergical
wants to merge
2
commits into
main
Choose a base branch
from
feat/oauth-cowork
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import type { OAuthHelpers, AuthRequest } from "@cloudflare/workers-oauth-provider"; | ||
|
|
||
| export interface AuthEnv { | ||
| OAUTH_PROVIDER: OAuthHelpers; | ||
| OAUTH_KV: KVNamespace; | ||
| GOOGLE_CLIENT_ID: string; | ||
| GOOGLE_CLIENT_SECRET: string; | ||
| } | ||
|
|
||
| /** | ||
| * Handles the OAuth authorization flow using Google SSO. | ||
| * Only @sentry.io emails are allowed. | ||
| */ | ||
| export const authHandler: ExportedHandler<AuthEnv> = { | ||
| async fetch(request: Request, env: AuthEnv): Promise<Response> { | ||
| const url = new URL(request.url); | ||
|
|
||
| if (url.pathname === "/authorize") { | ||
| return handleAuthorize(request, env); | ||
| } | ||
| if (url.pathname === "/callback") { | ||
| return handleCallback(request, env); | ||
| } | ||
|
|
||
| return new Response("Not Found", { status: 404 }); | ||
| }, | ||
| }; | ||
|
|
||
| async function handleAuthorize(request: Request, env: AuthEnv): Promise<Response> { | ||
| // Parse the OAuth authorization request from the MCP client | ||
| const oauthReq = await env.OAUTH_PROVIDER.parseAuthRequest(request); | ||
| if (!oauthReq.clientId) { | ||
| return new Response("Invalid OAuth request", { status: 400 }); | ||
| } | ||
|
|
||
| // Store the OAuth request in KV so we can retrieve it after Google callback | ||
| const stateKey = crypto.randomUUID(); | ||
| await env.OAUTH_KV.put(`auth:${stateKey}`, JSON.stringify(oauthReq), { | ||
| expirationTtl: 600, // 10 minutes | ||
| }); | ||
|
|
||
| // Redirect to Google OAuth | ||
| const googleAuthUrl = new URL("https://accounts.google.com/o/oauth2/v2/auth"); | ||
| googleAuthUrl.searchParams.set("client_id", env.GOOGLE_CLIENT_ID); | ||
| googleAuthUrl.searchParams.set("redirect_uri", `${new URL(request.url).origin}/callback`); | ||
| googleAuthUrl.searchParams.set("response_type", "code"); | ||
| googleAuthUrl.searchParams.set("scope", "openid email profile"); | ||
| googleAuthUrl.searchParams.set("state", stateKey); | ||
| googleAuthUrl.searchParams.set("hd", "sentry.io"); // Restrict to Sentry domain | ||
|
|
||
| return Response.redirect(googleAuthUrl.toString(), 302); | ||
| } | ||
|
|
||
| async function handleCallback(request: Request, env: AuthEnv): Promise<Response> { | ||
| const url = new URL(request.url); | ||
| const code = url.searchParams.get("code"); | ||
| const stateKey = url.searchParams.get("state"); | ||
|
|
||
| if (!code || !stateKey) { | ||
| return new Response("Missing code or state", { status: 400 }); | ||
| } | ||
|
|
||
| // Retrieve the original OAuth request | ||
| const stored = await env.OAUTH_KV.get(`auth:${stateKey}`); | ||
| if (!stored) { | ||
| return new Response("Authorization request expired", { status: 400 }); | ||
| } | ||
| await env.OAUTH_KV.delete(`auth:${stateKey}`); | ||
| const oauthReq: AuthRequest = JSON.parse(stored); | ||
|
|
||
| // Exchange Google auth code for tokens | ||
| const tokenRes = await fetch("https://oauth2.googleapis.com/token", { | ||
| method: "POST", | ||
| headers: { "Content-Type": "application/x-www-form-urlencoded" }, | ||
| body: new URLSearchParams({ | ||
| code, | ||
| client_id: env.GOOGLE_CLIENT_ID, | ||
| client_secret: env.GOOGLE_CLIENT_SECRET, | ||
| redirect_uri: `${url.origin}/callback`, | ||
| grant_type: "authorization_code", | ||
| }), | ||
| }); | ||
|
|
||
| if (!tokenRes.ok) { | ||
| return new Response("Failed to exchange Google auth code", { status: 502 }); | ||
| } | ||
|
|
||
| const tokens = (await tokenRes.json()) as { access_token: string }; | ||
|
|
||
| // Get user info from Google | ||
| const userRes = await fetch("https://www.googleapis.com/oauth2/v2/userinfo", { | ||
| headers: { Authorization: `Bearer ${tokens.access_token}` }, | ||
| }); | ||
|
|
||
| if (!userRes.ok) { | ||
| return new Response("Failed to get user info", { status: 502 }); | ||
| } | ||
|
|
||
| const user = (await userRes.json()) as { | ||
| email: string; | ||
| name: string; | ||
| hd?: string; | ||
| }; | ||
|
|
||
| // Verify @sentry.io email | ||
| if (user.hd !== "sentry.io" || !user.email.endsWith("@sentry.io")) { | ||
| return new Response("Access restricted to @sentry.io accounts", { | ||
| status: 403, | ||
| }); | ||
| } | ||
|
|
||
| // Complete the OAuth authorization — issue our own token | ||
| const { redirectTo } = await env.OAUTH_PROVIDER.completeAuthorization({ | ||
| request: oauthReq, | ||
| userId: user.email, | ||
| metadata: { label: `${user.name} (${user.email})` }, | ||
| scope: oauthReq.scope, | ||
| props: { | ||
| email: user.email, | ||
| name: user.name, | ||
| }, | ||
| }); | ||
|
|
||
| return Response.redirect(redirectTo, 302); | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: The authentication logic incorrectly uses an
||operator, which will deny access to legitimate users with@sentry.ioemails if they use personal Google accounts.Severity: MEDIUM
Suggested Fix
Change the logical operator from
||(OR) to&&(AND) in theifcondition. A simpler and more robust fix would be to remove the check foruser.hdand only validate thatuser.email.endsWith("@sentry.io")is true.Prompt for AI Agent
Did we get this right? 👍 / 👎 to inform future reviews.