[英]Creating a new user with credentials, then obtaining a token for that user with Doorkeeper in an API
I'm building an API, protected by Doorkeeper. 我正在构建一个受门卫保护的API。
If I manually create the user (with password) in the backend, and then post the following to oauth/token
, Doorkeeper successfully generates an access token for the user and returns it: 如果我在后端手动创建用户(带密码),然后将以下内容发布到oauth/token
,则Doorkeeper会成功为用户生成访问令牌并将其返回:
data = {
username: $("#email_sign_in").val(),
password: $("#password").val(),
grant_type: 'password',
client_id: '880c16e50aee5893446541a8a0b3788....',
client_secret: 'a5108e1a1aeb87d0bb49d33d8c50d....',
provider: 'identity'
}
However, I'm trying to get my head around how I could do a sign up flow. 但是,我正试图了解如何进行注册流程。
I've happily got users/create
working, in so far as it creates a user and password, but I'm not sure how to then generate the Doorkeeper::AccessToken in the next step, and return it to the client. 我很高兴让users/create
工作,只要它创建用户和密码,但我不知道如何在下一步生成Doorkeeper :: AccessToken,并将其返回给客户端。 Ideally, after creating the user in the user#create action I'd then redirect to POST to oauth/token
, with the user's name and password, but I know that you can't redirect to a POST. 理想情况下,在用户oauth/token
操作中创建用户之后,我会使用用户的名称和密码重定向到POST到oauth/token
,但我知道您无法重定向到POST。
I've had a dig around the Doorkeeper source, but am getting a bit lost in all this clever middleware. 我已经对门把源进行了挖掘,但是在这些聪明的中间件中我有点迷失了。 Any advice on this is greatly appreciated! 对此有任何建议非常感谢!
It was the simplest of things! 这是最简单的事情! I was overcomplicating it by trying to POST, when in actual fact I could simply generate the DoorKeeper::AccessToken in user#create, and then return this. 我试图通过POST过度复杂化,实际上我可以在用户#create中生成DoorKeeper :: AccessToken,然后返回它。
Here's the code to generate the token: 这是生成令牌的代码:
access_token = Doorkeeper::AccessToken.create!(:application_id => application_id, :resource_owner_id => user_id)
I dig a bit in the doorkeeper source code, like the way that creating token using standard api way, you'd better using the following method if you are manually doing this. 我在门卫源代码中挖掘了一下,就像使用标准api方式创建令牌一样,如果你手动执行此操作,最好使用以下方法。
find_or_create_for(application, resource_owner_id, scopes, expires_in, use_refresh_token)
for your case 为你的情况
access_token = Doorkeeper::AccessToken.find_or_create_for(application: application, resource_owner_id: user_id)
link to source code of doorkeeper find_or_create_for in doorkeeper 链接到门卫的门卫find_or_create_for的源代码
In rails we can create access token using DoorKeeper using: 在rails中,我们可以使用DoorKeeper创建访问令牌:
Doorkeeper::AccessToken.create!(
application_id: nil,
resource_owner_id: user.id,
expires_in: 2.hours,
scopes: 'public'
)
Ideally, the best answer is not the one you posted, I think itś better to create a controller that inherits from Doorkeeper::TokensController
: 理想情况下,最好的答案不是你发布的那个,我认为最好创建一个继承自Doorkeeper::TokensController
的控制器:
# app/controllers/custom_tokens_controller.rb
class CustomTokensController < Doorkeeper::TokensController
# Override create action
def create
(... your custom code ...)
super
end
end
Then in routes.rb
define a new route like post 'custom_tokens', to: 'custom_tokens#create'
or whatever naming you prefer, but the action should be create
. 然后在routes.rb
定义一个新的路由,比如routes.rb
post 'custom_tokens', to: 'custom_tokens#create'
routes.rb
post 'custom_tokens', to: 'custom_tokens#create'
或者你喜欢的任何命名,但是应该create
动作。
More details about this solution here: https://github.com/doorkeeper-gem/doorkeeper/wiki/Using-Resource-Owner-Password-Credentials-flow 有关此解决方案的更多详细信息,请访问: https : //github.com/doorkeeper-gem/doorkeeper/wiki/Using-Resource-Owner-Password-Credentials-flow
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.