@@ -225,17 +225,75 @@ else
225225 [ -f " $( user_record_path " ${username} " ) " ]
226226 }
227227
228+ ensure_jsonc_parser () {
229+ if ! command -v jq > /dev/null 2>&1 ; then
230+ log " ERROR: jq is required to parse JSONC configs."
231+ return 1
232+ fi
233+ return 0
234+ }
235+
236+ jsonc_get_auth_enabled () {
237+ local file=" $1 "
238+ ensure_jsonc_parser || return 1
239+
240+ local auth_enabled
241+ if ! auth_enabled=" $( grep -v ' ^\s*//' " ${file} " | jq -r ' .auth.enabled // false' ) " ; then
242+ return 1
243+ fi
244+ printf ' %s' " ${auth_enabled} "
245+ }
246+
247+ jsonc_set_auth_enabled () {
248+ local file=" $1 "
249+ ensure_jsonc_parser || return 1
250+
251+ local patched
252+ if ! patched=" $( grep -v ' ^\s*//' " ${file} " | jq ' .auth.enabled = true' ) " ; then
253+ return 1
254+ fi
255+ printf ' %s\n' " ${patched} " > " ${file} "
256+ }
257+
228258 ensure_auth_config () {
229259 local config_dir=" /home/opencoder/.config/opencode"
230260 local config_json=" ${config_dir} /opencode.json"
231261 local config_jsonc=" ${config_dir} /opencode.jsonc"
232262
233263 install -d -m 0755 " ${config_dir} "
234264
235- if [ -f " ${config_json} " ] || [ -f " ${config_jsonc} " ]; then
265+ # Check if an existing config already has auth enabled
266+ local config_file=" "
267+ for candidate in " ${config_json} " " ${config_jsonc} " " ${config_dir} /config.json" ; do
268+ if [ -f " ${candidate} " ]; then
269+ config_file=" ${candidate} "
270+ break
271+ fi
272+ done
273+
274+ if [ -n " ${config_file} " ]; then
275+ # File exists — verify auth is enabled
276+ local auth_enabled
277+ if ! auth_enabled=" $( jsonc_get_auth_enabled " ${config_file} " ) " ; then
278+ log " ERROR: Failed to parse ${config_file} for auth settings."
279+ exit 1
280+ fi
281+ if [ " ${auth_enabled} " = " true" ]; then
282+ return # Already configured correctly
283+ fi
284+
285+ # Auth not enabled — patch the existing config to enable it
286+ log " Auth is not enabled in ${config_file} ; patching to enable."
287+ if ! jsonc_set_auth_enabled " ${config_file} " ; then
288+ log " ERROR: Failed to update ${config_file} to enable auth."
289+ exit 1
290+ fi
291+ chown opencoder:opencoder " ${config_file} " 2> /dev/null || true
292+ chmod 644 " ${config_file} " 2> /dev/null || true
236293 return
237294 fi
238295
296+ # No config file — create default
239297 if ! cat > " ${config_jsonc} " << 'EOF '
240298{
241299 "auth": {
@@ -255,23 +313,24 @@ EOF
255313
256314 check_auth_enabled () {
257315 local config_dir=" /home/opencoder/.config/opencode"
258- local config_file=" "
259316
260- for candidate in " ${config_dir} /opencode.json" " ${config_dir} /opencode.jsonc" ; do
317+ # Check all config files that opencode's global loader merges
318+ local candidate auth_enabled
319+ for candidate in " ${config_dir} /opencode.json" \
320+ " ${config_dir} /opencode.jsonc" \
321+ " ${config_dir} /config.json" ; do
261322 if [ -f " ${candidate} " ]; then
262- config_file=" ${candidate} "
263- break
323+ if ! auth_enabled=" $( jsonc_get_auth_enabled " ${candidate} " ) " ; then
324+ log " ERROR: Failed to parse ${candidate} for auth settings."
325+ exit 1
326+ fi
327+ if [ " ${auth_enabled} " = " true" ]; then
328+ return 0
329+ fi
264330 fi
265331 done
266332
267- if [ -z " ${config_file} " ]; then
268- return 1
269- fi
270-
271- local auth_enabled
272- # Strip // line-comments before parsing (JSONC compat)
273- auth_enabled=" $( grep -v ' ^\s*//' " ${config_file} " | jq -r ' .auth.enabled // false' 2> /dev/null || echo " false" ) "
274- [ " ${auth_enabled} " = " true" ]
333+ return 1
275334 }
276335
277336 warn_security_posture () {
521580 }
522581
523582 load_builtin_home_users
583+ if ! ensure_jsonc_parser; then
584+ log " ERROR: JSONC parser is required for auth config checks."
585+ exit 1
586+ fi
524587 ensure_auth_config
525588 restore_or_bootstrap_users
526589 migrate_unmanaged_home_users_to_records
0 commit comments