Skip to content
Open
Show file tree
Hide file tree
Changes from 49 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
0ee480e
feat(openid-connect): support stateless session revocation
Hockenba Aug 27, 2026
cf2cc5e
refactor(openid-connect): expose session revocation backend
Hockenba Aug 28, 2026
7572d22
fix(openid-connect): avoid unconditional revocation default
Hockenba Aug 28, 2026
0c3c35c
for lua-resty-openidc changes
Hockenba Sep 2, 2026
cac900b
update to newest openidc with my commit
Hockenba Sep 5, 2026
a56f816
Merge apache/master into feat/openid-connect-stateless-session-revoca…
Hockenba Sep 7, 2026
e9022c1
in memory cache config options
Hockenba Sep 15, 2026
5c1b46d
Merge branch 'master' of https://github.com/apache/apisix into feat/o…
Hockenba Sep 18, 2026
56083de
chore(deps): bump lua-resty-session to 4.3.0-1
Hockenba Nov 9, 2026
df39083
Merge branch 'master' of https://github.com/apache/apisix into feat/o…
Hockenba Nov 9, 2026
b420af1
fix(openid-connect): disable revocation error cache with the lookup c…
Hockenba Nov 9, 2026
ddcf668
test(openid-connect): cover revocation cache TTL normalization
Hockenba Nov 9, 2026
6afeda5
test(openid-connect): move revocation TTL test into the revocation suite
Hockenba Nov 9, 2026
07ecd1f
docs(openid-connect): explain the revocation lookup cache
Hockenba Nov 9, 2026
f494d1e
chore(openid-connect): drop the library version from a comment
Hockenba Nov 9, 2026
ad249f3
fix(openid-connect): override the revocation error TTL only where it …
Hockenba Nov 9, 2026
5ecfa78
chore(openid-connect): reword the revocation error TTL comments
Hockenba Nov 9, 2026
dfccf89
test(openid-connect): open the cached session in a separate request
Hockenba Nov 9, 2026
8641cc6
fix(openid-connect): address revocation review feedback
Hockenba Nov 16, 2026
9a90e3a
docs(openid-connect): describe lineage revocation in the session limi…
Hockenba Nov 17, 2026
aae225d
refactor(openid-connect): rely on the session and openidc fixes inste…
Hockenba Nov 17, 2026
48a3d2a
test(openid-connect): replay across workers in the fail-open/closed r…
Hockenba Nov 17, 2026
d278942
test(openid-connect): drop stub tests covered by real-service tests
Hockenba Nov 17, 2026
9f059bf
Merge branch 'apache:master' into feat/openid-connect-stateless-sessi…
Hockenba Nov 17, 2026
32f20cb
refactor(openid-connect): name the revocation store error check
Hockenba Nov 17, 2026
8cef328
refactor(openid-connect): use lua-resty-session to recognize revocati…
Hockenba Nov 18, 2026
1a35b65
refactor(openid-connect): match the revocation store error message again
Hockenba Nov 18, 2026
cf85818
test(openid-connect): replay the cookie issued by a refresh after logout
Hockenba Nov 18, 2026
cd0cad4
docs(openid-connect): define the revocation cache size variable with …
Hockenba Nov 18, 2026
db414b0
feat(openid-connect): create the session revocation storage once per …
Hockenba Nov 18, 2026
96ed640
refactor(openid-connect): create the revocation storage with the docu…
Hockenba Nov 18, 2026
f6a3cb4
fix(openid-connect): keep the revocation storage of up to 512 routes …
Hockenba Nov 18, 2026
ed4da6a
feat(openid-connect): share one revocation storage between routes tha…
Hockenba Nov 18, 2026
1b97fda
docs(openid-connect): routes with the same redis share a revocation c…
Hockenba Nov 18, 2026
5a976cd
test(openid-connect): turn the lookup cache off where the refreshed c…
Hockenba Nov 18, 2026
101ec52
refactor(openid-connect): pass the session configuration to the libra…
Hockenba Nov 19, 2026
ef973ee
docs(openid-connect): document the revocation error cache size variable
Hockenba Nov 19, 2026
628b9f1
docs(openid-connect): shorten the revocation notes
Hockenba Nov 19, 2026
354ff08
docs(openid-connect): drop an upgrade note that does not apply to a n…
Hockenba Nov 19, 2026
c8a4de5
docs(openid-connect): drop the same upgrade note from the Chinese docs
Hockenba Nov 19, 2026
8a00848
fix(openid-connect): name the session revocation cache entries by rou…
Hockenba Nov 19, 2026
91592d5
refactor(openid-connect): pass the session configuration to the libra…
Hockenba Nov 19, 2026
257e131
docs(openid-connect): routes with the same redis share the revocation…
Hockenba Nov 19, 2026
b734659
refactor(openid-connect): inline the revocation store error check
Hockenba Nov 19, 2026
281b1dc
docs(openid-connect): revocation markers live only in Redis
Hockenba Nov 19, 2026
4f7452f
Merge remote-tracking branch 'apache/master' into feat/openid-connect…
Hockenba Nov 19, 2026
ed2bdd5
Merge remote-tracking branch 'apache/master' into feat/openid-connect…
Hockenba Nov 20, 2026
32a9d73
test(openid-connect): retry a logout that failed on a Redis write; do…
Hockenba Nov 23, 2026
860f3bb
test(openid-connect): retry a logout that wrote only the lineage mark
Hockenba Nov 23, 2026
2002786
test(openid-connect): let TEST 7 allow the lineage mark of any ID
Hockenba Nov 24, 2026
9b91085
Merge remote-tracking branch 'apache/master' into feat/openid-connect…
Hockenba Nov 24, 2026
bd32fa7
test(openid-connect): use resty.redis for the ACL commands in TEST 7
Hockenba Nov 24, 2026
54f6b37
test(openid-connect): check the presented session id mark, widen the …
Hockenba Nov 24, 2026
2236517
test(openid-connect): always delete the ACL user of TEST 7
Hockenba Nov 24, 2026
a6eae16
docs(openid-connect): shorter revocation docs; clearer test names
Hockenba Nov 24, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apisix-master-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ dependencies = {
"api7-lua-resty-jwt = 0.2.6-0",
"lua-resty-hmac-ffi = 0.06-1",
"lua-resty-cookie = 0.4.1-1",
"lua-resty-session = 4.2.0-1",
"lua-resty-session = 4.3.0-1",
"lua-resty-openapi-validator = 1.0.6-1",
"opentracing-openresty = 0.1-0",
"lua-resty-radixtree = 2.9.2-0",
Expand Down
53 changes: 52 additions & 1 deletion apisix/plugins/openid-connect.lua
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,33 @@ local schema = {
description = "keepalive timeout in milliseconds",
},
}
}
},
revocation = {
type = "string",
enum = {"redis"},
description = "Redis-backed revocation for cookie sessions.",
},
revocation_fail_mode = {
type = "string",
enum = {"open", "closed"},
description =
"When the revocation store is unreachable, open treats the "
.. "session as not revoked and closed rejects open/destroy.",
},
revocation_cache_ttl = {
type = "integer",
minimum = 0,
description =
"Worker-local TTL in seconds for successful revocation "
.. "lookups. 0 disables. Default is 5.",
},
revocation_error_cache_ttl = {
type = "integer",
minimum = 0,
description =
"Worker-local TTL in seconds for revocation store errors. "
.. "0 disables. Default is 5.",
},
},
required = {"secret"},
["if"] = {
Expand All @@ -306,6 +332,19 @@ local schema = {
["then"] = {
required = {"redis"},
},
allOf = {
{
["if"] = {
required = {"revocation"},
},
["then"] = {
required = {"redis"},
properties = {
storage = { const = "cookie" },
},
},
},
},
additionalProperties = false,
},
realm = {
Expand Down Expand Up @@ -1467,6 +1506,18 @@ local function authenticate(plugin_conf, ctx)
return 401
end

-- With revocation_fail_mode closed, an unreachable revocation store
-- fails opening or destroying a session. Reject without destroying
-- the cookie.
if core.string.find(err, "unable to check session revocation")
or core.string.find(err, "unable to mark session revoked") then
if session then
session:close()
end
core.log.error("OIDC session revocation store unavailable: ", err)
return 503
end

-- Recoverable authorization-callback failures: a stale state
-- (replayed or pruned callback), or the ID provider redirecting
-- back with error=temporarily_unavailable, e.g. Keycloak after
Expand Down
40 changes: 39 additions & 1 deletion docs/en/latest/plugins/openid-connect.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ The `openid-connect` Plugin supports the integration with [OpenID Connect (OIDC)
| session.absolute_timeout | integer | False | | | Absolute session lifetime in seconds. Forwarded to lua-resty-session as `absolute_timeout`. |
| session.cookie.lifetime | integer | False | | | Deprecated. Mapped to `session.absolute_timeout` at runtime when `absolute_timeout` is not set. Use `session.absolute_timeout` instead. |
| session.storage | string | False | cookie | ["cookie", "redis"] | Session storage method. |
| session.redis | object | False | | | Redis configuration when `storage` is `redis`. |
| session.revocation | string | False | | ["redis"] | Session revocation backend. Set to `redis` to enable revocation for cookie sessions. Requires `session.redis` and cannot be used when `session.storage` is `redis`. |
| session.revocation_fail_mode | string | False | open | ["open", "closed"] | When the revocation store is unreachable, `open` treats the session as not revoked, while `closed` rejects session open and destroy operations. |
| session.revocation_cache_ttl | integer | False | 5 | | Worker-local TTL in seconds for successful revocation lookups. `0` disables. |
| session.revocation_error_cache_ttl | integer | False | 5 | | Worker-local TTL in seconds for revocation store errors. `0` disables. |
| session.redis | object | False | | | Redis connection. Required when `storage` is `redis`, or when `revocation` is `redis`. |
| session.redis.host | string | False | 127.0.0.1 | | Redis host. |
| session.redis.port | integer | False | 6379 | | Redis port. |
| session.redis.username | string | False | | | Redis username. |
Expand Down Expand Up @@ -357,6 +361,40 @@ spec:

See [Implement Authorization Code Grant](../tutorials/keycloak-oidc.md#implement-authorization-code-grant) for a complete example to use the `openid-connect` Plugin to integrate with Keycloak using the authorization code flow.

### Cookie Session Revocation

Cookie sessions are stored by the client and cannot normally be invalidated individually by APISIX. To enable server-side revocation, set `session.revocation` to `redis` and configure a Redis denylist:

```json
"session": {
"secret": "your-session-secret-min-16-chars",
"storage": "cookie",
"revocation": "redis",
"revocation_fail_mode": "closed",
"revocation_cache_ttl": 5,
"revocation_error_cache_ttl": 1,
"redis": {
"host": "127.0.0.1",
"port": 6379,
"prefix": "oidc:session:"
}
}
```

`revocation_fail_mode` defaults to `open`, which continues accepting sessions if the revocation store is unavailable. Use `closed` to reject session open and destroy operations instead. Opening a cookie session checks the revocation store, so each worker keeps a small cache of recent lookups to avoid one Redis call per request. `revocation_cache_ttl` is the worker-local TTL for successful lookups, and `revocation_error_cache_ttl` is the TTL for store errors. Set both `revocation_cache_ttl` and `revocation_error_cache_ttl` to `0` to check Redis on every request: while either is above `0`, the lookups that it covers are cached. Omit `revocation` to leave cookie-session revocation disabled.

A logout takes effect immediately on the worker that handled it and on other workers once their cached entry expires. `revocation_cache_ttl` therefore bounds how long a session whose revocation marker was written successfully can still be accepted by another worker. It does not cover the limitations below.

Routes that use the same `session.redis` settings share their cache in each worker, so a logout on one is seen at once by the others in that worker. Each worker holds up to 1024 cached lookups and 1024 cached store errors by default. To change that, define the nginx variables `$session_revocation_cache_size` and `$session_revocation_error_cache_size` where every request sees them, for example `map $host $session_revocation_cache_size { default 4096; }` in an `http` configuration snippet. Workers read them once, so a reload is needed.

#### Logout behavior and limitations

- **Logout revokes every cookie of the same login.** Each save of the session, such as a token renewal or rolling refresh, gives it a new session ID, so logout revokes the whole lineage: the cookie presented, older copies and refreshed ones, whether issued before or after the logout. A new login is not affected.
- **Cookies issued before lineage IDs existed are revoked one by one.** A session gets its lineage ID when it is first saved by a `lua-resty-session` version that has it. A cookie saved before that, and still valid after an upgrade, is revoked by its own session ID only, so logout does not revoke its older copies. They stop working at their timeout.
- **With `revocation_fail_mode` set to `open` (the default), a logout can succeed without revoking anything.** If Redis is unavailable during logout, the browser cookie is cleared but no revocation marker is written, and nothing retries it later. A copied cookie then stays valid until it times out. Use `closed` for deployments that rely on revocation, so that a failed revocation write is reported and the session is not destroyed.
- **Revocation markers live only in Redis.** If Redis loses its data, for example after a restart without persistence or a flush, revoked cookies are accepted again until they expire. Use a Redis with persistence if logouts must survive that.
- When Redis is unavailable, `closed` rejects requests that carry a cookie session with a `503` response.

### Authorization Code Flow with PAR and DPoP

To use Pushed Authorization Requests (PAR), set `par.enabled` to `true`. If `par.endpoint` is not configured, the Plugin uses the PAR endpoint from the well-known discovery document.
Expand Down
40 changes: 39 additions & 1 deletion docs/zh/latest/plugins/openid-connect.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ import TabItem from '@theme/TabItem';
| session.absolute_timeout | integer | 否 | | | 会话绝对生存时间(秒)。作为 `absolute_timeout` 透传到 lua-resty-session。 |
| session.cookie.lifetime | integer | 否 | | | 已弃用。当未设置 `session.absolute_timeout` 时,运行时会将该值映射到 `session.absolute_timeout`。请改用 `session.absolute_timeout`。 |
| session.storage | string | 否 | cookie | ["cookie", "redis"] | 会话存储方式。 |
| session.redis | object | 否 | | | `storage` 为 `redis` 时的 Redis 配置。 |
| session.revocation | string | 否 | | ["redis"] | 会话吊销后端。设置为 `redis` 可为 Cookie 会话启用吊销功能。必须配置 `session.redis`,且不能在 `session.storage` 为 `redis` 时使用。 |
| session.revocation_fail_mode | string | 否 | open | ["open", "closed"] | 当吊销存储不可用时,`open` 将会话视为未吊销,`closed` 则拒绝打开和销毁会话。 |
| session.revocation_cache_ttl | integer | 否 | 5 | | 成功吊销查询的 worker 本地缓存 TTL(秒)。`0` 禁用。 |
| session.revocation_error_cache_ttl | integer | 否 | 5 | | 吊销存储错误的 worker 本地缓存 TTL(秒)。`0` 禁用。 |
| session.redis | object | 否 | | | Redis 连接配置。当 `storage` 为 `redis`,或 `revocation` 为 `redis` 时必填。 |
| session.redis.host | string | 否 | 127.0.0.1 | | Redis 主机。 |
| session.redis.port | integer | 否 | 6379 | | Redis 端口。 |
| session.redis.username | string | 否 | | | Redis 用户名。 |
Expand Down Expand Up @@ -356,6 +360,40 @@ spec:

详见[实现授权码授权](../tutorials/keycloak-oidc.md#实现-authorization-code-grant),获取使用 `openid-connect` 插件与 Keycloak 集成并使用授权码流程的完整示例。

### Cookie 会话吊销

Cookie 会话存储在客户端,APISIX 通常无法单独将其失效。要启用服务端吊销,请将 `session.revocation` 设置为 `redis` 并配置 Redis 拒绝列表:

```json
"session": {
"secret": "your-session-secret-min-16-chars",
"storage": "cookie",
"revocation": "redis",
"revocation_fail_mode": "closed",
"revocation_cache_ttl": 5,
"revocation_error_cache_ttl": 1,
"redis": {
"host": "127.0.0.1",
"port": 6379,
"prefix": "oidc:session:"
}
}
```

`revocation_fail_mode` 默认为 `open`,即吊销存储不可用时继续接受会话。使用 `closed` 则会拒绝打开和销毁会话。打开 Cookie 会话时会查询吊销存储,因此每个 worker 会缓存最近的查询结果,避免每个请求都访问一次 Redis。`revocation_cache_ttl` 是成功查询的 worker 本地 TTL,`revocation_error_cache_ttl` 是存储错误的 TTL。将 `revocation_cache_ttl` 和 `revocation_error_cache_ttl` 都设为 `0` 才会让每个请求都查询 Redis:只要其中一个大于 `0`,它所涵盖的查询结果就会被缓存。省略 `revocation` 即不启用 Cookie 会话吊销。

登出在处理该请求的 worker 上立即生效,在其他 worker 上则在其缓存条目过期后生效。因此 `revocation_cache_ttl` 限定的是:吊销标记已成功写入的会话,在其他 worker 上仍可能被接受的最长时间。它不涵盖下面的限制。

使用相同 `session.redis` 配置的路由在每个 worker 中共享同一份缓存,因此其中一个路由处理的登出会立即被同一 worker 中的其他路由看到。默认情况下,每个 worker 最多缓存 1024 条查询结果和 1024 条存储错误。如需调整,请在所有请求都能访问的位置定义 nginx 变量 `$session_revocation_cache_size` 和 `$session_revocation_error_cache_size`,例如在 `http` 配置片段中添加 `map $host $session_revocation_cache_size { default 4096; }`。worker 只读取一次,因此修改后需要重新加载。

#### 登出行为与限制

- **登出会吊销同一次登录产生的所有 Cookie。** 会话每次保存(例如令牌续期或滚动刷新)都会生成新的会话 ID,因此登出会吊销整个谱系:当前提交的 Cookie、较早的副本和刷新得到的 Cookie,无论是在登出之前还是之后签发的。新的登录不受影响。
- **在引入谱系 ID 之前签发的 Cookie 只能逐个吊销。** 会话在首次由带有该功能的 `lua-resty-session` 版本保存时获得谱系 ID。在此之前保存、升级后仍然有效的 Cookie 只会按其自身的会话 ID 吊销,因此登出不会吊销它较早的副本,这些副本在超时后失效。
- **当 `revocation_fail_mode` 为 `open`(默认值)时,登出可能在没有吊销任何内容的情况下成功。** 如果登出时 Redis 不可用,浏览器中的 Cookie 会被清除,但不会写入吊销标记,之后也不会重试。被复制的 Cookie 在超时之前仍然有效。依赖吊销功能的部署请使用 `closed`,这样吊销写入失败时会返回错误,且不会销毁会话。
- **吊销标记只保存在 Redis 中。** 如果 Redis 丢失数据,例如在没有持久化的情况下重启或被清空,已吊销的 Cookie 在过期之前会再次被接受。如果登出必须在这种情况下仍然有效,请使用启用持久化的 Redis。
- 当 Redis 不可用时,`closed` 会对携带 Cookie 会话的请求返回 `503`。

### 使用 PAR 和 DPoP 的授权码流程

如需使用 Pushed Authorization Requests (PAR),请将 `par.enabled` 设置为 `true`。如果未配置 `par.endpoint`,插件将使用 well-known 发现文档中的 PAR 端点。
Expand Down
Loading
Loading