Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto substandard.blog
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091// Guards threadbare's production OAuth client metadata. The authorization// server refuses a loopback redirect_uri on a web client ("Loopback redirect// URIs are only allowed for native apps"), which takes sign-in down for// everyone. Development gets its loopback client from src/auth.ts instead, so// the deployed metadata must carry only the production https callback.//// Usage:// node scripts/check-threadbare-oauth-metadata.mjs//// Plain node, no dependencies. The pure check is exported for the tests.
import { readFileSync } from 'node:fs'import { resolve } from 'node:path'
export const CLIENT_ID = 'https://threadbare.substandard.blog/client-metadata.json'const CALLBACK_PATH = '/oauth/callback'const LOOPBACK_HOSTS = new Set(['127.0.0.1', '[::1]', 'localhost'])
/** Problems with the production metadata, one line each. */export function checkMetadata(metadata) { const problems = [] if (metadata.client_id !== CLIENT_ID) { problems.push( `client_id is ${JSON.stringify(metadata.client_id)}, expected ${CLIENT_ID}`, ) }
const redirects = Array.isArray(metadata.redirect_uris) ? metadata.redirect_uris : [] if (redirects.length === 0) { problems.push('redirect_uris is empty') } for (const uri of redirects) { let url try { url = new URL(uri) } catch { problems.push(`redirect_uri ${JSON.stringify(uri)} is not a URL`) continue } if (url.protocol !== 'https:') { problems.push(`redirect_uri ${uri} is not https`) } else if (LOOPBACK_HOSTS.has(url.hostname)) { problems.push(`redirect_uri ${uri} is a loopback address`) } } if ( !redirects.some((uri) => { try { const url = new URL(uri) return url.protocol === 'https:' && url.pathname.endsWith(CALLBACK_PATH) } catch { return false } }) ) { problems.push(`redirect_uris has no https callback ending ${CALLBACK_PATH}`) }
const scopes = String(metadata.scope ?? '') .split(/\s+/) .filter(Boolean) if (!scopes.includes('atproto')) { problems.push(`scope ${JSON.stringify(metadata.scope)} does not include atproto`) } return problems}
function main() { const path = resolve( import.meta.dirname, '../threadbare/public/client-metadata.json', ) let metadata try { metadata = JSON.parse(readFileSync(path, 'utf8')) } catch (error) { console.error(`check-threadbare-oauth-metadata: FAILED — cannot read ${path}: ${error.message}`) process.exit(1) }
const problems = checkMetadata(metadata) if (problems.length > 0) { console.error(`check-threadbare-oauth-metadata: FAILED (${problems.length} problem(s)):`) for (const problem of problems) console.error(` - ${problem}`) process.exit(1) } console.log('check-threadbare-oauth-metadata: OK — production web client has no loopback redirect')}
if (process.argv[1] === import.meta.filename) main()