Skip to content

Commit c28cd6d

Browse files
committed
use config, disable registrations
1 parent 6802907 commit c28cd6d

File tree

18 files changed

+194
-103
lines changed

18 files changed

+194
-103
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"check": "tsc --noEmit"
77
},
88
"dependencies": {
9-
"@start9labs/start-sdk": "^0.4.0-beta.45"
9+
"@start9labs/start-sdk": "^0.4.0-beta.46"
1010
},
1111
"devDependencies": {
1212
"@types/node": "^22.19.3",

startos/actions/admin-token.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { T, utils } from '@start9labs/start-sdk'
2-
import { storeJson } from '../fileModels/store.json'
32
import * as crypto from 'crypto'
43
import { sdk } from '../sdk'
4+
import { configJson } from '../fileModels/config.json'
55

66
export const setAdminToken = sdk.Action.withoutInput(
77
// id
88
'set-admin-token',
99

1010
// metadata
1111
async ({ effects }) => {
12-
const existing = await storeJson.read((s) => s.ADMIN_TOKEN).const(effects)
12+
const existing = await configJson.read((s) => s.admin_token).const(effects)
1313

1414
return {
1515
name: existing ? 'Update Admin Token' : 'Create Admin Token',
@@ -32,7 +32,7 @@ export const setAdminToken = sdk.Action.withoutInput(
3232

3333
const hash = await hashToken(effects, token)
3434

35-
await storeJson.merge(effects, { ADMIN_TOKEN: hash })
35+
await configJson.merge(effects, { admin_token: hash })
3636

3737
return {
3838
version: '1',

startos/actions/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import { sdk } from '../sdk'
22
import { setAdminToken } from './admin-token'
33
import { manageSmtp } from './manageSmtp'
44
import { setPrimaryDomain } from './setPrimaryUrl'
5+
import { toggleSignups } from './toggleSignups'
56

67
export const actions = sdk.Actions.of()
8+
.addAction(toggleSignups)
79
.addAction(setAdminToken)
810
.addAction(setPrimaryDomain)
911
.addAction(manageSmtp)

startos/actions/manageSmtp.ts

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { storeJson } from '../fileModels/store.json'
1+
import { configJson } from '../fileModels/config.json'
2+
import { systemSmtpJson } from '../fileModels/systemSmtp.json'
23
import { sdk } from '../sdk'
34

45
const { InputSpec } = sdk
@@ -25,10 +26,64 @@ export const manageSmtp = sdk.Action.withInput(
2526
inputSpec,
2627

2728
// optionally pre-fill the input form
28-
async ({ effects }) => ({
29-
smtp: (await storeJson.read((s) => s.smtp).once()) || undefined,
30-
}),
29+
async ({ effects }) => {
30+
const smtp = await systemSmtpJson.read().once()
31+
32+
if (smtp?.enabled) {
33+
return {
34+
smtp: {
35+
selection: 'system' as const,
36+
value: { customFrom: smtp.customFrom || undefined },
37+
},
38+
}
39+
}
40+
41+
const config = await configJson.read().once()
42+
43+
if (config?.smtp_host) {
44+
const { smtp_host, smtp_port, smtp_from, smtp_username, smtp_password } =
45+
config
46+
return {
47+
smtp: {
48+
selection: 'custom' as const,
49+
value: {
50+
server: smtp_host,
51+
port: smtp_port,
52+
from: smtp_from,
53+
login: smtp_username,
54+
password: smtp_password,
55+
},
56+
},
57+
}
58+
}
59+
60+
return { smtp: { selection: 'disabled' as const, value: {} } }
61+
},
3162

3263
// the execution function
33-
async ({ effects, input }) => storeJson.merge(effects, { smtp: input.smtp }),
64+
async ({ effects, input }) => {
65+
if (input.smtp.selection === 'system') {
66+
await systemSmtpJson.merge(effects, {
67+
enabled: true,
68+
customFrom: input.smtp.value.customFrom,
69+
})
70+
} else if (input.smtp.selection === 'custom') {
71+
const { server, port, from, login, password } = input.smtp.value
72+
await configJson.merge(effects, {
73+
smtp_host: server,
74+
smtp_port: port,
75+
smtp_from: from,
76+
smtp_username: login,
77+
smtp_password: password || undefined,
78+
})
79+
} else {
80+
await configJson.merge(effects, {
81+
smtp_host: undefined,
82+
smtp_port: undefined,
83+
smtp_from: undefined,
84+
smtp_username: undefined,
85+
smtp_password: undefined,
86+
})
87+
}
88+
},
3489
)

startos/actions/setPrimaryUrl.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { storeJson } from '../fileModels/store.json'
1+
import { configJson } from '../fileModels/config.json'
22
import { sdk } from '../sdk'
33
import { getVaultInterfaceUrls } from '../utils'
44

@@ -42,10 +42,10 @@ export const setPrimaryDomain = sdk.Action.withInput(
4242

4343
// optionally pre-fill the input form
4444
async ({ effects }) => ({
45-
domain: (await storeJson.read((s) => s.DOMAIN).once()) || undefined,
45+
domain: (await configJson.read((c) => c.domain).once()) || undefined,
4646
}),
4747

4848
// the execution function
4949
async ({ effects, input }) =>
50-
storeJson.merge(effects, { DOMAIN: input.domain }),
50+
configJson.merge(effects, { domain: input.domain }),
5151
)

startos/actions/toggleSignups.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { configJson } from '../fileModels/config.json'
2+
import { sdk } from '../sdk'
3+
4+
export const toggleSignups = sdk.Action.withoutInput(
5+
// id
6+
'toggle-signups',
7+
8+
// metadata
9+
async ({ effects }) => {
10+
const allowed = await configJson
11+
.read((c) => c.signups_allowed)
12+
.const(effects)
13+
14+
return {
15+
name: allowed ? 'Disable Signups' : 'Enable Signups',
16+
description: allowed
17+
? 'Signups are currently enabled. Run this action to prohibit new signups.'
18+
: 'Signups are currently disabled. Run this action to permit new signups.',
19+
warning: allowed
20+
? null
21+
: 'Anyone with your Vaultwarden URL will be able to create an account on your server, which represents a security risk. Be careful!',
22+
allowedStatuses: 'any',
23+
group: null,
24+
visibility: 'enabled',
25+
}
26+
},
27+
28+
// the execution function
29+
async ({ effects }) => {
30+
const allowed = await configJson
31+
.read((c) => c.signups_allowed)
32+
.const(effects)
33+
34+
await configJson.merge(effects, {
35+
signups_allowed: !allowed,
36+
})
37+
},
38+
)

startos/fileModels/config.json.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { matches, FileHelper } from '@start9labs/start-sdk'
2+
3+
const { object, string, literal, natural, boolean } = matches
4+
5+
const shape = object({
6+
domain: string,
7+
admin_token: string,
8+
signups_allowed: boolean.onMismatch(false),
9+
smtp_host: string.optional(),
10+
smtp_security: literal('starttls').onMismatch('starttls'),
11+
smtp_port: natural.optional().onMismatch(undefined),
12+
smtp_from: string.optional().onMismatch(undefined),
13+
smtp_from_name: string.optional().onMismatch(undefined),
14+
smtp_username: string.optional().onMismatch(undefined),
15+
smtp_password: string.optional().onMismatch(undefined),
16+
})
17+
18+
export const configJson = FileHelper.json(
19+
{ volumeId: 'main', subpath: '/config.json' },
20+
shape,
21+
)

startos/fileModels/store.json.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { matches, FileHelper } from '@start9labs/start-sdk'
2+
3+
const { object, string, boolean } = matches
4+
5+
const shape = object({
6+
enabled: boolean,
7+
customFrom: string.nullable(),
8+
})
9+
10+
export const systemSmtpJson = FileHelper.json(
11+
{ volumeId: 'main', subpath: '/systemSmtp.json' },
12+
shape,
13+
)

0 commit comments

Comments
 (0)