对于第三方身份验证失败,我应该使用什么 HTTP 代码?

What HTTP code should I use for a third party authentication failure?

我正在创建一个与第三方应用程序集成的应用程序。

为此,登录用户提交第三方集成的 API 密钥。

如果他们提交的 API 密钥无效 - (并且 return 来自第三方的 401),我应该 return 哪个 HTTP 响应?

从我的应用程序返回 401 听起来令人困惑,因为从前端的角度来看,不清楚它们是否未经我的应用程序或第三方应用程序的身份验证。

我很想给它一个 400 - 就好像他们提交了一个包含无效电子邮件地址的表单一样。

您可以使用状态代码 HTTP 状态代码 - 407(需要代理身份验证)。来自 Mozilla Developers Reference:

The HTTP 407 Proxy Authentication Required client error status response code indicates that the request has not been applied because it lacks valid authentication credentials for a proxy server that is between the browser and the server that can access the requested resource.

您的后端应用程序充当第三方 API 的代理,因此在这种情况下使用 407 是可以的。

这个问题似乎暗示认证失败是客户端发出请求的错误。在那种情况下,如果我必须选择一个代码,我可能会选择 return 403 Forbidden.

RFC 7231 §6.5.3对403代码的描述如下:

The 403 (Forbidden) status code indicates that the server understood the request but refuses to authorize it. A server that wishes to make public why the request has been forbidden can describe that reason in the response payload (if any).

If authentication credentials were provided in the request, the server considers them insufficient to grant access. The client SHOULD NOT automatically repeat the request with the same credentials. The client MAY repeat the request with new or different credentials. However, a request might be forbidden for reasons unrelated to the credentials.

An origin server that wishes to "hide" the current existence of a forbidden target resource MAY instead respond with a status code of 404 (Not Found).

此状态代码通常用作通用 'authentication failed' 响应,不太可能触发任何特定的身份验证机制,例如 401 可以强制浏览器显示 username/password 提示。身份验证失败的具体原因可以在响应正文中描述,可以是机器可读的形式(例如 JSON 或 XML),也可以是人类可读的文档(例如 HTML ).

代码 400 并不是最糟糕的选择,但它相当通用。

发送403总是好的

但如果需要,您可以添加 json 信息-

[Serializable]
[DataContract]
class Response
{
    [DataMember]
    public bool IsSuccess { get; set; }
    [DataMember]
    public string Message { get; set; }
    [DataMember]
    public object ResponseData { get; set; }

    public Response(bool status, string message, object data)
    {
        IsSuccess = status;
        Message = message;
        ResponseData = data;
    }
}

然后在响应中也添加以下信息

 return Json(new Response(false, "Login Failed, The user name or password provided is incorrect.", null));

发出请求时,您可以禁用登录按钮 - 只有在进行更新时才能启用按钮 - 客户端逻辑。

424 - Failed Dependency 非常适合这种情况。

The 424 (Failed Dependency) status code means that the method could not be performed on the resource because the requested action depended on another action and that action failed.

本例中间服务器登录成功,上游服务器以无效密钥为由拒绝完成认证。似乎 502 代码(错误的网关)适合这种情况,因为该代码代表充当网关的服务器(您的)并且正在从上游服务器(第三方)接收无效响应。

我会选择 400,它符合语义要求。用户提供了一些错误数据。正如您所说,401 / 403 意味着用户与您的网站之间存在问题,而不是您的网站与其他应用程序之间存在问题。

HTTP 1.1 RFC 第 1 节指出:

Introduction

每条超文本传输​​协议 (HTTP) 消息都是一个请求 或回应。服务器监听请求的连接, 解析接收到的每条消息,解释消息语义 与已识别的请求目标相关,并对此做出响应 带有一个或多个响应消息的请求。客户端构建 请求消息以传达特定意图,检查 收到回复以查看意图是否得到执行,以及 决定如何解释结果。本文件定义 HTTP/1.1 架构方面的请求和响应语义 在 [RFC7230].

中定义

因此状态码的定义是在客户端和服务器之间。对我来说,这意味着选择对其他(服务器到服务器、后端等)交互最有意义的那个。

这里提供的所有其他外来事物只会让服务的消费者感到困惑。

我会倾向于 407。 407 需要代理身份验证 此代码类似于 401(未授权),但表示客户端必须首先向代理进行身份验证。代理必须 return 一个 Proxy-Authenticate header 字段(第 14.33 节)包含适用于所请求资源的代理的质询。客户端可以使用合适的 Proxy-Authorization header 字段重复请求(第 14.34 节)。 "HTTP Authentication: Basic and Digest Access Authentication". 中解释了 HTTP 访问身份验证,如 Iskander 所述。

它最好地提示用户问题可能是什么。您也可以继续实施 proxy-authorization header 以完全符合规范。

使用 400 会让您的客户在构建请求时挠头寻找他做错了什么。

使用 401 或 403 保留它们用于您自己的 API 身份验证和授权更有意义。

502 提示用户问题出在上游,因此在您修复之前他可能会挂起。

407 不正确。在这种情况下,您的代码是代理并且已通过身份验证。是没有认证的国外系统

401 是合理的,但它会误导未验证的内容,因为客户端已通过您的系统验证。如果您的外国身份验证被推迟到 100Continue 之后,这也不起作用。

400 不正确,因为请求的格式有效,但外地代理的身份验证失败。

所有其他 4xx 回复很容易被忽略,因为这里不适用。

因此,403 Forbidden 在我看来是您在这种情况下唯一真正的选择:

403禁止 客户没有访问内容的权限;也就是说,它是未经授权的,因此服务器拒绝提供所请求的资源。与 401 不同,客户端的身份是服务器已知的。在这种情况下,也可以使用指示故障“根本原因”的状态消息进行响应。这实际上取决于您的应用程序的安全配置。

我的 $.02

我会支持 401。 401 Unauthorized 错误是一种 HTTP 状态代码,表示在用户首次使用有效的用户 ID 和密码登录之前,无法加载用户尝试访问的页面。如果用户刚刚登录并收到 401 Unauthorized 错误,这意味着用户输入的凭据由于某种原因无效。在我们的案例中,他们提交的 API 密钥无效。当我对错误代码感到困惑时,我使用了一个 activity 图。

下面是相同的 link :

https://i.stack.imgur.com/ppsbq.jpg