Changeset 276

Show
Ignore:
Timestamp:
03/11/08 23:47:04 (5 months ago)
Author:
gdagley
Message:

add simple OpenID support, needs tests

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • incubator/jumpstart/app/controllers/sessions_controller.rb

    r214 r276  
    77 
    88  def create 
    9     self.current_user = User.authenticate(params[:email], params[:password]) 
    10     if logged_in? 
    11       if params[:remember_me] == "1" 
    12         self.current_user.remember_me 
    13         cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at } 
    14       end 
    15       redirect_back_or_default('/') 
    16       flash[:notice] = "Logged in successfully" 
    17     else 
    18       render :action => 'new' 
     9    if using_open_id? 
     10      open_id_authentication 
     11    elsif params[:email] 
     12      password_authentication(params[:email], params[:password]) 
    1913    end 
    2014  end 
     
    2519    reset_session 
    2620    flash[:notice] = "You have been logged out." 
    27     redirect_back_or_default('/') 
     21    redirect_back_or_default root_path 
    2822  end 
     23   
     24  protected 
     25   
     26    def password_authentication(login, password) 
     27      self.current_user = User.authenticate(params[:email], params[:password]) 
     28      if logged_in? 
     29        if params[:remember_me] == "1" 
     30          self.current_user.remember_me 
     31          cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at } 
     32        end         
     33        successful_login 
     34      else 
     35        failed_login "Invalid login or password" 
     36      end 
     37    end 
     38 
     39    def open_id_authentication 
     40      authenticate_with_open_id do |result, identity_url| 
     41        if result.successful? 
     42          if self.current_user = User.find_or_create_by_identity_url(identity_url) 
     43            successful_login 
     44          else 
     45            failed_login "Sorry, no user by that identity URL exists (#{identity_url})" 
     46          end 
     47        else 
     48          failed_login result.message 
     49        end 
     50      end 
     51    end 
     52   
     53  private 
     54  
     55    def successful_login 
     56      flash[:notice] = "Logged in successfully" 
     57      redirect_back_or_default root_path 
     58    end 
     59 
     60    def failed_login(message) 
     61      flash[:warning] = message 
     62      redirect_to login_path 
     63    end 
    2964end 
  • incubator/jumpstart/app/controllers/users_controller.rb

    r271 r276  
    4949    else 
    5050      flash[:user] = @user  
    51       redirect_to edit_user_path @user 
     51      redirect_to edit_user_path(@user) 
    5252    end 
    5353  end 
  • incubator/jumpstart/app/views/layouts/application.html.erb

    r271 r276  
    44    <%= html_title AppConfiguration.domain %> 
    55    <%= meta_tags @meta %> 
     6    <%= stylesheet_link_tag 'application' %> 
    67  </head> 
    78  <body> 
  • incubator/jumpstart/app/views/sessions/new.html.erb

    r271 r276  
    44<%= page_title 'User Login' %> 
    55 
    6 <% form_tag session_path do -%> 
    7   <p> 
    8     <label for="email">Email</label><br/> 
    9     <%= text_field_tag 'email' %> 
    10   </p> 
     6<div id="openid-login"> 
     7  <% form_tag session_path do -%> 
     8    <p> 
     9      <label for="openid_url">OpenID</label><br/> 
     10      <%= text_field_tag 'openid_url' %>  
     11    </p> 
    1112 
    12   <p> 
    13     <label for="password">Password</label><br/> 
    14     <%= password_field_tag 'password' %> 
    15   </p> 
     13    <p> 
     14      <%= submit_tag 'Log in with OpenID' %> 
     15      <%= link_to 'Cancel', root_path %> 
     16    </p> 
     17  <% end -%> 
     18</div> 
    1619 
    17   <p> 
    18     <label for="remember_me">Remember me:</label> 
    19     <%= check_box_tag 'remember_me' %> 
    20   </p> 
     20<div id="regular-login"> 
     21  <% form_tag session_path do -%> 
     22    <p> 
     23      <label for="email">Email</label><br/> 
     24      <%= text_field_tag 'email' %> 
     25    </p> 
    2126 
    22   <p> 
    23     <%= submit_tag 'Log in' %> 
    24     <%= link_to 'Cancel', root_path %> 
    25   </p> 
    26 <% end -%> 
     27    <p> 
     28      <label for="password">Password</label><br/> 
     29      <%= password_field_tag 'password' %> 
     30    </p> 
     31 
     32    <p> 
     33      <label for="remember_me">Remember me:</label> 
     34      <%= check_box_tag 'remember_me' %> 
     35    </p> 
     36 
     37    <p> 
     38      <%= submit_tag 'Log in' %> 
     39      <%= link_to 'Cancel', root_path %> 
     40    </p> 
     41  <% end -%> 
     42</div> 
    2743 
    2844<p> 
  • incubator/jumpstart/app/views/users/forgot_password.html.erb

    r251 r276  
    1010          <%= text_field_tag :email %> 
    1111        </p> 
    12  
    13         <%= submit_tag "Retrieve it" %> 
    1412         
    1513  <p> 
     14        <%= submit_tag "Retrieve it" %> 
    1615          <%= link_to 'Cancel', login_path %> 
    1716        </p> 
  • incubator/jumpstart/app/views/users/new.html.erb

    r271 r276  
    55 
    66<%= error_messages_for :user %> 
     7<div id="regular-signup"> 
     8  <% form_for @user do |f| -%> 
     9    <p> 
     10      <label for="username">Username</label><br/> 
     11      <%= f.text_field :username %> 
     12    </p> 
    713 
    8 <% form_for @user do |f| -%> 
    9   <p> 
    10     <label for="username">Username</label><br/> 
    11     <%= f.text_field :username %> 
    12   </p> 
     14    <p> 
     15      <label for="email">Email</label><br/> 
     16      <%= f.text_field :email %> 
     17    </p> 
    1318 
    14   <p> 
    15     <label for="email">Email</label><br/> 
    16     <%= f.text_field :email %> 
    17   </p> 
     19    <p> 
     20      <label for="password">Password</label><br/> 
     21      <%= f.password_field :password %> 
     22    </p> 
    1823 
    19   <p> 
    20     <label for="password">Password</label><br/> 
    21     <%= f.password_field :password %> 
    22   </p> 
     24    <p> 
     25      <label for="password_confirmation">Confirm Password</label><br/> 
     26      <%= f.password_field :password_confirmation %> 
     27    </p> 
    2328 
    24   <p> 
    25     <label for="password_confirmation">Confirm Password</label><br/> 
    26     <%= f.password_field :password_confirmation %> 
    27   </p> 
    28  
    29   <p> 
    30     <%= submit_tag 'Sign up' %> 
    31     <%= link_to 'Cancel', root_path %> 
    32   </p> 
    33 <% end -%> 
     29    <p> 
     30      <%= submit_tag 'Sign up' %> 
     31      <%= link_to 'Cancel', root_path %> 
     32    </p> 
     33  <% end -%> 
     34</div> 
    3435 
    3536<p> 
    36   Already have an account? <%= link_to 'Login now', login_path %><br /> 
     37  Already have an account or <span id="openid_url">OpenID</span>? <%= link_to 'Login now', login_path %><br /> 
    3738  <%= link_to 'Forgot your password?', forgot_password_path %> 
    3839</p> 
  • incubator/jumpstart/app/views/welcome/index.html.erb

    r271 r276  
    1414    <%= link_to 'Logout', logout_path %> 
    1515  <% else %> 
    16     Need an account? <%= link_to 'Sign up now', signup_path %><br /> 
    17     Already have an account? <%= link_to 'Login now', login_path %>   
     16    <%= link_to 'Login now', login_path %>   
    1817  <% end %> 
    1918</p> 
  • incubator/jumpstart/config/routes.rb

    r231 r276  
    11ActionController::Routing::Routes.draw do |map| 
    2   map.root                               :controller => 'welcome',  :action => 'index' 
    3   map.signup          'signup',          :controller => 'users',    :action => 'new' 
    4   map.activation      'activate',        :controller => 'users',    :action => 'activate' 
    5   map.login           'login',           :controller => 'sessions', :action => 'new' 
    6   map.logout          'logout',          :controller => 'sessions', :action => 'destroy' 
    7   map.forgot_password 'forgot_password', :controller => 'users',    :action => 'forgot_password' 
    8  
     2  map.root                                :controller => 'welcome',  :action => 'index' 
     3  map.signup           'signup',          :controller => 'users',    :action => 'new' 
     4  map.activation       'activate',        :controller => 'users',    :action => 'activate' 
     5  map.login            'login',           :controller => 'sessions', :action => 'new' 
     6  map.logout           'logout',          :controller => 'sessions', :action => 'destroy' 
     7  map.forgot_password  'forgot_password', :controller => 'users',    :action => 'forgot_password' 
     8  map.open_id_complete 'session',         :controller => "sessions", :action => "create", :requirements => { :method => :get } 
     9   
    910  map.resources :users 
    1011  map.resource  :session 
  • incubator/jumpstart/db/schema.rb

    r275 r276  
    1010# It's strongly recommended to check this file into your version control system. 
    1111 
    12 ActiveRecord::Schema.define(:version => 1) do 
     12ActiveRecord::Schema.define(:version => 2) do 
     13 
     14  create_table "open_id_authentication_associations", :force => true do |t| 
     15    t.binary  "server_url" 
     16    t.string  "handle" 
     17    t.binary  "secret" 
     18    t.integer "issued" 
     19    t.integer "lifetime" 
     20    t.string  "assoc_type" 
     21  end 
     22 
     23  create_table "open_id_authentication_nonces", :force => true do |t| 
     24    t.string  "nonce" 
     25    t.integer "created" 
     26  end 
     27 
     28  create_table "open_id_authentication_settings", :force => true do |t| 
     29    t.string "setting" 
     30    t.binary "value" 
     31  end 
    1332 
    1433  create_table "users", :force => true do |t| 
  • incubator/jumpstart/lib/authentable_entity.rb

    r270 r276  
    1111      attr_accessor :password 
    1212 
    13       validates_presence_of   :username 
    14       validates_length_of     :username, :within => 3..40 
    15       validates_uniqueness_of :username, :case_sensitive => false 
     13      validates_presence_of   :username, :if => :not_openid? 
     14      validates_length_of     :username, :within => 3..40, :if => :not_openid? 
     15      validates_uniqueness_of :username, :case_sensitive => false, :allow_nil => true 
    1616 
    17       validates_presence_of   :email 
    18       validates_uniqueness_of :email, :case_sensitive => false 
     17      validates_presence_of   :email, :if => :not_openid? 
     18      validates_uniqueness_of :email, :case_sensitive => false, :allow_nil => true 
    1919 
    2020      with_options :if => :password_required? do |new_password| 
     
    9191     
    9292    def password_required? 
    93       crypted_password.blank? || !password.blank? 
     93      not_openid? && (crypted_password.blank? || !password.blank?) 
     94    end 
     95 
     96    def not_openid? 
     97      identity_url.blank? 
    9498    end 
    9599     
  • incubator/jumpstart/test/functional/users_controller_test.rb

    r271 r276  
    111111     
    112112    put :update, :id => user.id 
    113     should.redirect_to edit_user_path user 
     113    should.redirect_to edit_user_path(user) 
    114114  end 
    115115 
  • incubator/jumpstart/vendor/gems/gems/ruby-openid-1.1.4/lib/openid/discovery.rb

    r275 r276  
    66begin 
    77  require 'rubygems' 
    8   require_gem 'ruby-yadis', ">=0.3.3"   
     8  gem 'ruby-yadis', ">=0.3.3"   
    99rescue LoadError 
    1010  require "yadis" 
  • incubator/jumpstart/vendor/gems/gems/ruby-openid-1.1.4/lib/openid/service.rb

    r275 r276  
    55rescue LoadError 
    66  require 'rubygems' 
    7   require_gem 'ruby-yadis' 
     7  gem 'ruby-yadis' 
    88end 
    99 
  • incubator/jumpstart/vendor/plugins

    • Property svn:externals changed from
      acts_as_authentable http://acts-as-authentable.googlecode.com/svn/trunk/acts_as_authentable
      test_spec_on_rails http://svn.techno-weenie.net/projects/plugins/test_spec_on_rails
      fixture_replacement2 http://thmadb.com/public_svn/plugins/fixture_replacement2
      seo_helper http://opensource.thinkrelevance.com/svn/incubator/seo_helper
      tarantula http://opensource.thinkrelevance.com/svn/rubygems/tarantula/trunk
      to
      acts_as_authentable http://acts-as-authentable.googlecode.com/svn/trunk/acts_as_authentable
      test_spec_on_rails http://svn.techno-weenie.net/projects/plugins/test_spec_on_rails
      fixture_replacement2 http://thmadb.com/public_svn/plugins/fixture_replacement2
      seo_helper http://opensource.thinkrelevance.com/svn/incubator/seo_helper
      tarantula http://opensource.thinkrelevance.com/svn/rubygems/tarantula/trunk
      open_id_authentication http://svn.rubyonrails.org/rails/plugins/open_id_authentication