15

我编写了一个 oauth 提供程序,旨在与我公司的几个 Web 应用程序一起工作。我正在使用门卫 gem,到目前为止效果很好。

典型的行为是用户访问客户端应用程序,重定向到提供程序以登录,确认客户端应用程序有权访问该用户的信息,然后重定向回客户端应用程序。但是,我想跳过用户确认客户端应用程序的步骤。我想为他们做,所以没有提示。

我试图模仿我在这里找到的代码,例如:

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

但这不起作用,它仍然为用户提供客户端应用程序的授权/拒绝提示。建议?

4

5 回答 5

27

OAuth 对此有资源所有者凭证授予流程,Doorkeeper 支持该流程。基本上,您使用用户凭据(用户名和密码)请求访问令牌。这样您就可以跳过用户确认,也不需要回调 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

于 2012-07-31T13:45:45.820 回答
13

Doorkeeper 0.6.7 提供了配置选项来执行此操作。

要配置门卫:

Doorkeeper.configure do
  skip_authorization do
    true
  end  
end
于 2013-02-22T17:05:31.780 回答
13

您可以通过添加让您的应用程序对所有客户端应用程序进行预授权

skip_authorization do
  true
 end

到门卫初始化程序,或者在每个应用程序的基础上通过向preauthorized门卫oauth_applications表添加一个布尔值。然后在初始化程序中添加类似这样的内容:

skip_authorization do |resource_owner, client|
  client.application.preauthorized?
 end
于 2013-10-08T23:57:47.193 回答
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

于 2013-02-22T19:45:27.490 回答
0

从您的问题看来,您的公司有很多应用程序,您希望为所有这些应用程序使用一个身份验证平台。

现在,我假设您希望将登录屏幕放在一个地方(大概在身份验证器应用程序上)。如果是这种情况,您将无法为此使用资源所有者凭据授予流程。

最好的方法是有一个值得信赖的客户列表并有条件地跳过授权,如下所示:

# 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

如果您想让客户端拥有自己的登录屏幕,资源所有者凭据授予流程就足够了。

于 2018-09-27T17:25:19.140 回答