简体   繁体   English

我有一个带有 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?

I have an Rails APP API with Doorkeeper OAuth2 client_credentials.我有一个带有 Doorkeeper OAuth2 client_credentials 的 Rails APP API。 I want to get the token information within the controller and get the uid of the user who consumed the bearer token and send a logger.info to the logs.我想获取控制器中的令牌信息,并获取使用了不记名令牌的用户的 uid,并将 logger.info 发送到日志。 Seeking your suggestions / ideas in mind寻求您的建议/想法

I'm̀ not sure if I understood your question correctly but I'll try my best here.我不确定我是否正确理解了您的问题,但我会在这里尽力而为。

Once you set up your doorkeeper configuration a helper method called doorkeeper_token will be available in your controller.一旦您设置了 doorkeeper 配置,一个名为doorkeeper_token的辅助方法将在您的控制器中可用。 This method returns a Doorkeeper::AccessToken instance and should have the proper resource owner and application for the issued token.此方法返回一个Doorkeeper::AccessToken实例,并且应该具有适当的资源所有者和已颁发令牌的应用程序。

If you are using the client credentials flow, your issued token will only contain an application_id but no resource_owner_id (as the resource owner is your user).如果您使用客户端凭据流,您颁发的令牌将只包含application_id而没有resource_owner_id (因为资源所有者是您的用户)。

To have a resource owner a token has to be issued by the resource owner password credentials flow .要拥有资源所有者,必须由资源所有者密码凭据流颁发令牌。

As for the log, you could have a before_action in your application_controller (or whatever base controller you use) that calls a method or simply a proc to log the token information:至于日志,您可以在 application_controller(或您使用的任何基本控制器)中有一个before_action来调用方法或只是一个 proc 来记录令牌信息:

before_action :log_token_info

def log_token_info
  # log user name or whatever attribute you wish
  # for the user id you can simply access the doorkeeper.resource_owner_id
  logger.info "Token resource owner: #{doorkeeper.resource_owner.name}"
end

Same goes for the application related to the token与令牌相关的应用程序也是如此

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM