简体   繁体   English

使用凭据创建新用户,然后在API中使用Doorkeeper获取该用户的令牌

[英]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.

相关问题 Doorkeeper-如果用户不是管理员,则拒绝访问令牌请求 - Doorkeeper - Reject access token request if user not an admin 获取用户的Facebook访问令牌 - Obtaining user's facebook access token 我有一个带有 Doorkeeper OAuth2 client_credentials 的 Rails APP API。 如何在控制器中获取不记名令牌信息? - I have an Rails APP API with Doorkeeper OAuth2 client_credentials. How to get bearer token information within the controller? 访问Active_model_serializers中的doorkeeper_token(或current_user) - Access doorkeeper_token (or current_user) in Active_model_serializers 创建新用户 - rails creating a new user 我可以使用多个(access_token,refresh_token)来通过Gatekeeper在不同的客户端/设备上为同一用户提供服务吗? - Can I have multiple (access_token, refresh_token) to serve the same user on different client/devices with doorkeeper? 将用户凭据发送到 rails API 的正确方法 - Proper way to send user credentials to rails API 创建新用户时出错 - Error when creating new user 使用Devise,Doorkeeper和OAuth2令牌进行API和应用程序身份验证 - API and Application authentication using Devise, Doorkeeper and OAuth2 token 使用Rails API和JSON创建用户? - Creating User with Rails API and JSON?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM