我写了一个oauth提供程序,用于处理我公司的几个Web应用程序。我正在使用门卫宝石,到目前为止效果很好。
典型的行为是用户转到客户端应用程序,重定向到提供程序以登录,确认客户端应用程序有权访问该用户的信息,并重定向回客户端应用程序。但是,我想跳过用户确认客户端应用程序的步骤。我想为他们做,所以没有提示。
我试图模仿代码I found here,例如:
Doorkeeper::Application.all.each do |application|
auth_params = {response_type: 'code', client_id: application.uid, redirect_uri: application.redirect_uri}
client = Doorkeeper::OAuth::Client.find(application.uid)
authorization = Doorkeeper::OAuth::AuthorizationRequest.new(client, user, auth_params)
authorization.authorize
end
但是这不起作用,它仍然为用户提供了客户端应用程序的授权/拒绝提示。建议?
答案 0 :(得分:27)
OAuth拥有Resource Owner Credentials Grant流量,门卫支持。基本上,您使用用户凭据(用户名和密码)请求访问令牌。这样您就可以跳过用户确认,也不需要回调URL。
配置门卫:
Doorkeeper.configure do
resource_owner_from_credentials do |routes|
User.authenticate!(params[:username], params[:password]) # change this if needed
end
end
示例令牌请求:
curl -i https://example.com/oauth/token \
-F grant_type=password \
-F client_id=<client_id> \
-F client_secret=<client_secret> \
-F username=user@example.com \
-F password=password
如果您的OAuth客户端应用程序是Rails应用程序,您可以使用oauth2 gem进行此操作:
client = OAuth2::Client.new('client_id', 'client_secret', :site => "https://example.com")
access_token = client.password.get_token('user@example.com', 'password')
另见门卫维基:
https://github.com/applicake/doorkeeper/wiki/Using-Resource-Owner-Password-Credentials-flow
答案 1 :(得分:12)
配置门卫:
Doorkeeper.configure do
skip_authorization do
true
end
end
答案 2 :(得分:12)
您可以通过添加
让您的应用预先授权所有客户端应用skip_authorization do
true
end
到门卫初始化程序,或者在每个应用程序的基础上,通过向门卫preauthorized
表添加一个布尔oauth_applications
。然后将这样的内容添加到初始化程序中:
skip_authorization do |resource_owner, client|
client.application.preauthorized?
end
答案 3 :(得分:2)
您可以获取应用程序的令牌以绕过该确认屏幕,将帖子发送到/ oauth / token。根据自己的喜好调整它。
在您的客户端应用程序中:
require 'rest-client'
require 'json'
client_id = '4ea1b...'
client_secret = 'a2982...'
response = RestClient.post 'http://localhost:3000/oauth/token', {
grant_type: 'client_credentials',
client_id: client_id,
client_secret: client_secret
}
token = JSON.parse(response)["access_token"]
现在,您可以请求访问不需要资源所有者的受保护资源:
RestClient.get 'http://localhost:3000/api/v1/profiles.json', { 'Authorization' => "Bearer #{token}" }
来源:https://github.com/applicake/doorkeeper/wiki/Client-Credentials-flow
答案 4 :(得分:0)
从您的问题来看,您的公司似乎有很多应用程序,并且您希望为所有这些应用程序使用一个身份验证平台。
现在,我假设您希望将登录屏幕放在一个位置(大概在身份验证器应用程序中)。如果是这种情况,您将无法使用Resource Owner Credentials Grant流。
最好的方法是拥有可信任的客户端列表,并有条件地跳过授权,如下所示:
# config/initializers/doorkeeper.rb
Doorkeeper.configure do
skip_authorization do |resource_owner, client|
client.uid == "client application id of the trusted app goes here"
end
end
如果要让客户端拥有自己的登录屏幕,则资源所有者凭据授予流程就足够了。