feat(ocm-cli): workspace repo sync and manager token targeting (#233)

This commit is contained in:
Chris Scott authored and GitHub committed 2026-05-23 19:49:44 -04:00
1 parent e12f319592
commit a86e4b1238
69 files changed
+4858 -161

No files matched your search

+29 -4
View File
@@ -3,17 +3,42 @@ import { timingSafeEqual } from 'node:crypto'
import type { Database } from 'bun:sqlite'
import { getOrCreateInternalToken } from '../services/internal-token'
function extractTokenFromBasic(header: string): string | null {
if (!header.startsWith('Basic ')) return null
const decoded = Buffer.from(header.slice(6), 'base64').toString('utf8')
const colonIndex = decoded.indexOf(':')
if (colonIndex === -1) return null
return decoded.slice(colonIndex + 1)
}
function tokenMatch(provided: string, expected: string): boolean {
const a = Buffer.from(provided)
const b = Buffer.from(expected)
return a.length === b.length && timingSafeEqual(a, b)
}
export function createInternalTokenMiddleware(db: Database) {
return createMiddleware(async (c, next) => {
const header = c.req.header('authorization') ?? c.req.header('Authorization')
if (!header || !header.startsWith('Bearer ')) {
if (!header) {
return c.json({ error: 'Unauthorized' }, 401)
}
const provided = Buffer.from(header.slice(7))
const expected = Buffer.from(getOrCreateInternalToken(db))
if (provided.length !== expected.length || !timingSafeEqual(provided, expected)) {
const expected = getOrCreateInternalToken(db)
if (header.startsWith('Bearer ')) {
if (!tokenMatch(header.slice(7), expected)) {
return c.json({ error: 'Unauthorized' }, 401)
}
} else if (header.startsWith('Basic ')) {
const password = extractTokenFromBasic(header)
if (!password || !tokenMatch(password, expected)) {
return c.json({ error: 'Unauthorized' }, 401)
}
} else {
return c.json({ error: 'Unauthorized' }, 401)
}
await next()
})
}