From d8a8724f2bd180308d52270c8964708be0010395 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Thu, 30 Apr 2020 17:15:09 +0100 Subject: [PATCH] FEATURE: Add detailed OAuth2 request and response logs This makes use of Faraday middleware to log precise details about all requests made by the OAuth2 gem. This should make it easier to debug configuration issues --- plugin.rb | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/plugin.rb b/plugin.rb index 6a06561..5c9632c 100644 --- a/plugin.rb +++ b/plugin.rb @@ -46,6 +46,34 @@ class ::OmniAuth::Strategies::Oauth2Basic < ::OmniAuth::Strategies::OAuth2 end end + +if Gem::Version.new(Faraday::VERSION) > Gem::Version.new('1.0') + require 'faraday/logging/formatter' + class OAuth2FaradayFormatter < Faraday::Logging::Formatter + def request(env) + warn <<~LOG + OAuth2 Debugging: request #{env.method.upcase} #{env.url.to_s} + + Headers: #{env.request_headers} + + Body: #{env[:body]} + LOG + end + + def response(env) + warn <<~LOG + OAuth2 Debugging: response status #{env.status} + + From #{env.method.upcase} #{env.url.to_s} + + Headers: #{env.response_headers} + + Body: #{env[:body]} + LOG + end + end +end + class ::OAuth2BasicAuthenticator < Auth::ManagedAuthenticator def name 'oauth2_basic' @@ -80,6 +108,16 @@ class ::OAuth2BasicAuthenticator < Auth::ManagedAuthenticator unless SiteSetting.oauth2_scope.blank? opts[:scope] = SiteSetting.oauth2_scope end + + if SiteSetting.oauth2_debug_auth && defined? OAuth2FaradayFormatter + opts[:client_options][:connection_build] = lambda{ |builder| + builder.response :logger, Rails.logger, { bodies: true, formatter: OAuth2FaradayFormatter } + + # Default stack: + builder.request :url_encoded # form-encode POST params + builder.adapter Faraday.default_adapter # make requests with Net::HTTP + } + end } end