【问题标题】:Ruby OAuth2.0: client credential type has unsupported client authentication methodRuby OAuth2.0:客户端凭据类型具有不受支持的客户端身份验证方法
【发布时间】:2019-06-29 00:03:02
【问题描述】:

我正在使用 OAuth2 gem 来进行 client_credential 身份验证。我的代码如下,

require 'oauth2'
client = OAuth2::Client.new("my_client_id", "my_client_secret", :site => "my_site_url", :token_url => "oauth2/token")
client.client_credentials.get_token

当我执行上面的代码块时,它会响应以下错误,

OAuth2::Error (invalid_client: Client authentication failed (e.g., unknown client, no client authentication included, or unsupported authentication method))
{
  "error":"invalid_client","error_description":"Client authentication failed (e.g., unknown client, no client authentication included, or unsupported authentication method)",
  "error_hint":"The OAuth 2.0 Client supports client authentication method "client_secret_basic", but method "client_secret_post" was requested. 
  You must configure the OAuth 2.0 client's "token_endpoint_auth_method" value to accept "client_secret_post".","status_code":401}

我检查了使用 'net/http' 库,我的 client_idclient_secrets 有效且正常工作。

我看到的唯一问题是上述消息提示中所说的身份验证方法,

The OAuth 2.0 Client supports client authentication method "client_secret_basic", but method "client_secret_post" was requested. You must configure the OAuth 2.0 client's "token_endpoint_auth_method" value to accept "client_secret_post"

我想知道的是?

  1. OAuth2 gem 如何决定使用 client_secret_post 还是 client_secret_basic?我的意思是如何在 OAuth2 gem 中使用 client_secret_basic 请求?
  2. 如果不是以上那么,我应该如何指定 token_endpoint_auth_method 来接受 client_secret_post?

【问题讨论】:

  • 缺少报价。在stackoverflow或代码中粘贴时是否错过了它,请检查
  • 这里只漏了,代码里有。

标签: ruby-on-rails ruby oauth oauth-2.0 net-http


【解决方案1】:

我也遇到了同样的问题。

请在您的客户端代码中添加或更改此客户端选项设置。

:auth_scheme => :basic_auth

默认设置如下。

:auth_scheme => :request_body

我摘录了 OAuth2::Client 代码的一部分。

请检查一下。

require 'faraday'
require 'logger'

module OAuth2
  # The OAuth2::Client class
  class Client # rubocop:disable Metrics/ClassLength
    attr_reader :id, :secret, :site
    attr_accessor :options
    attr_writer :connection

    # @option opts [Symbol] :auth_scheme (:basic_auth) HTTP method to use to authorize request (:basic_auth or :request_body)

    def initialize(client_id, client_secret, options = {}, &block)
      opts = options.dup
      @id = client_id
      @secret = client_secret
      @site = opts.delete(:site)
      ssl = opts.delete(:ssl)
      @options = {:authorize_url    => '/oauth/authorize',
                  :token_url        => '/oauth/token',
                  :token_method     => :post,
                  :auth_scheme      => :request_body,  # <-- Here !!!
                  :connection_opts  => {},
                  :connection_build => block,
                  :max_redirects    => 5,
                  :raise_errors     => true}.merge(opts)
      @options[:connection_opts][:ssl] = ssl if ssl
    end

示例 sn-p 在这里https://gist.github.com/mtoshi/cd74f57631805fb1b2290137f58dac9f

【讨论】:

    【解决方案2】:

    好的,所以最后我清除了这些点。

    1. OAuth2 gem 确实向 OAuth 服务器发出请求,其中 --token_endpoint_auth_method 设置为“client_secret_post”。

    2. 在向 OAuth 服务器注册客户端时,我们必须将 token_endpoint_auth_method 设置为“client_secret_post”,这样它才能工作。

    就我而言,我使用的是 Hydra,所以我使用以下命令来创建客户端:

    hydra clients create --endpoint <OAuth server url> --id CLIENT_ID --secret CLIENT_SECRET \
    --token-endpoint-auth-method 'client_secret_post' -g client_credentials
    

    现在,将这些 CLIENT_ID 和 CLIENT_SECRET 与 oauth2 一起使用。

    但仍有一点不清楚 - 我可以使用 oauth2 gem 将 token_endpoint_auth_method 设置为 client_secret_basic 提出请求吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-23
      • 1970-01-01
      • 2017-08-15
      • 1970-01-01
      • 1970-01-01
      • 2012-01-03
      相关资源
      最近更新 更多