Reference
import Functions from "../../../../expressions/reference/_functions.mdx"; import Objects from "../../../../expressions/reference/_objects.mdx";
This page documents the expression policy execution environment in authentik.
Return values¶
An expression policy passes when it returns True and fails when it returns False.
return True
return False
Available helper functions¶
ak_message(message: str)¶
Add a user-visible message to the current policy result.
ak_message("Access denied")
return False
Available variables and objects¶
Expression-specific context¶
The imported object reference above covers the generic expression environment. The values below are the ones that are most specific to expression policies in authentik.
request¶
request is a policy request object with the following properties:
request.user: the current user against which the policy is being evaluated. See User.request.http_request: the Django HTTP request, when one exists. See the Django request documentation.request.obj: the object the policy is being evaluated againstrequest.context: a dictionary with dynamic data for the current execution
:::caution Flow User Context
When an expression policy runs inside a flow, request.user is not always the user who is currently being authenticated. Until a user_login stage updates the active user, this may still be AnonymousUser or another previously authenticated user.
In authentication and enrollment flows, use context["pending_user"] when you want the user currently being identified or enrolled.
If the user is not authenticated, request.user will be AnonymousUser, which is still an instance of authentik.core.models.User.
:::
Common flow-related values¶
The most commonly used values in flow-related expressions are:
context["pending_user"]: the user currently being identified or enrolled in a flowcontext["prompt_data"]: data collected from a prompt stagecontext["application"]: the application involved in the current authorization flowcontext["source"]: the source involved in the current login or enrollmentcontext["flow_plan"]: the active flow plan and its mutable contextcontext["geoip"]andcontext["asn"]: GeoIP and ASN lookups for the client IPak_client_ip: the parsed client IP address objectak_is_sso_flow: whether the current flow was initiated by an external identity provider source
geoip¶
context["geoip"] contains GeoIP information for the client IP.
Available fields include:
continent: a two-character continent code such asNAorEUcountry: the two-character ISO 3166-1 alpha codelat: approximate latitudelong: approximate longitudecity: city name, when available
return context["geoip"]["continent"] == "EU"
:::info Prefer GeoIP Policy For simple country matching, prefer a GeoIP policy. :::
asn¶
context["asn"] contains autonomous system information for the client IP.
Available fields include:
asn: the autonomous system numberas_org: the organization name for that ASNnetwork: the matching network
return context["asn"]["asn"] == 6939
:::info Prefer GeoIP Policy For simple ASN matching, prefer a GeoIP policy. :::
ak_client_ip¶
ak_client_ip is the parsed client IP address object. You can compare it with Python's ipaddress helpers:
from ipaddress import ip_network
return ak_client_ip in ip_network("10.0.0.0/24")
You can also inspect properties on the parsed IP object, for example:
return ak_client_ip.is_private
ak_client_ip.is_private follows Python's ipaddress semantics and returns True for addresses that are not globally reachable, not only for RFC1918 private ranges.
ak_is_sso_flow¶
ak_is_sso_flow is True when the current flow was initiated by an external identity provider source.
Flow context keys¶
When the policy runs inside a flow, authentik exposes the current flow context under context.
Common keys include:
context["flow_plan"]: the active flow plancontext["flow_plan"].context: the mutable flow-plan contextcontext["flow_plan"].context["redirect"]: the post-flow redirect URL, when setcontext["prompt_data"]: data collected from prompt stages or external sourcescontext["application"]: the application being authorizedcontext["source"]: the source being used for authentication or enrollmentcontext["pending_user"]: the user currently being identified or enrolledcontext["is_restored"]: the flow token when the flow was restored from a linkcontext["auth_method"]: the authentication method recorded by the flow