]> git.openstreetmap.org Git - rails.git/commitdiff
Add support for Wikimedia authentication
authorTom Hughes <tom@compton.nu>
Fri, 17 Jun 2016 15:47:03 +0000 (16:47 +0100)
committerTom Hughes <tom@compton.nu>
Tue, 20 Dec 2016 22:32:25 +0000 (22:32 +0000)
Closes #1146

13 files changed:
Gemfile
Gemfile.lock
app/assets/images/wikipedia.png [new file with mode: 0644]
app/views/user/login.html.erb
config/example.application.yml
config/initializers/omniauth.rb
config/locales/en.yml
lib/auth.rb
test/controllers/user_controller_test.rb
test/fixtures/users.yml
test/integration/user_creation_test.rb
test/integration/user_login_test.rb
test/models/user_test.rb

diff --git a/Gemfile b/Gemfile
index 829403819659b2ac566e29e426172690b374b480..ef93f0d8ff0960b072d87945805c46c87daecee1 100644 (file)
--- a/Gemfile
+++ b/Gemfile
@@ -64,6 +64,7 @@ gem "omniauth-google-oauth2", ">= 0.2.7"
 gem "omniauth-facebook"
 gem "omniauth-windowslive"
 gem "omniauth-github"
+gem "omniauth-mediawiki", ">= 0.0.3"
 
 # Markdown formatting support
 gem "redcarpet"
index 181da7f095f380698887abf2f834c4af9dd00f6a..5ad974190875fc5b7b4dd8501f43f335faf981d2 100644 (file)
@@ -190,6 +190,12 @@ GEM
       multi_json (~> 1.3)
       omniauth (>= 1.1.1)
       omniauth-oauth2 (>= 1.3.1)
+    omniauth-mediawiki (0.0.3)
+      jwt (~> 1.0)
+      omniauth-oauth (~> 1.0)
+    omniauth-oauth (1.1.0)
+      oauth
+      omniauth (~> 1.0)
     omniauth-oauth2 (1.4.0)
       oauth2 (~> 1.0)
       omniauth (~> 1.2)
@@ -353,6 +359,7 @@ DEPENDENCIES
   omniauth-facebook
   omniauth-github
   omniauth-google-oauth2 (>= 0.2.7)
+  omniauth-mediawiki (>= 0.0.3)
   omniauth-openid
   omniauth-windowslive
   paperclip (~> 4.0)
diff --git a/app/assets/images/wikipedia.png b/app/assets/images/wikipedia.png
new file mode 100644 (file)
index 0000000..784c184
Binary files /dev/null and b/app/assets/images/wikipedia.png differ
index 3d28a770aa303a3da94a8f1079a122146c282413..26bb8089579130ab849c3511a2021cde6e5fd946 100644 (file)
@@ -58,6 +58,9 @@
           <% if defined?(GITHUB_AUTH_ID) -%>
           <li><%= auth_button "github", "github" %></li>
           <% end -%>
+          <% if defined?(WIKIPEDIA_AUTH_ID) -%>
+          <li><%= auth_button "wikipedia", "wikipedia" %></li>
+          <% end -%>
           <li><%= auth_button "yahoo", "openid", :openid_url => "yahoo.com" %></li>
           <li><%= auth_button "wordpress", "openid", :openid_url => "wordpress.com" %></li>
           <li><%= auth_button "aol", "openid", :openid_url => "aol.com" %></li>
index b5d4db363f054d8b14af4eab88777850b2bf99d2..fd388ad88221dd202570366c14aa6cb207ccff8b 100644 (file)
@@ -107,6 +107,8 @@ defaults: &defaults
   #windowslive_auth_secret: ""
   #github_auth_id: ""
   #github_auth_secret: ""
+  #wikipedia_auth_id: ""
+  #wikipedia_auth_secret: ""
   # MapQuest authentication details
   #mapquest_key: ""
   # Mapzen authentication details
@@ -134,3 +136,5 @@ test:
   windowslive_auth_secret: "dummy"
   github_auth_id: "dummy"
   github_auth_secret: "dummy"
+  wikipedia_auth_id: "dummy"
+  wikipedia_auth_secret: "dummy"
index f7d0c5f6ac4b0d698a46ac13ea7dfd29942d5d1a..79041372185630a0f32cb7cd29d1922d387f17cb 100644 (file)
@@ -24,6 +24,7 @@ google_options = { :name => "google", :scope => "email", :access_type => "online
 facebook_options = { :name => "facebook", :scope => "email" }
 windowslive_options = { :name => "windowslive", :scope => "wl.signin,wl.emails" }
 github_options = { :name => "github", :scope => "user:email" }
+wikipedia_options = { :name => "wikipedia", :client_options => { :site => "https://commons.wikimedia.org" } }
 
 if defined?(GOOGLE_OPENID_REALM)
   google_options[:openid_realm] = GOOGLE_OPENID_REALM
@@ -35,6 +36,7 @@ Rails.application.config.middleware.use OmniAuth::Builder do
   provider :facebook, FACEBOOK_AUTH_ID, FACEBOOK_AUTH_SECRET, facebook_options if defined?(FACEBOOK_AUTH_ID)
   provider :windowslive, WINDOWSLIVE_AUTH_ID, WINDOWSLIVE_AUTH_SECRET, windowslive_options if defined?(WINDOWSLIVE_AUTH_ID)
   provider :github, GITHUB_AUTH_ID, GITHUB_AUTH_SECRET, github_options if defined?(GITHUB_AUTH_ID)
+  provider :mediawiki, WIKIPEDIA_AUTH_ID, WIKIPEDIA_AUTH_SECRET, wikipedia_options if defined?(WIKIPEDIA_AUTH_ID)
 end
 
 # Pending fix for: https://github.com/intridea/omniauth/pull/795
index 2681fde86107d98c6aea17b73d21eacf061c0adc..1b7c113b0a5203116f9c89911b96e90cfab80761 100644 (file)
@@ -1746,6 +1746,9 @@ en:
         github:
           title: Login with GitHub
           alt: Login with a GitHub Account
+        wikipedia:
+          title: Login with Wikipedia
+          alt: Login with a Wikipedia Account
         yahoo:
           title: Login with Yahoo
           alt: Login with a Yahoo OpenID
index 16102ed203fa1c0d15dd0a9940c8decf5b4516b0..434994329d7b62a435335dbbeb6da5c3489ee4b5 100644 (file)
@@ -4,5 +4,6 @@ module Auth
     providers["Facebook"] = "facebook" if defined?(FACEBOOK_AUTH_ID)
     providers["Windows Live"] = "windowslive" if defined?(WINDOWSLIVE_AUTH_ID)
     providers["GitHub"] = "github" if defined?(GITHUB_AUTH_ID)
+    providers["Wikipedia"] = "wikipedia" if defined?(WIKIPEDIA_AUTH_ID)
   end.freeze
 end
index e309b1662b0d49073d23d552e0231cd937331c83..95eccb2c5b8acff30962f896329bedb073c37ca0 100644 (file)
@@ -1384,7 +1384,7 @@ class UserControllerTest < ActionController::TestCase
     get :list, :page => 3
     assert_response :success
     assert_template :list
-    assert_select "table#user_list tr", :count => 25
+    assert_select "table#user_list tr", :count => 26
   end
 
   def test_list_post_confirm
index bee5aa282f64d7f4e42de78091bd300b6d34397c..18b33f6bfc5753ff2bc876ceca5f5757202ef7c8 100644 (file)
@@ -333,3 +333,17 @@ second_gravatar_user:
   terms_agreed: "2010-01-01 11:22:33"
   terms_seen: true
   creation_ip: "1.2.3.4"
+
+wikipedia_user:
+  id: 25
+  email: wikipedia-user@example.com
+  status: active
+  pass_crypt: <%= Digest::MD5.hexdigest('test') %>
+  creation_time: "2008-05-01 01:23:45"
+  display_name: wikipediauser
+  data_public: true
+  auth_provider: wikipedia
+  auth_uid: 123456789
+  terms_agreed: "2010-01-01 11:22:33"
+  terms_seen: true
+  languages: en
index cdf4fcfe9cffafb1d924a7286dc34a6d7851a556..db35be5f350e8db3bd7b223cab7d8f6c6a8ca56d 100644 (file)
@@ -19,6 +19,7 @@ class UserCreationTest < ActionDispatch::IntegrationTest
     OmniAuth.config.mock_auth[:facebook] = nil
     OmniAuth.config.mock_auth[:windowslive] = nil
     OmniAuth.config.mock_auth[:github] = nil
+    OmniAuth.config.mock_auth[:wikipedia] = nil
     OmniAuth.config.test_mode = false
   end
 
@@ -725,4 +726,117 @@ class UserCreationTest < ActionDispatch::IntegrationTest
     assert_response :success
     assert_template "site/welcome"
   end
+
+  def test_user_create_wikipedia_success
+    OmniAuth.config.add_mock(:wikipedia, :uid => "123454321")
+
+    new_email = "newtester-wikipedia@osm.org"
+    display_name = "new_tester-wikipedia"
+    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 => "wikipedia", :pass_crypt => "", :pass_crypt_confirmation => "" }
+        assert_response :redirect
+        assert_redirected_to auth_path(:provider => "wikipedia", :origin => "/user/new")
+        follow_redirect!
+        assert_response :redirect
+        assert_redirected_to auth_success_path(:provider => "wikipedia", :origin => "/user/new")
+        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 => "wikipedia", :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_wikipedia_failure
+    OmniAuth.config.mock_auth[:wikipedia] = :connection_failed
+
+    new_email = "newtester-wikipedia2@osm.org"
+    display_name = "new_tester-wikipedia2"
+    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 => "wikipedia", :pass_crypt => "", :pass_crypt_confirmation => "" }
+        assert_response :redirect
+        assert_redirected_to auth_path(:provider => "wikipedia", :origin => "/user/new")
+        follow_redirect!
+        assert_response :redirect
+        assert_redirected_to auth_success_path(:provider => "wikipedia", :origin => "/user/new")
+        follow_redirect!
+        assert_response :redirect
+        assert_redirected_to auth_failure_path(:strategy => "wikipedia", :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_wikipedia_redirect
+    OmniAuth.config.add_mock(:wikipedia, :uid => "123454321")
+
+    new_email = "redirect_tester_wikipedia@osm.org"
+    display_name = "redirect_tester_wikipedia"
+    # 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 => "wikipedia", :pass_crypt => "", :pass_crypt_confirmation => "" }, :referer => referer
+        assert_response :redirect
+        assert_redirected_to auth_path(:provider => "wikipedia", :origin => "/user/new")
+        follow_redirect!
+        assert_response :redirect
+        assert_redirected_to auth_success_path(:provider => "wikipedia", :origin => "/user/new")
+        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 => "wikipedia", :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_wikipedia/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
index d027334d0d3cad5b85eff9b2903334854b8e8dcd..e442eba6841a1998a3df87413d6cd9a634bcbbdf 100644 (file)
@@ -13,6 +13,7 @@ class UserLoginTest < ActionDispatch::IntegrationTest
     OmniAuth.config.mock_auth[:facebook] = nil
     OmniAuth.config.mock_auth[:windowslive] = nil
     OmniAuth.config.mock_auth[:github] = nil
+    OmniAuth.config.mock_auth[:wikipedia] = nil
     OmniAuth.config.test_mode = false
   end
 
@@ -824,6 +825,94 @@ class UserLoginTest < ActionDispatch::IntegrationTest
     assert_select "span.username", false
   end
 
+  def test_login_wikipedia_success
+    OmniAuth.config.add_mock(:wikipedia, :uid => "123456789")
+
+    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 => "wikipedia", :origin => "/login?referer=%2Fhistory", :referer => "/history")
+    assert_response :redirect
+    assert_redirected_to auth_success_path(:provider => "wikipedia", :origin => "/login?referer=%2Fhistory", :referer => "/history")
+    follow_redirect!
+    assert_response :redirect
+    follow_redirect!
+    assert_response :success
+    assert_template "changeset/history"
+    assert_select "span.username", "wikipediauser"
+  end
+
+  def test_login_wikipedia_connection_failed
+    OmniAuth.config.mock_auth[:wikipedia] = :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 => "wikipedia", :origin => "/login?referer=%2Fhistory", :referer => "/history")
+    assert_response :redirect
+    assert_redirected_to auth_success_path(:provider => "wikipedia", :origin => "/login?referer=%2Fhistory", :referer => "/history")
+    follow_redirect!
+    assert_response :redirect
+    assert_redirected_to auth_failure_path(:strategy => "wikipedia", :message => "connection_failed", :origin => "/login?referer=%2Fhistory")
+    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_wikipedia_invalid_credentials
+    OmniAuth.config.mock_auth[:wikipedia] = :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 => "wikipedia", :origin => "/login?referer=%2Fhistory", :referer => "/history")
+    assert_response :redirect
+    assert_redirected_to auth_success_path(:provider => "wikipedia", :origin => "/login?referer=%2Fhistory", :referer => "/history")
+    follow_redirect!
+    assert_response :redirect
+    assert_redirected_to auth_failure_path(:strategy => "wikipedia", :message => "invalid_credentials", :origin => "/login?referer=%2Fhistory")
+    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_wikipedia_unknown
+    OmniAuth.config.add_mock(:wikipedia, :uid => "987654321")
+
+    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 => "wikipedia", :origin => "/login?referer=%2Fhistory", :referer => "/history")
+    assert_response :redirect
+    assert_redirected_to auth_success_path(:provider => "wikipedia", :origin => "/login?referer=%2Fhistory", :referer => "/history")
+    follow_redirect!
+    assert_response :redirect
+    follow_redirect!
+    assert_response :success
+    assert_template "user/new"
+    assert_select "span.username", false
+  end
+
   private
 
   def try_password_login(username, password, remember_me = nil)
index b7b37d3b42afa0b2af119321b5e6f08510cb1da1..f8f46cfaff85cc7b788397cd04d3983e5ad51464 100644 (file)
@@ -161,7 +161,7 @@ class UserTest < ActiveSupport::TestCase
   end
 
   def test_visible
-    assert_equal 22, User.visible.count
+    assert_equal 23, User.visible.count
     assert_raise ActiveRecord::RecordNotFound do
       User.visible.find(users(:suspended_user).id)
     end
@@ -171,7 +171,7 @@ class UserTest < ActiveSupport::TestCase
   end
 
   def test_active
-    assert_equal 21, User.active.count
+    assert_equal 22, User.active.count
     assert_raise ActiveRecord::RecordNotFound do
       User.active.find(users(:inactive_user).id)
     end
@@ -184,7 +184,7 @@ class UserTest < ActiveSupport::TestCase
   end
 
   def test_identifiable
-    assert_equal 23, User.identifiable.count
+    assert_equal 24, User.identifiable.count
     assert_raise ActiveRecord::RecordNotFound do
       User.identifiable.find(users(:normal_user).id)
     end