Allow additional token form parameters

We have a usecase for a "audience" parameter to be added to the oauth token request, there seems to be no other way to add custom form parameters to this request.
This commit is contained in:
Oliver Morgan 2025-09-21 13:17:57 +01:00 committed by GitHub
parent 1d6e735bd6
commit 2fa03dd8af
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -16,7 +16,7 @@ module HTTPX
SUPPORTED_AUTH_METHODS = %w[client_secret_basic client_secret_post].freeze
class OAuthSession
attr_reader :grant_type, :client_id, :client_secret, :access_token, :refresh_token, :scope
attr_reader :grant_type, :client_id, :client_secret, :access_token, :refresh_token, :scope, :token_endpoint_form_post
def initialize(
issuer:,
@ -28,7 +28,8 @@ module HTTPX
token_endpoint: nil,
response_type: nil,
grant_type: nil,
token_endpoint_auth_method: nil
token_endpoint_auth_method: nil,
token_endpoint_form_post: {}
)
@issuer = URI(issuer)
@client_id = client_id
@ -44,6 +45,7 @@ module HTTPX
@access_token = access_token
@refresh_token = refresh_token
@token_endpoint_auth_method = String(token_endpoint_auth_method) if token_endpoint_auth_method
@token_endpoint_form_post = token_endpoint_form_post.transform_keys(&:to_s)
@grant_type = grant_type || (@refresh_token ? "refresh_token" : "client_credentials")
unless @token_endpoint_auth_method.nil? || SUPPORTED_AUTH_METHODS.include?(@token_endpoint_auth_method)
@ -125,7 +127,7 @@ module HTTPX
grant_type = oauth_session.grant_type
headers = {}
form_post = { "grant_type" => grant_type, "scope" => Array(oauth_session.scope).join(" ") }.compact
form_post = { "grant_type" => grant_type, "scope" => Array(oauth_session.scope).join(" ") }.reverse_merge(oauth_session.token_endpoint_form_post).compact
# auth
case oauth_session.token_endpoint_auth_method