Add OpenShell Control auth and installer

This commit is contained in:
Codex
2026-04-24 20:52:44 +00:00
parent 8e3b5332d4
commit ad8e25c056
27 changed files with 2057 additions and 298 deletions
+24
View File
@@ -0,0 +1,24 @@
import { NextRequest, NextResponse } from "next/server"
import { createSessionCookieValue, getAuthSettings, sessionCookieOptions, verifyPassword } from "@/app/lib/controlAuth"
export async function POST(request: NextRequest) {
const settings = getAuthSettings()
if (!settings.configured) {
return NextResponse.json({
ok: false,
error: "Dashboard auth is not configured. Set OPENSHELL_CONTROL_PASSWORD and restart the dashboard.",
}, { status: 503 })
}
const body = await request.json().catch(() => ({}))
const password = typeof body?.password === "string" ? body.password : ""
const nextPath = typeof body?.next === "string" && body.next.startsWith("/") ? body.next : "/"
if (!await verifyPassword(password)) {
return NextResponse.json({ ok: false, error: "Invalid password" }, { status: 401 })
}
const response = NextResponse.json({ ok: true, next: nextPath })
response.cookies.set(settings.cookieName, await createSessionCookieValue(), sessionCookieOptions)
return response
}
+15
View File
@@ -0,0 +1,15 @@
import { NextResponse } from "next/server"
import { getAuthSettings } from "@/app/lib/controlAuth"
export async function POST() {
const settings = getAuthSettings()
const response = NextResponse.json({ ok: true })
response.cookies.set(settings.cookieName, "", {
httpOnly: true,
sameSite: "lax",
secure: process.env.NODE_ENV === "production",
path: "/",
maxAge: 0,
})
return response
}
+27
View File
@@ -0,0 +1,27 @@
import { NextRequest, NextResponse } from "next/server"
import { createSessionCookieValue, getAuthSettings, sessionCookieOptions, verifyRecoveryToken } from "@/app/lib/controlAuth"
import { updateLocalAuthCredentials } from "@/app/lib/controlAuthConfig"
export async function POST(request: NextRequest) {
const body = await request.json().catch(() => ({}))
const token = typeof body?.recoveryToken === "string" ? body.recoveryToken : ""
const password = typeof body?.password === "string" ? body.password : ""
if (password.length < 8) {
return NextResponse.json({ ok: false, error: "Password must be at least 8 characters." }, { status: 400 })
}
if (!await verifyRecoveryToken(token)) {
return NextResponse.json({ ok: false, error: "Invalid recovery token." }, { status: 401 })
}
const result = await updateLocalAuthCredentials(password)
const settings = getAuthSettings()
const response = NextResponse.json({
ok: true,
recoveryToken: result.recoveryToken,
note: "Password reset. Save the new recovery token from .env.local.",
})
response.cookies.set(settings.cookieName, await createSessionCookieValue(), sessionCookieOptions)
return response
}
+44
View File
@@ -0,0 +1,44 @@
import { NextRequest, NextResponse } from "next/server"
import {
createSessionCookieValue,
getAuthSettings,
sessionCookieOptions,
verifyPassword,
verifyRecoveryToken,
verifySessionCookieValue,
} from "@/app/lib/controlAuth"
import { updateLocalAuthCredentials } from "@/app/lib/controlAuthConfig"
function validPassword(password: string) {
return password.length >= 8
}
export async function POST(request: NextRequest) {
const settings = getAuthSettings()
const body = await request.json().catch(() => ({}))
const password = typeof body?.password === "string" ? body.password : ""
const currentPassword = typeof body?.currentPassword === "string" ? body.currentPassword : ""
const recoveryToken = typeof body?.recoveryToken === "string" ? body.recoveryToken : ""
if (!validPassword(password)) {
return NextResponse.json({ ok: false, error: "Password must be at least 8 characters." }, { status: 400 })
}
const signedIn = await verifySessionCookieValue(request.cookies.get(settings.cookieName)?.value)
const currentPasswordOk = currentPassword ? await verifyPassword(currentPassword) : false
const recoveryOk = recoveryToken ? await verifyRecoveryToken(recoveryToken) : false
const firstRun = !settings.configured
if (!firstRun && !signedIn && !currentPasswordOk && !recoveryOk) {
return NextResponse.json({ ok: false, error: "Current password or recovery token required." }, { status: 401 })
}
const result = await updateLocalAuthCredentials(password)
const response = NextResponse.json({
ok: true,
recoveryToken: result.recoveryToken,
note: "Password updated. Save the new recovery token from .env.local.",
})
response.cookies.set(settings.cookieName, await createSessionCookieValue(), sessionCookieOptions)
return response
}
@@ -0,0 +1,34 @@
import { NextResponse } from "next/server"
import { downloadSandboxFile } from "@/app/lib/sandboxFiles"
function contentDisposition(fileName: string) {
const fallback = fileName.replace(/[^\w.-]/g, "_") || "download.bin"
return `attachment; filename="${fallback}"; filename*=UTF-8''${encodeURIComponent(fileName)}`
}
export async function GET(
request: Request,
{ params }: { params: Promise<{ sandboxId: string }> }
) {
try {
const { sandboxId } = await params
const requestUrl = new URL(request.url)
const sourcePath = requestUrl.searchParams.get("path") || ""
const downloaded = await downloadSandboxFile(sandboxId, sourcePath)
return new NextResponse(new Uint8Array(downloaded.bytes), {
status: 200,
headers: {
"cache-control": "no-store",
"content-disposition": contentDisposition(downloaded.fileName),
"content-length": String(downloaded.bytes.byteLength),
"content-type": "application/octet-stream",
"x-sandbox-name": downloaded.sandboxName,
"x-sandbox-path": downloaded.path,
},
})
} catch (error) {
const message = error instanceof Error ? error.message : "Failed to download file"
return NextResponse.json({ ok: false, error: message }, { status: /required|path|large|exist/.test(message) ? 400 : 500 })
}
}
@@ -0,0 +1,29 @@
import { NextResponse } from "next/server"
import { uploadSandboxFile } from "@/app/lib/sandboxFiles"
export async function POST(
request: Request,
{ params }: { params: Promise<{ sandboxId: string }> }
) {
try {
const { sandboxId } = await params
const form = await request.formData()
const file = form.get("file")
const rawPath = form.get("path")
if (!(file instanceof File)) throw new Error("file is required")
const destinationPath = typeof rawPath === "string" && rawPath.trim()
? rawPath.trim()
: `/sandbox/${file.name}`
const payload = Buffer.from(await file.arrayBuffer())
const uploaded = await uploadSandboxFile(sandboxId, destinationPath, file.name, payload)
return NextResponse.json({
ok: true,
uploaded,
note: `Uploaded ${file.name} to ${uploaded.path}.`,
})
} catch (error) {
const message = error instanceof Error ? error.message : "Failed to upload file"
return NextResponse.json({ ok: false, error: message }, { status: /required|path|large|exist/.test(message) ? 400 : 500 })
}
}
@@ -0,0 +1,41 @@
import { NextResponse } from "next/server"
import { getSandboxPermissionFeed, resolveSandboxNetworkRule } from "@/app/lib/sandboxPermissions"
export async function GET(
_request: Request,
{ params }: { params: Promise<{ sandboxId: string }> }
) {
try {
const { sandboxId } = await params
const feed = await getSandboxPermissionFeed(sandboxId)
return NextResponse.json({
ok: true,
feed,
})
} catch (error) {
const message = error instanceof Error ? error.message : "Failed to read permission feed"
return NextResponse.json({ ok: false, error: message }, { status: 500 })
}
}
export async function POST(
request: Request,
{ params }: { params: Promise<{ sandboxId: string }> }
) {
try {
const { sandboxId } = await params
const body = await request.json()
const action = typeof body?.action === "string" ? body.action : ""
const chunkId = typeof body?.chunkId === "string" ? body.chunkId : ""
const reason = typeof body?.reason === "string" ? body.reason : ""
const result = await resolveSandboxNetworkRule(sandboxId, action, chunkId, reason)
return NextResponse.json({
ok: true,
result,
note: `Network rule ${result.action === "approve" ? "approved" : "rejected"} for ${result.sandboxName}.`,
})
} catch (error) {
const message = error instanceof Error ? error.message : "Failed to resolve permission request"
return NextResponse.json({ ok: false, error: message }, { status: /unsupported|invalid/.test(message) ? 400 : 500 })
}
}