ruby-on-rails - 在登录重定向之前访问 Doorkeeper 应用程序信息
问题描述
解释
我想停止客户端应用程序(运行 OAuth2)进入父应用程序(运行 Doorkeeper)的授权过程,以查看哪个客户端应用程序正在请求登录。这样我就可以查找 clientID 并为客户端应用程序动态构建自定义登录屏幕。现在,我的客户去父, AuthorizationController 被调用,但在 new 被调用之前,我可以得到params[:client_id]
, authenticate_resource_owner!
被调用before_action
。然后,如果用户尚未使用父级登录,则会将用户发送到登录页面。所以,在我得到参数之前,它被重定向了。
问题
authenticate_resource_owner!
保存在 Doorkeeper 帮助文件中。我认为我正确设置它以绕过默认帮助程序并转到我的那里我可以尝试获取参数并在重定向之前保存在会话中,但我想我的路线设置不正确,我找不到任何有关如何正确调用它的文档。任何人都可以帮忙吗?
代码
设置客户端的代码:
def setup_client
@client = Application.find_by(uid: params[:client_id])
session[:client_name] = @client.name
authenticate_resource_owner!
end
我知道前两行在我将它们放在CustomAuthorizationsController
with a中时起作用byebug
,它在登录后和重定向回客户端之前触发,并显示存储在会话变量中的客户端名称。
在我的config/routes.rb
use_doorkeeper do
controllers :applications => 'doorkeeper/custom_applications'
controllers :authorizations => 'doorkeeper/custom_authorizations'
helpers :doorkeeper => 'doorkeeper/doorkeeper'
end
帮助文件位于app/helpers/doorkeeper/doorkeeper_helper.rb
错误
当我启动我的服务器时,我得到:
: from ~/ruby-2.5.0/gems/doorkeeper-5.0.2/lib/doorkeeper/rails/routes/mapper.rb:12:in `instance_eval'
~/settingsParentApp/config/routes.rb:65:in `block (2 levels) in <top (required)>': undefined method `helpers' for #<Doorkeeper::Rails::Routes::Mapper:0x00007ffd539b9c10> (NoMethodError)
结论
我这样做对吗?Doorkeeper 中是否有一种我没有看到的更简单的方法来获取此信息来自定义登录屏幕?或者在我如何调用帮助文件时我没有看到一些错误?
解决方案
After thinking through my problem in order to ask this question, a solution dawned on me. I tested it out and it worked. I forgot that in a controller, the before_action
statements are called in the order they are presented. So, my solution was just to reorder my statements to call the setup_client
first before the authenticate_resource_owner!
. This set up the session variable before redirecting to the login screen and then allowed me to have the variable available for use.
Code
Within my config/routes.rb
file:
use_doorkeeper do
controllers :applications => 'doorkeeper/custom_applications'
controllers :authorizations => 'doorkeeper/custom_authorizations'
end
This custom route bypasses the doorkeeper default authorization controller and goes to a custom one which inherits from the default controller. So, all I need within this custom one is this code:
Found: app/controllers/doorkeeper/custom_authorizations_controller.rb
module Doorkeeper
class CustomAuthorizationsController < Doorkeeper::AuthorizationsController
before_action :setup_client
before_action :authenticate_resource_owner!
def setup_client
@client = Application.find_by(uid: params[:client_id])
session[:client_name] = @client.name
end
end
end
This code is then run before it looks to the Doorkeeper's default AuthorizationsController
and thus calls setup_client
first. The session variable is then saved and in the login screen I can call it this way:
<%
if session[:client_name].nil?
@client_name = ''
else
@client_name = ' for ' + session[:client_name]
end
@page_name = "Login" + @client_name
%>
And then in header of the page I call this within the HTML:
<h1><%= @page_name %></h1>
I may do more fancy things later, like saving client icons/logos and color schemes to make branding specific on the login page, but for now, this basic issue has been resolved. Thank you all for acting as my sounding board and problem-solving ducks... if you know of that reference. :-) Happy Coding!
推荐阅读
- google-cloud-pubsub - 批处理谷歌发布/订阅事件,然后将集合发布为单个事件 - Apache Beam
- javascript - How to filter an array that excludes from object's property array
- firebase - InvalidStateError:在不允许突变的数据库上尝试了突变操作
- jenkins - 在 stable/jenkins helm 图表中将 jenkins tmp 文件夹定义为持久性卷
- python - Discord.py 删除服务器中的每个频道
- javascript - 传单中是否有任何功能可以在特定角度(例如:30 度)上绘制图层(标记或圆圈)?
- mysql - 带有函数的 MySQL 索引
- c++ - C++中数组的频率
- java - 谷歌地图标记 showinfowWindow 自定义 Android
- mysql - 如何获取按联接表字段排序的记录