Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 6331

Show
Ignore:
Timestamp:
03/05/07 01:10:16 (1 year ago)
Author:
david
Message:

Added normalize_url and applied it to all operations going through the plugin [DHH] Removed open_id? as the idea of using the same input box for both OpenID and username has died -- use using_open_id? instead (which checks for the presence of params[:openid_url] by default) [DHH]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/open_id_authentication/CHANGELOG

    r6324 r6331  
     1* Added normalize_url and applied it to all operations going through the plugin [DHH] 
     2 
     3* Removed open_id? as the idea of using the same input box for both OpenID and username has died -- use using_open_id? instead (which checks for the presence of params[:openid_url] by default) [DHH] 
     4 
    15* Added OpenIdAuthentication::Result to make it easier to deal with default situations where you don't care to do something particular for each error state [DHH] 
    26 
  • plugins/open_id_authentication/lib/open_id_authentication.rb

    r6324 r6331  
    33 
    44  class Result 
    5     MESSAGES = { 
     5    ERROR_MESSAGES = { 
    66      :missing    => "Sorry, the OpenID server couldn't be found", 
    77      :canceled   => "OpenID verification was canceled", 
    8       :failed     => "Sorry, the OpenID verification failed", 
    9       :successful => "OpenID authentication successful" 
     8      :failed     => "Sorry, the OpenID verification failed" 
    109    } 
    11      
    12     ERROR_STATES = [ :missing, :canceled, :failed ] 
    1310     
    1411    def self.[](code) 
     
    2825    end 
    2926     
    30     MESSAGES.keys.each { |state| define_method("#{state}?") { @code == state } } 
     27    ERROR_MESSAGES.keys.each { |state| define_method("#{state}?") { @code == state } } 
     28 
     29    def successful? 
     30      @code == :successful 
     31    end 
    3132 
    3233    def unsuccessful? 
    33       ERROR_STATES.include?(@code) 
     34      ERROR_MESSAGES.keys.include?(@code) 
    3435    end 
    3536     
    3637    def message 
    37       MESSAGES[@code] 
     38      ERROR_MESSAGES[@code] 
    3839    end 
    3940  end 
     
    4142 
    4243  protected 
    43     # OpenIDs are expected to begin with http:// or https:// 
    44     def open_id?(user_name) #:doc: 
    45       (Object.const_defined?(:OpenID) && user_name =~ /^https?:\/\//i) || params[:open_id_complete] 
     44    def normalize_url(url) 
     45      url = url.downcase 
     46     
     47      case url 
     48      when %r{^https?://[^/]+/[^/]*} 
     49        url # already normalized 
     50      when %r{^https?://[^/]+$} 
     51        url + "/" 
     52      when %r{^[.\d\w]+/.*$} 
     53        "http://" + url 
     54      when %r{^[.\d\w]+$} 
     55        "http://" + url + "/" 
     56      else 
     57        raise "Unable to normalize: #{url}" 
     58      end 
    4659    end 
    4760 
    48     def authenticate_with_open_id(identity_url, fields = {}, &block) #:doc: 
     61    # The parameter name of "openid_url" is used rather than the Rails convention "open_id_url" 
     62    # because that's what the specification dictates in order to get browser auto-complete working across sites 
     63    def using_open_id?(identity_url = params[:openid_url]) #:doc: 
     64      !identity_url.blank? || params[:open_id_complete] 
     65    end 
     66 
     67    def authenticate_with_open_id(identity_url = params[:openid_url], fields = {}, &block) #:doc: 
    4968      if params[:open_id_complete].nil? 
    50         begin_open_id_authentication(identity_url, fields, &block) 
     69        begin_open_id_authentication(normalize_url(identity_url), fields, &block) 
    5170      else 
    5271        complete_open_id_authentication(&block) 
    5372      end 
    5473    end 
     74 
    5575 
    5676  private 
     
    6989    def complete_open_id_authentication 
    7090      open_id_response = timeout_protection_from_identity_server { open_id_consumer.complete(params) } 
     91      identity_url     = normalize_url(open_id_response.identity_url) 
    7192 
    7293      case open_id_response.status 
    7394      when OpenID::CANCEL 
    74         yield Result[:canceled], open_id_response.identity_url, nil 
     95        yield Result[:canceled], identity_url, nil 
    7596      when OpenID::FAILURE 
    7697        logger.info "OpenID authentication failed: #{open_id_response.msg}" 
    77         yield Result[:failed], open_id_response.identity_url, nil 
     98        yield Result[:failed], identity_url, nil 
    7899      when OpenID::SUCCESS 
    79         yield Result[:successful], open_id_response.identity_url, open_id_response.extension_response('sreg') 
     100        yield Result[:successful], identity_url, open_id_response.extension_response('sreg') 
    80101      end       
    81102    end 
     
    98119    end 
    99120 
    100  
    101121    def timeout_protection_from_identity_server 
    102122      yield 
  • plugins/open_id_authentication/README

    r6324 r6331  
    88To understand what OpenID is about and how it works, it helps to read the documentation for lib/openid/consumer.rb 
    99from that gem. 
     10 
     11The specification used is http://openid.net/specs/openid-authentication-1_1.html (not the 2.0 draft). 
    1012 
    1113 
     
    3638 
    3739 
     40app/views/sessions/new.erb 
     41 
     42  <% form_tag(session_url) do %> 
     43    <p> 
     44      <label for="name">Username:</label> 
     45      <%= text_field_tag "name" %> 
     46    </p> 
     47 
     48    <p> 
     49      <label for="password">Password:</label> 
     50      <%= password_field_tag %> 
     51    </p> 
     52 
     53    <p> 
     54      ...or use: 
     55    </p> 
     56 
     57    <p> 
     58      <label for="openid_url">OpenID:</label> 
     59      <%= text_field_tag "openid_url" %> 
     60    </p> 
     61 
     62    <p> 
     63      <%= submit_tag 'Sign in', :disable_with => "Signing in&hellip;" %> 
     64    </p> 
     65  <% end %> 
     66 
    3867app/controllers/session_controller.rb 
    3968  class SessionController < ApplicationController 
    4069    def create 
    41       if open_id?(params[:name]) 
    42         open_id_authentication(params[:name]) 
     70      if using_open_id? 
     71        open_id_authentication 
    4372      else 
    4473        password_authentication(params[:name], params[:password]) 
     
    5685      end 
    5786 
    58       def open_id_authentication(identity_url) 
    59         authenticate_with_open_id(identity_url) do |result, identity_url| 
     87      def open_id_authentication 
     88        authenticate_with_open_id do |result, identity_url| 
    6089          case result 
    6190          when :missing 
     
    6998              successful_login 
    7099            else 
    71               failed_login "Sorry, no user by that identity URL exists" 
     100              failed_login "Sorry, no user by that identity URL exists (#{identity_url})") 
    72101            end 
    73102          end 
     
    93122you can collapse the case into a mere boolean: 
    94123 
    95     def open_id_authentication(identity_url) 
    96       authenticate_with_open_id(identity_url) do |result, identity_url| 
    97         if result.successful? 
    98           if @current_user = @account.users.find_by_identity_url(identity_url) 
    99             successful_login 
    100           else 
    101             failed_login "Sorry, no user by that identity URL exists" 
    102           end 
     124    def open_id_authentication 
     125      authenticate_with_open_id do |result, identity_url| 
     126        if result.successful? && @current_user = @account.users.find_by_identity_url(identity_url) 
     127          successful_login 
    103128        else 
    104           failed_login(result.message
     129          failed_login(result.message || "Sorry, no user by that identity URL exists (#{identity_url})"
    105130        end 
    106131      end