diff --git a/api/src/commonMain/kotlin/run/ostrom/core/api/models/Auth.kt b/api/src/commonMain/kotlin/run/ostrom/core/api/models/Auth.kt index dc0ad49..bf6ccd8 100644 --- a/api/src/commonMain/kotlin/run/ostrom/core/api/models/Auth.kt +++ b/api/src/commonMain/kotlin/run/ostrom/core/api/models/Auth.kt @@ -95,15 +95,29 @@ public data class DeviceTokenRequest( /** * The one-time code of the mobile sign-in (PKCE), with the verifier and the exact redirect URI of its start. * + * With `devices.v1` the app also names its installation, as [DeviceLoginStart] does for the CLI (TeamCode's companion + * sends it with its login, docs/devices.md): the server registers the phone once and every later sign-in from it lands + * on the same device. A server without it ignores the four fields. + * * @property code The code the browser came back with. * @property codeVerifier The PKCE verifier whose challenge started the sign-in. * @property redirectUri The redirect URI of the start, byte for byte. + * @property installationId The app's installation, a random lowercase UUID kept in its settings. Never shown and never + * returned. + * @property deviceName The name of a device registered by this sign-in ([DeviceLimits.MAX_NAME] characters, no control + * character); a device already registered keeps its name. + * @property platform `ios` or `android` for a phone ([DevicePlatforms]). + * @property osVersion The system's version, only with [platform] ([DeviceLimits.MAX_OS_VERSION] printable characters). */ @Serializable public data class MobileTokenRequest( val code: String, val codeVerifier: String, val redirectUri: String, + val installationId: String? = null, + val deviceName: String? = null, + val platform: String? = null, + val osVersion: String? = null, ) /** diff --git a/api/src/commonMain/kotlin/run/ostrom/core/api/models/Devices.kt b/api/src/commonMain/kotlin/run/ostrom/core/api/models/Devices.kt index 8eee161..7281d52 100644 --- a/api/src/commonMain/kotlin/run/ostrom/core/api/models/Devices.kt +++ b/api/src/commonMain/kotlin/run/ostrom/core/api/models/Devices.kt @@ -59,7 +59,7 @@ public data class RenameDeviceRequest( val name: String, ) -/** The values of [Device.platform] and [DeviceLoginStart.platform]. */ +/** The values of [Device.platform], [DeviceLoginStart.platform] and [MobileTokenRequest.platform]. */ public object DevicePlatforms { /** macOS. */ public const val MACOS: String = "macos" diff --git a/client/openapi.version b/client/openapi.version index daac391..ebf03de 100644 --- a/client/openapi.version +++ b/client/openapi.version @@ -1 +1 @@ -0.1.3648 +0.1.3658 diff --git a/client/src/commonMain/kotlin/run/ostrom/core/client/AuthClient.kt b/client/src/commonMain/kotlin/run/ostrom/core/client/AuthClient.kt index e4449d1..0af489a 100644 --- a/client/src/commonMain/kotlin/run/ostrom/core/client/AuthClient.kt +++ b/client/src/commonMain/kotlin/run/ostrom/core/client/AuthClient.kt @@ -46,13 +46,27 @@ public class AuthClient internal constructor(private val transport: Transport) { /** * Exchanges the one-time code of the browser's return for the app's token (`POST /api/v1/auth/mobile/token`). * The code works once, for 60 s, with the exact [redirectUri] of the start. + * + * @param installationId This installation of the app; pass it (and the three below) only to a server that + * announces `devices.v1`, so that the phone is one device. + * @param deviceName The name of a device this sign-in registers. + * @param platform `ios` or `android`. + * @param osVersion The system's version, with [platform]. */ - public suspend fun exchangeMobileCode(code: String, codeVerifier: String, redirectUri: String): MobileToken = - transport.jsonBody( - HttpMethod.Post, - "/api/v1/auth/mobile/token", - MobileTokenRequest(code, codeVerifier, redirectUri), - ) + @Suppress("LongParameterList") // The code and its proof, then the installation of startDeviceLogin, defaulted. + public suspend fun exchangeMobileCode( + code: String, + codeVerifier: String, + redirectUri: String, + installationId: String? = null, + deviceName: String? = null, + platform: String? = null, + osVersion: String? = null, + ): MobileToken = transport.jsonBody( + HttpMethod.Post, + "/api/v1/auth/mobile/token", + MobileTokenRequest(code, codeVerifier, redirectUri, installationId, deviceName, platform, osVersion), + ) /** * Records the phone's biometric unlock (`POST /api/v1/auth/mobile/unlock`), after which the app's token answers a diff --git a/client/src/commonTest/kotlin/run/ostrom/core/client/AuthClientTest.kt b/client/src/commonTest/kotlin/run/ostrom/core/client/AuthClientTest.kt index e52286a..d211fe9 100644 --- a/client/src/commonTest/kotlin/run/ostrom/core/client/AuthClientTest.kt +++ b/client/src/commonTest/kotlin/run/ostrom/core/client/AuthClientTest.kt @@ -6,6 +6,7 @@ import io.ktor.http.HttpStatusCode import io.ktor.http.Url import kotlinx.coroutines.test.runTest import kotlinx.serialization.json.Json +import run.ostrom.core.api.models.DevicePlatforms import run.ostrom.core.api.models.MemberRole import run.ostrom.core.api.models.SignUpPolicy import kotlin.test.Test @@ -68,6 +69,30 @@ class AuthClientTest { assertJson("""{"code":"code-1","codeVerifier":"verifier-1","redirectUri":"ostrom://cb"}""", body) } + @Test + fun aPhoneNamesItsInstallationWithTheCode() = runTest { + val server = ScriptedServer { + json("""{"token":"osm_1","user":{"login":"ada"},"unlockKey":"uk"}""") + } + server.client(token = null).use { + it.auth.exchangeMobileCode( + "code-1", + "verifier-1", + "ostrom://cb", + installationId = "0f8fad5b-d9cb-469f-a165-70867728950e", + deviceName = "Ostrom iOS · iPhone", + platform = DevicePlatforms.IOS, + osVersion = "18.1", + ) + } + assertJson( + """{"code":"code-1","codeVerifier":"verifier-1","redirectUri":"ostrom://cb",""" + + """"installationId":"0f8fad5b-d9cb-469f-a165-70867728950e","deviceName":"Ostrom iOS · iPhone",""" + + """"platform":"ios","osVersion":"18.1"}""", + server.single().second, + ) + } + @Test fun anUnlockIsSentWithItsApprovalOnlyWhenGiven() = runTest { val server = ScriptedServer { json("""{"authenticatedAt":"2026-11-13T10:00:00Z"}""") } diff --git a/docs/client.md b/docs/client.md index a5834af..ba034da 100644 --- a/docs/client.md +++ b/docs/client.md @@ -60,7 +60,7 @@ client.close() | Property | Calls | Routes | | --- | --- | --- | | (client) | `capabilities()` | `GET /api/v1/capabilities` | -| `auth` | `signInMethods()`, `githubStartUrl(redirectUri, state, codeChallenge)`, `startUrl(provider, …)`, `exchangeMobileCode(code, codeVerifier, redirectUri)`, `unlock(unlockKey, requestId)`, `me()`, `account()`, `logout()`, `startDeviceLogin(scopes)`, `pollDeviceLogin(deviceCode)` | `/api/v1/auth/providers`, `/api/v1/auth/{provider}/start` (a URL to open, no request), `/api/v1/auth/mobile/token`, `/api/v1/auth/mobile/unlock`, `/api/v1/auth/me`, `/api/v1/auth/me/account`, `/api/v1/auth/logout`, `/api/v1/auth/device`, `/api/v1/auth/device/token` | +| `auth` | `signInMethods()`, `githubStartUrl(redirectUri, state, codeChallenge)`, `startUrl(provider, …)`, `exchangeMobileCode(code, codeVerifier, redirectUri, installationId, deviceName, platform, osVersion)`, `unlock(unlockKey, requestId)`, `me()`, `account()`, `logout()`, `startDeviceLogin(scopes)`, `pollDeviceLogin(deviceCode)` | `/api/v1/auth/providers`, `/api/v1/auth/{provider}/start` (a URL to open, no request), `/api/v1/auth/mobile/token`, `/api/v1/auth/mobile/unlock`, `/api/v1/auth/me`, `/api/v1/auth/me/account`, `/api/v1/auth/logout`, `/api/v1/auth/device`, `/api/v1/auth/device/token` | | `organizations` | `mine()` | `GET /api/v1/me/organizations` | | `approvals` | `queue()`, `view(id)`, `answer(id, answer, shownDigest, note)` | `GET /api/v1/approvals/queue`, `GET /api/v1/approvals/{id}/view`, `POST /api/v1/approvals/{id}/answer` | | `receipts` | `list(before, limit)`, `get(id)`, `keys()` | `GET /api/v1/receipts`, `GET /api/v1/receipts/{id}`, `GET /api/v1/receipts/keys` | @@ -109,7 +109,8 @@ throws `403 step_up_required`, with `requestBound` when the unlock is bound to a **PKCE.** `Pkce.create(randomBytes)` draws a verifier (32 bytes, unpadded base64url) and its S256 `challenge`; `Pkce.base64Url(bytes)` encodes an OAuth `state`. The SHA-256 is Okio's, on every target. The app opens `auth.githubStartUrl(redirectUri, state, pkce.challenge)`, checks the `state` of the return, then calls -`auth.exchangeMobileCode(code, pkce.verifier, redirectUri)`. +`auth.exchangeMobileCode(code, pkce.verifier, redirectUri)`; with `devices.v1` it adds its installation id, the name +a new device takes and its platform, as `startDeviceLogin` does for a computer, so the phone is one device. **Event streams.** An internal reader serves every followed stream: lines bounded at 256 Ki characters and the data of one event at 1 Mi characters, comments (keep-alives), `id:` and `retry:` skipped (resumption uses the sequence in