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
14 changes: 14 additions & 0 deletions api/src/commonMain/kotlin/run/ostrom/core/api/models/Auth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion client/openapi.version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.3648
0.1.3658
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"}""") }
Expand Down
5 changes: 3 additions & 2 deletions docs/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down Expand Up @@ -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
Expand Down
Loading