REST

Type-safe API routes, authorization DSL, endpoint documentation, and SSE support.

Typed API routes

Every route carries its parameter types, request body type, and response type at compile time. Four route variants cover all REST patterns:

Variant Use case
ApiRoute.Plain GET without parameters
ApiRoute.WithParams GET/DELETE with path or query parameters
ApiRoute.WithBody POST/PUT with a request body
ApiRoute.WithBodyAndParams POST/PUT with both params and body
// Define endpoints in shared code (client + server)
companion object {
    val GetUsers = TypedApiEndpoint.Get(
        uri = "/api/users",
        response = UserModel.serializer().apiList(),
    )
    val CreateUser = TypedApiEndpoint.Post(
        uri = "/api/users",
        body = CreateUserRequest.serializer(),
        response = UserModel.serializer().api(),
    )
}

// Mount on the server with handler, docs, and auth
val getUsers = GetUsers.mount {
    docs { name = "List users" }
    .codeGen { funcName = "getUsers" }
    .authorize { public() }
    .handle {
        ApiResponse.ok(userService.listAll())
    }
}

Authentication pipeline

Funktor ships three Ktor auth providers that together produce a typed Caller principal for every request. Install them in authentication { } and list them on the route's authenticate(...) block in the same order:

authentication {
    jwtCaller(AUTH_JWT, realm = "MyApp")             // Caller.JwtCaller   on a valid JWT
    apiKeyCaller(AUTH_API_KEY, realm = "MyApp") {    // Caller.ApiKeyCaller on a valid key
        token -> kontainer.get<ApiKeyService>().tryResolve(token)
    }
    anonymous(AUTH_ANON)                             // Caller.AnonymousCaller — terminal fallback
}

routing {
    authenticate(AUTH_JWT, AUTH_API_KEY, AUTH_ANON) {
        // ...routes
    }
}

Order matters — under Ktor's default FirstSuccessful strategy, the first provider that returns a principal wins. anonymous() always succeeds, so it must be listed last. Invalid credentials (expired JWT, revoked key) silently fall through to AnonymousCaller rather than 401-ing — routes that require real auth check User.isAnonymous().

UserRecord variants

currentUserProvider() dispatches on the Caller and produces a sealed UserRecord:

when (record) {
    is UserRecord.Anonymous -> // no credentials
    is UserRecord.System    -> // internal system actor (background jobs)
    is UserRecord.LoggedIn  -> // JWT-authenticated end-user
    is UserRecord.ApiKey    -> // API-key authenticated; carries keyId + keyName for audit
}

// Wire the provider into the per-call kontainer
installKontainer { call ->
    app.kontainers.create {
        with { call.currentUserProvider() }
    }
}

The default jwtCaller validates the bearer token via the kontainer-bound JwtGenerator. Pass a custom validate lambda when you need extra checks (revocation list, audience switching, multi-tenant verifier selection):

jwtCaller(AUTH_JWT) { token ->
    val payload = jwtGenerator.tryVerify(token) ?: return@jwtCaller null
    if (kontainer.get<RevocationList>().isRevoked(payload.subject)) return@jwtCaller null
    Caller.JwtCaller(payload)
}
API-key resolver checklist: hashed storage (Argon2/HMAC, never plaintext), constant-time comparison via MessageDigest.isEqual, fast-reject by prefix before any DB lookup, never log the raw token. The permissions on Caller.ApiKeyCaller defaults to UserPermissions.anonymous — derive privileges from your authoritative store, never from the caller's input.

The authorization floor

Every ApiRoutes group must declare an authFloor. It has no default, so a group cannot be written without deciding who may reach it — the floor is prepended to every route in the group, and it is append-only: a route can add restrictions, never remove them.

class OrgsApi : ApiRoutes("orgs", authFloor = { isSuperUser() }) {
    val list = ApiRoute.Get("", ...)
}

Floors are materialized and validated at group construction, so a malformed one aborts app start rather than surfacing as a hole at runtime.

Authorization DSL

Rules are statements, not values. Each call appends to the chain, and the chain is an AND. There is no and/or infix — to express alternatives, nest a block combinator.

.authorize {
    // The chain is an AND — both must hold
    authenticated()
    forPermission("orders.read")

    // Alternatives: nest forAny {} (OR) or forAll {} (AND)
    forAny {
        forRole("admin")
        forGroup("staff")
    }

    // Custom rule
    forCall("owns resource") { params ->
        user.id == params.ownerId
    }
}
Rule Passes for
authenticated()any non-anonymous caller
isSuperUser()super users
forUserType(t)callers of that user type
forRole(r) / forAnyRole(…)role holders
forGroup(g) / forAnyGroup(…)group members
forPermission(p) / forAnyPermission(…)permission holders
forCall(desc) { } a custom predicate over params/body
public() / forbidden()everyone / nobody — see below

public() and forbidden() must be the SOLE rule of a chain. Combined with anything else they silently become a no-op or an always-allow, so the builder rejects it at boot. To serve both a public and a protected audience, split them into separate ApiRoutes groups, each with its own floor. An empty forAll {} / forAny {} is rejected for the same reason — an empty AND allows everyone and an empty OR denies everyone.

The statements-only shape is deliberate. An expression DSL made the last expression silently win, which reads as a tightening and evaluates as a replacement — the kind of bug that looks correct in review and is wide open in production.
Authorization rules are also used for access estimation — the admin UI can grey out actions a user cannot perform, without making the actual API call.

SSE routes

Server-Sent Events routes stream data from server to client. Same type-safe pattern, but the handler receives a ServerSSESession.

val SseEvents = TypedApiEndpoint.Sse(uri = "/api/events")

val sseEvents = SseEvents.mount(Unit::class) {
    authorize { public() }
    .handle {
        while (true) {
            send(ServerSentEvent(data = Json.encodeToString(payload), event = "update"))
            delay(1000)
        }
    }
}

API features

Group related routes into ApiFeature implementations. Register them in Kontainer and they are auto-discovered and mounted.

class UserApiFeature : ApiFeature {
    override val name = "Users"
    override val description = "User management endpoints"

    val users = UserApi()
    val profiles = ProfileApi()

    override fun getRouteGroups() = listOf(users, profiles)
}