Skip to content

Commit 2ef351d

Browse files
tanberryauthentik-automation[bot]
authored andcommitted
Cherry-pick #16878 to version-2025.4 (with conflicts)
This cherry-pick has conflicts that need manual resolution. Original PR: #16878 Original commit: dbf85d0
1 parent dc91c8e commit 2ef351d

5 files changed

Lines changed: 943 additions & 12 deletions

File tree

website/docs/customize/policies/expression.mdx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
title: Expression Policies
33
---
44

5-
Expression policies are perhaps the most flexible way to define specific implementations of flows and stages. With Expression polices, you can provide Python code to enforce custom checks and validation.
5+
Expression policies are perhaps the most flexible way to define specific implementations of flows and stages. When you [create](./working_with_policies.md#create-a-policy) an expression policy, you provide your own custom Python code to enforce custom checks and validation.
66

7-
The passing of the policy is determined by the return value of the code.
7+
The passing of the policy is determined by the return value of the code: `True` or `False`.
88

99
To pass a policy, use:
1010

@@ -27,9 +27,18 @@ if request.context["pending_user"].username == "marie":
2727
return False
2828
```
2929

30-
If the return is `True` that means Yes, use this stage (i.e. the user will get the MFA prompt). A return of `False` means remove this stage from the plan.
30+
When the policy returns `True`, the MFA validation stage is enforced. When it returns `False`, the MFA validation stage is skipped.
3131

32-
## Available Functions
32+
## Sample expression policies
33+
34+
Expressions policies with sample Python that we have documented:
35+
36+
- [Source-switching based on email address](./expression/source_switch.md)
37+
- [Managing flow context keys](./expression/managing_flow_context_keys.md)
38+
- [Unique email addresses](./expression/unique_email.md)
39+
- [Create an email address allow list](./expression/whitelist_email.md)
40+
41+
## Available functions
3342

3443
### `ak_message(message: str)`
3544

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
title: Switch which source is used based on email address
3+
---
4+
5+
You can use an expression policy to determine which [source](../../../../users-sources/sources/) (a set of user credentials and data, stored in authentik, Google, GitHub, etc) is used for a particular user, based on the domain of the email address the user enters when they log in and authenticate.
6+
7+
To switch which source is used for a specific user based on their email domain, you need to 1. and then 2. bind that policy to the appropriate stage.
8+
9+
## 1. Create an expression policy
10+
11+
[Create](../working_with_policies.md#create-a-policy) an expression policy] that does the following:
12+
13+
1. Maps the desired source for each user domain.
14+
2. Determines the user's domain based on their email address.
15+
3. Then "switches" the user to the desired source.
16+
17+
## Bind the policy to the stage
18+
19+
The new expression policy needs to be bound to the stage binding that comes after the Identification stage (or any custom stage that you might have created). For more information read our documentation about [bindings](../../../../add-secure-apps/flows-stages/bindings/), and for instructions to bind a policy, see [Bind a policy to a stage]](../../../../customize/policies/working_with_policies/#bind-a-policy-to-a-stage).
20+
21+
## Example expression
22+
23+
```python
24+
# This is a mapping of domains to sources
25+
# the key is a domain for the user and the value is the `slug` of the source to redirect to
26+
source_email_map = {
27+
"foo.bar.com": "entra-foo",
28+
"bar.baz.com": "entra-bar",
29+
}
30+
31+
user_email = request.context["pending_user_identifier"]
32+
33+
_username, _, domain = user_email.partition("@")
34+
source = source_email_map.get(domain)
35+
if not source:
36+
return True
37+
plan = request.context.get("flow_plan")
38+
if not plan:
39+
return False
40+
# For OIDC
41+
# plan.redirect(f"/source/oauth/login/{source}/")
42+
# For SAML
43+
plan.redirect(f"/source/saml/{source}")
44+
return False
45+
```

website/docs/customize/policies/index.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ In effect, policies determine whether or not a specific stage is applied to a fl
88

99
For example, you can create a policy that, for certain users, skips over a stage that prompts for MFA input. Or, you can define a policy that allows users to access a login flow only if the policy criteria are met. See below for other policies, including the reputation policy and an events-driven policy to manage notifications.
1010

11-
For instructions about creating and binding policies to flows and stages, refer to ["Working with policies](./working_with_policies.md)".
11+
## Create and manage policies
12+
13+
For instructions about creating and binding policies to flows and stages, refer to [Working with policies](./working_with_policies.md).
1214

1315
## Standard policies
1416

15-
The following policies are our standard, out-of-the box policies.
17+
The following are our standard, out-of-the box policies that you can [create and customize](./working_with_policies.md) as needed.
1618

1719
### Event-matcher policy
1820

@@ -61,9 +63,9 @@ The following rules can be set:
6163
- Minimum length.
6264
- Symbol charset (define which characters are counted as symbols).
6365

64-
Starting with authentik 2022.11.0, the following checks can also be done with this policy:
66+
The following checks can also be done with this policy:
6567

66-
- Check the password hash against the database of [Have I Been Pwned](https://haveibeenpwned.com/). Only the first 5 characters of the hashed password are transmitted, the rest is compared in authentik
68+
- Check the password hash against the database of [Have I Been Pwned](https://haveibeenpwned.com/). Only the first 5 characters of the hashed password are transmitted, the rest is compared in authentik.
6769
- Check the password against the password complexity checker [zxcvbn](https://github.com/dropbox/zxcvbn), which detects weak password on various metrics.
6870

6971
### Password Uniqueness Policy

website/docs/customize/policies/working_with_policies.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,27 @@ title: Working with policies
44

55
For an overview of policies, refer to our documentation on [Policies](./index.md).
66

7-
authentik provides several [standard policy types](./index.md#standard-policies), which can be configured for your specific needs.
7+
authentik provides several [standard policy types](./index.md#standard-policies), which can be configured for your specific needs. We also document several useful [expression policies](./expression.mdx#sample-expression-policies).
88

9-
We also document how to use a policy to [whitelist email domains](./expression/whitelist_email.md) and to [ensure unique email addresses](./expression/unique_email.md).
9+
:::info
10+
You can add expressions to our standard policies to further customize them.
11+
:::
1012

11-
To learn more see also [bindings](../../add-secure-apps/flows-stages/bindings/index.md) and how to [bind policy bindings to a new application when the application is created](../../add-secure-apps/applications/manage_apps.mdx#instructions) (for example, to configure application-specific access).
13+
To learn more, see the [bindings](../../add-secure-apps/flows-stages/bindings/index.md) and how to [bind policy bindings to a new application when the application is created](../../add-secure-apps/applications/manage_apps.mdx#instructions) documentation (for example, to configure application-specific access).
1214

1315
## Create a policy
1416

15-
To create a new policy, follow these steps:
17+
To create a new policy, _either a pre-configured one or an expression policy_, follow these steps:
1618

19+
<<<<<<< HEAD
1720
1. Log in as an admin to authentik, and go to the Admin interface.
1821
2. In the Admin interface, navigate to **Customization -> Policies**.
1922
3. Click **Create**, and select the type of policy.
23+
=======
24+
1. Log in to authentik as an administrator and open the authentik Admin interface.
25+
2. Navigate to **Customization** > **Policies**.
26+
3. Click **Create**, and select the type of policy. Here you select whether you want to create a custom expression policy, or a standard, out-of-the box one.
27+
>>>>>>> dbf85d04e (website/docs: add docs for source switch expression policy (#16878))
2028
4. Define the policy and click **Finish**.
2129

2230
## Bind a policy to a flow or stage
@@ -41,9 +49,15 @@ These bindings control which users can access a flow.
4149

4250
These bindings control which stages are applied to a flow.
4351

52+
<<<<<<< HEAD
4453
1. Log in as an admin to authentik, and open the Admin interface.
4554
2. In the Admin interface, navigate to **Flows and Stages -> Flows**.
4655
3. In the list of flows, click on the name of the flow to which you want to bind a policy.
56+
=======
57+
1. Log in to authentik as an administrator and open the authentik Admin interface.
58+
2. Navigate to **Flows and Stages > Stages**.
59+
3. In the list of stages, click on the name of the stage to which you want to bind a policy.
60+
>>>>>>> dbf85d04e (website/docs: add docs for source switch expression policy (#16878))
4761
4. Click on the **Stage Bindings** tab at the top of the page.
4862
5. Click the arrow (**>**) beside the name of the stage to which you want to bind a policy.
4963
The details for that stage displays.

0 commit comments

Comments
 (0)