discourse/lib/auth/twitter_authenticator.rb
Ted Johansson ce1c48b1a9
FIX: Twitter health check broken on Faraday update (#35407)
Faraday [updated their authentication/authorization middleware in version 2](https://github.com/lostisland/faraday/pull/1306), which was a breaking change for the Twitter auth health check.

The relevant test was manually mocking and stubbing the individual Faraday objects, so the relevant code path wasn't exercised by our tests.

This commit:

- Updates the test to use `stub_request` instead. (This correctly catches
the deprecated method error.)
- Updates the health check to use the new middleware.
2025-10-15 15:27:36 +08:00

56 lines
1.5 KiB
Ruby

# frozen_string_literal: true
class Auth::TwitterAuthenticator < Auth::ManagedAuthenticator
def name
"twitter"
end
def display_name
"X / Twitter"
end
def provider_url
"https://x.com"
end
def enabled?
SiteSetting.enable_twitter_logins
end
def healthy?
connection =
Faraday.new(url: "https://api.twitter.com") do |connection|
connection.request(
:authorization,
:basic,
SiteSetting.twitter_consumer_key,
SiteSetting.twitter_consumer_secret,
)
end
connection.post("/oauth2/token").status == 200
rescue Faraday::Error
false
end
def after_authenticate(auth_token, existing_account: nil)
# Twitter sends a huge amount of data which we don't need, so ignore it
auth_token[:extra] = {}
super
end
def register_middleware(omniauth)
omniauth.provider :twitter,
setup:
lambda { |env|
strategy = env["omniauth.strategy"]
strategy.options[:consumer_key] = SiteSetting.twitter_consumer_key
strategy.options[:consumer_secret] = SiteSetting.twitter_consumer_secret
}
end
# twitter doesn't return unverfied email addresses in the API
# https://developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/manage-account-settings/api-reference/get-account-verify_credentials
def primary_email_verified?(auth_token)
true
end
end