Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #5 from tomhughes/master
Browse files Browse the repository at this point in the history
The controller actions have been placed into a ControllerMethods module and there is now a proper Railtie for Rails 3.X applications.
  • Loading branch information
patricksrobertson committed Sep 18, 2011
2 parents 82379c8 + 94ec97b commit b93512b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 47 deletions.
2 changes: 1 addition & 1 deletion init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@

config.after_initialize do
OpenID::Util.logger = Rails.logger
ActionController::Base.send :include, OpenIdAuthentication
ActionController::Base.send :include, OpenIdAuthentication::ControllerMethods
end
109 changes: 63 additions & 46 deletions lib/open_id_authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ def self.store=(*store_option)

self.store = nil

if Rails.version >= '3'
class Railtie < ::Rails::Railtie
config.app_middleware.use OpenIdAuthentication

config.after_initialize do
OpenID::Util.logger = Rails.logger
end

ActiveSupport.on_load :action_controller do
ActionController::Base.send :include, ControllerMethods
#ActionController::Base.extend ControllerMethods
end
end
end

class Result
ERROR_MESSAGES = {
:missing => "Sorry, the OpenID server couldn't be found",
Expand Down Expand Up @@ -73,57 +88,59 @@ def message
end
end

protected
# The parameter name of "openid_identifier" is used rather than
# the Rails convention "open_id_identifier" because that's what
# the specification dictates in order to get browser auto-complete
# working across sites
def using_open_id?(identifier = nil) #:doc:
identifier ||= open_id_identifier
!identifier.blank? || request.env[Rack::OpenID::RESPONSE]
end
module ControllerMethods
protected
# The parameter name of "openid_identifier" is used rather than
# the Rails convention "open_id_identifier" because that's what
# the specification dictates in order to get browser auto-complete
# working across sites
def using_open_id?(identifier = nil) #:doc:
identifier ||= open_id_identifier
!identifier.blank? || request.env[Rack::OpenID::RESPONSE]
end

def authenticate_with_open_id(identifier = nil, options = {}, &block) #:doc:
identifier ||= open_id_identifier
def authenticate_with_open_id(identifier = nil, options = {}, &block) #:doc:
identifier ||= open_id_identifier

if request.env[Rack::OpenID::RESPONSE]
complete_open_id_authentication(&block)
else
begin_open_id_authentication(identifier, options, &block)
if request.env[Rack::OpenID::RESPONSE]
complete_open_id_authentication(&block)
else
begin_open_id_authentication(identifier, options, &block)
end
end
end

private
def open_id_identifier
params[:openid_identifier] || params[:openid_url]
end
private
def open_id_identifier
params[:openid_identifier] || params[:openid_url]
end

def begin_open_id_authentication(identifier, options = {})
options[:identifier] = identifier
value = Rack::OpenID.build_header(options)
response.headers[Rack::OpenID::AUTHENTICATE_HEADER] = value
head :unauthorized
end
def begin_open_id_authentication(identifier, options = {})
options[:identifier] = identifier
value = Rack::OpenID.build_header(options)
response.headers[Rack::OpenID::AUTHENTICATE_HEADER] = value
head :unauthorized
end

def complete_open_id_authentication
response = request.env[Rack::OpenID::RESPONSE]
identifier = response.display_identifier

case response.status
when OpenID::Consumer::SUCCESS
yield Result[:successful], identifier,
OpenID::SReg::Response.from_success_response(response),
OpenID::AX::FetchResponse.from_success_response(response)
when :missing
yield Result[:missing], identifier, nil
when :invalid
yield Result[:invalid], identifier, nil
when OpenID::Consumer::CANCEL
yield Result[:canceled], identifier, nil
when OpenID::Consumer::FAILURE
yield Result[:failed], identifier, nil
when OpenID::Consumer::SETUP_NEEDED
yield Result[:setup_needed], response.setup_url, nil
def complete_open_id_authentication
response = request.env[Rack::OpenID::RESPONSE]
identifier = response.display_identifier

case response.status
when OpenID::Consumer::SUCCESS
yield Result[:successful], identifier,
OpenID::SReg::Response.from_success_response(response),
OpenID::AX::FetchResponse.from_success_response(response)
when :missing
yield Result[:missing], identifier, nil
when :invalid
yield Result[:invalid], identifier, nil
when OpenID::Consumer::CANCEL
yield Result[:canceled], identifier, nil
when OpenID::Consumer::FAILURE
yield Result[:failed], identifier, nil
when OpenID::Consumer::SETUP_NEEDED
yield Result[:setup_needed], response.setup_url, nil
end
end
end
end
end

0 comments on commit b93512b

Please sign in to comment.