跳到主要内容

4.12. Client Authentication

4.12. Client Authentication

使用 HTTP 的 application 经常需要认证 client. HTTP 提供若干 authentication 机制:

  • HTTP Authentication: 使用 [HTTP] Section 11 中定义的 WWW-AuthenticateAuthorization header field. 常见 scheme 包括 Basic [RFC7617] 和 Digest [RFC7616] authentication.

  • Bearer tokens: 在 Authorization header field 中使用 Bearer scheme 携带 token (例如 OAuth 2.0 token).

  • Cookies: 使用 cookie [COOKIES] 维护 authenticated session.

  • TLS client certificates: 在 TLS layer 使用 certificate 认证 client.

  • Application-specific mechanisms: 由 application 定义的自定义 authentication mechanism.

application SHOULD 尽可能使用现有且充分理解的 authentication mechanism, 而不是定义新的机制. 使用 authentication 时:

  • application MUST 使用 TLS 保护传输中的 authentication credential.

  • application SHOULD 使用适当的 HTTP status code (特别是 401 Unauthorized403 Forbidden) 指示 authentication failure.

  • authentication 失败时, application SHOULD 提供清楚的 error message, 但 MUST NOT 泄露 sensitive information (例如某个 username 是否存在).

  • application SHOULD 考虑 authentication credential 的 lifetime 和 renewal.

  • application SHOULD 规定 authentication state 如何跨 request 维护 (例如使用 cookie, 或要求每个 request 都带 Authorization header field).

application SHOULD 意识到 authentication 不同于 authorization. authentication 确立 client 的 identity, 而 authorization 决定该 client 被允许做什么.