Skip to content

Commit

Permalink
Merge pull request #42 from bglusman/omniauth
Browse files Browse the repository at this point in the history
Omniauth support added and Tractis modified to work together
  • Loading branch information
voodoorai2000 committed Nov 20, 2011
2 parents 776e6d1 + 1fc486c commit 407ac2a
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 17 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Expand Up @@ -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'
Expand Down
63 changes: 63 additions & 0 deletions 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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -163,13 +222,16 @@ 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)
treetop (1.4.10)
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)
Expand All @@ -192,6 +254,7 @@ DEPENDENCIES
inherited_resources
launchy
mechanize
omniauth
progressbar
rails (= 3.1.1)
rspec-rails
Expand Down
15 changes: 12 additions & 3 deletions app/controllers/user_sessions_controller.rb
Expand Up @@ -10,23 +10,32 @@ 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

session[:current_user_id] = @current_user.id
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
Expand Down
13 changes: 11 additions & 2 deletions app/models/user.rb
Expand Up @@ -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)
Expand Down Expand Up @@ -67,4 +68,12 @@ def is_admin?
admin
end

end
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
9 changes: 6 additions & 3 deletions app/views/user_sessions/new.html.erb
@@ -1,12 +1,15 @@
<h2><%= t(:dnie_auth_required)%></h2>
<h2><%= t(:auth_required)%></h2>

<section id="login-section">

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

<div style="margin-bottom: 10px;">
<%= identity_verification_gateway(t(:identify_with_dnie), ENV["TRACTIS_API_KEY"], :action => 'authenticate') %>
</div>

<% if Rails.env != 'production' %>
<%= form_tag user_session_path do %>
<%= form_tag create_fake_user_session_path do %>
<p><%= t(:try_with_test_id) %></p>
<p>
<%= label_tag t(:fake_name) %>
Expand All @@ -15,4 +18,4 @@
<%= submit_tag t(:try_here) %>
<% end %>
<% end %>
</section>
</section>
10 changes: 10 additions & 0 deletions 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
2 changes: 1 addition & 1 deletion config/locales/es.yml
Expand Up @@ -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'
Expand Down
3 changes: 3 additions & 0 deletions config/routes.rb
Expand Up @@ -17,6 +17,7 @@

resource :user_session do
get 'authenticate'
post 'create_fake'
end

resources :users do
Expand All @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions 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
4 changes: 3 additions & 1 deletion db/schema.rb
Expand Up @@ -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"
Expand Down Expand Up @@ -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|
Expand Down
2 changes: 1 addition & 1 deletion spec/acceptance/dni_authentication_spec.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/acceptance/spokesmen_spec.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion 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)
Expand Down
2 changes: 1 addition & 1 deletion spec/acceptance/vote_for_proposals_spec.rb
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions spec/support/factories.rb
Expand Up @@ -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
Expand All @@ -30,4 +31,4 @@ def create_proposer(attrs = {})

end

RSpec.configuration.include Factories
RSpec.configuration.include Factories

0 comments on commit 407ac2a

Please sign in to comment.