feat: Add Bearer Token Support to key-auth Plugin
メンテナーはふだん 2 日以内に返信
まだ誰も着手していません。
- #12909 @r-it-ae による — マージされずにクローズ
評価
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 初心者へのやさしさ
- 45/100
- issue の種類
- 機能追加
- 明瞭さ
- おおむね明確
- 活発さ
- 停滞
- 技術スタック
- lua
- 領域
- api, authentication
調査の方向性
まず、key-auth プラグインの実装、そのスキーマ、および既存の認証テストを見つけます。設定されたヘッダーと WWW-Authenticate レスポンスがどのように処理されるかを追跡し、続いて bearer_token が有効な場合と無効な場合について、有効および無効な Authorization 値を含むテストカバレッジを追加します。要求されたトークン抽出、厳密な形式エラー、レスポンススキーム、後方互換性が検証されれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Description
Description
The key-auth plugin currently only supports custom header names (default: apikey) for API key authentication. However, it does not support the standard OAuth 2.0 Bearer token format (Authorization: Bearer <token>), which is widely used in modern APIs and is the standard way to pass authentication tokens.
Current Behavior
Currently, when using the key-auth plugin:
- The API key can be passed via a custom header (default:
apikey) or query parameter - The
headerparameter allows customization of the header name - Example:
apikey: my-secret-keyorX-API-KEY: my-secret-key
Expected Behavior
The plugin should support the standard Bearer token format:
- Accept tokens in the format:
Authorization: Bearer <token> - When enabled, the plugin should:
- Force the use of the
Authorizationheader (ignoring theheaderparameter) - Extract the token after the "Bearer " prefix
- Validate the token format strictly
- Update the
WWW-Authenticateresponse header to use "Bearer" scheme instead of "apikey"
- Force the use of the
Use Case
Many APIs and OAuth 2.0-based authentication systems use the Bearer token format as a standard. Supporting this format in the key-auth plugin would:
- Standards Compliance: Follow RFC 6750 (Bearer Token Usage) and OAuth 2.0 specifications
- Interoperability: Allow APISIX to work seamlessly with existing OAuth 2.0 clients and tools
- Migration: Enable easier migration from other API gateways that support Bearer tokens
- Developer Experience: Provide familiar authentication patterns that developers expect
Proposed Solution
Add a new boolean parameter bearer_token to the plugin schema:
{
"plugins": {
"key-auth": {
"bearer_token": true
}
}
}
When bearer_token is set to true:
- The plugin should read from the
Authorizationheader only - Extract the token after "Bearer " prefix
- Return
401with error message "Invalid Bearer token format" if the prefix is missing - Set
WWW-Authenticate: Bearer realm="key"in error responses
Example Configuration
# Create a consumer
curl "http://127.0.0.1:9180/apisix/admin/consumers" -X PUT \
-H "X-API-KEY: ${admin_key}" \
-d '{
"username": "jack",
"plugins": {
"key-auth": {
"key": "my-secret-token"
}
}
}'
# Create a route with bearer_token enabled
curl "http://127.0.0.1:9180/apisix/admin/routes/1" -X PUT \
-H "X-API-KEY: ${admin_key}" \
-d '{
"uri": "/protected",
"plugins": {
"key-auth": {
"bearer_token": true
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
# Valid request
curl "http://127.0.0.1:9080/protected" \
-H "Authorization: Bearer my-secret-token"
# Returns: 200 OK
# Invalid request (missing Bearer prefix)
curl "http://127.0.0.1:9080/protected" \
-H "Authorization: my-secret-token"
# Returns: 401 Unauthorized
# Response: {"message":"Invalid Bearer token format"}
Benefits
- Backward compatible (default:
false) - No breaking changes to existing configurations
- Follows OAuth 2.0 and RFC 6750 standards
- Simple to implement and use
- Improves security by enforcing standard token format
Alternative Considered
Using the existing header parameter set to "Authorization" doesn't work because:
- It doesn't validate the "Bearer " prefix
- The
WWW-Authenticateresponse header still uses "apikey" scheme - Tokens without "Bearer " prefix would be accepted, which is non-standard
Environment
- APISIX Version: [version]
- Operating System: [OS]
- Deployment: [Docker/Bare Metal/Kubernetes]
- 主要言語
- Lua
- スター
- 17.3k
- フォーク
- 3k
- 平均マージ
- 4日 16時間
- マージ済み PR(30日)
- 69
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
apache/apisix のほかの issue
-
bug: an admin key injected with ${{VAR}} that contains only digits becomes a number, so the correct key gets 401対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 2 日以内に返信
-
bug(body-transformer): runtime crash when request or response body is a JSON primitive対応中かも @bhuvan-somisetty が 5 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 2 日以内に返信
-
bug: openapi-to-mcp appends a second ? to a query-suffixed path template対応中かも @lordcheng10 が 5 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 83/100
メンテナーはふだん 2 日以内に返信
-
ai-proxy forwards client `Transfer-Encoding: chunked` but sends an unchunked body (upstream 400)対応中かも @locker95 が 3 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
メンテナーはふだん 2 日以内に返信
-
As a user, I want the opa plugin to propagate trace context (traceparent) to OPA, so that OPA spans are linked to the APISIX request trace対応中かも @GitNimay が 20 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
apache/apisix#14051 · コメント 3 件 ·
メンテナーはふだん 2 日以内に返信
似ている issue
-
bug discord guide:04-dustwallow-marsh locale:enUS lua-error triage v0.3.6
難易度 2/5 1〜3時間 初心者へのやさしさ 64/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
NaixGames/Polycosmos#79 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 79/100
google/executor.nvim#46 ·
-
forever from-game
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
openmap-world/openmap#96 ·
メンテナーはふだん 1 日以内に返信