Automate Nextcloud account settings

I’m setting up a couple of demo machines and I’d like Nextcloud to automatically login.

Quick note to self how to do that:
occ user:add --display-name="Foo Bar" --generate-password someuser4
occ user:auth-tokens:add -n someuser4

This shows something like:
lDY7D3nsaZzzQVg8nvs45xwdSPhIIZDzI8QFL2vs7XI6PrpbvZJ15cJyqBCbEQL4sZDGTS9c

Add this app password to the kdewallet under the Nextcloud folder:
passwords: someuser4:https://nextcloud.example.com/:0

As a cli command, this means: echo lDY7D3nsaZzzQVg8nvs45xwdSPhIIZDzI8QFL2vs7XI6PrpbvZJ15cJyqBCbEQL4sZDGTS9c | kwallet-query -f Nextcloud -w 'someuser4:https://nextcloud.example.com/:0' kdewallet

Unfortunately, this only works if the Nextcloud folder already exists. If not, kwallet-query won’t create it. In order to create a Nextcloud folder without having to insert it in a GUI interface, I came up with the following approach that is UNSAFE (no salt) but will work for demo machines.

Step 0a and 0b: prepare the salt: dd if=/dev/zero of=~/.local/share/kwalletd/kdewallet.salt bs=1 count=56; now unpack a previously created empty blowfish encrypted wallet onto kdewallet.kwl. Yeah I know it’s messy. Here is a Stackexchange question that didn’t get an answer for 6+ months. But hey, it may change and if this blog posting doesn’t reflect it, you’re lucky and I’m not.

Anyway, step 1: we’ll use a Python script to add the app password. Install python3-keyring (with apt or pip) and do, as a logged in user:

import keyring
keyring.set_keyring(next((bk for bk in keyring.backend.get_all_keyring() if bk.name == 'kwallet DBusKeyring'), None))
keyring.set_password('Nextcloud', 'someuser4:https://nextcloud.example.com/:0', 'lDY7D3nsaZzzQVg8nvs45xwdSPhIIZDzI8QFL2vs7XI6PrpbvZJ15cJyqBCbEQL4sZDGTS9c')

This will work, even if the Nextcloud folder does not yet exist. Please note that you do need the kwalletd6 running, i.e. you need an actual kde session with an actual user to do this. I implemented it with a ~/.config/autostart/somethingsomething.desktop file, so the app password is re-added every session.

Now add a config to ~/.local/Nextcloud/nextcloud.cfg that includes someuser4 as username.

Leave a Reply

Your email address will not be published. Required fields are marked *