Skip to content

feat: add dingtalk-auth plugin#13381

Open
AlinsRan wants to merge 4 commits into
masterfrom
feat/dingtalk-auth
Open

feat: add dingtalk-auth plugin#13381
AlinsRan wants to merge 4 commits into
masterfrom
feat/dingtalk-auth

Conversation

@AlinsRan
Copy link
Copy Markdown
Contributor

Summary

Add the dingtalk-auth plugin that integrates DingTalk (Ding Talk / 钉钉) OAuth 2.0 authentication into APISIX routes.

How it works

  1. When a request arrives without a valid session cookie, the plugin checks for a DingTalk authorization code in a configurable query parameter (default: code) or HTTP header (default: X-DingTalk-Code).
  2. If no code is found, the request is redirected (302) to the configured redirect_uri (typically the DingTalk OAuth login page).
  3. If a code is present, the plugin exchanges it for an access token via the DingTalk token API (access_token_url), then retrieves user information from the DingTalk user info API (userinfo_url).
  4. The access token is cached in an LRU cache (TTL: 7000 s) to avoid redundant requests.
  5. Verified user information is stored in an encrypted lua-resty-session v4 cookie session. Subsequent requests carrying the session cookie bypass all DingTalk API calls.
  6. When set_userinfo_header is true (default), the upstream receives the user information in the X-Userinfo header as a Base64-encoded JSON object.

Key attributes

Attribute Default Notes
app_key Required
app_secret Required; stored encrypted
secret Required; 8–32 chars; stored encrypted
redirect_uri Required
code_query code Query param name for the auth code
code_header X-DingTalk-Code Header name for the auth code
cookie_expires_in 86400 Session cookie TTL in seconds
secret_fallbacks Supports zero-downtime key rotation

Plugin priority: 2430 (between key-auth 2500 and consumer-restriction 2400).

Changes

  • apisix/plugins/dingtalk-auth.lua — plugin implementation
  • t/plugin/dingtalk-auth.t — test suite (13 test cases)
  • docs/en/latest/plugins/dingtalk-auth.md — English documentation
  • conf/config.yaml.example — register plugin in default list
  • docs/en/latest/config.json — add to sidebar navigation
  • t/admin/plugins.t — register plugin in admin test list

@dosubot dosubot Bot added size:XL This PR changes 500-999 lines, ignoring generated files. enhancement New feature or request plugin labels May 17, 2026
@AlinsRan AlinsRan requested a review from Copilot May 18, 2026 01:09
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a new dingtalk-auth plugin that integrates DingTalk OAuth 2.0 authentication into APISIX routes. The plugin extracts an authorization code from query/header, exchanges it for an access token (with an LRU cache), retrieves user info from DingTalk, and persists the verified user in an encrypted lua-resty-session v4 cookie. It also optionally forwards user info to the upstream via an X-Userinfo header.

Changes:

  • New plugin implementation apisix/plugins/dingtalk-auth.lua with priority 2430 and encrypted app_secret/secret fields.
  • New test suite t/plugin/dingtalk-auth.t (13 cases) and registration in t/admin/plugins.t, apisix/cli/config.lua, and conf/config.yaml.example.
  • New English documentation docs/en/latest/plugins/dingtalk-auth.md and sidebar entry in docs/en/latest/config.json.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
apisix/plugins/dingtalk-auth.lua New plugin implementing DingTalk OAuth2 code-to-userinfo flow with session cookie.
apisix/cli/config.lua Registers dingtalk-auth in the default plugin list at priority 2430.
conf/config.yaml.example Adds dingtalk-auth to the example plugin list.
t/plugin/dingtalk-auth.t New test suite mocking DingTalk endpoints and exercising schema, redirect, code, session, and custom code-source paths.
t/admin/plugins.t Adds plugin to expected admin plugin list.
docs/en/latest/plugins/dingtalk-auth.md New English plugin documentation.
docs/en/latest/config.json Adds doc page to the English sidebar.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread apisix/plugins/dingtalk-auth.lua
Comment thread apisix/plugins/dingtalk-auth.lua Outdated
Comment thread apisix/plugins/dingtalk-auth.lua Outdated
Comment thread apisix/plugins/dingtalk-auth.lua Outdated
Comment thread apisix/plugins/dingtalk-auth.lua
Comment thread t/plugin/dingtalk-auth.t
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 8 comments.

Comment thread apisix/plugins/dingtalk-auth.lua
Comment thread apisix/plugins/dingtalk-auth.lua
Comment thread apisix/plugins/dingtalk-auth.lua Outdated
Comment thread apisix/plugins/dingtalk-auth.lua Outdated
Comment thread apisix/plugins/dingtalk-auth.lua Outdated
Comment thread apisix/plugins/dingtalk-auth.lua Outdated
Comment thread apisix/plugins/dingtalk-auth.lua Outdated
Comment thread t/plugin/dingtalk-auth.t
AlinsRan and others added 4 commits May 20, 2026 13:44
Add the dingtalk-auth plugin that integrates DingTalk OAuth 2.0
authentication into APISIX routes. The plugin:

- Validates a DingTalk authorization code from a configurable query
  parameter (default: "code") or request header (default:
  "X-DingTalk-Code")
- Exchanges the code for an access token via the DingTalk token API
  and caches it with a 7000-second TTL to avoid repeated token fetches
- Retrieves DingTalk user information and stores it in an encrypted
  cookie session (lua-resty-session v4)
- Forwards user information to upstream in the X-Userinfo header
  (Base64-encoded JSON) when set_userinfo_header is true
- Supports secret_fallbacks for zero-downtime session key rotation

Priority: 2430 (between key-auth 2500 and consumer-restriction 2400)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…nfo encoding error

- Check return value of sess:save() and return 500 if it fails, rather
  than silently proceeding with an unsaved session
- Handle encoding error for the X-Userinfo header gracefully instead of
  passing nil to base64_encode

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add dingtalk-auth to apisix/cli/config.lua so the plugin is loaded
by the APISIX runtime and recognized by the Admin API.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Add minimum=1 to timeout and cookie_expires_in to reject non-positive
  values that would cause undefined behavior
- Add minLength=1 to optional string fields (code_header, code_query,
  redirect_uri, userinfo_url, access_token_url) to reject empty strings
- Use direct truthiness check for set_userinfo_header (schema always
  provides a boolean default)
- Distinguish auth failures (errcode != 0 → 401) from transient upstream
  errors (network/decode/non-200 → 503) in fetch_userinfo
- Improve access token failure message: 'Failed to obtain access token'
  instead of the misleading 'Invalid configuration'
- Redirect to redirect_uri instead of returning 500 when session cookie
  contains corrupted JSON; this matches the no-session behavior
- Remove ctx.external_user assignment (no standard consumer in OSS APISIX)
- Add error message assertions to schema tests 2 and 3
- Add note in schema comment about secret_fallbacks encrypt_fields
  limitation (array traversal not supported)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@AlinsRan AlinsRan force-pushed the feat/dingtalk-auth branch from 6d03b36 to 0adcd44 Compare May 20, 2026 05:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request plugin size:XL This PR changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants