mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
feat(backend,frontend): add Web UI for configuring OpenCode server Basic Auth password
Allow administrators to set and clear the managed OpenCode server password from the Settings UI without requiring container or environment variable changes. Passwords are encrypted before database persistence and never returned to the frontend. The server manager resolves the password dynamically at restart time, falling back to the env var if no stored password exists. - Add system_settings table migration (v12) - Add SettingsService methods for encrypted password storage - Add GET/PATCH /api/settings/opencode-server-auth routes - Make OpenCodeServerManager resolve password dynamically from DB - Add frontend API methods, React Query hook, and auth settings component
This commit is contained in:
1 parent
efae3b86c1
commit
954bc60ddb
13 files changed
+731
-20
No files matched your search
@@ -11,6 +11,9 @@ import type {
|
||||
CreateSkillRequest,
|
||||
UpdateSkillRequest,
|
||||
SkillScope,
|
||||
OpenCodeServerAuthSettings,
|
||||
UpdateOpenCodeServerAuthSettings,
|
||||
OpenCodeServerAuthSettingsResponse,
|
||||
} from './types/settings'
|
||||
import { API_BASE_URL } from '@/config'
|
||||
import { fetchWrapper, FetchError } from './fetchWrapper'
|
||||
@@ -269,6 +272,18 @@ export const settingsApi = {
|
||||
method: 'DELETE',
|
||||
})
|
||||
},
|
||||
|
||||
getOpenCodeServerAuthSettings: async (): Promise<OpenCodeServerAuthSettings> => {
|
||||
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-server-auth`)
|
||||
},
|
||||
|
||||
updateOpenCodeServerAuthSettings: async (request: UpdateOpenCodeServerAuthSettings): Promise<OpenCodeServerAuthSettingsResponse> => {
|
||||
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-server-auth`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(request),
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
export interface VersionInfo {
|
||||
|
||||
@@ -132,3 +132,19 @@ export interface SyncOpenCodeImportResponse extends OpenCodeImportStatus {
|
||||
errors: Array<{ path: string; error: string }>
|
||||
}
|
||||
}
|
||||
|
||||
export interface OpenCodeServerAuthSettings {
|
||||
username: string
|
||||
hasPassword: boolean
|
||||
source: 'configured' | 'env' | 'none'
|
||||
}
|
||||
|
||||
export interface UpdateOpenCodeServerAuthSettings {
|
||||
password?: string
|
||||
clearPassword?: boolean
|
||||
}
|
||||
|
||||
export interface OpenCodeServerAuthSettingsResponse extends OpenCodeServerAuthSettings {
|
||||
serverRestarted: boolean
|
||||
restartError?: string
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
import { useState } from 'react'
|
||||
import { useOpenCodeServerAuthSettings } from '@/hooks/useOpenCodeServerAuthSettings'
|
||||
import { showToast } from '@/lib/toast'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Loader2, Key } from 'lucide-react'
|
||||
|
||||
export function OpenCodeServerAuthSettings() {
|
||||
const { settings, isLoading, updateAuthSettingsAsync, isUpdating } = useOpenCodeServerAuthSettings()
|
||||
const [password, setPassword] = useState('')
|
||||
|
||||
const handleSave = async () => {
|
||||
if (!password.trim()) {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await updateAuthSettingsAsync({ password: password.trim() })
|
||||
setPassword('')
|
||||
|
||||
if (result.serverRestarted) {
|
||||
showToast.success('Password updated and OpenCode server restarted')
|
||||
} else if (result.restartError) {
|
||||
showToast.success('Password updated (restart failed: ' + result.restartError + ')')
|
||||
} else {
|
||||
showToast.success('Password updated')
|
||||
}
|
||||
} catch {
|
||||
showToast.error('Failed to update password')
|
||||
}
|
||||
}
|
||||
|
||||
const handleClear = async () => {
|
||||
try {
|
||||
const result = await updateAuthSettingsAsync({ clearPassword: true })
|
||||
|
||||
if (result.serverRestarted) {
|
||||
showToast.success('Password cleared and OpenCode server restarted')
|
||||
} else if (result.restartError) {
|
||||
showToast.success('Password cleared (restart failed: ' + result.restartError + ')')
|
||||
} else {
|
||||
showToast.success('Password cleared')
|
||||
}
|
||||
} catch {
|
||||
showToast.error('Failed to clear password')
|
||||
}
|
||||
}
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="flex items-center justify-center py-12">
|
||||
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const getPasswordStatusText = () => {
|
||||
if (!settings) return 'Loading...'
|
||||
|
||||
switch (settings.source) {
|
||||
case 'configured':
|
||||
return 'Password configured in Web UI'
|
||||
case 'env':
|
||||
return 'Using environment password'
|
||||
case 'none':
|
||||
return 'No password configured'
|
||||
}
|
||||
}
|
||||
|
||||
const showClearButton = settings?.source === 'configured'
|
||||
|
||||
return (
|
||||
<div className="bg-card border border-border rounded-lg p-6">
|
||||
<h2 className="text-lg font-semibold text-foreground mb-6 flex items-center gap-2">
|
||||
<Key className="h-5 w-5" />
|
||||
OpenCode Server Authentication
|
||||
</h2>
|
||||
|
||||
<div className="space-y-4">
|
||||
<div className="space-y-2">
|
||||
<Label>Username</Label>
|
||||
<div className="text-sm text-muted-foreground">
|
||||
{settings?.username || 'Loading...'}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label>Password Status</Label>
|
||||
<div className="text-sm text-muted-foreground">
|
||||
{getPasswordStatusText()}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="password">New Password</Label>
|
||||
<Input
|
||||
id="password"
|
||||
type="password"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
placeholder="Leave blank to keep current password"
|
||||
autoComplete="off"
|
||||
/>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Set a new password for the OpenCode server Basic Auth
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div className="flex gap-2">
|
||||
<Button
|
||||
onClick={handleSave}
|
||||
disabled={!password.trim() || isUpdating}
|
||||
>
|
||||
{isUpdating && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
||||
Save Password
|
||||
</Button>
|
||||
|
||||
{showClearButton && (
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={handleClear}
|
||||
disabled={isUpdating}
|
||||
>
|
||||
{isUpdating && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
||||
Clear Web UI Password
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import { GeneralSettings } from '@/components/settings/GeneralSettings'
|
||||
import { GitSettings } from '@/components/settings/GitSettings'
|
||||
import { KeyboardShortcuts } from '@/components/settings/KeyboardShortcuts'
|
||||
import { OpenCodeConfigManager } from '@/components/settings/OpenCodeConfigManager'
|
||||
import { OpenCodeServerAuthSettings } from '@/components/settings/OpenCodeServerAuthSettings'
|
||||
import { ProviderSettings } from '@/components/settings/ProviderSettings'
|
||||
import { AccountSettings } from '@/components/settings/AccountSettings'
|
||||
import { VoiceSettings } from '@/components/settings/VoiceSettings'
|
||||
@@ -118,7 +119,12 @@ export function SettingsDialog() {
|
||||
<TabsContent key="voice" value="voice" className="mt-0"><VoiceSettings /></TabsContent>
|
||||
<TabsContent key="git" value="git" className="mt-0"><GitSettings /></TabsContent>
|
||||
<TabsContent key="shortcuts" value="shortcuts" className="mt-0"><KeyboardShortcuts /></TabsContent>
|
||||
<TabsContent key="opencode" value="opencode" className="mt-0"><OpenCodeConfigManager /></TabsContent>
|
||||
<TabsContent key="opencode" value="opencode" className="mt-0">
|
||||
<div className="space-y-6">
|
||||
<OpenCodeServerAuthSettings />
|
||||
<OpenCodeConfigManager />
|
||||
</div>
|
||||
</TabsContent>
|
||||
<TabsContent key="providers" value="providers" className="mt-0"><ProviderSettings /></TabsContent>
|
||||
</div>
|
||||
</div>
|
||||
@@ -184,7 +190,12 @@ export function SettingsDialog() {
|
||||
{mobileView === 'voice' && <div key="voice"><VoiceSettings /></div>}
|
||||
{mobileView === 'git' && <div key="git"><GitSettings /></div>}
|
||||
{mobileView === 'shortcuts' && <div key="shortcuts"><KeyboardShortcuts /></div>}
|
||||
{mobileView === 'opencode' && <div key="opencode"><OpenCodeConfigManager /></div>}
|
||||
{mobileView === 'opencode' && (
|
||||
<div key="opencode" className="space-y-6">
|
||||
<OpenCodeServerAuthSettings />
|
||||
<OpenCodeConfigManager />
|
||||
</div>
|
||||
)}
|
||||
{mobileView === 'providers' && <div key="providers"><ProviderSettings /></div>}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { settingsApi } from '@/api/settings'
|
||||
import type { OpenCodeServerAuthSettings, UpdateOpenCodeServerAuthSettings } from '@/api/types/settings'
|
||||
|
||||
export function useOpenCodeServerAuthSettings() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
const { data: settings, isLoading, error } = useQuery<OpenCodeServerAuthSettings>({
|
||||
queryKey: ['settings', 'opencode-server-auth'],
|
||||
queryFn: () => settingsApi.getOpenCodeServerAuthSettings(),
|
||||
})
|
||||
|
||||
const mutation = useMutation({
|
||||
mutationFn: (request: UpdateOpenCodeServerAuthSettings) =>
|
||||
settingsApi.updateOpenCodeServerAuthSettings(request),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['settings', 'opencode-server-auth'] })
|
||||
},
|
||||
})
|
||||
|
||||
return {
|
||||
settings,
|
||||
isLoading,
|
||||
error,
|
||||
updateAuthSettingsAsync: mutation.mutateAsync,
|
||||
isUpdating: mutation.isPending,
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user