Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ The version numbers are explained in "Project status" in the [README](README.md)

## [Unreleased]

### Changed

- The setup page asks for **First name** and **Last name** instead of one name, and the optional family name field is now **Friendly name**. It fills in with "<first> <last> Family" as the admin types, and the admin can change it to anything. It is saved as the family name, which the app shows as the server's name.

### Added

- `POST /auth/login` and `POST /auth/register` accept an optional `device_name` (up to 60 characters). It is the session `name` in `GET /auth/sessions`, so the app can show each signed-in device. Without it the name stays `mobile-app`. `POST /auth/refresh` keeps the name.
Expand Down
4 changes: 2 additions & 2 deletions docs/reference/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ Returned by login, register and `/auth/me` (`/auth/me` adds the count fields):

**First admin: web setup only.** The mobile app never creates the admin. After installing the server (with or without Docker), the owner opens `APP_URL` in a web browser:
- `GET /` (web): when the instance has no user, redirects to `/setup`. Otherwise a small status page: "Kinlore is running", the server address to type in the app, and the app's sign-in steps. No user data.
- `GET /setup` (web): the setup form (name, email, an optional family name, password, password confirmation, and the setup token). When the instance already has a user: redirect to `/`.
- `GET /setup` (web): the setup form (`first_name`, `last_name`, an optional `friendly_name`, `email`, `password`, `password_confirmation`, and `setup_token`). A small script on the page fills `friendly_name` with `<first> <last> Family` until the admin types in it. When the instance already has a user: redirect to `/`.
- The setup token is `SETUP_TOKEN` when it is set. Without it, the server makes a random token and keeps it in `storage/app/setup-token` until the first admin exists. `php artisan kinlore:setup-token` prints it, and the Docker container prints it in its log at start. No HTTP response contains the token.
- `POST /setup` (web, CSRF-protected, throttled 5/min): creates the admin under a lock, so two parallel setups cannot both succeed. Validation errors show on the form. Success shows "Your admin account is ready" with the server address and the email to use in the app. It never shows the password.
- `POST /setup` (web, CSRF-protected, throttled 5/min): creates the admin under a lock, so two parallel setups cannot both succeed. The account name is `first_name` and `last_name` joined by one space. `friendly_name` becomes the family name (below); empty gives `<first> <last> Family`. Validation errors show on the form. Success shows "Your admin account is ready" with the server address and the email to use in the app. It never shows the password.
- The seeded admin (`SEED_USER_*`) also works and skips the web setup.
- Behind a reverse proxy, the pages and form action use the proxy's `https` address (trusted proxies, section 10).

Expand Down
7 changes: 4 additions & 3 deletions docs/self-hosting/first-admin.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ The setup page always asks for a setup token. So a stranger cannot take over a n

The command prints `Setup token:` and the token. If `SETUP_TOKEN` is set, it tells you to use that value. In Docker, the token is also in the log of each start: `docker compose logs api | grep "Setup token"`.
2. Open the server address in a web browser, for example `https://photos.example.com`. An empty server sends you to `/setup`.
3. Enter your name, your email address and a password two times. The password needs at least 8 characters.
4. Enter a family name, or leave the field empty. Empty gives your full name and "Family", for example "Anna Example Family". The name can have up to 60 characters. You can change it later in the app.
5. Paste the setup token and send the form.
3. Enter your **First name** and **Last name**.
4. Check the **Friendly name**. It fills in from your name as you type, for example "Anna Example Family". Change it to anything you like, for example "The Smiths": after you type in it, it no longer follows your name. Your family sees this name in the app and on invitations, and the app shows it as the name of the server. It can have up to 60 characters, and you can change it later in the app. An empty field gives the same suggestion.
5. Enter your email address and a password two times. The password needs at least 8 characters.
6. Paste the setup token and send the form.

The page shows "Your admin account is ready", with the server address and the email address to use in the app.

Expand Down
9 changes: 8 additions & 1 deletion kinlore-api/app/Http/Controllers/SetupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ public function store(SetupRequest $request): View|RedirectResponse
return redirect('/');
}

$user = $this->authService->createFirstAdmin($request->validated());
$data = $request->validated();
$user = $this->authService->createFirstAdmin([
'name' => $data['name'],
'email' => $data['email'],
'password' => $data['password'],
'family_name' => $data['friendly_name'] ?? null,
'setup_token' => $data['setup_token'],
]);

// The new admin sees the family name; the public pages keep APP_NAME.
return view('setup-done', ['instanceName' => $this->family->name(), 'email' => $user->email] + $this->pageData());
Expand Down
9 changes: 8 additions & 1 deletion kinlore-api/app/Http/Requests/Web/SetupRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ class SetupRequest extends FormRequest
public function rules(): array
{
return [
'first_name' => 'required|string|max:120',
'last_name' => 'required|string|max:120',
// Built from first_name and last_name in prepareForValidation; the account keeps one name.
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255',
'family_name' => 'nullable|string|max:'.FamilyService::NAME_MAX_LENGTH,
// The family name the app shows. Empty: "<first> <last> Family", as the page suggests.
'friendly_name' => 'nullable|string|max:'.FamilyService::NAME_MAX_LENGTH,
'password' => 'required|string|min:8|max:255|confirmed',
// The value itself is checked with hash_equals in AuthService::createFirstAdmin.
'setup_token' => 'required|string|max:255',
Expand All @@ -31,5 +35,8 @@ protected function prepareForValidation(): void
if (is_string($this->input('email'))) {
$this->merge(['email' => strtolower(trim($this->input('email')))]);
}
$first = is_string($this->input('first_name')) ? $this->input('first_name') : '';
$last = is_string($this->input('last_name')) ? $this->input('last_name') : '';
$this->merge(['name' => trim((string) preg_replace('/\s+/', ' ', "$first $last"))]);
}
}
24 changes: 22 additions & 2 deletions kinlore-api/resources/views/setup.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

@php
$fields = [
'name' => ['label' => 'Your name', 'type' => 'text', 'autocomplete' => 'name'],
'first_name' => ['label' => 'First name', 'type' => 'text', 'autocomplete' => 'given-name'],
'last_name' => ['label' => 'Last name', 'type' => 'text', 'autocomplete' => 'family-name'],
'friendly_name' => ['label' => 'Friendly name', 'type' => 'text', 'autocomplete' => 'off', 'optional' => true, 'hint' => 'Your family sees this name in the app and on invitations. It fills in from your name, for example "Anna Example Family". Change it to anything you like; you can also change it later in the app.'],
'email' => ['label' => 'Email', 'type' => 'email', 'autocomplete' => 'email'],
'family_name' => ['label' => 'Family name (optional)', 'type' => 'text', 'autocomplete' => 'off', 'optional' => true, 'hint' => 'Your family sees this name in the app and on invitations. Leave it empty to use your name, for example "Anna Example Family". You can change it later in the app.'],
'password' => ['label' => 'Password', 'type' => 'password', 'autocomplete' => 'new-password', 'hint' => 'At least 8 characters.'],
'password_confirmation' => ['label' => 'Repeat the password', 'type' => 'password', 'autocomplete' => 'new-password'],
'setup_token' => ['label' => 'Setup token', 'type' => 'password', 'autocomplete' => 'off', 'hint' => 'The server log shows it at start. You can also run "php artisan kinlore:setup-token" on the server. If you set SETUP_TOKEN in the server settings, enter that value.'],
Expand Down Expand Up @@ -37,4 +38,23 @@
@endforeach
<button type="submit">Create admin account</button>
</form>
<script>
// Fill "Friendly name" from the first and last name until the admin types in it. Without script, the
// field stays as typed, and the server uses the same suggestion when it is empty.
(() => {
const first = document.getElementById('first_name');
const last = document.getElementById('last_name');
const friendly = document.getElementById('friendly_name');
const suggestion = () => {
const full = `${first.value} ${last.value}`.trim().replace(/\s+/g, ' ');
return full ? `${full.slice(0, {{ \App\Services\FamilyService::NAME_MAX_LENGTH - 7 }}).trim()} Family` : '';
};
// A value from a failed submit counts as the admin's own when it differs from the suggestion.
let own = friendly.value !== '' && friendly.value !== suggestion();
const fill = () => { if (!own) friendly.value = suggestion(); };
first.addEventListener('input', fill);
last.addEventListener('input', fill);
friendly.addEventListener('input', () => { own = friendly.value.trim() !== ''; if (!own) fill(); });
})();
</script>
@endsection
38 changes: 31 additions & 7 deletions kinlore-api/tests/Feature/FamilyNameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class FamilyNameTest extends TestCase
use RefreshDatabase;

private const FORM = [
'name' => 'Anna Example',
'first_name' => 'Anna',
'last_name' => 'Example',
'email' => 'anna@example.test',
'password' => 'correct horse 42',
'password_confirmation' => 'correct horse 42',
Expand All @@ -31,24 +32,47 @@ protected function setUp(): void
config(['app.setup_token' => 'the-secret', 'app.name' => 'Kinlore']);
}

/** The name the app gets with each sync page. */
private function actingAsAdminAndPull(): string
{
Sanctum::actingAs(User::first());

return $this->pulledName();
}

public function test_setup_joins_first_and_last_name_and_requires_both(): void
{
$this->post('/setup', ['first_name' => ' Anna ', 'last_name' => ' van Dijk '] + self::FORM)->assertOk();
$this->assertSame('Anna van Dijk', User::first()->name);
$this->assertSame('Anna van Dijk Family', app(FamilyService::class)->name());

User::query()->delete();
$this->from('/setup')->post('/setup', ['first_name' => ''] + self::FORM)->assertRedirect('/setup')->assertSessionHasErrors('first_name');
$this->from('/setup')->post('/setup', ['last_name' => ''] + self::FORM)->assertRedirect('/setup')->assertSessionHasErrors('last_name');
$this->from('/setup')->post('/setup', ['friendly_name' => str_repeat('x', 61)] + self::FORM)->assertSessionHasErrors('friendly_name');
$this->assertDatabaseCount('users', 0);
}

private function pulledName(): string
{
return $this->postJson('/api/v1/sync/pull', ['lastPulledAt' => null])->assertOk()->json('family_name');
}

public function test_setup_stores_the_given_name_or_suggests_the_admins_full_name(): void
{
// The field is optional: only the other fields are `required`.
$form = $this->get('/setup')->assertOk()->assertSee('name="family_name"', false)->getContent();
$this->assertSame(1, preg_match('/<input id="family_name"[^>]*>/', $form, $input));
// Friendly name is optional: only the other fields are `required`.
$form = $this->get('/setup')->assertOk()->assertSee('name="friendly_name"', false)->getContent();
$this->assertSame(1, preg_match('/<input id="friendly_name"[^>]*>/', $form, $input));
$this->assertStringNotContainsString('required', $input[0]);
$this->assertSame(5, substr_count($form, ' required'));
$this->assertSame(6, substr_count($form, ' required'));
$this->assertStringContainsString("getElementById('friendly_name')", $form);

$this->post('/setup', self::FORM + ['family_name' => 'The Smiths'])->assertOk()->assertSee('The Smiths is set up');
$this->post('/setup', self::FORM + ['friendly_name' => 'The Smiths'])->assertOk()->assertSee('The Smiths is set up');
$this->assertSame('The Smiths', app(FamilyService::class)->name());
$this->assertSame('The Smiths', $this->actingAsAdminAndPull());

User::query()->delete();
$this->post('/setup', self::FORM + ['family_name' => ''])->assertOk()->assertSee('Anna Example Family is set up');
$this->post('/setup', self::FORM + ['friendly_name' => ''])->assertOk()->assertSee('Anna Example Family is set up');

// Stored at setup: a later change of the admin's own name does not rename the family.
User::first()->update(['name' => 'Anna Other']);
Expand Down
6 changes: 4 additions & 2 deletions kinlore-api/tests/Feature/WebSetupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class WebSetupTest extends TestCase
use RefreshDatabase;

private const FORM = [
'name' => 'Owner',
'first_name' => 'Owner',
'last_name' => 'Example',
'email' => 'owner@example.test',
'password' => 'correct horse 42',
'password_confirmation' => 'correct horse 42',
Expand Down Expand Up @@ -205,7 +206,8 @@ public function test_pages_escape_the_instance_name(): void

$this->get('/setup')->assertOk()
->assertSee('&lt;script&gt;alert(1)&lt;/script&gt;', false)
->assertDontSee('<script>', false);
// The page has its own script (the friendly name fill); the name must not add one.
->assertDontSee('<script>alert(1)', false);
}

public function test_form_action_uses_the_https_address_behind_a_reverse_proxy(): void
Expand Down
4 changes: 3 additions & 1 deletion scripts/test-bare-metal.sh
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,11 @@ test "${#SETUP}" -ge 32
CSRF=$(curl -fsS -c /tmp/setup-jar http://localhost/setup | sed -n 's/.*name="_token" value="\([^"]*\)".*/\1/p')
test -n "$CSRF"
curl -fsS -b /tmp/setup-jar -o /tmp/setup-done.html --data-urlencode "_token=$CSRF" --data-urlencode "setup_token=$SETUP" \
--data-urlencode "name=$SEED_USER_NAME" --data-urlencode "email=$SEED_USER_EMAIL" \
--data-urlencode "first_name=${SEED_USER_NAME%% *}" --data-urlencode "last_name=${SEED_USER_NAME#* }" --data-urlencode "email=$SEED_USER_EMAIL" \
--data-urlencode "password=$SEED_USER_PASSWORD" --data-urlencode "password_confirmation=$SEED_USER_PASSWORD" http://localhost/setup
grep -q 'Your admin account is ready' /tmp/setup-done.html
# No friendly_name sent: the server suggests "<first> <last> Family"
grep -q 'Test Admin Family' /tmp/setup-done.html
test ! -e storage/app/setup-token
runuser -u www-data -- php artisan kinlore:setup-token | grep -q 'already set up'
echo "admin created with the setup token; the token file is gone"
Expand Down
Loading