Skip to content

Commit

Permalink
Add support for Wikimedia authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhughes committed Dec 20, 2016
1 parent 12964ae commit 69b967c
Show file tree
Hide file tree
Showing 13 changed files with 242 additions and 4 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 7 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Binary file added app/assets/images/wikipedia.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions app/views/user/login.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
4 changes: 4 additions & 0 deletions config/example.application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
2 changes: 2 additions & 0 deletions config/initializers/omniauth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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
Expand All @@ -35,6 +36,7 @@
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
Expand Down
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion test/controllers/user_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1384,7 +1384,7 @@ def test_list_get_paginated
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
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
114 changes: 114 additions & 0 deletions test/integration/user_creation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def teardown
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

Expand Down Expand Up @@ -725,4 +726,117 @@ def test_user_create_github_redirect
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
89 changes: 89 additions & 0 deletions test/integration/user_login_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def teardown
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

Expand Down Expand Up @@ -824,6 +825,94 @@ def test_login_github_unknown
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)
Expand Down
6 changes: 3 additions & 3 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def test_user_preferred_editor
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
Expand All @@ -171,7 +171,7 @@ def test_visible
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
Expand All @@ -184,7 +184,7 @@ def test_active
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
Expand Down

0 comments on commit 69b967c

Please sign in to comment.