mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
feat: add dynamic OAuth prompt system with conditional fields
- Add PromptFieldSchema supporting text and select input types - Support conditional prompt visibility based on previous inputs - Add inputs parameter to OAuth authorize request - Update OAuth dialogs to render dynamic prompts - Add OPENCODE_PUBLIC_URL config for OAuth callback URLs - Remove redundant OAuthMethod enum in favor of backend schema
This commit is contained in:
1 parent
9d94c333b4
commit
2b5e130916
12 files changed
+371
-78
No files matched your search
@@ -1,55 +1,135 @@
|
||||
import { useState } from 'react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog'
|
||||
import { Copy } from 'lucide-react'
|
||||
import { oauthApi, type OAuthAuthorizeResponse } from '@/api/oauth'
|
||||
import { mapOAuthError, OAuthMethod } from '@/lib/oauthErrors'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
|
||||
import { ExternalLink } from 'lucide-react'
|
||||
import { oauthApi, type OAuthAuthorizeResponse, type ProviderAuthMethod } from '@/api/oauth'
|
||||
import { mapOAuthError } from '@/lib/oauthErrors'
|
||||
|
||||
interface OAuthAuthorizeDialogProps {
|
||||
providerId: string
|
||||
providerName: string
|
||||
methods: ProviderAuthMethod[]
|
||||
open: boolean
|
||||
onOpenChange: (open: boolean) => void
|
||||
onSuccess: (response: OAuthAuthorizeResponse) => void
|
||||
onSuccess: (response: OAuthAuthorizeResponse, methodIndex: number) => void
|
||||
}
|
||||
|
||||
type OAuthPrompt = NonNullable<ProviderAuthMethod['prompts']>[number]
|
||||
|
||||
function isBrowserLocalMethod(method: ProviderAuthMethod): boolean {
|
||||
return method.label.toLowerCase().includes('browser')
|
||||
}
|
||||
|
||||
function getVisiblePrompts(method: ProviderAuthMethod, inputs: Record<string, string>): OAuthPrompt[] {
|
||||
const visiblePrompts: OAuthPrompt[] = []
|
||||
const activeInputs: Record<string, string> = {}
|
||||
|
||||
for (const prompt of method.prompts ?? []) {
|
||||
const isVisible = !('when' in prompt) || !prompt.when || activeInputs[prompt.when.key] === prompt.when.value
|
||||
|
||||
if (!isVisible) {
|
||||
continue
|
||||
}
|
||||
|
||||
visiblePrompts.push(prompt)
|
||||
|
||||
if (inputs[prompt.key]) {
|
||||
activeInputs[prompt.key] = inputs[prompt.key]
|
||||
}
|
||||
}
|
||||
|
||||
return visiblePrompts
|
||||
}
|
||||
|
||||
function hasPrompts(method: ProviderAuthMethod): boolean {
|
||||
return (method.prompts?.length ?? 0) > 0
|
||||
}
|
||||
|
||||
export function OAuthAuthorizeDialog({
|
||||
providerId,
|
||||
providerName,
|
||||
providerName,
|
||||
methods,
|
||||
open,
|
||||
onOpenChange,
|
||||
onSuccess
|
||||
}: OAuthAuthorizeDialogProps) {
|
||||
const [isLoading, setIsLoading] = useState(false)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [selectedMethodIndex, setSelectedMethodIndex] = useState<number | null>(null)
|
||||
const [promptInputs, setPromptInputs] = useState<Record<number, Record<string, string>>>({})
|
||||
|
||||
const getMethodInputs = (methodIndex: number) => promptInputs[methodIndex] || {}
|
||||
|
||||
const handleAuthorize = async (methodIndex: number) => {
|
||||
const method = methods[methodIndex]
|
||||
const methodInputs = getMethodInputs(methodIndex)
|
||||
const visiblePrompts = getVisiblePrompts(method, methodInputs)
|
||||
const missingPrompt = visiblePrompts.some((prompt) => !methodInputs[prompt.key]?.trim())
|
||||
|
||||
if (missingPrompt) {
|
||||
setError('Please complete all authentication fields')
|
||||
setSelectedMethodIndex(methodIndex)
|
||||
return
|
||||
}
|
||||
|
||||
setIsLoading(true)
|
||||
setError(null)
|
||||
setSelectedMethodIndex(methodIndex)
|
||||
|
||||
try {
|
||||
const response = await oauthApi.authorize(providerId, methodIndex)
|
||||
onSuccess(response)
|
||||
const inputs = visiblePrompts.length > 0
|
||||
? Object.fromEntries(
|
||||
visiblePrompts.map((prompt) => [prompt.key, methodInputs[prompt.key].trim()])
|
||||
)
|
||||
: undefined
|
||||
const response = await oauthApi.authorize(providerId, methodIndex, inputs)
|
||||
onSuccess(response, methodIndex)
|
||||
} catch (err) {
|
||||
setError(mapOAuthError(err, 'authorize'))
|
||||
console.error('OAuth authorize error:', err)
|
||||
} finally {
|
||||
setIsLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handleMethodSelection = (methodIndex: number) => {
|
||||
setError(null)
|
||||
setSelectedMethodIndex(methodIndex)
|
||||
|
||||
if (!hasPrompts(methods[methodIndex])) {
|
||||
void handleAuthorize(methodIndex)
|
||||
}
|
||||
}
|
||||
|
||||
const handlePromptChange = (methodIndex: number, key: string, value: string) => {
|
||||
setPromptInputs((prev) => ({
|
||||
...prev,
|
||||
[methodIndex]: {
|
||||
...prev[methodIndex],
|
||||
[key]: value,
|
||||
},
|
||||
}))
|
||||
}
|
||||
|
||||
const handleClose = () => {
|
||||
setError(null)
|
||||
setPromptInputs({})
|
||||
setSelectedMethodIndex(null)
|
||||
onOpenChange(false)
|
||||
}
|
||||
|
||||
const oauthMethods = methods.flatMap((method, index) => method.type === 'oauth' ? [{ method, index }] : [])
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={handleClose}>
|
||||
<DialogContent className="bg-card border-border max-w-lg">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Connect to {providerName}</DialogTitle>
|
||||
<DialogDescription>
|
||||
Connect your {providerName} account using authorization code.
|
||||
Select an authentication method to connect your {providerName} account.
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
@@ -59,20 +139,92 @@ export function OAuthAuthorizeDialog({
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="space-y-3">
|
||||
<Button
|
||||
onClick={() => handleAuthorize(OAuthMethod.CODE)}
|
||||
disabled={isLoading}
|
||||
className="w-full justify-start"
|
||||
variant="outline"
|
||||
>
|
||||
<Copy className="h-4 w-4 mr-2" />
|
||||
{isLoading ? 'Authorizing...' : 'Use Authorization Code'}
|
||||
</Button>
|
||||
<div className="space-y-4">
|
||||
{oauthMethods.map(({ method, index }) => {
|
||||
const isBrowserLocal = isBrowserLocalMethod(method)
|
||||
const methodInputs = getMethodInputs(index)
|
||||
const visiblePrompts = getVisiblePrompts(method, methodInputs)
|
||||
const canSubmitPrompts = visiblePrompts.every((prompt) => methodInputs[prompt.key]?.trim())
|
||||
|
||||
return (
|
||||
<div key={index} className="space-y-3">
|
||||
<div className="flex items-center gap-2">
|
||||
<Button
|
||||
onClick={() => handleMethodSelection(index)}
|
||||
disabled={isLoading}
|
||||
className="flex-1 justify-start"
|
||||
variant={selectedMethodIndex === index ? 'default' : 'outline'}
|
||||
>
|
||||
<ExternalLink className="h-4 w-4 mr-2" />
|
||||
{isLoading && selectedMethodIndex === index ? 'Authorizing...' : method.label}
|
||||
</Button>
|
||||
{isBrowserLocal && (
|
||||
<Badge variant="secondary" className="text-xs">
|
||||
Localhost only
|
||||
</Badge>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{selectedMethodIndex === index && hasPrompts(method) && (
|
||||
<div className="space-y-3 pl-10 pr-1 py-2">
|
||||
{visiblePrompts.map((prompt) => (
|
||||
prompt.type === 'text' ? (
|
||||
<div key={prompt.key} className="space-y-2">
|
||||
<Label htmlFor={prompt.key}>{prompt.message}</Label>
|
||||
<Input
|
||||
id={prompt.key}
|
||||
value={methodInputs[prompt.key] || ''}
|
||||
onChange={(e) => handlePromptChange(index, prompt.key, e.target.value)}
|
||||
placeholder={prompt.placeholder}
|
||||
className="bg-background border-border"
|
||||
disabled={isLoading}
|
||||
/>
|
||||
</div>
|
||||
) : (
|
||||
<div key={prompt.key} className="space-y-2">
|
||||
<Label>{prompt.message}</Label>
|
||||
<Select
|
||||
value={methodInputs[prompt.key] || ''}
|
||||
onValueChange={(value) => handlePromptChange(index, prompt.key, value)}
|
||||
disabled={isLoading}
|
||||
>
|
||||
<SelectTrigger className="bg-background border-border">
|
||||
<SelectValue placeholder="Select an option" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{prompt.options.map((option) => (
|
||||
<SelectItem key={option.value} value={option.value}>
|
||||
{option.label}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
)
|
||||
))}
|
||||
|
||||
<Button
|
||||
onClick={() => void handleAuthorize(index)}
|
||||
disabled={isLoading || !canSubmitPrompts}
|
||||
className="w-full"
|
||||
>
|
||||
{isLoading && selectedMethodIndex === index ? 'Authorizing...' : 'Continue'}
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{isBrowserLocal && (
|
||||
<p className="text-xs text-muted-foreground pl-1">
|
||||
This method relies on a callback server started by OpenCode and may not work when OCM is remote.
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
|
||||
<div className="text-xs text-muted-foreground">
|
||||
<p>• "Use Authorization Code" will give you a code to manually enter</p>
|
||||
<p>• Some methods may require completing authorization in your browser</p>
|
||||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
|
||||
@@ -5,12 +5,13 @@ import { Label } from '@/components/ui/label'
|
||||
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog'
|
||||
import { Loader2, ExternalLink, CheckCircle } from 'lucide-react'
|
||||
import { oauthApi, type OAuthAuthorizeResponse } from '@/api/oauth'
|
||||
import { mapOAuthError, OAuthMethod } from '@/lib/oauthErrors'
|
||||
import { mapOAuthError } from '@/lib/oauthErrors'
|
||||
|
||||
interface OAuthCallbackDialogProps {
|
||||
providerId: string
|
||||
providerName: string
|
||||
authResponse: OAuthAuthorizeResponse
|
||||
methodIndex: number
|
||||
open: boolean
|
||||
onOpenChange: (open: boolean) => void
|
||||
onSuccess: () => void
|
||||
@@ -20,6 +21,7 @@ export function OAuthCallbackDialog({
|
||||
providerId,
|
||||
providerName,
|
||||
authResponse,
|
||||
methodIndex,
|
||||
open,
|
||||
onOpenChange,
|
||||
onSuccess
|
||||
@@ -29,19 +31,19 @@ export function OAuthCallbackDialog({
|
||||
const [authCode, setAuthCode] = useState('')
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
|
||||
const handleCodeCallback = async () => {
|
||||
if (!authCode.trim()) {
|
||||
setError('Please enter the authorization code')
|
||||
return
|
||||
}
|
||||
|
||||
const handleCallback = async () => {
|
||||
setIsLoading(true)
|
||||
setLoadingMessage('Completing authentication...')
|
||||
setError(null)
|
||||
|
||||
try {
|
||||
setLoadingMessage('Restarting server with new credentials...')
|
||||
await oauthApi.callback(providerId, { method: OAuthMethod.CODE, code: authCode.trim() })
|
||||
await oauthApi.callback(
|
||||
providerId,
|
||||
authResponse.method === 'code'
|
||||
? { method: methodIndex, code: authCode.trim() }
|
||||
: { method: methodIndex }
|
||||
)
|
||||
onSuccess()
|
||||
} catch (err) {
|
||||
setError(mapOAuthError(err, 'callback'))
|
||||
@@ -62,13 +64,18 @@ export function OAuthCallbackDialog({
|
||||
onOpenChange(false)
|
||||
}
|
||||
|
||||
const isAutoMethod = authResponse.method === 'auto'
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={handleClose}>
|
||||
<DialogContent className="bg-card border-border max-w-lg">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Complete {providerName} Authentication</DialogTitle>
|
||||
<DialogDescription>
|
||||
Enter the authorization code from the provider.
|
||||
{isAutoMethod
|
||||
? 'Follow the instructions below to complete authentication.'
|
||||
: 'Enter the authorization code from the provider.'
|
||||
}
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
@@ -93,22 +100,24 @@ export function OAuthCallbackDialog({
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="authCode">Authorization Code</Label>
|
||||
<Input
|
||||
id="authCode"
|
||||
value={authCode}
|
||||
onChange={(e) => setAuthCode(e.target.value)}
|
||||
placeholder="Enter the authorization code..."
|
||||
className="bg-background border-border"
|
||||
disabled={isLoading}
|
||||
/>
|
||||
</div>
|
||||
{!isAutoMethod && (
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="authCode">Authorization Code</Label>
|
||||
<Input
|
||||
id="authCode"
|
||||
value={authCode}
|
||||
onChange={(e) => setAuthCode(e.target.value)}
|
||||
placeholder="Enter the authorization code..."
|
||||
className="bg-background border-border"
|
||||
disabled={isLoading}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<Button
|
||||
onClick={handleCodeCallback}
|
||||
onClick={handleCallback}
|
||||
className="w-full"
|
||||
disabled={isLoading || !authCode.trim()}
|
||||
disabled={isLoading || (!isAutoMethod && !authCode.trim())}
|
||||
>
|
||||
{isLoading ? (
|
||||
<>
|
||||
|
||||
@@ -19,6 +19,7 @@ export function ProviderSettings() {
|
||||
const [oauthDialogOpen, setOauthDialogOpen] = useState(false)
|
||||
const [oauthCallbackDialogOpen, setOauthCallbackDialogOpen] = useState(false)
|
||||
const [oauthResponse, setOauthResponse] = useState<OAuthAuthorizeResponse | null>(null)
|
||||
const [oauthMethodIndex, setOauthMethodIndex] = useState<number | null>(null)
|
||||
const [connectedExpanded, setConnectedExpanded] = useState(false)
|
||||
const [availableExpanded, setAvailableExpanded] = useState(false)
|
||||
const [availableSearch, setAvailableSearch] = useState('')
|
||||
@@ -68,14 +69,16 @@ export function ProviderSettings() {
|
||||
setDeleteTarget(null)
|
||||
}
|
||||
|
||||
const handleOAuthAuthorize = (response: OAuthAuthorizeResponse) => {
|
||||
const handleOAuthAuthorize = (response: OAuthAuthorizeResponse, methodIndex: number) => {
|
||||
setOauthResponse(response)
|
||||
setOauthMethodIndex(methodIndex)
|
||||
setOauthDialogOpen(false)
|
||||
setOauthCallbackDialogOpen(true)
|
||||
}
|
||||
|
||||
const handleOAuthDialogClose = () => {
|
||||
setOauthDialogOpen(false)
|
||||
setOauthMethodIndex(null)
|
||||
setSelectedProvider(null)
|
||||
}
|
||||
|
||||
@@ -83,6 +86,7 @@ export function ProviderSettings() {
|
||||
invalidateProviderCaches(queryClient)
|
||||
setOauthCallbackDialogOpen(false)
|
||||
setOauthResponse(null)
|
||||
setOauthMethodIndex(null)
|
||||
setSelectedProvider(null)
|
||||
}
|
||||
|
||||
@@ -239,17 +243,19 @@ export function ProviderSettings() {
|
||||
<OAuthAuthorizeDialog
|
||||
providerId={selectedProvider}
|
||||
providerName={selectedProviderName}
|
||||
methods={authMethods?.[selectedProvider] || []}
|
||||
open={oauthDialogOpen}
|
||||
onOpenChange={handleOAuthDialogClose}
|
||||
onSuccess={handleOAuthAuthorize}
|
||||
/>
|
||||
)}
|
||||
|
||||
{selectedProvider && oauthResponse && (
|
||||
{selectedProvider && oauthResponse && oauthMethodIndex !== null && (
|
||||
<OAuthCallbackDialog
|
||||
providerId={selectedProvider}
|
||||
providerName={selectedProviderName}
|
||||
authResponse={oauthResponse}
|
||||
methodIndex={oauthMethodIndex}
|
||||
open={oauthCallbackDialogOpen}
|
||||
onOpenChange={setOauthCallbackDialogOpen}
|
||||
onSuccess={handleOAuthSuccess}
|
||||
|
||||
Reference in new issue
Block a user