-
Notifications
You must be signed in to change notification settings - Fork 928
fix(integrations): handle Linear OAuth refresh token rotation #4002
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import { db } from "@superset/db/client"; | ||
| import { integrationConnections } from "@superset/db/schema"; | ||
| import { refreshLinearToken } from "@superset/trpc/integrations/linear"; | ||
| import { Receiver } from "@upstash/qstash"; | ||
| import { and, eq, isNotNull, isNull, lt, sql } from "drizzle-orm"; | ||
| import { env } from "@/env"; | ||
|
|
||
| const receiver = new Receiver({ | ||
| currentSigningKey: env.QSTASH_CURRENT_SIGNING_KEY, | ||
| nextSigningKey: env.QSTASH_NEXT_SIGNING_KEY, | ||
| }); | ||
|
|
||
| export async function POST(request: Request) { | ||
| const body = await request.text(); | ||
| const signature = request.headers.get("upstash-signature"); | ||
|
|
||
| const isDev = env.NODE_ENV === "development"; | ||
|
|
||
| if (!isDev) { | ||
| if (!signature) { | ||
| return Response.json({ error: "Missing signature" }, { status: 401 }); | ||
| } | ||
|
|
||
| try { | ||
| const isValid = await receiver.verify({ | ||
| body, | ||
| signature, | ||
| url: `${env.NEXT_PUBLIC_API_URL}/api/integrations/linear/jobs/refresh-tokens`, | ||
| }); | ||
|
|
||
| if (!isValid) { | ||
| return Response.json({ error: "Invalid signature" }, { status: 401 }); | ||
| } | ||
| } catch (verifyError) { | ||
| console.error( | ||
| "[linear-refresh-cron] Signature verification failed:", | ||
| verifyError, | ||
| ); | ||
| return Response.json( | ||
| { error: "Signature verification failed" }, | ||
| { status: 401 }, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| const stale = await db.query.integrationConnections.findMany({ | ||
| where: and( | ||
| eq(integrationConnections.provider, "linear"), | ||
| isNull(integrationConnections.disconnectedAt), | ||
| isNotNull(integrationConnections.refreshToken), | ||
| lt( | ||
| integrationConnections.tokenExpiresAt, | ||
| sql`now() + interval '90 minutes'`, | ||
| ), | ||
| ), | ||
| columns: { id: true }, | ||
| }); | ||
|
|
||
| const results = await Promise.allSettled( | ||
| stale.map(async (connection) => { | ||
| try { | ||
| await refreshLinearToken(connection.id); | ||
| return { id: connection.id, ok: true }; | ||
| } catch (error) { | ||
| console.error( | ||
| `[linear-refresh-cron] failed for ${connection.id}:`, | ||
| error, | ||
| ); | ||
| return { id: connection.id, ok: false }; | ||
| } | ||
| }), | ||
| ); | ||
|
|
||
| const succeeded = results.filter( | ||
| (result) => result.status === "fulfilled" && result.value.ok, | ||
| ).length; | ||
|
|
||
| return Response.json({ candidates: stale.length, succeeded }); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| ALTER TABLE "integration_connections" ADD COLUMN "disconnected_at" timestamp;--> statement-breakpoint | ||
| ALTER TABLE "integration_connections" ADD COLUMN "disconnect_reason" text; | ||
|
Comment on lines
+1
to
+2
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regenerate this migration instead of hand-editing it.
As per coding guidelines, "Create database migrations by modifying schema files in 🤖 Prompt for AI Agents |
||
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.
Cap the cron's refresh fan-out.
Promise.allSettled(stale.map(...))refreshes every candidate at once. Since this route is the primary refresh path, a larger tenant set will stampede Linear's token endpoint and your advisory-lock/DB path on the same tick. Batch or limit concurrency so the hourly run stays predictable.Suggested change
📝 Committable suggestion
🤖 Prompt for AI Agents