Warden Help

Getting started

This guide protects a small blog with three rules:

  • Any user can read any article.

  • The author of an article can modify and delete it.

  • Only users with the Author role can create articles.

Model the domain

Users and articles extend HasAtts from warden-atts, which turns their public properties into attributes. The AttType adds a type attribute, so policies can tell a User from an Article. Actions are attribute maps too:

val UserType = AttType(type = "User", typeKeyword = "type") val ArticleType = AttType(type = "Article", typeKeyword = "type") /** A signed-in user. `HasAtts` turns its properties into the subject's attributes. */ data class User(val id: String, val roles: List<String>) : HasAtts(UserType) /** A blog post. Its properties, with the `type` attribute, are the resource's attributes. */ data class Article(val id: String, val title: String, val authorId: String) : HasAtts(ArticleType) /** What a user is trying to do to an article. */ enum class ArticleAction : HasAttsI { READ, CREATE, MODIFY, DELETE; override fun atts(): Atts = mapOf("type" to name) }

Article(id = "a1", title = "Warden", authorId = "ada").atts() is {type=Article, id=a1, title=Warden, authorId=ada}.

Write the policies

One allOf per rule. Each expression names a part of the request and an attribute path, then an operator and a value. resourceVal("authorId") refers to an attribute of the resource rather than a literal, so the last expression of the second policy compares the subject's ID with the article's author:

val articlePolicies: List<Policy> = 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 users with the Author role can create articles. allOf { resource("type") equalTo "Article" action("type") equalTo "CREATE" subject("roles") contains "Author" }, )

A policy that refers to an attribute the request does not have is denied, not an error. Policies lists every operator.

Enforce them

EnforcementPointDefault(allow, deny) builds an in-memory decision point from the policies. The service builds an AccessRequest for each operation and calls enforceAuthorization before doing anything:

/** * The service layer. Every operation builds an [AccessRequest] from the user, the action and the * article, and lets the enforcement point stop it before anything happens. */ class ArticleService( private val articles: ArticleStore, private val enforcementPoint: EnforcementPoint, ) { suspend fun readArticle(user: User, articleId: String): Article { val article = articles.get(articleId) enforcementPoint.enforceAuthorization( AccessRequest( subject = user.atts(), action = ArticleAction.READ.atts(), resource = article.atts(), )) return article } suspend fun createArticle(user: User, article: Article): Article { enforcementPoint.enforceAuthorization( AccessRequest( subject = user.atts(), action = ArticleAction.CREATE.atts(), resource = article.atts(), )) articles.save(article) return article } suspend fun deleteArticle(user: User, articleId: String) { val article = articles.get(articleId) enforcementPoint.enforceAuthorization( AccessRequest( subject = user.atts(), action = ArticleAction.DELETE.atts(), resource = article.atts(), )) articles.remove(articleId) } /** Only the articles this user may read. */ suspend fun listArticles(user: User): List<Article> = enforcementPoint.filterAuthorization( FilterAccessRequest( subject = user.atts(), action = ArticleAction.READ.atts(), resources = articles.all().map { ResourceAttributePair(it, it.atts()) }, )) }

When access is denied, enforceAuthorization throws NotAuthorizedException and the method ends there. The exception carries the evaluated request and the properties of the denial, for logging or for an HTTP status mapping.

listArticles shows the other operation an enforcement point offers. filterAuthorization evaluates the same subject and action against each resource and returns those that were granted.

Run it

val articles = ArticleStore(listOf(Article("a1", "Warden", authorId = "ada"))) val service = ArticleService(articles, EnforcementPointDefault(articlePolicies)) val bob = User(id = "bob", roles = emptyList()) service.readArticle(bob, "a1") // returns the article service.deleteArticle(bob, "a1") // throws NotAuthorizedException

The service never mentions roles, authors or URLs. Those live in the policies, which the tests in Testing exercise directly.

Next steps

  • Decision and enforcement: deny policies, and enriching a request that arrives with only an ID.

  • Ktor: make every HTTP route go through an enforcement point.

14 November 2026