Authentik 2026.8 核心原理详解
Authentik Flows 状态机、OAuth2/SAML/Proxy Provider、Outpost 代理模型与 RBAC 机制详解,每机制均配本机 2026.8.4 实例实测命令与响应
Authentik 2026.8 核心原理详解
> 目标:拆解 Authentik 的四大核心机制——Flows 状态机、Provider 协议适配、Outpost 代理模型、RBAC 权限模型,每个机制均配本机 2026.8.4 实例的实测证据(命令与响应逐条照抄实测结果),不转述文档。
一、环境信息
- 操作系统:Ubuntu 22.04(x86_64)
- Authentik 版本:2026.8.4(
GET /api/v3/admin/version/实测{"version_current":"2026.8.4","version_latest":"2026.8.4",...}) - 部署形态:server + worker + PostgreSQL 16 三服务(官方 compose)
- API 端点:
http://localhost:13904/api/v3 - 实测时间:2026-10-18
- 认证方式:管理员 API Token(Bearer)+ OAuth2 client_credentials(两种方式均有实测)
二、机制总览
Authentik 的能力面由四层机制叠加而成:
| 机制 | 回答的问题 | 数据对象 |
|---|---|---|
| Flows 状态机 | 登录/授权旅程怎么走 | Flow、Stage、FlowStageBinding |
| Provider | 应用用什么协议接入 | OAuth2Provider、SAMLProvider、ProxyProvider |
| Outpost | 存量应用如何被代理保护 | Outpost 实例 + 服务账号 |
| RBAC | 谁能管什么 | User、Group、Role、权限对象 |
实例出厂自带 15 条 Flow、31 条托管 Blueprint(GET /api/v3/managed/blueprints/ 实测 count=31)。以下逐机制拆解并实测。
三、Flows 状态机机制
3.1 概念与数据结构
Flow 是一条带状态的旅程:每个 Flow 有 designation(authentication/authorization/invalidation/enrollment/stage_configuration 五类)与 slug;旅程中的「站点」是 Stage;Flow 与 Stage 通过 FlowStageBinding(含 order 序号)连接。运行时引擎按 order 逐站推进,每个请求会话(plan)由服务端保持,客户端凭 challenge 交互。
💡 技术拓展:认证流程编排业界主要有三种模型——
模型 代表 特点 固定管道 传统 LDAP/AD 登录步骤写死,改流程要改产品 可配置管道 Keycloak Auth Flow 步骤可增删排序,条件颗粒中等 状态机 + 策略引擎 Authentik Flows 每个绑定都可挂 Policy 动态跳过,颗粒最细 Authentik 实际采用第三种:绑定级 Policy 让同一 Flow 对不同用户呈现不同旅程。
3.2 设计动因
把「认证步骤」从代码里拿出来变成数据,管理员就能:在不升级/不重启的情况下改登录页顺序、按用户组跳过 MFA、为不同应用挂不同同意页。这是 Authentik 与同类产品的核心差异点。
3.3 本地实测:API 亲手建一条 Flow 并走通
第一步,创建一个 always-require 的同意 Stage(实测):
curl -s -X POST -H "Authorization: Bearer <API Token>" -H "Content-Type: application/json" \
http://localhost:13904/api/v3/stages/consent/ \
-d '{"name":"demo-consent-stage","mode":"always_require","consent_expire_in":"days=30"}'
实测返回:{"pk":"ff65ac71-57f7-4ec5-862b-bf38578ec11e","name":"demo-consent-stage",...}。
第二步,创建 authorization 型 Flow(实测):
curl -s -X POST -H "Authorization: Bearer <API Token>" -H "Content-Type: application/json" \
http://localhost:13904/api/v3/flows/instances/ \
-d '{"name":"Demo Consent Flow","slug":"demo-consent-flow","designation":"authorization","title":"Demo Consent","policy_engine_mode":"any","denied_action":"message_continue"}'
实测返回:{"pk":"2316e129-928b-48c6-9e9c-2bd4f4264020","slug":"demo-consent-flow",...}。
第三步,绑定 Stage 到 Flow(注意 2026.x 端点为 /flows/bindings/,旧版 /flowstagebindings/ 已不存在,实测 404)(实测):
curl -s -X POST -H "Authorization: Bearer <API Token>" -H "Content-Type: application/json" \
http://localhost:13904/api/v3/flows/bindings/ \
-d '{"stage":"ff65ac71-57f7-4ec5-862b-bf38578ec11e","order":0,"target":"2316e129-928b-48c6-9e9c-2bd4f4264020"}'
实测返回:{"pk":"57bfc595-75e3-4c46-83ba-a0f7cc443323","order":0,...}。
第四步,用 flow executor API 以匿名会话走通这条流程。GET 拿 challenge(实测):
curl -s -c /tmp/cookies.txt \
http://localhost:13904/api/v3/flows/executor/demo-consent-flow/
实测返回(节选):
{
"flow_info": {"title": "Demo Consent", "layout": "stacked", "cancel_url": "/flows/-/cancel/"},
"component": "ak-stage-consent",
"pending_user": "",
"token": "57ffe569-c1c2-4515-a969-38c56e50dba9"
}
第五步,POST 提交确认(必须携带 challenge 返回的 token,实测缺 token 报 This field is required.)(实测):
curl -s -b /tmp/cookies.txt -H "Content-Type: application/json" \
-X POST http://localhost:13904/api/v3/flows/executor/demo-consent-flow/ \
-d '{"component":"ak-stage-consent","confirm":true,"token":"57ffe569-c1c2-4515-a969-38c56e50dba9"}'
实测返回:
{"component": "xak-flow-redirect", "to": "/", "final_redirect": true}
final_redirect: true 即状态机判定流程走完。结论:Flow 创建→绑定→执行全链路可纯 API 完成,状态推进由服务端 plan 保持,客户端只需按 component 回帖。
3.4 本地实测:默认登录 Flow 全程(验证引导密码)
用出厂默认的 default-authentication-flow 以管理员账号走完整登录(同时验证初始密码已生效):
- GET executor 得到
component: ak-stage-identification; - POST
{"component":"ak-stage-identification","uid_field":"akadmin"},实测返回进入密码站:"component": "ak-stage-password"; - POST
{"component":"ak-stage-password","password":"<初始密码>"},实测返回{"component": "xak-flow-redirect", "to": "/", "final_redirect": true}; - 用会话 cookie 查询身份(实测):
curl -s -b /tmp/lc.txt http://localhost:13904/api/v3/core/users/me/
实测返回节选:
{
"user": {
"pk": 4,
"username": "akadmin",
"name": "authentik Default Admin",
"is_superuser": true,
"groups": [{"name": "authentik Admins", "pk": "a5a101fc-9684-44c2-98d4-160cdd500897"}],
"roles": []
}
}
错误密码时状态机停在原地并给出可读错误(实测):
{"password": [{"string": "Invalid password", "code": "invalid"}]}
3.5 调优关联
Flow 有独立缓存(cache_timeout_flows,实测 /root/config/ 返回 300 秒);改完流程后可调 POST /api/v3/flows/instances/cache_clear/ 强制刷新(端点在 OpenAPI Schema 实测存在)。
四、Provider 协议适配机制
4.1 概念与三类主协议
Provider 把「应用」与「Authentik」用标准协议粘合。OAuth2/OIDC、SAML、Proxy 三类覆盖绝大多数场景,2026.x 还提供 SCIM、RAC、LDAP Outpost 等扩展型 Provider。
4.2 OAuth2 Provider 实测:client_credentials 端到端
创建 Provider(注意 2026.x 三处格式变化:flow 传 UUID、invalidation_flow 必填、redirect_uris 传对象数组——用旧格式实测报错):
curl -s -X POST -H "Authorization: Bearer <API Token>" -H "Content-Type: application/json" \
http://localhost:13904/api/v3/providers/oauth2/ \
-d '{"name":"demo-api-provider","authorization_flow":"<authorization-flow-uuid>","invalidation_flow":"<invalidation-flow-uuid>","client_type":"confidential","grant_types":["client_credentials"],"access_token_validity":"hours=1","redirect_uris":[{"matching_mode":"strict","url":"http://localhost:9009/callback"}]}'
实测返回关键字段:
pk = 1
client_id = bw1sAlVoDMyNjZ8ge5LBpAJX2Y1lVnf4TApfXNOF
sub_mode = hashed_user_id
access_token_validity = hours=1
旧格式实测报错(留档):
{"authorization_flow":["“default-provider-authorization-implicit-consent” is not a valid UUID."],"invalidation_flow":["This field is required."],"redirect_uris":["This field is required."]}
创建 Application 并绑定 Provider(实测):
curl -s -X POST -H "Authorization: Bearer <API Token>" -H "Content-Type: application/json" \
http://localhost:13904/api/v3/core/applications/ \
-d '{"name":"Demo API","slug":"demo-api","provider":1}'
实测返回:{"pk":"fc3f388c-3864-4084-8006-0fddec16e727","name":"Demo API","slug":"demo-api",...}。
端到端换 token(实测,/application/o/token/ 为协议端点):
curl -s -u "<client_id>:<client_secret>" -X POST \
http://localhost:13904/application/o/token/ \
-d "grant_type=client_credentials&scope=openid goauthentik.io/api"
实测返回:
{
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"token_type": "Bearer",
"scope": "",
"expires_in": 3600,
"id_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
JWT payload 解码实测(HS256,claims 齐全):
{
"iss": "http://localhost:13904/application/o/demo-api/",
"sub": "b385c5a28e739e4dd3b3b38be9453700fccbe689e5da0fdb7c3b2990bf765d06",
"aud": "bw1sAlVoDMyNjZ8ge5LBpAJX2Y1lVnf4TApfXNOF",
"exp": 1792271115, "iat": 1792267515,
"acr": "goauthentik.io/providers/oauth2/default",
"uid": "p1PkU5tebTYmaWkmVDnnanXal0BdDkUB1EAlwITD"
}
发现(实测证据):client_credentials 模式下 Authentik 会自动创建类型为 service_account 的影子用户(实测 GET /api/v3/core/users/?type=service_account 返回 ak-demo-api-provider-client_credentials);其 token 默认 scope 为空、调管理 API 403——需把该用户加入授权分组后才可调 API(实测加入 authentik Admins 后 GET /api/v3/core/users/me/ 返回该服务账号身份)。这体现「协议身份 ≠ 管理权限」的最小权限设计。
4.3 SAML Provider 实测
curl -s -X POST -H "Authorization: Bearer <API Token>" -H "Content-Type: application/json" \
http://localhost:13904/api/v3/providers/saml/ \
-d '{"name":"demo-saml-provider","authorization_flow":"<uuid>","invalidation_flow":"<uuid>","acs_url":"https://sp.example.com/acs","audience":"sp-example","sp_binding":"post"}'
实测返回:pk = 3、audience = sp-example、sp_binding = post、acs_url = https://sp.example.com/acs。绑定 Application 后元数据即时可拉(实测):
curl -sL http://localhost:13904/application/saml/demo-saml-app/metadata/
实测返回(节选):
<md:EntityDescriptor ... entityID="http://localhost:13904/application/saml/demo-saml-app/metadata/">
<md:IDPSSODescriptor protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol">
<md:SingleLogoutService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect" Location="http://localhost:13904/application/saml/demo-saml-app/"/>
4.4 Proxy Provider 实测
curl -s -X POST -H "Authorization: Bearer <API Token>" -H "Content-Type: application/json" \
http://localhost:13904/api/v3/providers/proxy/ \
-d '{"name":"demo-proxy-provider","authorization_flow":"<uuid>","invalidation_flow":"<uuid>","external_host":"http://demo.internal:8080","internal_host":"http://127.0.0.1:8080","mode":"proxy","access_token_validity":"hours=1","refresh_token_validity":"days=30"}'
实测返回:pk = 2、external_host = http://demo.internal:8080、mode = proxy。
五、Outpost 代理模型机制
5.1 概念与两种形态
Outpost 是部署在业务应用前的小型反向代理,替后端做认证。两种形态:嵌入式(随 server 进程常驻,零额外部署)与手动(独立进程,适合跨网段/跨集群)。手动形态支持对接宿主编排系统的服务连接后自动派发。
5.2 本地实测:手动 Outpost 创建与绑定
创建手动 proxy outpost 并绑定 Provider 2(实测):
curl -s -X POST -H "Authorization: Bearer <API Token>" -H "Content-Type: application/json" \
http://localhost:13904/api/v3/outposts/instances/ \
-d '{"name":"demo-proxy-outpost","type":"proxy","providers":[2],"config":{"log_level":"info"}}'
实测返回:{"pk":"e4a7eafd-7655-4bc5-b754-009d97f5a2f5","name":"demo-proxy-outpost","type":"proxy","providers":[2],...}。
实测列表快照(嵌入式 + 手动并存):
authentik Embedded Outpost type= proxy providers= []
demo-proxy-outpost type= proxy providers= [2]
实测要点:
- 每个 Outpost 自动生成一个
internal_service_account型用户(ak-outpost-<pk去横线>),实测用户列表可见两条; - 嵌入式 Outpost 心跳正常时 health 返回 version/last_seen(实测
last_seen: 2026-10-17T20:09:30Z、version: 2026.8.4);手动 Outpost 未实际部署进程时 health 返回空数组[]——这正是「配置已建、实例未跑」的可观测信号; /outposts/proxy/端点为 Outpost 服务账号专用(管理员 Token 实测 403You do not have permission to perform this action.),体现控制面/数据面分离。
5.3 调优关联
Outpost 的 config 支持自定义日志级别、刷新间隔(默认 minutes=5,实测 config 字段 refresh_interval: "minutes=5");嵌入式 Outpost 由 server 进程内事件触发即时刷新(server 日志实测:received update trigger, refreshing outpost)。
六、RBAC 权限模型机制
6.1 概念
主体为 User 与 Group;2026.x 引入显式 Role 对象(实测 users/me 返回 roles: [] 字段)。权限绑定在「对象 + 角色/组」粒度;超管组 authentik Admins 出厂即有全量权限。
6.2 本地实测:分组与授权链路
创建分组(实测):POST /api/v3/core/groups/ -d '{"name":"demo-group","is_superuser":false}' 返回 pk=5e7af8a9-bf5b-4a21-bee3-cdc84da553ca。
用户入组(实测):POST /api/v3/core/groups/<pk>/add_user/ -d '{"pk":6}' 返回 HTTP 204;复查分组详情实测 users = ['qdemo']。
服务账号授权前后对比(实测):M2M token 直调 /api/v3/core/users/me/,入组前 403(body {"detail":"Token invalid/expired"}),把服务账号加入 authentik Admins 后返回 200 与该账号身份。RBAC 生效即刻体现在 API 授权结果上。
七、机制协作全景
一次「应用访问」串联四机制:
- 用户访问被 Proxy Provider 保护的地址 → 对应 Outpost 拦截;
- Outpost 把用户重定向到 authorization Flow(Flows 状态机接管:identification → password → [MFA] → consent);
- 流程完成后签发 token(OAuth2 Provider 层),Outpost 校验后放行到 internal_host;
- 全程的分组/策略判定走 RBAC 与 Policy 引擎;审计事件写入 PostgreSQL。
八、常见误区
- 以为还要装 Redis:2026.x 官方 compose 已无 Redis 服务(实测官网 compose 仅三服务);
- 把 client_credentials 的 token 当管理员 API Token:实测 scope 空、403,服务账号需显式授权;
- 用旧版 API 路径:
/flowstagebindings/、/tenants/tenants/实测 404,分别改为/flows/bindings/与/core/brands/; - 创建 OAuth2 Provider 用字符串 flow slug:实测必须传 UUID,且 redirect_uris 必须为对象数组。