Add support for Windows Live authentication
authorTom Hughes <tom@compton.nu>
Mon, 27 Apr 2015 21:52:42 +0000 (22:52 +0100)
committerTom Hughes <tom@compton.nu>
Tue, 19 May 2015 13:04:09 +0000 (14:04 +0100)
13 files changed:
Gemfile
Gemfile.lock
app/assets/images/windowslive.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 09fa2d8..b5547dc 100644 (file)
--- a/Gemfile
+++ b/Gemfile
@@ -59,6 +59,7 @@ gem "omniauth"
 gem "omniauth-openid"
 gem "openstreetmap-omniauth-google-oauth2", ">= 0.2.6.1", :require => "omniauth-google-oauth2"
 gem "omniauth-facebook"
+gem "omniauth-windowslive"
 
 # Markdown formatting support
 gem "redcarpet"
index 05b8733..333fb25 100644 (file)
@@ -158,6 +158,9 @@ GEM
     omniauth-openid (1.0.1)
       omniauth (~> 1.0)
       rack-openid (~> 1.3.1)
+    omniauth-windowslive (0.0.9)
+      multi_json (>= 1.0.3)
+      omniauth-oauth2 (~> 1.0)
     openstreetmap-i18n-js (3.0.0.rc5.3)
       i18n
     openstreetmap-omniauth-google-oauth2 (0.2.6.1)
@@ -315,6 +318,7 @@ DEPENDENCIES
   omniauth
   omniauth-facebook
   omniauth-openid
+  omniauth-windowslive
   openstreetmap-i18n-js (>= 3.0.0.rc5.3)
   openstreetmap-omniauth-google-oauth2 (>= 0.2.6.1)
   paperclip (~> 4.0)
diff --git a/app/assets/images/windowslive.png b/app/assets/images/windowslive.png
new file mode 100644 (file)
index 0000000..8df253c
Binary files /dev/null and b/app/assets/images/windowslive.png differ
index 2e4daf3..ded0331 100644 (file)
@@ -48,6 +48,9 @@
           <% if defined?(FACEBOOK_AUTH_ID) -%>
           <li><%= auth_button "facebook", "facebook" %></li>
           <% end -%>
+          <% if defined?(WINDOWSLIVE_AUTH_ID) -%>
+          <li><%= auth_button "windowslive", "windowslive" %></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 dd2a174..0af929e 100644 (file)
@@ -94,6 +94,8 @@ defaults: &defaults
   #google_openid_realm: ""
   #facebook_auth_id: ""
   #facebook_auth_secret: ""
+  #windowslive_auth_id: ""
+  #windowslive_auth_secret: ""
 
 development:
   <<: *defaults
@@ -111,3 +113,5 @@ test:
   google_openid_realm: "https://www.openstreetmap.org"
   facebook_auth_id: "dummy"
   facebook_auth_secret: "dummy"
+  windowslive_auth_id: "dummy"
+  windowslive_auth_secret: "dummy"
index 508d326..a654f2a 100644 (file)
@@ -22,6 +22,7 @@ end
 openid_options = { :name => "openid", :store => openid_store }
 google_options = { :name => "google", :scope => "email", :access_type => "online" }
 facebook_options = { :name => "facebook", :scope => "email" }
+windowslive_options = { :name => "windowslive", :scope => "wl.signin,wl.emails" }
 
 if defined?(GOOGLE_OPENID_REALM)
   google_options[:openid_realm] = GOOGLE_OPENID_REALM
@@ -31,6 +32,7 @@ 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)
+  provider :windowslive, WINDOWSLIVE_AUTH_ID, WINDOWSLIVE_AUTH_SECRET, windowslive_options if defined?(WINDOWSLIVE_AUTH_ID)
 end
 
 # Pending fix for: https://github.com/intridea/omniauth/pull/795
index e554832..531c70f 100644 (file)
@@ -1723,6 +1723,9 @@ en:
         facebook:
           title: Login with Facebook
           alt: Login with a Facebook Account
+        windowslive:
+          title: Login with Windows Live
+          alt: Login with a Windows Live Account
         yahoo:
           title: Login with Yahoo
           alt: Login with a Yahoo OpenID
@@ -1996,6 +1999,7 @@ en:
       invalid_credentials: Invalid authentication credentials
       no_authorization_code: No authorization code
       unknown_signature_algorithm: Unknown signature algorithm
+      invalid_scope: Invalid scope
   user_role:
     filter:
       not_an_administrator: "Only administrators can perform user role management, and you are not an administrator."
index b5ac0d2..6361739 100644 (file)
@@ -2,4 +2,5 @@ module Auth
   PROVIDERS = { "None" => "", "OpenID" => "openid" }
   PROVIDERS["Google"] = "google" if defined?(GOOGLE_AUTH_ID)
   PROVIDERS["Facebook"] = "facebook" if defined?(FACEBOOK_AUTH_ID)
+  PROVIDERS["Windows Live"] = "windowslive" if defined?(WINDOWSLIVE_AUTH_ID)
 end
index 0f08ced..03238ea 100644 (file)
@@ -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 => 21
+    assert_select "table#user_list tr", :count => 22
   end
 
   def test_list_post_confirm
index 30b27c7..cb861ab 100644 (file)
@@ -232,7 +232,7 @@ north_pole_user:
   languages: en
   home_lat: 89.9
   home_lon: 146.8
+
 german_user:
   id: 18
   email: german@example.com
@@ -276,3 +276,17 @@ facebook_user:
   terms_agreed: "2010-01-01 11:22:33"
   terms_seen: true
   languages: en
+
+windowslive_user:
+  id: 21
+  email: windowslive-user@example.com
+  status: active
+  pass_crypt: <%= Digest::MD5.hexdigest('test') %>
+  creation_time: "2008-05-01 01:23:45"
+  display_name: windowsliveuser
+  data_public: true
+  auth_provider: windowslive
+  auth_uid: 123456789
+  terms_agreed: "2010-01-01 11:22:33"
+  terms_seen: true
+  languages: en
index 15fcaf2..940eb30 100644 (file)
@@ -15,6 +15,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.mock_auth[:windowslive] = nil
     OmniAuth.config.test_mode = false
   end
 
@@ -499,4 +500,121 @@ class UserCreationTest < ActionDispatch::IntegrationTest
     assert_response :success
     assert_template "site/welcome"
   end
+
+  def test_user_create_windowslive_success
+    OmniAuth.config.add_mock(:windowslive, :uid => "123454321", :extra => {
+                               :id_info => { "openid_id" => "http://localhost:1123/new.tester" }
+                             })
+
+    new_email = "newtester-windowslive@osm.org"
+    display_name = "new_tester-windowslive"
+    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 => "windowslive", :pass_crypt => "", :pass_crypt_confirmation => "" }
+        assert_response :redirect
+        assert_redirected_to auth_path(:provider => "windowslive", :origin => "/user/new")
+        follow_redirect!
+        assert_response :redirect
+        assert_redirected_to auth_success_path(:provider => "windowslive")
+        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 => "windowslive", :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_windowslive_failure
+    OmniAuth.config.mock_auth[:windowslive] = :connection_failed
+
+    new_email = "newtester-windowslive2@osm.org"
+    display_name = "new_tester-windowslive2"
+    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 => "windowslive", :pass_crypt => "", :pass_crypt_confirmation => "" }
+        assert_response :redirect
+        assert_redirected_to auth_path(:provider => "windowslive", :origin => "/user/new")
+        follow_redirect!
+        assert_response :redirect
+        assert_redirected_to auth_success_path(:provider => "windowslive")
+        follow_redirect!
+        assert_response :redirect
+        assert_redirected_to auth_failure_path(:strategy => "windowslive", :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_windowslive_redirect
+    OmniAuth.config.add_mock(:windowslive, :uid => "123454321", :extra => {
+                               :id_info => { "openid_id" => "http://localhost:1123/new.tester" }
+                             })
+
+    new_email = "redirect_tester_windowslive@osm.org"
+    display_name = "redirect_tester_windowslive"
+    # 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 => "windowslive", :pass_crypt => "", :pass_crypt_confirmation => "" }, :referer => referer
+        assert_response :redirect
+        assert_redirected_to auth_path(:provider => "windowslive", :origin => "/user/new")
+        follow_redirect!
+        assert_response :redirect
+        assert_redirected_to auth_success_path(:provider => "windowslive")
+        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 => "windowslive", :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_windowslive/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 9cab196..ae1c3cf 100644 (file)
@@ -11,6 +11,7 @@ class UserLoginTest < ActionDispatch::IntegrationTest
     OmniAuth.config.mock_auth[:openid] = nil
     OmniAuth.config.mock_auth[:google] = nil
     OmniAuth.config.mock_auth[:facebook] = nil
+    OmniAuth.config.mock_auth[:windowslive] = nil
     OmniAuth.config.test_mode = false
   end
 
@@ -1061,4 +1062,96 @@ class UserLoginTest < ActionDispatch::IntegrationTest
     assert_template "user/new"
     assert_select "span.username", false
   end
+
+  def test_login_windowslive_success
+    OmniAuth.config.add_mock(:windowslive, :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 => "windowslive", :origin => "/login")
+    assert_response :redirect
+    assert_redirected_to auth_success_path(:provider => "windowslive")
+    follow_redirect!
+    assert_response :redirect
+    follow_redirect!
+    assert_response :success
+    assert_template "changeset/history"
+    assert_select "span.username", "windowsliveuser"
+  end
+
+  def test_login_windowslive_connection_failed
+    OmniAuth.config.mock_auth[:windowslive] = :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 => "windowslive", :origin => "/login")
+    assert_response :redirect
+    assert_redirected_to auth_success_path(:provider => "windowslive")
+    follow_redirect!
+    assert_response :redirect
+    assert_redirected_to auth_failure_path(:strategy => "windowslive", :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_windowslive_invalid_credentials
+    OmniAuth.config.mock_auth[:windowslive] = :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 => "windowslive", :origin => "/login")
+    assert_response :redirect
+    assert_redirected_to auth_success_path(:provider => "windowslive")
+    follow_redirect!
+    assert_response :redirect
+    assert_redirected_to auth_failure_path(:strategy => "windowslive", :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_windowslive_unknown
+    OmniAuth.config.add_mock(:windowslive, :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 => "windowslive", :origin => "/login")
+    assert_response :redirect
+    assert_redirected_to auth_success_path(:provider => "windowslive")
+    follow_redirect!
+    assert_response :redirect
+    follow_redirect!
+    assert_response :success
+    assert_template "user/new"
+    assert_select "span.username", false
+  end
 end
index 14e00cc..b3e6eae 100644 (file)
@@ -163,7 +163,7 @@ class UserTest < ActiveSupport::TestCase
   end
 
   def test_visible
-    assert_equal 18, User.visible.count
+    assert_equal 19, 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 17, User.active.count
+    assert_equal 18, 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 19, User.identifiable.count
+    assert_equal 20, User.identifiable.count
     assert_raise ActiveRecord::RecordNotFound do
       User.identifiable.find(users(:normal_user).id)
     end