Skip to content

Commit bd50eba

Browse files
culdaBogdan Culdathdxr
authored
Adds OPENCONTROL_DISABLE_AUTH (#33)
* adds OPENCONTROL_DISABLE_AUTH * remove ts-ignore * revert d.ts file * Create dirty-spiders-float.md --------- Co-authored-by: Bogdan Culda <culda@Bogdans-Mac-mini.local> Co-authored-by: Dax <mail@thdxr.com>
1 parent 4f7894e commit bd50eba

File tree

5 files changed

+64
-24
lines changed

5 files changed

+64
-24
lines changed

.changeset/dirty-spiders-float.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"opencontrol-frontend": patch
3+
"opencontrol": patch
4+
---
5+
6+
Adds OPENCONTROL_DISABLE_AUTH

packages/frontend/src/client.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,20 @@ export const client = hc<App>(import.meta.env.VITE_OPENCONTROL_ENDPOINT || "", {
77
async fetch(...args: Parameters<typeof fetch>): Promise<Response> {
88
const [input, init] = args
99
const request = input instanceof Request ? input : new Request(input, init)
10-
const headers = new Headers(request.headers)
11-
headers.set("authorization", `Bearer ${password()}`)
12-
return fetch(
13-
new Request(request, {
14-
...init,
15-
headers,
16-
}),
17-
)
10+
11+
// Only add authorization header if password is set
12+
if (password()) {
13+
const headers = new Headers(request.headers)
14+
headers.set("authorization", `Bearer ${password()}`)
15+
return fetch(
16+
new Request(request, {
17+
...init,
18+
headers,
19+
}),
20+
)
21+
}
22+
23+
// Otherwise, just pass through the request without auth
24+
return fetch(request, init)
1825
},
1926
})

packages/frontend/src/index.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,25 @@ if (import.meta.env.DEV && !(root instanceof HTMLElement)) {
1818
render(() => {
1919
const [ready, setReady] = createSignal(false)
2020
onMount(async () => {
21+
// Try to authenticate without password first to check if auth is disabled
22+
try {
23+
const noAuthResult = await fetch(
24+
`${import.meta.env.VITE_OPENCONTROL_ENDPOINT || ""}/auth`,
25+
{
26+
method: "GET",
27+
},
28+
)
29+
30+
// If successful without auth, server has auth disabled
31+
if (noAuthResult.ok) {
32+
setReady(true)
33+
return
34+
}
35+
} catch (e) {
36+
// Continue with password auth if this fails
37+
}
38+
39+
// Regular password authentication flow
2140
setPassword(localStorage.getItem("opencontrol:password"))
2241
while (true) {
2342
if (!password()) {

packages/opencontrol/bin/index.mjs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,25 @@ const server = new Server({
99

1010
const url = process.argv[2]
1111
const key = process.argv[3]
12+
const disableAuth = process.env.OPENCONTROL_DISABLE_AUTH === "true"
1213

1314
class ProxyTransport {
1415
#stdio = new StdioServerTransport()
1516
async start() {
1617
this.#stdio.onmessage = (message) => {
1718
if ("id" in message) {
19+
const headers = {
20+
"Content-Type": "application/json",
21+
}
22+
23+
// Only add authorization header if auth is not disabled and key is provided
24+
if (!disableAuth && key) {
25+
headers.authorization = `Bearer ${key}`
26+
}
27+
1828
fetch(url + "/mcp", {
1929
method: "POST",
20-
headers: {
21-
"Content-Type": "application/json",
22-
authorization: `Bearer ${key}`,
23-
},
30+
headers,
2431
body: JSON.stringify(message),
2532
}).then(async (response) => this.send(await response.json()))
2633
return

packages/opencontrol/src/index.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,19 @@ export interface OpenControlOptions {
1919
password?: string
2020
model?: LanguageModelV1
2121
app?: Hono
22+
disableAuth?: boolean
2223
}
2324

2425
export type App = ReturnType<typeof create>
2526

2627
export function create(input: OpenControlOptions) {
2728
const mcp = createMcp({ tools: input.tools })
28-
const token =
29-
input.password ||
30-
process.env.OPENCONTROL_PASSWORD ||
31-
process.env.OPENCONTROL_KEY ||
32-
"password"
33-
console.log("opencontrol password:", token)
29+
const disableAuth =
30+
input.disableAuth || process.env.OPENCONTROL_DISABLE_AUTH === "true"
31+
const token = input.password || process.env.OPENCONTROL_PASSWORD || "password"
3432
const app = input.app ?? new Hono()
35-
return app
33+
34+
const baseApp = app
3635
.use(
3736
cors({
3837
origin: "*",
@@ -44,11 +43,13 @@ export function create(input: OpenControlOptions) {
4443
.get("/", (c) => {
4544
return c.html(HTML)
4645
})
47-
.use(
48-
bearerAuth({
49-
token,
50-
}),
51-
)
46+
47+
const authMiddleware = disableAuth
48+
? (c: any, next: () => Promise<any>) => next() // No-op middleware when auth is disabled
49+
: bearerAuth({ token })
50+
51+
return baseApp
52+
.use(authMiddleware)
5253
.get("/auth", (c) => {
5354
return c.json({})
5455
})

0 commit comments

Comments
 (0)