缺少必需参数:范围. 门卫 Gem 从 5.1.0 升级到 5.2.1 后



在提供授权的 Rails (5.2.3( 应用程序上将门卫从 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的实例返回属性scopenil,但不返回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="...">

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

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

这在从旧版本迁移中提到,但解释为数据库更改。链接的RFC6749#section-3.3更清楚地说明了新要求:

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

我同意这在从旧版本或范围迁移中似乎没有充分记录,但它们更忠实于RFC6749似乎很重要。我正在使用grant_type: 'authorization_code',授权代码流中甚至没有提到"范围"。

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

bundle exec rails generate doorkeeper:migration

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

rake db:migrate

最新更新