annotate .svn/pristine/bc/bcf601e4c2eab267682f5366299899e20f6d08d2.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

Merge from branch "live"
author Chris Cannam
date Tue, 09 Sep 2014 09:34:53 +0100
parents 038ba2d95de8
children
rev   line source
Chris@1296 1 OpenIdAuthentication
Chris@1296 2 ====================
Chris@1296 3
Chris@1296 4 Provides a thin wrapper around the excellent ruby-openid gem from JanRan. Be sure to install that first:
Chris@1296 5
Chris@1296 6 gem install ruby-openid
Chris@1296 7
Chris@1296 8 To understand what OpenID is about and how it works, it helps to read the documentation for lib/openid/consumer.rb
Chris@1296 9 from that gem.
Chris@1296 10
Chris@1296 11 The specification used is http://openid.net/specs/openid-authentication-2_0.html.
Chris@1296 12
Chris@1296 13
Chris@1296 14 Prerequisites
Chris@1296 15 =============
Chris@1296 16
Chris@1296 17 OpenID authentication uses the session, so be sure that you haven't turned that off.
Chris@1296 18
Chris@1296 19 Alternatively, you can use the file-based store, which just relies on on tmp/openids being present in RAILS_ROOT. But be aware that this store only works if you have a single application server. And it's not safe to use across NFS. It's recommended that you use the database store if at all possible. To use the file-based store, you'll also have to add this line to your config/environment.rb:
Chris@1296 20
Chris@1296 21 OpenIdAuthentication.store = :file
Chris@1296 22
Chris@1296 23 This particular plugin also relies on the fact that the authentication action allows for both POST and GET operations.
Chris@1296 24 If you're using RESTful authentication, you'll need to explicitly allow for this in your routes.rb.
Chris@1296 25
Chris@1296 26 The plugin also expects to find a root_url method that points to the home page of your site. You can accomplish this by using a root route in config/routes.rb:
Chris@1296 27
Chris@1296 28 map.root :controller => 'articles'
Chris@1296 29
Chris@1296 30 This plugin relies on Rails Edge revision 6317 or newer.
Chris@1296 31
Chris@1296 32
Chris@1296 33 Example
Chris@1296 34 =======
Chris@1296 35
Chris@1296 36 This example is just to meant to demonstrate how you could use OpenID authentication. You might well want to add
Chris@1296 37 salted hash logins instead of plain text passwords and other requirements on top of this. Treat it as a starting point,
Chris@1296 38 not a destination.
Chris@1296 39
Chris@1296 40 Note that the User model referenced in the simple example below has an 'identity_url' attribute. You will want to add the same or similar field to whatever
Chris@1296 41 model you are using for authentication.
Chris@1296 42
Chris@1296 43 Also of note is the following code block used in the example below:
Chris@1296 44
Chris@1296 45 authenticate_with_open_id do |result, identity_url|
Chris@1296 46 ...
Chris@1296 47 end
Chris@1296 48
Chris@1296 49 In the above code block, 'identity_url' will need to match user.identity_url exactly. 'identity_url' will be a string in the form of 'http://example.com' -
Chris@1296 50 If you are storing just 'example.com' with your user, the lookup will fail.
Chris@1296 51
Chris@1296 52 There is a handy method in this plugin called 'normalize_url' that will help with validating OpenID URLs.
Chris@1296 53
Chris@1296 54 OpenIdAuthentication.normalize_url(user.identity_url)
Chris@1296 55
Chris@1296 56 The above will return a standardized version of the OpenID URL - the above called with 'example.com' will return 'http://example.com/'
Chris@1296 57 It will also raise an InvalidOpenId exception if the URL is determined to not be valid.
Chris@1296 58 Use the above code in your User model and validate OpenID URLs before saving them.
Chris@1296 59
Chris@1296 60 config/routes.rb
Chris@1296 61
Chris@1296 62 map.root :controller => 'articles'
Chris@1296 63 map.resource :session
Chris@1296 64
Chris@1296 65
Chris@1296 66 app/views/sessions/new.erb
Chris@1296 67
Chris@1296 68 <% form_tag(session_url) do %>
Chris@1296 69 <p>
Chris@1296 70 <label for="name">Username:</label>
Chris@1296 71 <%= text_field_tag "name" %>
Chris@1296 72 </p>
Chris@1296 73
Chris@1296 74 <p>
Chris@1296 75 <label for="password">Password:</label>
Chris@1296 76 <%= password_field_tag %>
Chris@1296 77 </p>
Chris@1296 78
Chris@1296 79 <p>
Chris@1296 80 ...or use:
Chris@1296 81 </p>
Chris@1296 82
Chris@1296 83 <p>
Chris@1296 84 <label for="openid_identifier">OpenID:</label>
Chris@1296 85 <%= text_field_tag "openid_identifier" %>
Chris@1296 86 </p>
Chris@1296 87
Chris@1296 88 <p>
Chris@1296 89 <%= submit_tag 'Sign in', :disable_with => "Signing in&hellip;" %>
Chris@1296 90 </p>
Chris@1296 91 <% end %>
Chris@1296 92
Chris@1296 93 app/controllers/sessions_controller.rb
Chris@1296 94 class SessionsController < ApplicationController
Chris@1296 95 def create
Chris@1296 96 if using_open_id?
Chris@1296 97 open_id_authentication
Chris@1296 98 else
Chris@1296 99 password_authentication(params[:name], params[:password])
Chris@1296 100 end
Chris@1296 101 end
Chris@1296 102
Chris@1296 103
Chris@1296 104 protected
Chris@1296 105 def password_authentication(name, password)
Chris@1296 106 if @current_user = @account.users.authenticate(params[:name], params[:password])
Chris@1296 107 successful_login
Chris@1296 108 else
Chris@1296 109 failed_login "Sorry, that username/password doesn't work"
Chris@1296 110 end
Chris@1296 111 end
Chris@1296 112
Chris@1296 113 def open_id_authentication
Chris@1296 114 authenticate_with_open_id do |result, identity_url|
Chris@1296 115 if result.successful?
Chris@1296 116 if @current_user = @account.users.find_by_identity_url(identity_url)
Chris@1296 117 successful_login
Chris@1296 118 else
Chris@1296 119 failed_login "Sorry, no user by that identity URL exists (#{identity_url})"
Chris@1296 120 end
Chris@1296 121 else
Chris@1296 122 failed_login result.message
Chris@1296 123 end
Chris@1296 124 end
Chris@1296 125 end
Chris@1296 126
Chris@1296 127
Chris@1296 128 private
Chris@1296 129 def successful_login
Chris@1296 130 session[:user_id] = @current_user.id
Chris@1296 131 redirect_to(root_url)
Chris@1296 132 end
Chris@1296 133
Chris@1296 134 def failed_login(message)
Chris@1296 135 flash[:error] = message
Chris@1296 136 redirect_to(new_session_url)
Chris@1296 137 end
Chris@1296 138 end
Chris@1296 139
Chris@1296 140
Chris@1296 141
Chris@1296 142 If you're fine with the result messages above and don't need individual logic on a per-failure basis,
Chris@1296 143 you can collapse the case into a mere boolean:
Chris@1296 144
Chris@1296 145 def open_id_authentication
Chris@1296 146 authenticate_with_open_id do |result, identity_url|
Chris@1296 147 if result.successful? && @current_user = @account.users.find_by_identity_url(identity_url)
Chris@1296 148 successful_login
Chris@1296 149 else
Chris@1296 150 failed_login(result.message || "Sorry, no user by that identity URL exists (#{identity_url})")
Chris@1296 151 end
Chris@1296 152 end
Chris@1296 153 end
Chris@1296 154
Chris@1296 155
Chris@1296 156 Simple Registration OpenID Extension
Chris@1296 157 ====================================
Chris@1296 158
Chris@1296 159 Some OpenID Providers support this lightweight profile exchange protocol. See more: http://www.openidenabled.com/openid/simple-registration-extension
Chris@1296 160
Chris@1296 161 You can support it in your app by changing #open_id_authentication
Chris@1296 162
Chris@1296 163 def open_id_authentication(identity_url)
Chris@1296 164 # Pass optional :required and :optional keys to specify what sreg fields you want.
Chris@1296 165 # Be sure to yield registration, a third argument in the #authenticate_with_open_id block.
Chris@1296 166 authenticate_with_open_id(identity_url,
Chris@1296 167 :required => [ :nickname, :email ],
Chris@1296 168 :optional => :fullname) do |result, identity_url, registration|
Chris@1296 169 case result.status
Chris@1296 170 when :missing
Chris@1296 171 failed_login "Sorry, the OpenID server couldn't be found"
Chris@1296 172 when :invalid
Chris@1296 173 failed_login "Sorry, but this does not appear to be a valid OpenID"
Chris@1296 174 when :canceled
Chris@1296 175 failed_login "OpenID verification was canceled"
Chris@1296 176 when :failed
Chris@1296 177 failed_login "Sorry, the OpenID verification failed"
Chris@1296 178 when :successful
Chris@1296 179 if @current_user = @account.users.find_by_identity_url(identity_url)
Chris@1296 180 assign_registration_attributes!(registration)
Chris@1296 181
Chris@1296 182 if current_user.save
Chris@1296 183 successful_login
Chris@1296 184 else
Chris@1296 185 failed_login "Your OpenID profile registration failed: " +
Chris@1296 186 @current_user.errors.full_messages.to_sentence
Chris@1296 187 end
Chris@1296 188 else
Chris@1296 189 failed_login "Sorry, no user by that identity URL exists"
Chris@1296 190 end
Chris@1296 191 end
Chris@1296 192 end
Chris@1296 193 end
Chris@1296 194
Chris@1296 195 # registration is a hash containing the valid sreg keys given above
Chris@1296 196 # use this to map them to fields of your user model
Chris@1296 197 def assign_registration_attributes!(registration)
Chris@1296 198 model_to_registration_mapping.each do |model_attribute, registration_attribute|
Chris@1296 199 unless registration[registration_attribute].blank?
Chris@1296 200 @current_user.send("#{model_attribute}=", registration[registration_attribute])
Chris@1296 201 end
Chris@1296 202 end
Chris@1296 203 end
Chris@1296 204
Chris@1296 205 def model_to_registration_mapping
Chris@1296 206 { :login => 'nickname', :email => 'email', :display_name => 'fullname' }
Chris@1296 207 end
Chris@1296 208
Chris@1296 209 Attribute Exchange OpenID Extension
Chris@1296 210 ===================================
Chris@1296 211
Chris@1296 212 Some OpenID providers also support the OpenID AX (attribute exchange) protocol for exchanging identity information between endpoints. See more: http://openid.net/specs/openid-attribute-exchange-1_0.html
Chris@1296 213
Chris@1296 214 Accessing AX data is very similar to the Simple Registration process, described above -- just add the URI identifier for the AX field to your :optional or :required parameters. For example:
Chris@1296 215
Chris@1296 216 authenticate_with_open_id(identity_url,
Chris@1296 217 :required => [ :email, 'http://schema.openid.net/birthDate' ]) do |result, identity_url, registration|
Chris@1296 218
Chris@1296 219 This would provide the sreg data for :email, and the AX data for 'http://schema.openid.net/birthDate'
Chris@1296 220
Chris@1296 221
Chris@1296 222
Chris@1296 223 Copyright (c) 2007 David Heinemeier Hansson, released under the MIT license