Files
bare-operating-system/packages/bare-os-seeder/kernel/bin/oidc-publish
T
2026-04-03 23:21:30 -04:00

151 lines
4.3 KiB
Plaintext

/* BARE_OS_BIN_API 1.0.0 — bump when staged /bin script semantics change (see developer guide). */
/** Shared helpers for drive-resident /bin scripts (prepended before each command). */
function bareStdin(ctx) {
return typeof ctx.shellStdin === 'string' ? ctx.shellStdin : ''
}
/** @param {number} mode @param {'file' | 'directory' | 'symlink'} type */
function bareFormatModeString(mode, type) {
const typeChar = type === 'directory' ? 'd' : type === 'symlink' ? 'l' : '-'
const perm = mode & 0o777
const r = (bit) => (perm & bit ? 'r' : '-')
const w = (bit) => (perm & bit ? 'w' : '-')
const x = (bit) => (perm & bit ? 'x' : '-')
return (
typeChar +
r(0o400) +
w(0o200) +
x(0o100) +
r(0o040) +
w(0o020) +
x(0o010) +
r(0o004) +
w(0o002) +
x(0o001)
)
}
/** @param {number} mtimeMs @param {number} [nowMs] */
function bareFormatLsMtime(mtimeMs, nowMs) {
const now = nowMs != null ? nowMs : Date.now()
const d = new Date(mtimeMs)
const months = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
]
const mon = months[d.getMonth()]
const day = String(d.getDate()).padStart(2, ' ')
const sixMo = 180 * 24 * 3600 * 1000
if (Math.abs(now - mtimeMs) > sixMo) {
const yr = String(d.getFullYear()).padStart(4, ' ')
return mon + ' ' + day + ' ' + yr
}
const hh = String(d.getHours()).padStart(2, '0')
const mm = String(d.getMinutes()).padStart(2, '0')
return mon + ' ' + day + ' ' + hh + ':' + mm
}
/** @param {number} size */
function barePosixBlocks(size) {
return Math.ceil(Number(size) / 512) || 0
}
/**
* Raw stdout for NUL/binary when **`process.stdout.write`** is missing.
* If **`ctx.bareOsBinWrite(Uint8Array|string)`** is set (tests / host), use it.
* @param {Record<string, unknown>} ctx
* @param {string | Uint8Array} chunk
* @returns {boolean}
*/
function bareOsEmitRaw(ctx, chunk) {
if (typeof ctx.bareOsBinWrite === 'function') {
const b4 = ctx.b4a
const u8 =
typeof chunk === 'string'
? b4 && typeof b4.from === 'function'
? b4.from(chunk)
: new TextEncoder().encode(chunk)
: chunk
ctx.bareOsBinWrite(u8 instanceof Uint8Array ? u8 : new Uint8Array(u8))
return true
}
const w = globalThis.process?.stdout?.write
if (typeof w === 'function') {
w.call(globalThis.process.stdout, chunk)
return true
}
return false
}
/**
* Thin helper for OIDC-style token exchange workflows (Holepunch oidc-publishing patterns).
* Uses ctx.httpFetch when present and host HTTP policy allows the issuer URL.
*/
async function run(ctx, argv) {
if (argv[1] === 'help' || argv[1] === '-h' || argv[1] === '--help') {
ctx.console.log(`oidc-publish — optional OIDC token helper
Reads OIDC_ISSUER, OIDC_CLIENT_ID, OIDC_CLIENT_SECRET (or argv) and POSTs
to \${issuer}/oauth/token with grant_type=client_credentials when ctx.httpFetch exists.
Example:
export OIDC_ISSUER=https://issuer.example
oidc-publish
Requires delegated httpFetch and allowlisted host (BARE_OS_HTTP_ALLOWLIST).
`)
ctx.exitCode = 0
return
}
const env = ctx.vfs?.env || {}
const issuer = String(env.OIDC_ISSUER || argv[2] || '').replace(/\/+$/, '')
const cid = String(env.OIDC_CLIENT_ID || argv[3] || '')
const csec = String(env.OIDC_CLIENT_SECRET || argv[4] || '')
if (!issuer || !cid || !csec) {
ctx.console.error(
'oidc-publish: set OIDC_ISSUER, OIDC_CLIENT_ID, OIDC_CLIENT_SECRET'
)
ctx.exitCode = 2
return
}
if (typeof ctx.httpFetch !== 'function') {
ctx.console.error('oidc-publish: ctx.httpFetch not available')
ctx.exitCode = 1
return
}
const url = `${issuer}/oauth/token`
const body =
'grant_type=client_credentials&client_id=' +
encodeURIComponent(cid) +
'&client_secret=' +
encodeURIComponent(csec)
try {
const res = await ctx.httpFetch(url, {
method: 'POST',
headers: { 'content-type': 'application/x-www-form-urlencoded' },
body
})
const text = await res.text()
if (!res.ok) {
ctx.console.error('oidc-publish: HTTP ' + res.status + ' ' + text.slice(0, 200))
ctx.exitCode = 1
return
}
ctx.console.log(text.slice(0, 4000))
ctx.exitCode = 0
} catch (e) {
ctx.console.error('oidc-publish: ' + ((e && e.message) || String(e)))
ctx.exitCode = 1
}
}