Skip to content

Commit 9f8f78e

Browse files
committed
handel modify
1 parent 21f434c commit 9f8f78e

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

lib/oauth2_basic_authenticator.rb

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -171,39 +171,42 @@ def fetch_user_details(token, id)
171171

172172
log("user_json:\n#{user_json.to_yaml}")
173173

174-
# Fetch email separately
175-
email_json_url = "https://api.github.com/user/emails"
176-
email_json_response = connection.run_request(:get, email_json_url, nil, headers)
177-
178-
log <<-LOG
179-
email_json request: GET #{email_json_url}
180-
181-
request headers: #{headers}
182-
183-
response status: #{email_json_response.status}
184-
185-
response body:
186-
#{email_json_response.body}
187-
LOG
188-
189-
if email_json_response.status == 200
190-
email_json = JSON.parse(email_json_response.body)
191-
primary_email = email_json.find { |e| e["primary"] }&.dig("email")
192-
end
193174
result = {}
194175
if user_json.present?
195176
json_walk(result, user_json, :user_id)
196177
json_walk(result, user_json, :username)
197178
json_walk(result, user_json, :name)
198-
result[:email] = primary_email if primary_email.present?
179+
json_walk(result, user_json, :email)
199180
json_walk(result, user_json, :email_verified)
200181
json_walk(result, user_json, :avatar)
182+
end
183+
# Fetch user emails
184+
user_emails_response = connection.run_request(user_json_method, user_emails_url, nil, headers)
201185

202-
DiscoursePluginRegistry.oauth2_basic_additional_json_paths.each do |detail|
203-
prop = "extra:#{detail}"
204-
json_walk(result, user_json, prop, custom_path: detail)
186+
log <<-LOG
187+
user_emails request: #{user_json_method} #{user_emails_url}
188+
request headers: #{headers}
189+
response status: #{user_emails_response.status}
190+
response body:
191+
#{user_emails_response.body}
192+
LOG
193+
194+
if user_emails_response.status == 200
195+
user_emails = JSON.parse(user_emails_response.body)
196+
log("user_emails:\n#{user_emails.to_yaml}")
197+
198+
primary_email = user_emails.find { |email| email["primary"] && email["verified"] }
199+
if primary_email
200+
result[:email] = primary_email["email"]
201+
else
202+
# Handle scenario where no primary email is found
203+
result[:email] = nil
205204
end
205+
else
206+
# Handle error fetching emails
207+
result[:email] = nil
206208
end
209+
207210
result
208211
else
209212
nil

0 commit comments

Comments
 (0)