diff --git a/config/settings.yml b/config/settings.yml index bcf2bfb..b53d9b4 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -7,6 +7,12 @@ login: oauth2_authorize_url: "" oauth2_authorize_signup_url: "" oauth2_token_url: "" + oauth2_token_request_encoding: + default: "form_encoded" + type: enum + choices: + - form_encoded + - json oauth2_token_url_method: default: "POST" type: enum diff --git a/plugin.rb b/plugin.rb index c5e4dcb..f3ae231 100644 --- a/plugin.rb +++ b/plugin.rb @@ -51,9 +51,11 @@ def request(env) warn <<~LOG OAuth2 Debugging: request #{env.method.upcase} #{env.url.to_s} - Headers: #{env.request_headers} + Headers: + #{env.request_headers.to_yaml} - Body: #{env[:body]} + Body: + #{env[:body].to_yaml} LOG end @@ -63,9 +65,11 @@ def response(env) From #{env.method.upcase} #{env.url.to_s} - Headers: #{env.response_headers} + Headers: + #{env.request_headers.to_yaml} - Body: #{env[:body]} + Body: + #{env[:body].to_yaml} LOG end end @@ -121,9 +125,10 @@ def register_middleware(omniauth) opts[:client_options][:auth_scheme] = :request_body opts[:token_params] = { headers: { - "Authorization" => basic_auth_header, + "Authorization" => basic_auth_header }, } + opts[:token_params][:headers]["Content-Type"] = "application/json" if SiteSetting.oauth2_token_request_encoding == "json" elsif SiteSetting.oauth2_send_auth_header? opts[:client_options][:auth_scheme] = :basic_auth else @@ -141,7 +146,8 @@ def register_middleware(omniauth) { bodies: true, formatter: OAuth2FaradayFormatter } end - builder.request :url_encoded # form-encode POST params + encoding = SiteSetting.oauth2_token_request_encoding == "form_encoded" ? :url_encoded : :json + builder.request encoding builder.adapter FinalDestination::FaradayAdapter # make requests with FinalDestination::HTTP end } @@ -223,19 +229,22 @@ def fetch_user_details(token, id) user_json_url = SiteSetting.oauth2_user_json_url.sub(":token", token.to_s).sub(":id", id.to_s) user_json_method = SiteSetting.oauth2_user_json_url_method.downcase.to_sym - log("user_json_url: #{user_json_method} #{user_json_url}") - bearer_token = "Bearer #{token}" connection = Faraday.new { |f| f.adapter FinalDestination::FaradayAdapter } headers = { "Authorization" => bearer_token, "Accept" => "application/json" } user_json_response = connection.run_request(user_json_method, user_json_url, nil, headers) - log("user_json_response: #{user_json_response.inspect}") + log <<-LOG + user_json request: #{user_json_method} #{user_json_url} + + response: + #{user_json_response.to_yaml} + LOG if user_json_response.status == 200 user_json = JSON.parse(user_json_response.body) - log("user_json: #{user_json}") + log("user_json:\n#{user_json.to_yaml}") result = {} if user_json.present? @@ -270,9 +279,23 @@ def always_update_user_email? end def after_authenticate(auth, existing_account: nil) - log( - "after_authenticate response: \n\ncreds: #{auth["credentials"].to_hash}\nuid: #{auth["uid"]}\ninfo: #{auth["info"].to_hash}\nextra: #{auth["extra"].to_hash}", - ) + log <<-LOG + after_authenticate response: + + creds: + #{auth["credentials"].to_hash.to_yaml} + + uid: #{auth["uid"]} + + info: + #{auth["info"].to_hash.to_yaml} + + extra: + #{auth["extra"].to_hash.to_yaml} + + all: + #{auth&.to_hash&.to_yaml} + LOG if SiteSetting.oauth2_fetch_user_details? if fetched_user_details = fetch_user_details(auth["credentials"]["token"], auth["uid"])