我已经创建了几个通过门卫与我们的中央身份验证服务器通信的应用程序。我想使某些应用程序对特定用户可访问/不可访问。
是否可以限制对特定oauth_applications的访问并返回401?
答案 0 :(得分:0)
我相信最简单的方法如下:
这些权限可能只包含您要授予他们访问权限的应用程序的名称(或您选择的应用程序ID)
token_payload do |opts|
...
token[:permissions] = user.permissions.pluck(:application_name)
end
如果您在不使用JWT的情况下使用Doorkeeper,则仍可以通过向ResponseToken对象添加自定义响应来将额外信息传递给令牌,如下所示:
Doorkeeper::OAuth::TokenResponse.send :prepend, CustomTokenResponse
和CustomTokenResponse仅需要实现方法body
,如下所示:
module CustomTokenResponse
def body
additional_data = {
'username' => env[:clearance].current_user.username,
'userid' => @token.resource_owner_id # you have an access to the @token object
# any other data
}
# call original `#body` method and merge its result with the additional data hash
super.merge(additional_data)
end
end
其他信息可在《守门人》维基中找到:https://github.com/doorkeeper-gem/doorkeeper/wiki/Customizing-Token-Response 在Doorkeeper JWT gem中:https://github.com/doorkeeper-gem/doorkeeper-jwt#usage
答案 1 :(得分:0)
我想要同样的行为。我在resource_owner_authenticator
中使用config/initializer/doorkeeper.rb
块。当用户有一个或多个与Oauth应用程序连接的组时,它可以继续。
rails g model UserGroup user:references group:references
rails g model GroupApplications group:references oauth_application:references
resource_owner_authenticator do
app = OauthApplication.find_by(uid: request.query_parameters['client_id'])
user_id = session["warden.user.user.key"][0][0] rescue nil
user = User.find_by_id(user_id)
if !app && user
user
elsif app && user
if !(user.groups & app.groups).empty?
user
else
redirect_to main_app.root_url, notice: "You are not authorized to access this application."
end
else
begin
session['user_return_to'] = request.url
redirect_to(new_user_session_url)
end
end
end