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
9 changes: 7 additions & 2 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,13 @@ Caddy as TLS proxy; `.env.example` sets `DOMAIN` and signups).
option has a `KEYGO_*` env var. Secrets are one file each in `KEYGO_SECRETS_DIR`
(`/run/secrets`): `db_password`, `opaque_setup`, `invite_pepper`, read through typed decoders
in `secrets.rs`. `init` creates missing ones and never overwrites.
- **API** — `/health/live`, `/api/v1/meta`, `/api/v1/auth/{register,login}/{start,finish}`.
JSON is snake_case. Auth bodies are capped at `MAX_BODY_BYTES` (16 KiB).
- **API** — `/health/live`, `/api/v1/meta`, `/api/v1/auth/{register,login,step-up}/{start,finish}`,
`/api/v1/auth/{token,logout}`, `GET /api/v1/devices`, `DELETE /api/v1/devices/{id}`,
`DELETE /api/v1/account`. JSON is snake_case. Auth bodies are capped at `MAX_BODY_BYTES` (16 KiB).
- **Account deletion** — `DELETE /api/v1/account` requires a fresh sign-in (`FreshAuthenticated`);
one `delete from users` cascades to every table the account owns. The client always steps up
first (typing the password is the confirmation) and ends the local session; the local vault is
kept. Other devices learn of it only as a plain `session_revoked` on their next call.
- **Registration** — two-step OPAQUE; `start` reserves username/user_id in the unlogged
`pending_registrations` table (5 min TTL). `user_id` is client-generated (UUID v4) and equals the
local user id. Unless `KEYGO_ALLOW_SIGNUPS`, an invite is required; invite codes are stored only
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import de.davisalessandro.keygo.rust.UserId

class FakeSyncClient : SyncClientInterface {

var deleteAccountResult: Result<Unit> = Result.success(Unit)
var deleteDeviceResult: Result<Unit> = Result.success(Unit)
var devicesResult: Result<List<SyncDevice>> = Result.success(emptyList())
var loginResult: Result<Account>? = null
Expand All @@ -19,6 +20,8 @@ class FakeSyncClient : SyncClientInterface {
var registerResult: Result<Account>? = null
var stepUpResult: Result<Unit> = Result.success(Unit)

override suspend fun deleteAccount(password: String) = deleteAccountResult.getOrThrow()

override suspend fun deleteDevice(id: DeviceId) = deleteDeviceResult.getOrThrow()

override suspend fun devices(): List<SyncDevice> = devicesResult.getOrThrow()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ internal class SyncRepositoryImpl(
override suspend fun stepUp(password: String): Result<Unit, SyncError> =
withSessionClient { client, _ -> client.stepUp(password) }

override suspend fun deleteAccount(password: String): Result<Unit, SyncError> =
withSessionClient { client, _ ->
client.deleteAccount(password)
}

private suspend fun clientFor(serverUrl: String): SyncClient = clientLock.withLock {
// Rust stores the origin as `Url` prints it, with a trailing slash; ServerAddress has none.
val origin = serverUrl.trimEnd('/')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ interface SyncRepository {
suspend fun deleteDevice(deviceId: UUID): Result<Unit, SyncError>

suspend fun stepUp(password: String): Result<Unit, SyncError>

suspend fun deleteAccount(password: String): Result<Unit, SyncError>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package de.davis.keygo.core.sync.presentation.password

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.foundation.text.input.TextObfuscationMode
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Lock
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Icon
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedSecureTextField
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.DialogProperties
import de.davis.keygo.core.sync.R
import de.davis.keygo.core.sync.domain.model.SyncError
import de.davis.keygo.core.sync.presentation.messageRes
import de.davis.keygo.core.ui.components.VisibilityButton

@Composable
fun PasswordConfirmDialog(
password: TextFieldState,
busy: Boolean,
error: SyncError?,
onSubmit: () -> Unit,
onCancel: () -> Unit,
icon: @Composable () -> Unit,
title: @Composable () -> Unit,
message: @Composable () -> Unit,
confirmButton: @Composable (enabled: Boolean, onClick: () -> Unit) -> Unit,
) {
val hasText by remember(password) { derivedStateOf { password.text.isNotEmpty() } }
val canSubmit = !busy && hasText
val submit = {
if (canSubmit) onSubmit()
}

AlertDialog(
onDismissRequest = onCancel,
properties = DialogProperties(
dismissOnBackPress = !busy,
dismissOnClickOutside = !busy,
),
confirmButton = { confirmButton(canSubmit, submit) },
dismissButton = {
TextButton(onClick = onCancel, enabled = !busy) {
Text(text = stringResource(android.R.string.cancel))
}
},
icon = icon,
title = title,
text = {
Column(verticalArrangement = Arrangement.spacedBy(12.dp)) {
message()

var hidden by rememberSaveable { mutableStateOf(true) }
OutlinedSecureTextField(
state = password,
modifier = Modifier.fillMaxWidth(),
enabled = !busy,
label = { Text(text = stringResource(R.string.step_up_password)) },
isError = error != null,
textObfuscationMode =
if (hidden) TextObfuscationMode.RevealLastTyped
else TextObfuscationMode.Visible,
trailingIcon = {
VisibilityButton(isHidden = hidden, onClick = { hidden = !hidden })
},
keyboardOptions = KeyboardOptions(
autoCorrectEnabled = false,
keyboardType = KeyboardType.Password,
imeAction = ImeAction.Done,
),
onKeyboardAction = { submit() },
)

if (busy) LinearProgressIndicator(modifier = Modifier.fillMaxWidth())

error?.let {
Text(
text = stringResource(it.messageRes),
color = MaterialTheme.colorScheme.error,
)
}
}
},
)
}

@Preview
@Composable
private fun PasswordConfirmDialogPreview() {
MaterialTheme {
Surface(modifier = Modifier.fillMaxSize()) {
PasswordConfirmDialog(
password = TextFieldState(),
busy = false,
error = SyncError.InvalidCredentials,
onSubmit = {},
onCancel = {},
icon = { Icon(imageVector = Icons.Default.Lock, contentDescription = null) },
title = { Text(text = "Confirm it's you") },
message = { Text(text = "Enter your account password to continue.") },
confirmButton = { enabled, onClick ->
TextButton(onClick = onClick, enabled = enabled) {
Text(text = "Confirm")
}
},
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,44 +1,26 @@
package de.davis.keygo.core.sync.presentation.stepup

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.foundation.text.input.TextObfuscationMode
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Lock
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Icon
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedSecureTextField
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.DialogProperties
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import de.davis.keygo.core.sync.R
import de.davis.keygo.core.sync.domain.model.SyncError
import de.davis.keygo.core.sync.domain.stepup.StepUpPrompt
import de.davis.keygo.core.sync.presentation.messageRes
import de.davis.keygo.core.ui.components.VisibilityButton
import de.davis.keygo.core.sync.R
import de.davis.keygo.core.sync.presentation.password.PasswordConfirmDialog
import org.koin.androidx.compose.koinViewModel

@Composable
Expand All @@ -63,69 +45,20 @@ internal fun StepUpDialog(
onSubmit: () -> Unit,
onCancel: () -> Unit,
) {
val busy = prompt.busy
val hasText by remember(password) { derivedStateOf { password.text.isNotEmpty() } }
val canSubmit = !busy && hasText
val submit = {
if (canSubmit) onSubmit()
}

AlertDialog(
onDismissRequest = onCancel,
properties = DialogProperties(
dismissOnBackPress = !busy,
dismissOnClickOutside = !busy,
),
confirmButton = {
TextButton(onClick = submit, enabled = canSubmit) {
PasswordConfirmDialog(
password = password,
busy = prompt.busy,
error = prompt.error,
onSubmit = onSubmit,
onCancel = onCancel,
icon = { Icon(imageVector = Icons.Default.Lock, contentDescription = null) },
title = { Text(text = stringResource(R.string.step_up_title)) },
message = { Text(text = stringResource(R.string.step_up_message)) },
confirmButton = { enabled, onClick ->
TextButton(onClick = onClick, enabled = enabled) {
Text(text = stringResource(R.string.step_up_confirm))
}
},
dismissButton = {
TextButton(onClick = onCancel, enabled = !busy) {
Text(text = stringResource(android.R.string.cancel))
}
},
icon = {
Icon(imageVector = Icons.Default.Lock, contentDescription = null)
},
title = {
Text(text = stringResource(R.string.step_up_title))
},
text = {
Column(verticalArrangement = Arrangement.spacedBy(12.dp)) {
Text(text = stringResource(R.string.step_up_message))

var hidden by rememberSaveable { mutableStateOf(true) }
OutlinedSecureTextField(
state = password,
modifier = Modifier.fillMaxWidth(),
enabled = !busy,
label = { Text(text = stringResource(R.string.step_up_password)) },
isError = prompt.error != null,
textObfuscationMode =
if (hidden) TextObfuscationMode.RevealLastTyped
else TextObfuscationMode.Visible,
trailingIcon = {
VisibilityButton(isHidden = hidden, onClick = { hidden = !hidden })
},
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Password,
imeAction = ImeAction.Done,
),
onKeyboardAction = { submit() },
)

if (busy) LinearProgressIndicator(modifier = Modifier.fillMaxWidth())

prompt.error?.let {
Text(
text = stringResource(it.messageRes),
color = MaterialTheme.colorScheme.error,
)
}
}
},
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ package de.davis.keygo.core.sync

import de.davis.keygo.core.sync.domain.model.Device
import de.davis.keygo.core.sync.domain.model.ServerInfo
import de.davis.keygo.core.sync.domain.model.ServerLink
import de.davis.keygo.core.sync.domain.model.SyncAccount
import de.davis.keygo.core.sync.domain.model.SyncError
import de.davis.keygo.core.sync.domain.repository.SyncRepository
import de.davis.keygo.core.util.Result
import kotlinx.coroutines.CompletableDeferred
import java.util.UUID

class FakeSyncRepository : SyncRepository {
class FakeSyncRepository(
private val links: FakeServerLinkRepository? = null,
) : SyncRepository {

var devices: Result<List<Device>, SyncError> = Result.Success(emptyList())

Expand All @@ -29,6 +32,17 @@ class FakeSyncRepository : SyncRepository {

var stepUpGate: CompletableDeferred<Unit>? = null

var logoutResult: Result<Unit, SyncError> = Result.Success(Unit)
var logoutClearsLink = true
var logoutGate: CompletableDeferred<Unit>? = null
var logouts = 0
private set

var deleteAccountResults: List<Result<Unit, SyncError>> = listOf(Result.Success(Unit))
val deleteAccountPasswords = mutableListOf<String>()

var deleteAccountGate: CompletableDeferred<Unit>? = null

override suspend fun checkServer(serverUrl: String): Result<ServerInfo, SyncError> =
error("checkServer is not faked")

Expand All @@ -46,7 +60,12 @@ class FakeSyncRepository : SyncRepository {
password: String,
): Result<SyncAccount, SyncError> = error("login is not faked")

override suspend fun logout(): Result<Unit, SyncError> = Result.Success(Unit)
override suspend fun logout(): Result<Unit, SyncError> {
logouts++
logoutGate?.await()
if (logoutClearsLink) links?.link?.value = ServerLink.None
return logoutResult
}

override suspend fun devices(): Result<List<Device>, SyncError> {
loads++
Expand All @@ -70,5 +89,13 @@ class FakeSyncRepository : SyncRepository {
return result
}

override suspend fun deleteAccount(password: String): Result<Unit, SyncError> {
val result = deleteAccountResults.answerFor(deleteAccountPasswords.size)
deleteAccountPasswords += password
deleteAccountGate?.await()
if (result is Result.Success) links?.link?.value = ServerLink.None
return result
}

private fun <T> List<T>.answerFor(call: Int): T = getOrElse(call) { last() }
}
Loading