Skip to content

Commit 432d03b

Browse files
authored
feat: add sensitive Declarative setting example (#18)
* feat: add sensitive Declarative setting example Signed-off-by: Andrey Borysenko <[email protected]> * feat: add sensitive preferences with encryption check * chore: bump nc_py_api required version --------- Signed-off-by: Andrey Borysenko <[email protected]>
1 parent 688d83d commit 432d03b

File tree

4 files changed

+101
-2
lines changed

4 files changed

+101
-2
lines changed

lib/main.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,15 @@ async def lifespan(app: FastAPI):
183183
default="foo",
184184
options={_("First radio"): "foo", _("Second radio"): "bar", _("Third radio"): "baz"},
185185
),
186+
SettingsField(
187+
id="test_ex_app_sensitive_field",
188+
title=_("Password"),
189+
description=_("Set some secure value setting with encryption"),
190+
type=SettingsFieldType.PASSWORD,
191+
default="",
192+
placeholder=_("Set secure value"),
193+
sensitive=True,
194+
),
186195
],
187196
)
188197

@@ -194,7 +203,10 @@ def enabled_handler(enabled: bool, nc: NextcloudApp) -> str:
194203
"top_menu",
195204
"first_menu",
196205
"ui_example_state",
197-
{"initial_value": "test init value"},
206+
{
207+
"initial_value": "test init value",
208+
"initial_sensitive_value": "test_sensitive_value",
209+
},
198210
)
199211
nc.ui.resources.set_script("top_menu", "first_menu", "js/ui_example-main")
200212
nc.ui.top_menu.register("first_menu", "UI example", "img/app.svg")
@@ -234,6 +246,8 @@ def enabled_handler(enabled: bool, nc: NextcloudApp) -> str:
234246
],
235247
)
236248

249+
nc.appconfig_ex.set_value("test_ex_app_sensitive_field", "test_sensitive_value", sensitive=True)
250+
237251
if nc.srv_version["major"] >= 29:
238252
nc.ui.settings.register_form(SETTINGS_EXAMPLE)
239253
else:
@@ -247,13 +261,21 @@ def enabled_handler(enabled: bool, nc: NextcloudApp) -> str:
247261
nc.occ_commands.unregister("ui_example:ping")
248262
nc.occ_commands.unregister("ui_example:setup")
249263
nc.occ_commands.unregister("ui_example:stream")
264+
nc.appconfig_ex.delete("test_ex_app_sensitive_field")
250265
return ""
251266

252267

253268
class Button1Format(BaseModel):
254269
initial_value: str
255270

256271

272+
class Button2Format(BaseModel):
273+
sensitive_value: str
274+
275+
class Button3Format(BaseModel):
276+
preference_value: str
277+
278+
257279
@APP.post("/api/verify_initial_value")
258280
async def verify_initial_value(
259281
input1: Button1Format,
@@ -263,6 +285,28 @@ async def verify_initial_value(
263285
content={"initial_value": str(random.randint(0, 100))}, status_code=200
264286
)
265287

288+
@APP.post("/api/verify_sensitive_value")
289+
async def verify_sensitive_value(
290+
input1: Button2Format,
291+
nc: Annotated[NextcloudApp, Depends(nc_app)],
292+
):
293+
print("Old sensitive value: ", input1.sensitive_value)
294+
sensitive_value = nc.appconfig_ex.get_value("test_ex_app_sensitive_field")
295+
print("Sensitive value: ", sensitive_value)
296+
return responses.JSONResponse(content={"sensitive_value": sensitive_value}, status_code=200)
297+
298+
299+
@APP.post("/api/verify_preference_value")
300+
async def verify_preference_value(
301+
input1: Button3Format,
302+
nc: Annotated[NextcloudApp, Depends(nc_app)],
303+
):
304+
nc.preferences_ex.set_value("test_ex_app_sensitive_field", input1.preference_value, sensitive=True)
305+
preference_value = nc.preferences_ex.get_value("test_ex_app_sensitive_field")
306+
print("Old preference value: ", input1.preference_value)
307+
print("Preference value: ", preference_value)
308+
return responses.JSONResponse(content={"preference_value": preference_value}, status_code=200)
309+
266310

267311
@APP.post("/api/test_menu")
268312
async def test_menu_handler(

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
nc-py-api[app]>=0.10.0
1+
nc-py-api[app]>=0.20.2
22
fastapi
33
pydantic

src/store/example.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,38 @@ const actions = {
3737
})
3838
},
3939

40+
verifySensitiveValue(context, value) {
41+
axios.post(generateUrl(`${APP_API_PROXY_URL_PREFIX}/${EX_APP_ID}/api/verify_sensitive_value`), {
42+
sensitive_value: value,
43+
})
44+
.then((res) => {
45+
if ('sensitive_value' in res.data && res.data.sensitive_value === value) {
46+
showSuccess(t('ui_example', 'Sensitive value is correct'))
47+
} else {
48+
showError(t('ui_example', 'Sensitive value is incorrect'))
49+
}
50+
})
51+
.catch(() => {
52+
showError(t('ui_example', 'Sensitive value is incorrect'))
53+
})
54+
},
55+
56+
verifyPreferenceValue(context, value) {
57+
axios.post(generateUrl(`${APP_API_PROXY_URL_PREFIX}/${EX_APP_ID}/api/verify_preference_value`), {
58+
preference_value: value,
59+
})
60+
.then((res) => {
61+
if ('preference_value' in res.data && res.data.preference_value === value) {
62+
showSuccess(t('ui_example', 'Preference value is correct'))
63+
} else {
64+
showError(t('ui_example', 'Preference value is incorrect'))
65+
}
66+
})
67+
.catch(() => {
68+
showError(t('ui_example', 'Preference value is incorrect'))
69+
})
70+
},
71+
4072
sendNextcloudFileToExApp(context, fileInfo) {
4173
axios.post(generateUrl(`${APP_API_PROXY_URL_PREFIX}/${EX_APP_ID}/api/nextcloud_file`), {
4274
file_info: fileInfo,

src/views/ExAppView.vue

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,22 @@
1212
<NcButton @click="verifyInitialValue">
1313
{{ t('ui_example', 'Verify initial value') }}
1414
</NcButton>
15+
16+
<div style="margin: 10px 0; display: flex; align-items: center; width: 100%; justify-content: center; flex-direction: column;">
17+
<NcInputField :value.sync="initialState.initial_sensitive_value"
18+
:label="t('ui_example', 'Test sensitive value')" />
19+
<NcButton style="margin: 10px 0;" @click="verifySensitiveValue">
20+
{{ t('ui_example', 'Verify sensitive value') }}
21+
</NcButton>
22+
</div>
23+
24+
<div style="margin: 10px 0; display: flex; align-items: center; width: 100%; justify-content: center; flex-direction: column;">
25+
<NcInputField :value.sync="preference_value"
26+
:label="t('ui_example', 'Test preference sensitive value')" />
27+
<NcButton style="margin: 10px 0;" @click="verifyPreferenceValue">
28+
{{ t('ui_example', 'Verify preference value') }}
29+
</NcButton>
30+
</div>
1531
</div>
1632
</NcAppContent>
1733
</NcContent>
@@ -37,6 +53,7 @@ export default {
3753
data() {
3854
return {
3955
initialState: JSON.parse(loadState('app_api', 'ui_example_state')),
56+
preference_value: 'test_preference_value',
4057
}
4158
},
4259
computed: {
@@ -48,6 +65,12 @@ export default {
4865
verifyInitialValue() {
4966
this.$store.dispatch('verifyInitialStateValue', this.initialState?.initial_value)
5067
},
68+
verifySensitiveValue() {
69+
this.$store.dispatch('verifySensitiveValue', this.initialState?.initial_sensitive_value)
70+
},
71+
verifyPreferenceValue() {
72+
this.$store.dispatch('verifyPreferenceValue', this.preference_value)
73+
},
5174
},
5275
}
5376
</script>

0 commit comments

Comments
 (0)