根据 doorkeeper docs,我正在为API 用户提供 OmniAuth 策略。这是为了允许客户端应用程序的某些用户在 APi 上写入/编辑权限。使用范围的门卫 wiki说,如果您的客户端应用程序正在请求授权 URI,您可以执行以下操作:
http://provider.example.com/oauth/authorize?(... other params... )&scope=public+write
其中 public 和 write 是范围(例如,用于公共访问和写访问)。
看起来这样做的方法是在 OmniauthCallbacksController 下面的设置方法中
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def provider
# You need to implement the method below in your model (e.g. app/models/user.rb)
##Some code
end
def setup
render :text => "Setup complete.", :status => 404
end
end
在 setup 方法期间,request.env 可以看起来像这样,它正确传递了 scope 参数(注意:我通过命令行调试器添加了下面的“scope”参数)
# request.env['omniauth.strategy'].options
# => {"setup"=>true,
# "skip_info"=>false,
# "client_id"=>
# "***",
# "client_secret"=>
# "***",
# "client_options"=>
# {"site"=>"http://localhost:3000/", "authorize_url"=>"/oauth/authorize"},
# "authorize_params"=>{},
# "authorize_options"=>[:scope],
# "token_params"=>{},
# "token_options"=>[],
# "auth_token_params"=>{},
# "provider_ignores_state"=>false,
# "name"=>:datafeed,
# "scope"=>:write}
但我希望能够做到这样的事情:
request.env['omniauth.strategy']['scope']=:write if current_user.role=="admin"
但是目前 Devise 没有定义 current_user (而且我在任何地方都看不到用户 ID)......关于如何在授权过程中的某个时刻找出用户的任何想法,以便我可以将正确的范围传递给 API ?