Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

* Update generator messaging

* Consolidate ActiveRecord OpenID store into one file

git-svn-id: http://svn.eastmedia.com/svn/bantay/plugins/trunk/restful_open_id_authentication@40 9e3d647a-8015-0410-9c67-decdcf36f685
  • Loading branch information...
commit 1e47c6f81d03af60415178896d4230e9b04f38a9 1 parent 43034aa
matt authored
View
9 generators/open_id_authenticated/open_id_authenticated_generator.rb
@@ -162,9 +162,10 @@ def manifest
puts "Don't forget to:"
puts
puts " - add restful routes in config/routes.rb"
- puts " map.resource :#{model_controller_file_name}, :#{controller_file_name}"
- puts " map.activate '/activate/:activation_code', :controller => '#{model_controller_file_name}', :action => 'activate'"
+ puts " map.resources :#{model_controller_file_name}"
+ puts " map.resource :#{controller_file_name}, :collection => { :begin => :post, :complete => :get }"
if options[:include_activation]
+ puts " map.activate '/activate/:activation_code', :controller => '#{model_controller_file_name}', :action => 'activate'"
puts
puts " - add an observer to config/environment.rb"
puts " config.active_record.observers = :#{file_name}_observer"
@@ -173,7 +174,7 @@ def manifest
puts "Try these for some familiar login URLs if you like:"
puts
puts " map.signup '/signup', :controller => '#{model_controller_file_name}', :action => 'new'"
- puts " map.login '/login', :controller => '#{controller_file_name}', :action => 'new'"
+ puts " map.login '/login', :controller => '#{controller_file_name}', :action => 'new'"
puts " map.logout '/logout', :controller => '#{controller_file_name}', :action => 'destroy'"
puts
puts ("-" * 70)
@@ -182,7 +183,7 @@ def manifest
puts
puts ("-" * 70)
puts
- puts "Thanks for using restful_authentication"
+ puts "Thanks for using restful_open_id_authentication"
puts
puts "Don't forget to comment out the observer line in environment.rb"
puts " (This was optional so it may not even be there)"
View
7 init.rb
@@ -1,10 +1,9 @@
-# might using the ruby-openid gem
begin
- require 'rubygems'
+ gem 'ruby-openid'
+ require 'openid'
rescue LoadError
- nil
+ puts "Install the ruby-openid gem to enable OpenID support"
end
-require 'openid'
require 'open_id_store'
require 'controller_methods'
View
8 lib/association.rb
@@ -1,8 +0,0 @@
-module OpenIdConsumer
- class Association < ActiveRecord::Base
- set_table_name 'open_id_associations'
- def from_record
- OpenID::Association.new(handle, secret, issued, lifetime, assoc_type)
- end
- end
-end
View
86 lib/controller_methods.rb
@@ -14,6 +14,7 @@ def self.included(controller)
end
protected
+
def open_id_consumer
@open_id_consumer ||= OpenID::Consumer.new(
session[:open_id_session] ||= {},
@@ -34,6 +35,91 @@ def complete_open_id_auth
logger.debug @open_id_fields.inspect
logger.debug "***************** sreg params ***************"
end
+
+
+ def authenticate_with_open_id(identity_url)
+ @open_id_response = open_id_consumer.begin(identity_url)
+ yield(@open_id_response.status.to_sum)
+
+ # If the URL was unusable (either because of network conditions, a server error,
+ # or that the response returned was not an OpenID identity page), the library
+ # will return HTTP_FAILURE or PARSE_ERROR. Let the user know that the URL is unusable.
+ case open_id_response.status
+ when OpenID::SUCCESS
+ add_sreg_params!(@open_id_response)
+ redirect_to open_id_response.redirect_url((request.protocol + request.host_with_port + "/"), complete_session_url)
+ else
+ flash[:error] = "Unable to find OpenID server for <q>#{params[:open_id_url]}</q>"
+ render :action => :new
+ end
+ end
+
+ def complete
+ case open_id_response.status
+ when OpenID::FAILURE
+ # In the case of failure, if info is non-nil, it is the URL that we were verifying.
+ # We include it in the error message to help the user figure out what happened.
+ flash[:notice] = if open_id_response.identity_url
+ "Verification of #{open_id_response.identity_url} failed. "
+ else
+ "Verification failed. "
+ end
+ flash[:notice] += open_id_response.msg.to_s
+ when OpenID::SUCCESS
+ # Success means that the transaction completed without error. If info is nil,
+ # it means that the user cancelled the verification.
+ flash[:notice] = "You have successfully verified #{open_id_response.identity_url} as your identity."
+ if open_id_fields.any?
+ @user = User.find_by_open_id_url(open_id_response.identity_url)
+ @user ||= User.new(:open_id_url => open_id_response.identity_url)
+ @user.login = open_id_fields['nickname'] if open_id_fields['nickname']
+ @user.email = open_id_fields['email'] if open_id_fields['email']
+ if @user.save
+ self.current_user = @user
+ if params[:remember_me] == "1"
+ self.current_user.remember_me
+ cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at }
+ end
+ flash[:notice] = "You have successfully verified #{open_id_response.identity_url} as your identity."
+ return redirect_back_or_default('/')
+ else
+ flash[:notice] = @user.errors.full_messages.join('<br />')
+ render :action => 'new' and return
+ end
+ end
+ when OpenID::CANCEL
+ flash[:notice] = "Verification cancelled."
+ else
+ flash[:notice] = "Unknown response status: #{open_id_response.status}"
+ end
+ redirect_to :action => 'new'
+ end
+
+
+ def authenticate_with_open_id(identity_url)
+
+
+ case status
+ when :missing
+ failed_authentication "Sorry, the OpenID server couldn't be found"
+
+ when :canceled
+ failed_authentication "OpenID verification was canceled"
+
+ when :failed
+ failed_authentication "Sorry, the OpenID verification failed"
+
+ when :successful
+ if @current_user =
+ @account.users.find_by_identity_url(identity_url)
+ successful_authentication
+ else
+ failed_authentication "Sorry, no user by that identity URL exists"
+ end
+ end
+ end
+
+
def add_sreg_params!(openid_response)
open_id_consumer_options.keys.inject({}) do |params, key|
View
5 lib/nonce.rb
@@ -1,5 +0,0 @@
-module OpenIdConsumer
- class Nonce < ActiveRecord::Base
- set_table_name 'open_id_nonces'
- end
-end
View
22 lib/open_id_store.rb
@@ -1,7 +1,3 @@
-require 'association'
-require 'nonce'
-require 'setting'
-
module OpenIdConsumer
class OpenIdStore < OpenID::Store
def get_auth_key
@@ -68,7 +64,7 @@ def dumb?
false
end
- # not part of the api, but useful
+ # not part of the API, but useful
def gc
now = Time.now.to_i
@@ -81,4 +77,20 @@ def gc
assocs.each { |a| a.destroy if a.from_record.expired? } unless assocs.nil?
end
end
+
+ class Setting < ActiveRecord::Base
+ set_table_name 'open_id_settings'
+ validates_uniqueness_of :setting
+ end
+
+ class Nonce < ActiveRecord::Base
+ set_table_name 'open_id_nonces'
+ end
+
+ class Association < ActiveRecord::Base
+ set_table_name 'open_id_associations'
+ def from_record
+ OpenID::Association.new(handle, secret, issued, lifetime, assoc_type)
+ end
+ end
end
View
6 lib/setting.rb
@@ -1,6 +0,0 @@
-#module OpenIDConsumer
- class Setting < ActiveRecord::Base
- set_table_name 'open_id_settings'
- validates_uniqueness_of :setting
- end
-#end
Please sign in to comment.
Something went wrong with that request. Please try again.