diff --git a/Gemfile b/Gemfile index c900f93..cc05324 100644 --- a/Gemfile +++ b/Gemfile @@ -7,7 +7,8 @@ gem "mechanize" gem "inherited_resources" gem "dynamic_form" gem "congress-scrapper" - +gem "omniauth" + group :development, :test do gem 'sqlite3-ruby', :require => 'sqlite3' gem 'rspec-rails' diff --git a/Gemfile.lock b/Gemfile.lock index 6b693ae..13f06ba 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,6 +1,8 @@ GEM remote: http://rubygems.org/ specs: + XMLCanonicalizer (1.0.1) + log4r (>= 1.0.4) ZenTest (4.5.0) actionmailer (3.1.1) actionpack (= 3.1.1) @@ -58,6 +60,10 @@ GEM diff-lcs (1.1.3) dynamic_form (1.1.4) erubis (2.7.0) + faraday (0.7.5) + addressable (~> 2.2.6) + multipart-post (~> 1.1.3) + rack (>= 1.1.0, < 2) ffi (1.0.9) has_scope (0.5.1) heroku (2.11.0) @@ -79,6 +85,9 @@ GEM addressable (~> 2.2.6) linecache19 (0.5.12) ruby_core_source (>= 0.1.4) + log4r (1.1.9) + macaddr (1.4.0) + systemu (~> 2.2.0) mail (2.3.0) i18n (>= 0.4.0) mime-types (~> 1.16) @@ -90,16 +99,62 @@ GEM webrobots (~> 0.0, >= 0.0.9) mime-types (1.16) multi_json (1.0.3) + multi_xml (0.4.1) + multipart-post (1.1.3) net-http-digest_auth (1.1.1) net-http-persistent (1.9) + net-ldap (0.2.2) nokogiri (1.5.0) + oa-basic (0.3.2) + oa-core (= 0.3.2) + rest-client (~> 1.6.0) + oa-core (0.3.2) + oa-enterprise (0.3.2) + XMLCanonicalizer (~> 1.0.1) + addressable (~> 2.2.6) + net-ldap (~> 0.2.2) + nokogiri (~> 1.5.0) + oa-core (= 0.3.2) + pyu-ruby-sasl (~> 0.0.3.1) + rubyntlm (~> 0.1.1) + uuid + oa-more (0.3.2) + multi_json (~> 1.0.0) + oa-core (= 0.3.2) + rest-client (~> 1.6.0) + oa-oauth (0.3.2) + faraday (~> 0.7.3) + multi_json (~> 1.0.0) + multi_xml (~> 0.4.0) + oa-core (= 0.3.2) + oauth (~> 0.4.0) + oauth2 (~> 0.5.0) + oa-openid (0.3.2) + oa-core (= 0.3.2) + rack-openid (~> 1.3.1) + ruby-openid-apps-discovery (~> 1.2.0) + oauth (0.4.5) + oauth2 (0.5.1) + faraday (~> 0.7.4) + multi_json (~> 1.0.3) + omniauth (0.3.2) + oa-basic (= 0.3.2) + oa-core (= 0.3.2) + oa-enterprise (= 0.3.2) + oa-more (= 0.3.2) + oa-oauth (= 0.3.2) + oa-openid (= 0.3.2) polyglot (0.3.2) progressbar (0.9.1) + pyu-ruby-sasl (0.0.3.3) rack (1.3.5) rack-cache (1.1) rack (>= 0.4) rack-mount (0.8.3) rack (>= 1.0.0) + rack-openid (1.3.1) + rack (>= 1.1.0) + ruby-openid (>= 2.1.8) rack-ssl (1.3.2) rack rack-test (0.6.1) @@ -146,8 +201,12 @@ GEM columnize (>= 0.3.1) linecache19 (>= 0.5.11) ruby-debug-base19 (>= 0.11.19) + ruby-openid (2.1.8) + ruby-openid-apps-discovery (1.2.0) + ruby-openid (>= 2.1.7) ruby_core_source (0.1.5) archive-tar-minitar (>= 0.5.2) + rubyntlm (0.1.1) rubyzip (0.9.4) selenium-webdriver (2.9.1) childprocess (>= 0.2.1) @@ -163,6 +222,7 @@ GEM sqlite3 (>= 1.3.3) steak (1.1.0) rspec (>= 1.3) + systemu (2.2.0) term-ansicolor (1.0.7) thor (0.14.6) tilt (1.3.3) @@ -170,6 +230,8 @@ GEM polyglot polyglot (>= 0.3.1) tzinfo (0.3.30) + uuid (2.3.4) + macaddr (~> 1.0) webmock (1.7.7) addressable (~> 2.2, > 2.2.5) crack (>= 0.1.7) @@ -192,6 +254,7 @@ DEPENDENCIES inherited_resources launchy mechanize + omniauth progressbar rails (= 3.1.1) rspec-rails diff --git a/app/controllers/user_sessions_controller.rb b/app/controllers/user_sessions_controller.rb index f81ba11..3a47c20 100644 --- a/app/controllers/user_sessions_controller.rb +++ b/app/controllers/user_sessions_controller.rb @@ -10,7 +10,8 @@ def new def authenticate valid_tractis_identity_verification!(ENV["TRACTIS_API_KEY"], params) - @current_user = User.find_or_create_by_dni(params["tractis:attribute:dni"]) do |user| + @current_user = User.find_or_create_by_uid(params["tractis:attribute:dni"]) do |user| + user.provider = "tractis" user.name = params["tractis:attribute:name"] end @@ -18,15 +19,23 @@ def authenticate redirect_back_or_default root_url end - def create + def create_fake name = params[:name].present? ? params[:name] : "Backdoor Mother Fucking Fake User" @current_user = User.find_or_create_by_name(name) do |user| - user.dni = "#{rand(99999999)}V" + user.provider = "fake" + user.uid = name end session[:current_user_id] = @current_user.id redirect_back_or_default root_url end + + def create + auth = request.env["omniauth.auth"] + @current_user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth) + session[:current_user_id] = @current_user.id + redirect_back_or_default root_url + end def destroy session[:current_user_id] = nil diff --git a/app/models/user.rb b/app/models/user.rb index 4aaa54f..7379ad3 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -8,7 +8,8 @@ class User < ActiveRecord::Base after_save :count_votes - validates :dni, :presence => true + validates :provider, :presence => true + validates :uid, :presence => true def has_voted_for?(proposal) voted_proposals.include?(proposal) @@ -67,4 +68,12 @@ def is_admin? admin end -end \ No newline at end of file + def self.create_with_omniauth(auth) + create! do |user| + user.provider = auth["provider"] + user.uid = auth["uid"] + user.name = auth["user_info"]["name"] + end + end + +end diff --git a/app/views/user_sessions/new.html.erb b/app/views/user_sessions/new.html.erb index b508aef..ca40f86 100644 --- a/app/views/user_sessions/new.html.erb +++ b/app/views/user_sessions/new.html.erb @@ -1,12 +1,15 @@ -

<%= t(:dnie_auth_required)%>

+

<%= t(:auth_required)%>

+ +

<%= link_to "Sign in with Twitter", "/auth/twitter" %>

+
<%= identity_verification_gateway(t(:identify_with_dnie), ENV["TRACTIS_API_KEY"], :action => 'authenticate') %>
<% if Rails.env != 'production' %> - <%= form_tag user_session_path do %> + <%= form_tag create_fake_user_session_path do %>

<%= t(:try_with_test_id) %>

<%= label_tag t(:fake_name) %> @@ -15,4 +18,4 @@ <%= submit_tag t(:try_here) %> <% end %> <% end %> -

\ No newline at end of file + diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb new file mode 100644 index 0000000..69aed22 --- /dev/null +++ b/config/initializers/omniauth.rb @@ -0,0 +1,10 @@ +Rails.application.config.middleware.use OmniAuth::Builder do + if ENV['TWITTER_CONSUMER_KEY'].blank? or ENV['TWITTER_CONSUMER_SECRET'].blank? + warn "*" * 80 + warn "WARNING: Missing consumer key or secret. First, register an app with Twitter at" + warn "https://dev.twitter.com/apps to obtain OAuth credentials. Then, start the server" + warn "with the command: TWITTER_CONSUMER_KEY=abc TWITTER_CONSUMER_SECRET=123 rails server" + warn "*" * 80 + end + use OmniAuth::Strategies::Twitter, ENV['TWITTER_CONSUMER_KEY'], ENV['TWITTER_CONSUMER_SECRET'] +end diff --git a/config/locales/es.yml b/config/locales/es.yml index cd15be9..6e531b1 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -43,7 +43,7 @@ vote_proposal: 'Vota esta propuesta' explain: "Explica tu opinión (opcional)" optional_link: "Enlace (opcional)" - dnie_auth_required: 'Autenticación con DNIe requerida' + auth_required: 'Autenticación requerida' identify_with_dnie: 'Identifícate con tu DNIe' try_with_test_id: 'o haz tongo creando una identidad falsa...' fake_name: 'falso nombre' diff --git a/config/routes.rb b/config/routes.rb index fbcc1e4..3f24840 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -17,6 +17,7 @@ resource :user_session do get 'authenticate' + post 'create_fake' end resources :users do @@ -28,6 +29,8 @@ end end + get '/auth/twitter/callback', :to => 'user_sessions#create', :as => 'callback' + root :to => "welcome#index" # The priority is based upon order of creation: diff --git a/db/migrate/20111023200908_add_provider_and_uid_to_users.rb b/db/migrate/20111023200908_add_provider_and_uid_to_users.rb new file mode 100644 index 0000000..562321f --- /dev/null +++ b/db/migrate/20111023200908_add_provider_and_uid_to_users.rb @@ -0,0 +1,8 @@ +class AddProviderAndUidToUsers < ActiveRecord::Migration + def change + change_table :users do |t| + t.string :provider + t.string :uid + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 84a9a95..a46bef9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20110319234436) do +ActiveRecord::Schema.define(:version => 20111023200908) do create_table "categories", :force => true do |t| t.string "name" @@ -68,6 +68,8 @@ t.boolean "admin", :default => false t.string "dni" t.string "name" + t.string "provider" + t.string "uid" end create_table "votes", :force => true do |t| diff --git a/spec/acceptance/dni_authentication_spec.rb b/spec/acceptance/dni_authentication_spec.rb index c97f8af..9c4407d 100644 --- a/spec/acceptance/dni_authentication_spec.rb +++ b/spec/acceptance/dni_authentication_spec.rb @@ -26,7 +26,7 @@ register_as("Bad Ass Mother Fucking New User", "12345678V") page.should have_content I18n.t(:currently_logged_as, :username => "Bad Ass Mother Fucking New User") - User.find_by_name_and_dni("Bad Ass Mother Fucking New User", "12345678V").should be + User.find_by_name_and_uid("Bad Ass Mother Fucking New User", "12345678V").should be end scenario "Invalid authentication" do diff --git a/spec/acceptance/spokesmen_spec.rb b/spec/acceptance/spokesmen_spec.rb index 2a336e2..395de72 100644 --- a/spec/acceptance/spokesmen_spec.rb +++ b/spec/acceptance/spokesmen_spec.rb @@ -77,7 +77,7 @@ click_link "Fan de Punset" click_button "Elegir a Fan de Punset como mi portavoz" - page.should have_content("Autenticación con DNIe requerida") + page.should have_content("Autenticación requerida") page.should_not have_content("Has elegido a tu portavoz.") login_as @user diff --git a/spec/acceptance/support/helpers.rb b/spec/acceptance/support/helpers.rb index 42f1e7b..046808b 100644 --- a/spec/acceptance/support/helpers.rb +++ b/spec/acceptance/support/helpers.rb @@ -1,7 +1,7 @@ module HelperMethods def login_as(user) stub_tractis_request - get_tractis_callback(user.name, user.dni) + get_tractis_callback(user.name, user.uid) end def register_as(name, dni) diff --git a/spec/acceptance/vote_for_proposals_spec.rb b/spec/acceptance/vote_for_proposals_spec.rb index abe60a5..c62a52f 100644 --- a/spec/acceptance/vote_for_proposals_spec.rb +++ b/spec/acceptance/vote_for_proposals_spec.rb @@ -77,7 +77,7 @@ visit proposal_path(proposal) click_button I18n.t(:yes_option) - page.should have_content I18n.t(:dnie_auth_required) + page.should have_content I18n.t(:auth_required) page.should_not have_css("button", :text => I18n.t(:i_am_sure)) login_as @user diff --git a/spec/support/factories.rb b/spec/support/factories.rb index 964e4aa..5ce81d3 100644 --- a/spec/support/factories.rb +++ b/spec/support/factories.rb @@ -15,7 +15,8 @@ def create_vote(attrs = {}) end def create_user(attrs = {}) - attrs[:dni] ||= "#{rand(9999999)}V" + attrs[:provider] ||= "test" + attrs[:uid] ||= "#{rand(9999999)}V" attrs[:name] ||= "Mother Fucking Real User" User.create!(attrs) end @@ -30,4 +31,4 @@ def create_proposer(attrs = {}) end -RSpec.configuration.include Factories \ No newline at end of file +RSpec.configuration.include Factories