资源 · 43
OAuth 和 OpenID Connect:验证端到端集成
分别测试登录、委托和数据权限,然后测试重定向、令牌、会话和撤销。
已更新 · 16 min
本指南有助于实现的目标
- 将身份与授权分离。
- 拒绝意外重定向和令牌。
- 像测试成功一样仔细地测试拒绝操作。
- 验证用户离开或发生事件后访问权限的移除。
快速检查
- 预期使用哪个提供商和颁发者?
- 重定向 URI 是否已准确注册?
- ID 令牌是否被错误地用作访问令牌?
- 每个资源是否都检查所有权?
- 哪些访问权限在撤销后仍然有效?
分步指南
- 1
绘制已部署的流程图。
识别公共或机密客户端、浏览器、授权服务器、API 和 OIDC 提供商。 记录代码和令牌的传输路径、存储者以及授予的角色。 销售流程图并不代表实际部署。
交付内容:流程、信任边界和组件所有者。
- 2
检查登录事务。
使用适用于客户端的 PKCE 代码流。 检查 S256、事务绑定、已注册重定向和防止伪造请求的保护。 测试意外 URI、错误的验证器、重放代码和意外颁发者。 RFC 9700 根据客户端类型区分需求和建议。
交付内容:允许和拒绝的测试结果。
- 3
验证正确的令牌。
API 根据格式和提供商规则验证其访问令牌:签名或自省、颁发者、受众、过期时间和权限。 OIDC 客户端单独验证身份令牌。 有效的签名并不能证明该令牌属于此 API。
交付内容:验证合约、受众和过期时间测试。
- 4
检查业务权限
测试由其他帐户拥有的对象、较低角色、私有字段和管理操作。 范围或已验证的网关不能替代对请求资源的服务器端检查。 使用已授权的测试帐户和数据。
交付成果:角色、对象、操作和预期拒绝矩阵。
- 5
演练过期和撤销机制
分离本地会话、提供商会话、访问令牌和刷新令牌。 测试离场、设备丢失、角色变更、签名密钥轮换和提供商中断等情况。 记录剩余访问时间,而不是假设注销会立即撤销所有权限。
交付成果:访问权限移除时间表和例外情况。
- 6
在不泄露密钥的情况下进行观察
记录事务引用、验证决策、错误类别和配置版本。 排除代码、令牌和密钥。 准备诊断、集成关闭和回滚到已验证的配置;在提供商更改后重放测试。
交付成果:操作规程和回归测试。
虚构示例
示例情境
示例场景:两个客户端使用同一个身份提供商。 第一个客户端使用的令牌被提交给第二个客户端的 API。
决策及预期证据
API 拒绝了错误的受众,记录了无令牌跟踪,并且没有返回任何数据。 此拒绝操作成为回归测试。
区分机制
| 机制 | 目的 | 验证或限制 |
|---|---|---|
| OAuth 2.0 | 委托资源访问 | 服务器端对象权限 |
| OpenID Connect | 通过已验证的 ID 令牌建立身份 | 颁发者、受众和交易验证 |
| 应用程序会话 | 维护应用程序登录 | 过期、失效和会话保护 |
管理指标
| 指标 | 衡量内容 | 首要行动 |
|---|---|---|
| 更正拒绝 | 实际阻止禁止的情况 | 修复所有意外接受 |
| 撤销延迟 | 相关访问权限消失的时间 | 分别检查会话和令牌 |
| 验证失败 | 按原因和版本拒绝 | 将攻击与配置错误区分开来 |
常见错误
- 混淆身份验证和授权
- 检查签名后接受任何受众
- 注册广泛的重定向 URI
- 日志记录 便于调试的令牌
常见问题解答
PKCE 是否取代了所有控制?
否。 它保护了预期流程中的代码交换;令牌验证、重定向、业务授权和会话仍然需要检查。
JWT 是一种权限吗?
JWT 是一种格式。 声明只有在验证并应用 API 规则后才能使用。
是否应该从头开始构建验证?
建议使用维护良好的库和提供商文档,然后测试配置。 即使是正确的库也可能配置错误。
应保留什么证据以进行有效验证?
保留账户上下文、角色、资源和API响应。登录成功不证明每个资源的权限正确;不要记录会话秘密。
OAuth/OIDC:受众与权限
此语言版本的详细参考说明以英语提供。
Which token belongs to which recipient? A testable validation contract
A secure OAuth/OIDC integration connects each token to its purpose, issuer and intended recipient. An ID token tells the OIDC client about authentication; an access token is presented to a protected resource. Successful sign-in alone does not authorise a read, a change or an AI agent’s tool call.
This reference covers a specific profile: an API consuming JWT access tokens under RFC 9068. OAuth 2.0 does not require every access token to use JWT. Clients must treat their access tokens as opaque; the resource server owns the validation contract. The examples below are fictional, with no secret or usable signature.
Four objects with distinct purposes
| Object | Recipient and purpose | Boundary to retain |
|---|---|---|
| Authorisation code | Authorisation server token endpoint: redeem the code within the intended transaction. | The code is not an API permission; check binding to the client, redirect and PKCE. |
| ID token | OIDC client: validate identity and transaction; aud contains its client_id. | Do not reuse it as an API access credential; apply the OIDC rules for the relevant flow. |
| Access token | Resource server: access within the grant; its audience identifies the intended resource. | Validate the token, then operation and object permissions. The client must not depend on its internal content. |
| Refresh token | Authorisation server token endpoint: obtain new access tokens. | Do not send it to the resource server; its lifecycle differs from the local session. |
Eight controls for the validation contract
| Control | Rule and scope | Evidence without tokens |
|---|---|---|
| Profile and typ | For RFC 9068, check typ = at+jwt or application/at+jwt. Other profiles need their own rules; “any signed JWT” is too broad a contract. | Configured profile and a rejected ID/access token confusion case. |
| Algorithm and keys | Verify the signature with the expected issuer’s keys and algorithms allowed by configuration. RFC 9068 excludes alg = none; a received value does not set policy by itself. | Cryptographic policy version and result, with no secret material. |
| Issuer iss | Compare exactly with the expected issuer. A similar host, path or suffix does not establish trust. | Configuration reference and issuer_mismatch reason. |
| Audience aud | The expected recipient must appear in aud. An audience array is possible; finding the recipient does not replace other checks. | Expected resource and a test using a token intended for another API. |
| Expiry exp | Current time must precede exp; any small clock leeway is an explicit policy. The teaching fixture uses zero seconds of leeway. | Test clock, configured leeway and an exercised expiry boundary. |
| Scopes and object | Bind each scope to the resource and operation. Then check ownership, tenant and business policy on the server. RFC 9068 does not define all this authorisation. | Action, test object, applied rule and a cross-tenant denial. |
| OIDC transaction and PKCE | If nonce was sent, verify its presence and equality in the ID token. RFC 9700 requires PKCE for public clients and recommends it for confidential clients; bind the challenge to the transaction, with S256 recommended. | Wrong nonce, replayed code and wrong code_verifier tests in the authorised flow. |
| Access lifetime and removal | Separate access-token expiry, renewal and application session. RFC 9700 requires sender constraint or rotation to detect refresh-token replay for public clients. | Residual access timeline, user departure and an exercised revocation procedure. |
Read fictional claims without treating them as proof
Teaching clock: 2026-10-22 at 10:00:00 UTC. The JSON contains a readable header and claims only; no signed JWT serialization is supplied. exp is 10:05:00 UTC and iat is 09:59:00 UTC. The .example domains and identifiers belong solely to this scenario.
{
"header": {
"typ": "at+jwt",
"alg": "RS256",
"kid": "training-key-01"
},
"claims": {
"iss": "https://identity.example/",
"sub": "training-subject-17",
"aud": "https://api.example/evidence",
"exp": 1792663500,
"iat": 1792663140,
"jti": "training-event-001",
"client_id": "training-console",
"scope": "evidence:read"
}
}Decoding JSON does not validate a signature or establish trust in keys. Where stated, cryptography is an exercise assumption, never a check performed by this page. The profile also has required claims including sub, client_id, iat and jti: the eight-control table is not a complete validator. A subject does not necessarily identify a person; jti alone does not implement replay protection.
Ten review cases: decision, limit and expected evidence
01 · Token intended for the API: continue to business checks
Situation: iss, aud, typ and exp match the fictional contract; evidence:read is present. Signature verification is assumed solely for the exercise.
Decision: continue to operation and object checks, without automatically declaring “access allowed”. Expected evidence: business rule and test object reviewed separately.
02 · Assumed correct signature, wrong audience
Change: aud targets https://api.example/billing rather than the evidence resource. Other assumptions remain the same.
Decision: reject this token for this API. A correct signature does not change its recipient. Expected evidence: no resource content returned and a token-free audience failure record.
03 · Different issuer with plausible claims
Change: iss is https://other-identity.example/. An HTTPS URL does not make it equivalent to the configured issuer.
Decision: reject the issuer match. Expected evidence: exact comparison with https://identity.example/ and a regression test.
04 · Expiry reached at the exact second
Change: exp equals the test clock, 10:00:00 UTC, with zero seconds of leeway in this fixture.
Decision: reject as expired. “Now is before exp” is false at equality; this example does not establish a universal clock policy. Expected evidence: tests before, at and after the boundary under the real policy.
05 · ID token presented as an access token
Change: a fictional ID token has typ = JWT and aud = training-console. It targets the OIDC client.
Decision: reject it as an RFC 9068 JWT access token. Validating it at the OIDC client does not turn it into an access token. Expected evidence: separate validation rules for each token type.
06 · Write scope presented for a read operation
Change: scope is evidence:write while the call requires evidence:read.
Decision: deny the read under the exercise policy, which defines no write → read implication. Scope names and relationships belong to the contract. Expected evidence: an explicit operation/scope matrix, without inferring permissions from names.
07 · Readable JSON, unverified cryptography
Situation: the JSON is readable and values appear to match, but cryptographic validation and trusted keys are not established.
Decision: stop acceptance; the state is unverified. Expected evidence: configured library output and trust-contract checks. Reading claims alone must not release protected data.
08 · Correct token, another tenant’s object
Situation: test token checks are assumed to pass. The requested object belongs to another tenant and business policy denies this read.
Decision: deny the object. evidence:read does not grant access to every account’s evidence. Expected evidence: server-side cross-tenant test and no leakage through content or returned fields.
09 · Audience array containing the expected resource
Change: aud contains https://api.example/evidence and https://api.example/archive. The expected resource is listed.
Decision: this audience check can pass; continue all other checks. An array is not a wildcard. Expected evidence: scopes remain meaningful for the resource, with no ambiguous grant.
10 · ID token nonce differs from the transaction
Separate OIDC transaction, outside the API cases: sent nonce = training-transaction-A; received nonce = training-transaction-B.
Decision: reject the ID token for this transaction under OIDC Core §3.1.3.7. This does not replace PKCE or API rules. Expected evidence: compare with the original transaction and test missing nonce when one was sent.
Six useful acceptance-review records
- Map clients, issuers, APIs and expected audiences, with an owner for each boundary.
- Versioned validation contract: format, libraries, trusted keys, algorithms and clock leeway.
- Action, scope, tenant and object matrix; state denials and the permission implications actually defined.
- Synthetic fixtures, a fixed clock, negative variants and observed results in the authorised environment.
- Separate session, access-token and refresh-token timelines during departure, revocation and provider outage.
- Redacted log: internal transaction reference, policy version, decision reason and date; exclude tokens, codes and secrets.
Primary sources and reading scope
- IETF · RFC 9700 · OAuth 2.0 Security BCP · BCP 240 · January 2025
§2.1.1 PKCE · §2.2 replay · §2.3 privilege restriction · §4.14 refresh tokens - IETF · RFC 9068 · JWT access token profile · Standards Track · October 2021
§2.1 typ · §2.2 claims · §4 validation · §6 client treats access tokens as opaque - OpenID Foundation · OpenID Connect Core 1.0 · Final · incorporating errata set 2 · 2023-12-15
§2 ID Token · §3.1.3.7 issuer, audience, expiry and nonce - IETF · RFC 8725 · JWT Best Current Practices · BCP 225 · February 2020
§3.1 algorithms · §3.8–3.9 issuer and audience · §3.11–3.12 token type separation - IETF · RFC 6749 · OAuth 2.0 framework · Standards Track · October 2012 · read with RFC 9700
§1.3.1 authorization code · §1.4 access token · §1.5 refresh token · §3.3 scope
Sources reviewed on
官方参考文献
参考文献支持该方法。 请根据您的实际情况调整检查;它们并非认证。 原始参考文献标题和源文档可能使用其他语言。
参考资料查阅日期 .
