node.js - OAuth 2.0 Flow 它是如何工作的 node-oauth2-server

标签 node.js oauth oauth-2.0

在 NodeJS 中使用 https://github.com/thomseddon/node-oauth2-server 实现 OAuth 服务器时

我正在尝试了解 OAuth 2.0 的流程

不知何故,我成功地实现了这个 npm 包实现,但我怀疑,出了点问题。

我会解释我是如何成功的。

第一个请求:

POST: http://localhost:3000/oauth/token
grant_type=password
client_id=1011
client_secret=somesecret
username=admin
password=admin

第一个响应:

{
token_type: "bearer"
access_token: "7f5261011fb0f84a4e193889fff4b7478f2a4cb2"
expires_in: 3600
refresh_token: "da83de41966979ced65b3841e1758335a811c0c2"
}

获取访问 token 后,我将发送另一个 http 调用

第二个请求:

GET http://localhost:3000/secret
Authorization: Bearer 7f5261011fb0f84a4e193889fff4b7478f2a4cb2

第二个回复:

{"data":"Secret area accessible"}

但在这里我完全困惑

问题1.Authorization_code部分缺失

问题 2。在第一次通话中,我需要发送 client_secret 和 user_password - 如果我发送这两个意味着 oauth 客户端正在向用户(浏览器)公开 secret 或用户正在向 OAuth 客户端提供密码。

如果整个 OAuth 2.0 的任何请求/响应模式如下所示,请与我分享

a. browser -> oauth server POST /oauth/authorize?client_id,username,password
b. USER GRANTS PERMISSION
c. browser -> oauth server RESPONSE auth_code
d. browser -> oauth client POST auth_code
e. oauth_client -> oauth server POST auth_code
e. oauth server -> oauth_client  RESPONSE access_token
f. oauth_client  -> resource_server POST /resource?access_token (Question 3. But here how resource server validates access token is valid or not )

最佳答案

OAuth 2.0 定义了几种通过所谓的“授权”获取访问 token 的方法。您的请求表明您当前正在使用资源所有者密码凭证授权,请参阅:https://www.rfc-editor.org/rfc/rfc6749#section-1.3.3 .该授权确实将用户名/密码暴露给客户端,这就是为什么它破坏了 OAuth 2.0 的大部分目的并且仅用于迁移目的,请参阅:https://www.rfc-editor.org/rfc/rfc6749#section-10.7

授权码授权是一种单独的授权类型,用户通过浏览器被重定向到授权端点,这样客户端就不会参与用户身份验证过程。您似乎在 a.-f 中描述的流程中提到了这一点。由于这是一种不同的授权类型,您不会在资源所有者密码凭据授权中看到“授权代码”。

在正确的授权码授予流程中,a.将是重定向而不是 POST,如:a。浏览器 -> oauth 服务器重定向/oauth/authorize?client_id,response_type=code

关于node.js - OAuth 2.0 Flow 它是如何工作的 node-oauth2-server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31424701/

相关文章:

node.js - 在 sails.js 中,如何访问 Controller 外部的 session 变量?

javascript - Uncaught Error : Minified React error #130

oauth - Instagram API : do scopes work with OAuth2 implicit authentication flow?

获取请求 token 时的 Android Flickr Oauth invalid_signature

oauth - 为什么需要将授权的 OAuth 1.0 请求 token 交换为访问 token ?

twitter - 如何在shopify中获取twitter feed

ruby-on-rails-4 - Doorkeeper::AuthorizationsController#new 中的 NoMethodError - Doorkeeper

javascript - 将 node.js 登录到文件,无需其他模块

oauth-2.0 - Sakai 可以允许用户使用 OAuth2 通过 Google 登录吗?

javascript - 如何使用 react typescript 在 Electron 中获取 Node 类型?