Thanks to visit codestin.com
Credit goes to github.com

Skip to content

FEATURE: add option to encode request as json #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 36 additions & 13 deletions plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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"])
Expand Down