From: Tom Hughes Date: Mon, 27 Apr 2015 20:39:54 +0000 (+0100) Subject: Add support for Facebook authentication X-Git-Tag: live~4704 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/c9893e186b32bfd8e8ee8f00e78258387f4aac9c Add support for Facebook authentication --- diff --git a/Gemfile b/Gemfile index 7c916b011..09fa2d89b 100644 --- a/Gemfile +++ b/Gemfile @@ -58,6 +58,7 @@ gem "actionpack-page_caching" gem "omniauth" gem "omniauth-openid" gem "openstreetmap-omniauth-google-oauth2", ">= 0.2.6.1", :require => "omniauth-google-oauth2" +gem "omniauth-facebook" # Markdown formatting support gem "redcarpet" diff --git a/Gemfile.lock b/Gemfile.lock index eb37df066..05b873399 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -150,6 +150,8 @@ GEM omniauth (1.2.2) hashie (>= 1.2, < 4) rack (~> 1.0) + omniauth-facebook (2.0.1) + omniauth-oauth2 (~> 1.2) omniauth-oauth2 (1.3.0) oauth2 (~> 1.0) omniauth (~> 1.2) @@ -311,6 +313,7 @@ DEPENDENCIES minitest (~> 5.1) oauth-plugin (>= 0.5.1) omniauth + omniauth-facebook omniauth-openid openstreetmap-i18n-js (>= 3.0.0.rc5.3) openstreetmap-omniauth-google-oauth2 (>= 0.2.6.1) diff --git a/app/assets/images/facebook.png b/app/assets/images/facebook.png new file mode 100644 index 000000000..8a9efc970 Binary files /dev/null and b/app/assets/images/facebook.png differ diff --git a/app/views/user/login.html.erb b/app/views/user/login.html.erb index 99d7c0c34..2e4daf3a3 100644 --- a/app/views/user/login.html.erb +++ b/app/views/user/login.html.erb @@ -45,6 +45,9 @@ <% if defined?(GOOGLE_AUTH_ID) -%>
  • <%= auth_button "google", "google" %>
  • <% end -%> + <% if defined?(FACEBOOK_AUTH_ID) -%> +
  • <%= auth_button "facebook", "facebook" %>
  • + <% end -%>
  • <%= auth_button "yahoo", "openid", :openid_url => "yahoo.com" %>
  • <%= auth_button "wordpress", "openid", :openid_url => "wordpress.com" %>
  • <%= auth_button "aol", "openid", :openid_url => "aol.com" %>
  • diff --git a/config/example.application.yml b/config/example.application.yml index 34db9d766..dd2a174e9 100644 --- a/config/example.application.yml +++ b/config/example.application.yml @@ -92,6 +92,8 @@ defaults: &defaults #google_auth_id: "" #google_auth_secret: "" #google_openid_realm: "" + #facebook_auth_id: "" + #facebook_auth_secret: "" development: <<: *defaults @@ -107,3 +109,5 @@ test: google_auth_id: "dummy" google_auth_secret: "dummy" google_openid_realm: "https://www.openstreetmap.org" + facebook_auth_id: "dummy" + facebook_auth_secret: "dummy" diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb index 6de63f6ee..508d326d6 100644 --- a/config/initializers/omniauth.rb +++ b/config/initializers/omniauth.rb @@ -21,6 +21,7 @@ end openid_options = { :name => "openid", :store => openid_store } google_options = { :name => "google", :scope => "email", :access_type => "online" } +facebook_options = { :name => "facebook", :scope => "email" } if defined?(GOOGLE_OPENID_REALM) google_options[:openid_realm] = GOOGLE_OPENID_REALM @@ -29,6 +30,7 @@ end Rails.application.config.middleware.use OmniAuth::Builder do provider :openid, openid_options provider :google_oauth2, GOOGLE_AUTH_ID, GOOGLE_AUTH_SECRET, google_options if defined?(GOOGLE_AUTH_ID) + provider :facebook, FACEBOOK_AUTH_ID, FACEBOOK_AUTH_SECRET, facebook_options if defined?(FACEBOOK_AUTH_ID) end # Pending fix for: https://github.com/intridea/omniauth/pull/795 diff --git a/config/locales/en.yml b/config/locales/en.yml index d426faabe..e554832f8 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1720,6 +1720,9 @@ en: google: title: Login with Google alt: Login with a Google OpenID + facebook: + title: Login with Facebook + alt: Login with a Facebook Account yahoo: title: Login with Yahoo alt: Login with a Yahoo OpenID @@ -1991,6 +1994,8 @@ en: auth_failure: connection_failed: Connection to authentication provider failed invalid_credentials: Invalid authentication credentials + no_authorization_code: No authorization code + unknown_signature_algorithm: Unknown signature algorithm user_role: filter: not_an_administrator: "Only administrators can perform user role management, and you are not an administrator." diff --git a/lib/auth.rb b/lib/auth.rb index edd98e4ff..b5ac0d2eb 100644 --- a/lib/auth.rb +++ b/lib/auth.rb @@ -1,4 +1,5 @@ module Auth PROVIDERS = { "None" => "", "OpenID" => "openid" } PROVIDERS["Google"] = "google" if defined?(GOOGLE_AUTH_ID) + PROVIDERS["Facebook"] = "facebook" if defined?(FACEBOOK_AUTH_ID) end diff --git a/test/controllers/user_controller_test.rb b/test/controllers/user_controller_test.rb index 7a1310d6e..0f08ced7c 100644 --- a/test/controllers/user_controller_test.rb +++ b/test/controllers/user_controller_test.rb @@ -1321,7 +1321,7 @@ class UserControllerTest < ActionController::TestCase get :list, :page => 3 assert_response :success assert_template :list - assert_select "table#user_list tr", :count => 20 + assert_select "table#user_list tr", :count => 21 end def test_list_post_confirm diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index 9c5499b6e..30b27c7bd 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -262,3 +262,17 @@ google_user: terms_agreed: "2010-01-01 11:22:33" terms_seen: true languages: en + +facebook_user: + id: 20 + email: facebook-user@example.com + status: active + pass_crypt: <%= Digest::MD5.hexdigest('test') %> + creation_time: "2008-05-01 01:23:45" + display_name: facebookuser + data_public: true + auth_provider: facebook + auth_uid: 123456789 + terms_agreed: "2010-01-01 11:22:33" + terms_seen: true + languages: en diff --git a/test/integration/user_creation_test.rb b/test/integration/user_creation_test.rb index 1a09e6718..15fcaf236 100644 --- a/test/integration/user_creation_test.rb +++ b/test/integration/user_creation_test.rb @@ -14,6 +14,7 @@ class UserCreationTest < ActionDispatch::IntegrationTest OmniAuth.config.mock_auth[:openid] = nil OmniAuth.config.mock_auth[:google] = nil + OmniAuth.config.mock_auth[:facebook] = nil OmniAuth.config.test_mode = false end @@ -381,4 +382,121 @@ class UserCreationTest < ActionDispatch::IntegrationTest assert_response :success assert_template "site/welcome" end + + def test_user_create_facebook_success + OmniAuth.config.add_mock(:facebook, :uid => "123454321", :extra => { + :id_info => { "openid_id" => "http://localhost:1123/new.tester" } + }) + + new_email = "newtester-facebook@osm.org" + display_name = "new_tester-facebook" + password = "testtest" + assert_difference("User.count") do + assert_difference("ActionMailer::Base.deliveries.size", 1) do + post "/user/new", + :user => { :email => new_email, :email_confirmation => new_email, :display_name => display_name, :auth_provider => "facebook", :pass_crypt => "", :pass_crypt_confirmation => "" } + assert_response :redirect + assert_redirected_to auth_path(:provider => "facebook", :origin => "/user/new") + follow_redirect! + assert_response :redirect + assert_redirected_to auth_success_path(:provider => "facebook") + follow_redirect! + assert_response :redirect + assert_redirected_to "/user/terms" + post "/user/save", + :user => { :email => new_email, :email_confirmation => new_email, :display_name => display_name, :auth_provider => "facebook", :auth_uid => "123454321", :pass_crypt => password, :pass_crypt_confirmation => password } + assert_response :redirect + follow_redirect! + end + end + + # Check the page + assert_response :success + assert_template "user/confirm" + + ActionMailer::Base.deliveries.clear + end + + def test_user_create_facebook_failure + OmniAuth.config.mock_auth[:facebook] = :connection_failed + + new_email = "newtester-facebook2@osm.org" + display_name = "new_tester-facebook2" + assert_difference("User.count", 0) do + assert_difference("ActionMailer::Base.deliveries.size", 0) do + post "/user/new", + :user => { :email => new_email, :email_confirmation => new_email, :display_name => display_name, :auth_provider => "facebook", :pass_crypt => "", :pass_crypt_confirmation => "" } + assert_response :redirect + assert_redirected_to auth_path(:provider => "facebook", :origin => "/user/new") + follow_redirect! + assert_response :redirect + assert_redirected_to auth_success_path(:provider => "facebook") + follow_redirect! + assert_response :redirect + assert_redirected_to auth_failure_path(:strategy => "facebook", :message => "connection_failed", :origin => "/user/new") + follow_redirect! + assert_response :redirect + follow_redirect! + assert_response :success + assert_template "user/new" + end + end + + ActionMailer::Base.deliveries.clear + end + + def test_user_create_facebook_redirect + OmniAuth.config.add_mock(:facebook, :uid => "123454321", :extra => { + :id_info => { "openid_id" => "http://localhost:1123/new.tester" } + }) + + new_email = "redirect_tester_facebook@osm.org" + display_name = "redirect_tester_facebook" + # nothing special about this page, just need a protected page to redirect back to. + referer = "/traces/mine" + assert_difference("User.count") do + assert_difference("ActionMailer::Base.deliveries.size", 1) do + post "/user/new", + :user => { :email => new_email, :email_confirmation => new_email, :display_name => display_name, :auth_provider => "facebook", :pass_crypt => "", :pass_crypt_confirmation => "" }, :referer => referer + assert_response :redirect + assert_redirected_to auth_path(:provider => "facebook", :origin => "/user/new") + follow_redirect! + assert_response :redirect + assert_redirected_to auth_success_path(:provider => "facebook") + follow_redirect! + assert_response :redirect + assert_redirected_to "/user/terms" + post_via_redirect "/user/save", + :user => { :email => new_email, :email_confirmation => new_email, :display_name => display_name, :auth_provider => "facebook", :auth_uid => "http://localhost:1123/new.tester", :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest" } + end + end + + # Check the e-mail + register_email = ActionMailer::Base.deliveries.first + + assert_equal register_email.to[0], new_email + # Check that the confirm account url is correct + confirm_regex = Regexp.new("/user/redirect_tester_facebook/confirm\\?confirm_string=([a-zA-Z0-9]*)") + register_email.parts.each do |part| + assert_match confirm_regex, part.body.to_s + end + confirm_string = register_email.parts[0].body.match(confirm_regex)[1] + + # Check the page + assert_response :success + assert_template "user/confirm" + + ActionMailer::Base.deliveries.clear + + # Go to the confirmation page + get "/user/#{display_name}/confirm", :confirm_string => confirm_string + assert_response :success + assert_template "user/confirm" + + post "/user/#{display_name}/confirm", :confirm_string => confirm_string + assert_response :redirect + follow_redirect! + assert_response :success + assert_template "site/welcome" + end end diff --git a/test/integration/user_login_test.rb b/test/integration/user_login_test.rb index 258dd5e52..9cab196f2 100644 --- a/test/integration/user_login_test.rb +++ b/test/integration/user_login_test.rb @@ -10,6 +10,7 @@ class UserLoginTest < ActionDispatch::IntegrationTest def teardown OmniAuth.config.mock_auth[:openid] = nil OmniAuth.config.mock_auth[:google] = nil + OmniAuth.config.mock_auth[:facebook] = nil OmniAuth.config.test_mode = false end @@ -968,4 +969,96 @@ class UserLoginTest < ActionDispatch::IntegrationTest assert_equal "google", user.auth_provider assert_equal "987654321", user.auth_uid end + + def test_login_facebook_success + OmniAuth.config.add_mock(:facebook, :uid => "123456789", :extra => { + :id_info => { "openid_id" => "http://localhost:1123/fred.bloggs" } + }) + + get "/login", :referer => "/history" + assert_response :redirect + assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history" + follow_redirect! + assert_response :success + assert_template "user/login" + get auth_path(:provider => "facebook", :origin => "/login") + assert_response :redirect + assert_redirected_to auth_success_path(:provider => "facebook") + follow_redirect! + assert_response :redirect + follow_redirect! + assert_response :success + assert_template "changeset/history" + assert_select "span.username", "facebookuser" + end + + def test_login_facebook_connection_failed + OmniAuth.config.mock_auth[:facebook] = :connection_failed + + get "/login", :referer => "/history" + assert_response :redirect + assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history" + follow_redirect! + assert_response :success + assert_template "user/login" + get auth_path(:provider => "facebook", :origin => "/login") + assert_response :redirect + assert_redirected_to auth_success_path(:provider => "facebook") + follow_redirect! + assert_response :redirect + assert_redirected_to auth_failure_path(:strategy => "facebook", :message => "connection_failed", :origin => "/login") + follow_redirect! + assert_response :redirect + follow_redirect! + assert_response :success + assert_template "login" + assert_select "div.flash.error", "Connection to authentication provider failed" + assert_select "span.username", false + end + + def test_login_facebook_invalid_credentials + OmniAuth.config.mock_auth[:facebook] = :invalid_credentials + + get "/login", :referer => "/history" + assert_response :redirect + assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history" + follow_redirect! + assert_response :success + assert_template "user/login" + get auth_path(:provider => "facebook", :origin => "/login") + assert_response :redirect + assert_redirected_to auth_success_path(:provider => "facebook") + follow_redirect! + assert_response :redirect + assert_redirected_to auth_failure_path(:strategy => "facebook", :message => "invalid_credentials", :origin => "/login") + follow_redirect! + assert_response :redirect + follow_redirect! + assert_response :success + assert_template "login" + assert_select "div.flash.error", "Invalid authentication credentials" + assert_select "span.username", false + end + + def test_login_facebook_unknown + OmniAuth.config.add_mock(:facebook, :uid => "987654321", :extra => { + :id_info => { "openid_id" => "http://localhost:1123/fred.bloggs" } + }) + + get "/login", :referer => "/history" + assert_response :redirect + assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history" + follow_redirect! + assert_response :success + assert_template "user/login" + get auth_path(:provider => "facebook", :origin => "/login") + assert_response :redirect + assert_redirected_to auth_success_path(:provider => "facebook") + follow_redirect! + assert_response :redirect + follow_redirect! + assert_response :success + assert_template "user/new" + assert_select "span.username", false + end end diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 605eb73b9..14e00cc99 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -163,7 +163,7 @@ class UserTest < ActiveSupport::TestCase end def test_visible - assert_equal 17, User.visible.count + assert_equal 18, User.visible.count assert_raise ActiveRecord::RecordNotFound do User.visible.find(users(:suspended_user).id) end @@ -173,7 +173,7 @@ class UserTest < ActiveSupport::TestCase end def test_active - assert_equal 16, User.active.count + assert_equal 17, User.active.count assert_raise ActiveRecord::RecordNotFound do User.active.find(users(:inactive_user).id) end @@ -186,7 +186,7 @@ class UserTest < ActiveSupport::TestCase end def test_identifiable - assert_equal 18, User.identifiable.count + assert_equal 19, User.identifiable.count assert_raise ActiveRecord::RecordNotFound do User.identifiable.find(users(:normal_user).id) end