import { storeJson } from '../fileModels/storeJson' import { i18n } from '../i18n' import { sdk } from '../sdk' import { randomPassword } from '../utils' const { InputSpec, Value } = sdk const DEFAULT_USERNAME = 'spaces' const inputSpec = InputSpec.of({ username: Value.text({ name: i18n('Username'), description: i18n( 'Username for HTTP basic auth in front of subs (SUBS_BASIC_AUTH_USER).', ), warning: null, footnote: null, default: DEFAULT_USERNAME, required: true, masked: false, placeholder: 'spaces', minLength: 1, maxLength: null, }), password: Value.text({ name: i18n('Password'), description: i18n( 'Password for HTTP basic auth in front of subs (SUBS_BASIC_AUTH_PASSWORD). Leave blank to auto-generate a random one.', ), warning: null, footnote: null, default: null, required: false, masked: true, placeholder: '(leave blank to auto-generate)', minLength: 0, maxLength: null, }), }) export const setSubsCredentials = sdk.Action.withInput( // id 'set-subs-credentials', // metadata async ({ effects }) => ({ name: i18n('Set Subspaces Auth Credentials'), description: i18n( 'Set or rotate the HTTP basic auth credentials enforced in front of the Subspaces Web UI and Subs API. Auth is enabled automatically. Saving restarts the service.', ), warning: null, allowedStatuses: 'any', group: null, visibility: 'enabled', }), // input inputSpec, // prefill — current username, never the password (the user explicitly types // a new password or leaves blank to auto-generate) async ({ effects }) => { const subsAuth = await storeJson.read((s) => s.subsAuth).once() return { username: subsAuth?.username ?? DEFAULT_USERNAME, password: null, } }, // run async ({ effects, input }) => { const password = input.password && input.password.length > 0 ? input.password : randomPassword() await storeJson.merge(effects, { subsAuth: { username: input.username, password }, subsAuthEnabled: true, }) return { version: '1', title: i18n('Success'), message: i18n( 'Subspaces Auth credentials saved. The service is restarting so subs picks up the new credentials.', ), result: null, } }, )