【问题标题】:Missing required parameter: scope. after doorkeeper gem upgrade from 5.1.0 to 5.2.1缺少必需的参数:范围。门卫 gem 从 5.1.0 升级到 5.2.1 之后
【发布时间】:2019-09-28 21:31:04
【问题描述】:

在提供授权的 Rails (5.2.3) 应用程序上将 doorkeeper 从 5.1.0 升级到 5.2.1 后,请求授权的应用程序的登录不再起作用。授权应用程序上的页面声明 Missing required parameter: scope. 虽然我们不使用范围。

迁移说明中有一些关于范围的内容,但它们并没有告诉我。 https://github.com/doorkeeper-gem/doorkeeper/wiki/Migration-from-old-versions#database-changes

我知道我必须创建一个像这样的迁移,但问题仍然存在:

# frozen_string_literal: true

class ChangeScopesOnOAuthAccessGrants < ActiveRecord::Migration[5.2]
  def up
    change_column_default :oauth_access_grants, :scopes, from: nil, to: ''
    change_column_null :oauth_access_grants, :scopes, false
  end

  def down
    change_column_default :oauth_access_grants, :scopes, from: '', to: nil
    change_column_null :oauth_access_grants, :scopes, true
  end
end

在授权应用上配置doorkeeper.rb很简单:

Doorkeeper.configure do
  orm :active_record

  resource_owner_authenticator do
    current_admin_user || redirect_to(new_admin_user_session_path(params.permit(:client_id, :redirect_uri, :response_type, :state)))
  end

  admin_authenticator do
    current_admin_user || redirect_to(new_admin_user_session_path)
  end

  access_token_expires_in 24.hours
end

我对运行迁移前后的响应进行了更深入的了解。在 Doorkeeper 模块的AuthorizationsController#new 中使用binding.pry(继承自Doorkeeper::ApplicationController),我可以确认Doorkeeper::OAuth::PreAuthorization 的实例为nil 属性scope 返回nil,但不是scopes

调用pre_auth.authorizable? 后,我得到了这些对象和这些值:

#<Doorkeeper::OAuth::PreAuthorization:0x00007fad33f25390
 @client=
  #<Doorkeeper::OAuth::Client:0x00007fad364b22e8
   @application=
    #<Doorkeeper::Application:0x00007fad364b26d0
     id: 2,
     name: "...",
     uid: "...",
     secret: "..",
     redirect_uri:
      "http://localhost:3001/users/auth/doorkeeper/callback",
     scopes: "",
     created_at: Tue, 24 Oct 2017 11:56:13 CEST +02:00,
     updated_at: Thu, 03 Oct 2019 18:53:35 CEST +02:00,
     confidential: true>>,
 @client_id="...",
 @code_challenge=nil,
 @code_challenge_method=nil,
 @error=:invalid_request,
 @missing_param=:scope,
 @redirect_uri="http://localhost:3001/users/auth/doorkeeper/callback",
 @response_type="code",
 @scope=nil,
 @server=
  #<Doorkeeper::Config:0x00007fad33b72180
   @access_token_expires_in=24 hours,
   @api_only=false,
   @application_secret_strategy=Doorkeeper::SecretStoring::???,
   @authenticate_admin=#<Proc:0x00007fad33b71d20@/Users/.../config/initializers/doorkeeper.rb:11>,
   @authenticate_resource_owner=#<Proc:0x00007fad33b71eb0@/Users/.../config/initializers/doorkeeper.rb:6>,
   @default_scopes=#<Doorkeeper::OAuth::Scopes:0x00007fad364cb7c0 @scopes=[]>,
   @orm=:active_record,
   @token_secret_strategy=Doorkeeper::SecretStoring::???>,
 @state="...">

我目前没有任何解决问题的线索。感谢您的提示!

【问题讨论】:

    标签: ruby-on-rails doorkeeper


    【解决方案1】:

    我有同样的问题。似乎您总是需要在您的授权请求中提供scope 参数或配置default_scope(配置中有一个示例)。此外,默认或请求的范围必须与您的客户端应用程序范围之一匹配,否则您将获得The requested scope is invalid, unknown, or malformed.

    这在Migration from old versions 中有所提及,但解释为数据库更改。链接的RFC6749#section-3.3 更清楚地说明了新要求:

    如果客户端在请求时省略了范围参数 授权,授权服务器必须要么处理 使用预定义的默认值请求或使请求失败 表示无效范围。授权服务器应该 记录其范围要求和默认值(如果已定义)。

    我同意 Migration from old versionsScopes 中似乎没有充分记录这一点,但它们更忠实于 RFC6749 似乎很重要。我使用的是grant_type: 'authorization_code',而Authorization Code Flow 中甚至没有提到“范围”。

    【讨论】:

      【解决方案2】:

      您需要重新运行此命令以生成与新版本兼容的迁移。这将为oauth_access_grantsscopes 属性添加一个非空选项。

       bundle exec rails generate doorkeeper:migration
      

      完成后,像往常一样使用 rake 运行迁移。

      rake db:migrate
      

      【讨论】:

      • 我手动创建了这个迁移,但它不起作用。但我会用你的方法再试一次。我不知道这种可能性。
      • 迁移添加了我已经拥有的表以及新的表。所以它不是开箱即用的。
      • 您可以尝试使用全新的数据库吗?
      • 我会用新的数据库试试。
      • 我添加了更多调试见解。目前没有任何进展。
      猜你喜欢
      • 2021-05-13
      • 2021-05-07
      • 2019-06-14
      • 2018-05-16
      • 1970-01-01
      • 1970-01-01
      • 2016-12-19
      • 2015-04-19
      相关资源
      最近更新 更多