|
| 1 | +import { Hono } from 'hono' |
| 2 | +import { z } from 'zod' |
| 3 | +import { proxyRequest } from '../services/proxy' |
| 4 | +import { logger } from '../utils/logger' |
| 5 | +import { ENV } from '@opencode-webui/shared' |
| 6 | +import { |
| 7 | + OAuthAuthorizeRequestSchema, |
| 8 | + OAuthAuthorizeResponseSchema, |
| 9 | + OAuthCallbackRequestSchema |
| 10 | +} from '../../../shared/src/schemas/auth' |
| 11 | + |
| 12 | +const OPENCODE_SERVER_URL = `http://${ENV.OPENCODE.HOST}:${ENV.OPENCODE.PORT}` |
| 13 | + |
| 14 | +export function createOAuthRoutes() { |
| 15 | + const app = new Hono() |
| 16 | + |
| 17 | + app.post('/:id/oauth/authorize', async (c) => { |
| 18 | + try { |
| 19 | + const providerId = c.req.param('id') |
| 20 | + const body = await c.req.json() |
| 21 | + const validated = OAuthAuthorizeRequestSchema.parse(body) |
| 22 | + |
| 23 | + // Proxy to OpenCode server |
| 24 | + const response = await proxyRequest( |
| 25 | + new Request( |
| 26 | + `${OPENCODE_SERVER_URL}/provider/${providerId}/oauth/authorize`, |
| 27 | + { |
| 28 | + method: 'POST', |
| 29 | + headers: { 'Content-Type': 'application/json' }, |
| 30 | + body: JSON.stringify(validated) |
| 31 | + } |
| 32 | + ) |
| 33 | + ) |
| 34 | + |
| 35 | + if (!response.ok) { |
| 36 | + const error = await response.text() |
| 37 | + logger.error(`OAuth authorize failed for ${providerId}:`, error) |
| 38 | + return c.json({ error: 'OAuth authorization failed' }, 500) |
| 39 | + } |
| 40 | + |
| 41 | + const data = await response.json() |
| 42 | + const validatedResponse = OAuthAuthorizeResponseSchema.parse(data) |
| 43 | + |
| 44 | + return c.json(validatedResponse) |
| 45 | + } catch (error) { |
| 46 | + logger.error('OAuth authorize error:', error) |
| 47 | + if (error instanceof z.ZodError) { |
| 48 | + return c.json({ error: 'Invalid request data', details: error.issues }, 400) |
| 49 | + } |
| 50 | + return c.json({ error: 'OAuth authorization failed' }, 500) |
| 51 | + } |
| 52 | + }) |
| 53 | + |
| 54 | + app.post('/:id/oauth/callback', async (c) => { |
| 55 | + try { |
| 56 | + const providerId = c.req.param('id') |
| 57 | + const body = await c.req.json() |
| 58 | + const validated = OAuthCallbackRequestSchema.parse(body) |
| 59 | + |
| 60 | + // Proxy to OpenCode server |
| 61 | + const response = await proxyRequest( |
| 62 | + new Request( |
| 63 | + `${OPENCODE_SERVER_URL}/provider/${providerId}/oauth/callback`, |
| 64 | + { |
| 65 | + method: 'POST', |
| 66 | + headers: { 'Content-Type': 'application/json' }, |
| 67 | + body: JSON.stringify(validated) |
| 68 | + } |
| 69 | + ) |
| 70 | + ) |
| 71 | + |
| 72 | + if (!response.ok) { |
| 73 | + const error = await response.text() |
| 74 | + logger.error(`OAuth callback failed for ${providerId}:`, error) |
| 75 | + return c.json({ error: 'OAuth callback failed' }, 500) |
| 76 | + } |
| 77 | + |
| 78 | + const data = await response.json() |
| 79 | + |
| 80 | + return c.json(data) |
| 81 | + } catch (error) { |
| 82 | + logger.error('OAuth callback error:', error) |
| 83 | + if (error instanceof z.ZodError) { |
| 84 | + return c.json({ error: 'Invalid request data', details: error.issues }, 400) |
| 85 | + } |
| 86 | + return c.json({ error: 'OAuth callback failed' }, 500) |
| 87 | + } |
| 88 | + }) |
| 89 | + |
| 90 | + app.get('/auth-methods', async (c) => { |
| 91 | + try { |
| 92 | + // Proxy to OpenCode server |
| 93 | + const response = await proxyRequest( |
| 94 | + new Request(`${OPENCODE_SERVER_URL}/provider/auth`, { |
| 95 | + method: 'GET', |
| 96 | + headers: { 'Content-Type': 'application/json' } |
| 97 | + }) |
| 98 | + ) |
| 99 | + |
| 100 | + if (!response.ok) { |
| 101 | + const error = await response.text() |
| 102 | + logger.error('Failed to get provider auth methods:', error) |
| 103 | + return c.json({ error: 'Failed to get provider auth methods' }, 500) |
| 104 | + } |
| 105 | + |
| 106 | + const data = await response.json() |
| 107 | + |
| 108 | + // The OpenCode server returns the format we need directly |
| 109 | + return c.json({ providers: data }) |
| 110 | + } catch (error) { |
| 111 | + logger.error('Provider auth methods error:', error) |
| 112 | + if (error instanceof z.ZodError) { |
| 113 | + return c.json({ error: 'Invalid response data', details: error.issues }, 500) |
| 114 | + } |
| 115 | + return c.json({ error: 'Failed to get provider auth methods' }, 500) |
| 116 | + } |
| 117 | + }) |
| 118 | + |
| 119 | + return app |
| 120 | +} |
0 commit comments