Skip to content
lgwillmorePublic

About

Kotlin Attribute Based Access Control

Topics

Resources

Stars

27 stars

Watchers

1 watching

Forks

Repository files navigation

Warden

Check Maven Central License: MIT

Warden is attribute based access control (ABAC) for Kotlin on the JVM. You write authorization rules as policies over the attributes of a request: who is asking, what they want to do, what they want to do it to, and in what context. Warden evaluates them and guards your code, with no dependency on a web framework.

Guides · API reference

What you write

Policies are plain Kotlin values built with a small DSL. They talk about your domain, not your URLs:

val articlePolicies = listOf(
    // Any user can read any article
    allOf {
      resource("type") equalTo "Article"
      action("type") equalTo "READ"
    },
    // The author of an article can modify and delete it
    allOf {
      resource("type") equalTo "Article"
      action("type") isIn listOf("MODIFY", "DELETE")
      subject("id") equalTo resourceVal("authorId")
    },
    // Only authors can create articles
    allOf {
      resource("type") equalTo "Article"
      action("type") equalTo "CREATE"
      subject("roles") contains "Author"
    },
)

What you get

An enforcement point protects any execution path. If no policy grants access, it throws NotAuthorizedException and the code after it never runs:

val enforcementPoint = EnforcementPointDefault(articlePolicies)

suspend fun readArticle(user: User, articleId: String): Article {
  val article = articles.getById(articleId)
  enforcementPoint.enforceAuthorization(
      AccessRequest(
          subject = user.atts(),
          action = mapOf("type" to "READ"),
          resource = article.atts(),
      ))
  return article
}

The complete example, with its tests, is in docs/guides/snippets and is explained step by step in Getting started.

  • Rules live with your business logic, not in routing. One rule protects every entry point that needs it.
  • Attributes are maps. Subjects, actions, resources and environments are bags of attributes, so a check can use a partial object, merge attributes from several sources, or run against data from another service.
  • Decision and enforcement are separate. A DecisionPoint evaluates allow and deny policies; an EnforcementPoint turns a denial into an exception or filters a collection down to what is permitted.
  • Information points enrich requests. Load roles, ownership or anything else a policy needs before evaluation, without the caller knowing.
  • The web adapters close the doors you forgot. With the Ktor plugin, the http4k filter or the Spring WebFlux filter installed, every route must call the enforcement point or be explicitly opened, or it answers 403.

Install

repositories {
    mavenCentral()
}

dependencies {
    implementation("codes.laurence.warden:warden-core:<version>")
    implementation("codes.laurence.warden:warden-atts:<version>") // domain objects to attribute maps
    implementation("codes.laurence.warden:warden-ktor3:<version>") // Ktor plugin
    implementation("codes.laurence.warden:warden-http4k6:<version>") // http4k filter
    implementation("codes.laurence.warden:warden-springboot4-webflux:<version>") // Spring WebFlux filter, Spring Boot 4
}
Artifact Contents
warden-core Access requests, the policy DSL, decision, information and enforcement points
warden-atts HasAtts and AttType: turn domain objects into attribute maps by reflection
warden-ktor3 A Ktor 3 server plugin that makes enforcement mandatory per route
warden-http4k6 An http4k 6 filter that makes enforcement mandatory per route
warden-springboot4-webflux A Spring WebFlux filter for Spring Boot 4 that makes enforcement mandatory per route, auto-configured
warden-springboot4 Shared by the Spring adapters: route configuration, an enforcement point and denial handling

Requires JDK 17 or later; warden-http4k6 requires JDK 21, as http4k 6 does.

Documentation

Guide Covers
Key concepts Access requests, policies, decision, information and enforcement points
Getting started Policies and enforcement for a small blog
Policies Expression, boolean and collection member policies
Decision and enforcement Policy sources, information points, decision and enforcement points
Attributes warden-atts helpers to build attributes
Testing Testing policies, services and routes
Ktor The Ktor plugin
http4k The http4k filter
Spring WebFlux The Spring WebFlux filter and its Spring Boot auto-configuration

The API reference covers every public type.

Contributing

Requires JDK 17. Gradle downloads the JDK 21 toolchain that warden-http4k6 and the guide snippets build with.

./gradlew test
./gradlew ktfmtFormat   # before opening a PR
  • AGENTS.md holds the design rules the library is built to.
  • docs/roadmap.md lists outstanding work.
  • The guides are a Writerside project in docs/guides; open it with the Writerside plugin for IntelliJ IDEA to preview them. The code they quote lives in docs/guides/snippets and is compiled and tested by ./gradlew test. ./gradlew dokkaGenerate builds the API reference.
  • Releases are described in PUBLISHING_SETUP.md.

Licensed under MIT.

About

Kotlin Attribute Based Access Control

Topics

Resources

Stars

27 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages