Repository navigation
feat: integrate Nova web and enhance authentication flow: #322
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 |
|---|---|---|
| @@ -1,8 +1,13 @@ | ||
| import { createAuthClient } from "better-auth/react"; | ||
| import {deviceAuthorizationClient} from 'better-auth/client/plugins' | ||
| import { createAuthClient } from "better-auth/react" | ||
| import { | ||
| deviceAuthorizationClient, | ||
| oneTimeTokenClient, | ||
| } from "better-auth/client/plugins" | ||
|
|
||
| export const authClient = createAuthClient({ | ||
| baseURL: process.env.NEXT_PUBLIC_AUTH_URL || "http://localhost:3004", | ||
| plugins:[ | ||
| deviceAuthorizationClient() | ||
| ] | ||
| }) | ||
| baseURL: process.env.NEXT_PUBLIC_AUTH_URL || "http://localhost:3004", | ||
| plugins: [ | ||
| deviceAuthorizationClient(), | ||
| oneTimeTokenClient(), | ||
| ], | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,24 @@ | ||
|
|
||
| import { betterAuth } from "better-auth" | ||
| import { prismaAdapter } from "better-auth/adapters/prisma" | ||
| import { deviceAuthorization } from "better-auth/plugins" | ||
| import { deviceAuthorization, oneTimeToken } from "better-auth/plugins" | ||
| import prisma from "./prisma" | ||
|
|
||
| const serverUrl = process.env.BETTER_AUTH_URL || "http://localhost:3004" | ||
| const clientUrl = process.env.CLIENT_URL || "http://localhost:3000" | ||
| const isProduction = serverUrl.startsWith("https://") | ||
| const novaOrigins = [ | ||
| "https://nova.supercodeai.tech", | ||
| "http://nova.localhost:3003", | ||
| ] | ||
|
|
||
| export const auth = betterAuth({ | ||
| database: prismaAdapter(prisma, { | ||
| provider: "postgresql", | ||
| }), | ||
| baseURL: serverUrl, | ||
| basePath: "/api/auth", | ||
| trustedOrigins: [clientUrl, serverUrl], | ||
| trustedOrigins: [clientUrl, serverUrl, ...novaOrigins], | ||
| account: { | ||
| skipStateCookieCheck: true, | ||
| }, | ||
|
|
@@ -33,6 +37,10 @@ export const auth = betterAuth({ | |
| }, | ||
| }, | ||
| plugins: [ | ||
| oneTimeToken({ | ||
| expiresIn: 3, | ||
|
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.
|
||
| storeToken: "hashed", | ||
| }), | ||
| deviceAuthorization({ | ||
| schema: {}, | ||
| expiresIn: "10m", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import { Composio } from "@composio/core" | ||
|
|
||
| const READ_ONLY_TOOL_PATTERN = /(^|_)(CHECK|DESCRIBE|DOWNLOAD|FETCH|FIND|GET|LIST|LOOKUP|QUERY|READ|RETRIEVE|SEARCH|VIEW)(_|$)/i | ||
|
|
||
| export class ServerComposioService { | ||
| constructor(private client?: Composio) {} | ||
|
|
||
| private get composio(): Composio { | ||
| if (this.client) return this.client | ||
| const apiKey = process.env.COMPOSIO_API_KEY?.trim() | ||
| if (!apiKey) { | ||
| throw Object.assign(new Error("COMPOSIO_API_KEY is not configured on the CLI server"), { | ||
| statusCode: 503, | ||
| }) | ||
| } | ||
| this.client = new Composio({ apiKey }) | ||
| return this.client | ||
| } | ||
|
|
||
| private async activeAccounts(userId: string) { | ||
| const accounts = [] as Awaited<ReturnType<Composio["connectedAccounts"]["list"]>>["items"] | ||
| let cursor: string | undefined | ||
| do { | ||
| const page = await this.composio.connectedAccounts.list({ | ||
| userIds: [userId], | ||
| statuses: ["ACTIVE"], | ||
| accountType: "PRIVATE", | ||
| cursor, | ||
| limit: 100, | ||
| }) | ||
| accounts.push(...page.items.filter((account) => account.status === "ACTIVE" && !account.isDisabled)) | ||
| cursor = page.nextCursor ?? undefined | ||
| } while (cursor) | ||
| return accounts | ||
| } | ||
|
|
||
| private async authConfigs(toolkit?: string) { | ||
| const configs = [] as Awaited<ReturnType<Composio["authConfigs"]["list"]>>["items"] | ||
| let cursor: string | undefined | ||
| do { | ||
| const page = await this.composio.authConfigs.list({ toolkit, showDisabled: false, cursor, limit: 100 }) | ||
| configs.push(...page.items.filter((config) => config.status === "ENABLED")) | ||
| cursor = page.nextCursor ?? undefined | ||
| } while (cursor) | ||
| return configs | ||
| } | ||
|
|
||
| async listApps(userId: string) { | ||
| const [toolkits, configs, accounts] = await Promise.all([ | ||
| this.composio.toolkits.get(), | ||
| this.authConfigs(), | ||
| this.activeAccounts(userId), | ||
| ]) | ||
| const configured = new Set(configs.map((config) => config.toolkit.slug)) | ||
| const connected = new Map(accounts.map((account) => [account.toolkit.slug, account.id])) | ||
| return toolkits | ||
| .filter((toolkit) => configured.has(toolkit.slug) || connected.has(toolkit.slug)) | ||
| .map((toolkit) => ({ | ||
| slug: toolkit.slug, | ||
| name: toolkit.name, | ||
| description: toolkit.meta?.description ?? "", | ||
| logo: toolkit.meta?.logo ?? null, | ||
| connected: connected.has(toolkit.slug), | ||
| connectedAccountId: connected.get(toolkit.slug) ?? null, | ||
| })) | ||
| .sort((a, b) => a.connected === b.connected | ||
| ? a.name.localeCompare(b.name) | ||
| : a.connected ? -1 : 1) | ||
| } | ||
|
|
||
| async createSession(userId: string) { | ||
| const accounts = await this.activeAccounts(userId) | ||
| const connectedAccounts = Object.fromEntries(accounts.map((account) => [account.toolkit.slug, account.id])) | ||
| const session = await this.composio.sessions.create(userId, { | ||
| mcp: true, | ||
| connectedAccounts, | ||
| toolkits: Object.keys(connectedAccounts), | ||
| manageConnections: false, | ||
| }) | ||
| return { | ||
| url: session.mcp.url, | ||
| headers: session.mcp.headers, | ||
| sessionId: session.sessionId, | ||
|
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.
|
||
| } | ||
| } | ||
|
|
||
| async connect(userId: string, slug: string, callbackUrl: string) { | ||
| const accounts = await this.activeAccounts(userId) | ||
| const existing = accounts.find((account) => account.toolkit.slug === slug) | ||
| if (existing) return { connectedAccountId: existing.id, redirectUrl: null } | ||
|
|
||
| const configs = await this.authConfigs(slug) | ||
| const envId = process.env[`COMPOSIO_${slug.toUpperCase()}_AUTH_CONFIG_ID`]?.trim() | ||
| const config = configs.find((candidate) => candidate.id === envId) | ||
| ?? configs.find((candidate) => candidate.toolkit.slug === slug) | ||
| const configId = config?.id ?? (await this.composio.authConfigs.create(slug, { | ||
| type: "use_composio_managed_auth", | ||
| name: `Supercode ${slug}`, | ||
| })).id | ||
| const connection = await this.composio.connectedAccounts.link(userId, configId, { callbackUrl }) | ||
| if (!connection.redirectUrl) throw new Error("Composio did not return an authorization URL") | ||
| return { connectedAccountId: connection.id, redirectUrl: connection.redirectUrl } | ||
| } | ||
|
|
||
| async verifyConnection(userId: string, slug: string, connectedAccountId: string) { | ||
| const accounts = await this.activeAccounts(userId) | ||
| return accounts.some((account) => account.id === connectedAccountId && account.toolkit.slug === slug) | ||
| } | ||
|
|
||
| async disconnect(userId: string, connectedAccountId: string) { | ||
| const accounts = await this.activeAccounts(userId) | ||
| if (!accounts.some((account) => account.id === connectedAccountId)) { | ||
| throw Object.assign(new Error("Connected account not found"), { statusCode: 404 }) | ||
| } | ||
| await this.composio.connectedAccounts.delete(connectedAccountId) | ||
| } | ||
|
|
||
| async listTools(userId: string) { | ||
| const accounts = await this.activeAccounts(userId) | ||
| const toolkits = [...new Set(accounts.map((account) => account.toolkit.slug))] | ||
| if (toolkits.length === 0) return [] | ||
| const raw = await this.composio.tools.getRawComposioTools({ toolkits }) | ||
| return raw.filter((tool) => !tool.isDeprecated && tool.toolkit && toolkits.includes(tool.toolkit.slug)).map((tool) => ({ | ||
| name: tool.slug, | ||
| displayName: tool.name, | ||
| description: tool.description || tool.name, | ||
| parameters: tool.inputParameters ?? { type: "object", properties: {} }, | ||
| toolkit: tool.toolkit?.slug, | ||
| toolkitName: tool.toolkit?.name, | ||
| requiresApproval: !READ_ONLY_TOOL_PATTERN.test(tool.slug), | ||
| })) | ||
| } | ||
|
|
||
| async executeTool(userId: string, name: string, args: Record<string, unknown>) { | ||
| const tools = await this.composio.tools.getRawComposioTools({ tools: [name] }) | ||
| const tool = tools.find((candidate) => candidate.slug === name && !candidate.isDeprecated) | ||
| if (!tool?.toolkit) throw Object.assign(new Error("Composio tool not found"), { statusCode: 404 }) | ||
| const toolkit = tool.toolkit.slug | ||
| const accounts = await this.activeAccounts(userId) | ||
| const account = accounts.find((candidate) => candidate.toolkit.slug === toolkit) | ||
| if (!account) { | ||
| throw Object.assign(new Error(`Connect ${toolkit} before using this tool`), { statusCode: 403 }) | ||
| } | ||
| const version = process.env[`COMPOSIO_TOOLKIT_VERSION_${toolkit.toUpperCase()}`]?.trim() | ||
| || process.env.COMPOSIO_TOOLKIT_VERSION?.trim() | ||
| || "latest" | ||
| return this.composio.tools.execute(name, { | ||
| userId, | ||
| connectedAccountId: account.id, | ||
| arguments: args, | ||
| version, | ||
| }) | ||
| } | ||
| } | ||
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.
P2 · MEDIUM · Duplicated Nova origin/host allowlists across apps can drift
Nova redirect/origin logic is implemented separately in CLI (
ALLOWED_REDIRECT_ORIGINS) and web (NOVA_HOSTS). If ports/hosts/staging domains change, one side can drift and break redirects or weaken validation. Suggested fix: centralize these values via shared config/env and reuse the same source of truth in both places.