From 67aecff59c317b548d3dbe3fe88d0a6aed060595 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 14 Dec 2017 15:35:27 +1100 Subject: [PATCH] FEATURE: store twitter supplied email for auditing --- ...14040346_add_email_to_twitter_user_info.rb | 5 ++++ lib/auth/twitter_authenticator.rb | 9 ++++-- .../auth/twitter_authenticator_spec.rb | 28 +++++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20171214040346_add_email_to_twitter_user_info.rb create mode 100644 spec/components/auth/twitter_authenticator_spec.rb diff --git a/db/migrate/20171214040346_add_email_to_twitter_user_info.rb b/db/migrate/20171214040346_add_email_to_twitter_user_info.rb new file mode 100644 index 00000000000..fb33972afec --- /dev/null +++ b/db/migrate/20171214040346_add_email_to_twitter_user_info.rb @@ -0,0 +1,5 @@ +class AddEmailToTwitterUserInfo < ActiveRecord::Migration[5.1] + def change + add_column :twitter_user_infos, :email, :string, limit: 1000, null: true + end +end diff --git a/lib/auth/twitter_authenticator.rb b/lib/auth/twitter_authenticator.rb index a88d2124c60..36a16b1cf9d 100644 --- a/lib/auth/twitter_authenticator.rb +++ b/lib/auth/twitter_authenticator.rb @@ -16,6 +16,7 @@ class Auth::TwitterAuthenticator < Auth::Authenticator twitter_user_id = auth_token["uid"] result.extra_data = { + twitter_email: result.email, twitter_user_id: twitter_user_id, twitter_screen_name: result.username, twitter_image: data["image"], @@ -26,11 +27,12 @@ class Auth::TwitterAuthenticator < Auth::Authenticator user_info = TwitterUserInfo.find_by(twitter_user_id: twitter_user_id) result.user = user_info.try(:user) - 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( user_id: result.user.id, screen_name: result.username, - twitter_user_id: twitter_user_id + twitter_user_id: twitter_user_id, + email: result.email ) end @@ -46,7 +48,8 @@ class Auth::TwitterAuthenticator < Auth::Authenticator TwitterUserInfo.create( user_id: user.id, screen_name: extra_data[:twitter_screen_name], - twitter_user_id: extra_data[:twitter_user_id] + twitter_user_id: extra_data[:twitter_user_id], + email: extra_data[:email] ) retrieve_avatar(user, extra_data) diff --git a/spec/components/auth/twitter_authenticator_spec.rb b/spec/components/auth/twitter_authenticator_spec.rb new file mode 100644 index 00000000000..8c2d4659655 --- /dev/null +++ b/spec/components/auth/twitter_authenticator_spec.rb @@ -0,0 +1,28 @@ +require 'rails_helper' + +describe Auth::TwitterAuthenticator do + + it "takes over account if email is supplied" do + auth = Auth::TwitterAuthenticator.new + + user = Fabricate(:user) + + auth_token = { + info: { + "email" => user.email, + "username" => "test", + "name" => "test", + "nickname" => "minion", + }, + "uid" => "123" + } + + result = auth.after_authenticate(auth_token) + + expect(result.user.id).to eq(user.id) + + info = TwitterUserInfo.find_by(user_id: user.id) + expect(info.email).to eq(user.email) + end + +end