FEATURE: Revoke and reconnect for Twitter logins

This commit is contained in:
David Taylor 2018-07-27 12:28:51 +01:00
parent c74dd2fa08
commit 9c72c00206
3 changed files with 76 additions and 4 deletions

View File

@ -127,7 +127,7 @@ export function findAll(siteSettings, capabilities, isMobileDevice) {
params.displayPopup = true; params.displayPopup = true;
} }
if (["facebook", "google_oauth2"].includes(name)) { if (["facebook", "google_oauth2", "twitter"].includes(name)) {
params.canConnect = true; params.canConnect = true;
} }

View File

@ -13,7 +13,26 @@ class Auth::TwitterAuthenticator < Auth::Authenticator
info&.email || info&.screen_name || "" info&.email || info&.screen_name || ""
end end
def after_authenticate(auth_token) def can_revoke?
true
end
def revoke(user, skip_remote: false)
info = TwitterUserInfo.find_by(user_id: user.id)
raise Discourse::NotFound if info.nil?
# We get a token from twitter upon login but do not need it, and do not store it.
# Therefore we do not have any way to revoke the token automatically on twitter's end
info.destroy!
true
end
def can_connect_existing_user?
true
end
def after_authenticate(auth_token, existing_account: nil)
result = Auth::Result.new result = Auth::Result.new
data = auth_token[:info] data = auth_token[:info]
@ -35,9 +54,21 @@ class Auth::TwitterAuthenticator < Auth::Authenticator
user_info = TwitterUserInfo.find_by(twitter_user_id: twitter_user_id) user_info = TwitterUserInfo.find_by(twitter_user_id: twitter_user_id)
result.user = user_info.try(:user) if existing_account && (user_info.nil? || existing_account.id != user_info.user_id)
user_info.destroy! if user_info
result.user = existing_account
user_info = TwitterUserInfo.create!(
user_id: result.user.id,
screen_name: result.username,
twitter_user_id: twitter_user_id,
email: result.email
)
else
result.user = user_info&.user
end
if (!result.user) && result.email_valid && (result.user = User.find_by_email(result.email)) if (!result.user) && result.email_valid && (result.user = User.find_by_email(result.email))
TwitterUserInfo.create( TwitterUserInfo.create!(
user_id: result.user.id, user_id: result.user.id,
screen_name: result.username, screen_name: result.username,
twitter_user_id: twitter_user_id, twitter_user_id: twitter_user_id,

View File

@ -25,4 +25,45 @@ describe Auth::TwitterAuthenticator do
expect(info.email).to eq(user.email) expect(info.email).to eq(user.email)
end end
it 'can connect to a different existing user account' do
authenticator = Auth::TwitterAuthenticator.new
user1 = Fabricate(:user)
user2 = Fabricate(:user)
TwitterUserInfo.create!(user_id: user1.id, twitter_user_id: 100, screen_name: "boris")
hash = {
info: {
"email" => user1.email,
"username" => "test",
"name" => "test",
"nickname" => "minion",
},
"uid" => "100"
}
result = authenticator.after_authenticate(hash, existing_account: user2)
expect(result.user.id).to eq(user2.id)
expect(TwitterUserInfo.exists?(user_id: user1.id)).to eq(false)
expect(TwitterUserInfo.exists?(user_id: user2.id)).to eq(true)
end
context 'revoke' do
let(:user) { Fabricate(:user) }
let(:authenticator) { Auth::TwitterAuthenticator.new }
it 'raises exception if no entry for user' do
expect { authenticator.revoke(user) }.to raise_error(Discourse::NotFound)
end
it 'revokes correctly' do
TwitterUserInfo.create!(user_id: user.id, twitter_user_id: 100, screen_name: "boris")
expect(authenticator.can_revoke?).to eq(true)
expect(authenticator.revoke(user)).to eq(true)
expect(authenticator.description_for_user(user)).to eq("")
end
end
end end