Warden Help

Decision and enforcement

EnforcementPointDefault(policies) from Getting started is a shortcut. Behind it are three parts that can be replaced one at a time: a policy source, an information point and a decision point.

Policy source

A PolicySource returns the allow and deny policies for a request. PolicySourceInMemory(allow, deny) holds two lists and returns them for every request.

Implement the interface to load policies from elsewhere, or to return only the policies that apply. This source keeps a separate set per resource type. A request for an unknown type gets no policies and is therefore denied:

/** Serves each resource type its own policies, so unrelated policies are never evaluated. */ class PolicySourceByResourceType(private val byType: Map<String, Policies>) : PolicySource { override suspend fun policies(request: AccessRequest?): Policies { val type = request?.resource?.get("type") ?: return everything() return byType[type] ?: Policies(allow = emptyList(), deny = emptyList()) } private fun everything() = Policies( allow = byType.values.flatMap { it.allow }, deny = byType.values.flatMap { it.deny }, ) }

The source receives the request as it was submitted, before enrichment. Called with null, it should return every policy.

Information point

An InformationPoint enriches a request before it is evaluated. This one fetches the roles of a subject that arrived with only an ID, and leaves other requests alone:

/** Looks up the roles of a subject that arrived with only an ID. */ class RolesInformationPoint(private val rolesOf: suspend (userId: String) -> List<String>) : InformationPoint { override suspend fun enrich(request: AccessRequest): AccessRequest { val subject = request.subject if (subject["type"] != "User" || subject.containsKey("roles")) return request val userId = subject["id"] as? String ?: return request return request.copy(subject = subject + ("roles" to rolesOf(userId))) } }

Information points can also supply environment attributes, such as the time of day:

/** Adds the current hour to the environment, for policies that depend on the time of day. */ class HourInformationPoint(private val hourOfDay: () -> Int) : InformationPoint { override suspend fun enrich(request: AccessRequest): AccessRequest = request.copy(environment = request.environment + ("hour" to hourOfDay())) }

InformationPointAggregate runs several in order. Each sees what the previous one added:

/** Information points run in order, each seeing the previous one's additions. */ fun blogInformationPoint( rolesOf: suspend (userId: String) -> List<String>, hourOfDay: () -> Int, ): InformationPoint = InformationPointAggregate( listOf( RolesInformationPoint(rolesOf), HourInformationPoint(hourOfDay), ))

InformationPointPassThrough does nothing and is the default.

Decision point

DecisionPointLocal evaluates in process. For each request it:

  1. enriches the request with the information point;

  2. fetches the policies for the request from the policy source;

  3. evaluates the allow policies, then the deny policies, against the enriched request.

Access is granted only when some allow policy grants and no deny policy does. A deny policy is the place for a rule that must hold regardless of the allow policies:

/** A deny policy: nobody deletes outside business hours, whatever the allow policies say. */ val deletionOutsideBusinessHours: Policy = allOf { action("type") equalTo "DELETE" add(not(BusinessHoursPolicy())) } fun blogDecisionPoint( rolesOf: suspend (userId: String) -> List<String>, hourOfDay: () -> Int, ): DecisionPoint = DecisionPointLocal( policySource = PolicySourceByResourceType( mapOf( "Article" to Policies( allow = articlePolicies, deny = listOf(deletionOutsideBusinessHours), ))), informationPoint = blogInformationPoint(rolesOf, hourOfDay), )

The returned AccessResponse holds the Access and the enriched request that was evaluated, so a caller can see what the policies saw. Access.Granted and Access.Denied carry the properties set by the policy that decided; the DSL policies set none.

checkAuthorizedBatch evaluates one subject, action and environment against a list of resources and returns a response per resource, in order.

Enforcement point

An EnforcementPoint wraps a decision point. EnforcementPointDefault throws NotAuthorizedException for a denial, with the evaluated request and the denial's properties:

fun blogEnforcementPoint( rolesOf: suspend (userId: String) -> List<String>, hourOfDay: () -> Int, ): EnforcementPoint = EnforcementPointDefault(blogDecisionPoint(rolesOf, hourOfDay))

filterAuthorization takes a FilterAccessRequest, in which each resource is paired with its attributes, and returns the resources that were granted. ArticleService.listArticles in Getting started uses it.

Implement EnforcementPoint yourself to enforce differently, for example by returning a result instead of throwing. The Ktor module provides one that also reports to its plugin; see Ktor.

14 November 2026