Kest

Search

Search the documentation

Security

Authorization

Decide what a signed-in caller may do: roles and your own rules.

Once you know who is calling (authentication), authorization decides what they may do. There are two levels, and a real application needs both:

  1. Coarse: roles. "Only admins may delete users."
  2. Fine: ownership and rules. "A customer may read their own orders, not others'."

Roles

A role is a label on a user (ADMIN, SUPPORT, USER). Put them in the token or the principal, and require them on endpoints:

@Controller("/orders") @Authenticated                 // every method needs a signed-in caller
class OrdersController {
    @Get fun mine() = ...
    @Public @Get("/health") fun health() = "ok"        // opts one method out
}
 
@Controller("/admin")
class AdminController {
    @Get("/stats") fun stats() = ...                   // open
    @Authenticated @Roles("admin") @Delete("/{id}") fun remove(@Param id: String) = ...   // only this one is protected
}
  • @Authenticated on a class covers all its methods; on a method it covers that one. No global guard is needed.
  • @Roles("admin", "support") means any of these roles; @Role("ADMIN") is one role. They work with any guard or authenticator that sets the roles of the caller (JwtAuthGuard reads them from the token's roles claim).
  • A missing or bad credential is 401. A signed-in caller who lacks the role gets 403.
  • It works the same on GraphQL resolvers and WebSocket gateways.
  • AuthenticationModule.forRoot(ApiKeyStrategy(...), BasicStrategy { user, pass -> ... }) adds other ways to sign in. A global useGuards(JwtAuthGuard::class) that has already set the principal is respected.
Loading diagram…

Your own rules: guards

Anything more specific than a role is a guard. The classic case is ownership:

@Injectable
class OwnerGuard(private val orders: OrderRepository) : Guard {
    override suspend fun canActivate(ctx: ExecutionContext): Boolean {
        val id = ctx.http.pathParams["id"] ?: return true
        val order = orders.findById(id) ?: return true                       // let the handler answer 404
        return order.ownerId == ctx.principal?.id                            // only the owner may touch it
    }
}
 
@Get("/{id}") @UseGuards(OwnerGuard::class) fun one(@Param id: String) = ...

Prefer to check ownership in the query when you can: orders.findByIdAndOwner(id, user.id) returns nothing for someone else's order, so the rule cannot be forgotten and no extra read is needed.

Hide, or forbid?

For someone else's resource, returning 404 (not 403) avoids confirming that the resource exists. Choose per case: a private order: 404; an admin page: 403.

Common mistakes

  • Checking only in the UI. The server must enforce every rule: anyone can call your API directly.
  • Trusting a role the client sent (a field in the body). Roles come from the verified token or database, never from the request.
  • Forgetting a new endpoint. Safer defaults: put @Authenticated on the controller, and mark the few open ones @Public, so a new method is protected unless you decide otherwise.
  • Checking the role but not the owner. An authenticated USER must still not read other users' data.

There is no CASL-style policy engine: rules are ordinary Kotlin in guards, which is easy to read and test.

Testing

For each protected endpoint test four calls: no credentials (401), a user without the role (403), the right role (200), and a user acting on someone else's data (403 or 404).

Read more