Class: Rails::Authentication::Strategies::Basic::OpenID

Inherits:
Rails::Authentication::Strategy show all
Defined in:
lib/rails-auth/strategies/openid.rb

Instance Attribute Summary

Attributes inherited from Rails::Authentication::Strategy

#body, #params, #request, #status

Instance Method Summary collapse

Methods inherited from Rails::Authentication::Strategy

abstract!, abstract?, after, before, #cookies, #halt!, #halted?, #headers, inherited, #initialize, #redirect!, #redirected?, #session, #user_class

Constructor Details

This class inherits a constructor from Rails::Authentication::Strategy

Instance Method Details

#customize_openid_request!(openid_request) ⇒ Object

Overwrite this to add extra options to the OpenID request before it is made.

Examples:

request.return_to_args = 1 # remember_me=1 is added when returning from the OpenID provider.



70
71
# File 'lib/rails-auth/strategies/openid.rb', line 70

def customize_openid_request!(openid_request)
end

#find_user_by_identity_url(url) ⇒ Object

Overwrite this to properly support your user model



131
132
133
# File 'lib/rails-auth/strategies/openid.rb', line 131

def find_user_by_identity_url(url)
  user_class.first(:conditions => {:identity_url => url})
end

#on_cancel!(response) ⇒ Object



116
117
118
119
120
# File 'lib/rails-auth/strategies/openid.rb', line 116

def on_cancel!(response)
  session.authentication.errors.clear!
  session.authentication.errors.add(:openid, 'OpenID rejected our request')
  nil
end

#on_failure!(response) ⇒ Object

Overwrite the on_failure! method with the required behavior for failed logins



100
101
102
103
104
# File 'lib/rails-auth/strategies/openid.rb', line 100

def on_failure!(response)
  session.authentication.errors.clear!
  session.authentication.errors.add(:openid, 'OpenID verification failed, maybe the provider is down? Or the session timed out')
  nil
end

#on_setup_needed!(response) ⇒ Object



108
109
110
111
112
# File 'lib/rails-auth/strategies/openid.rb', line 108

def on_setup_needed!(response)
  session.authentication.errors.clear!
  session.authentication.errors.add(:openid, 'OpenID does not seem to be configured correctly')
  nil
end

#on_success!(response, sreg_response) ⇒ Object

Overwrite the on_success! method with the required behavior for successful logins



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rails-auth/strategies/openid.rb', line 84

def on_success!(response, sreg_response)
  if user = find_user_by_identity_url(response.identity_url)
    user
  else
    session[:openid] = {:identity_url => response.identity_url}
    required_reg_fields.each do |f|
      session[:openid][f.to_sym] = sreg_response.data[f] if sreg_response.data[f]
    end if sreg_response
    #redirect!(Rails::Router.url(:signup))
    redirect!("#{[request.protocol, request.host_with_port].join}/signup")
  end
end

#openid_callback_urlObject

Used to define the callback url for the openid provider. By default it is set to the named :openid route.



77
78
79
# File 'lib/rails-auth/strategies/openid.rb', line 77

def openid_callback_url
  "#{[request.protocol, request.host_with_port].join}/login"
end

#openid_storeObject

Overwrite this method to set your store



138
139
140
# File 'lib/rails-auth/strategies/openid.rb', line 138

def openid_store
  ::OpenID::Store::Filesystem.new("#{Rails.root}/tmp/openid")
end

#required_reg_fieldsObject



124
125
126
# File 'lib/rails-auth/strategies/openid.rb', line 124

def required_reg_fields
  ['nickname', 'email']
end

#run!Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rails-auth/strategies/openid.rb', line 29

def run!
  if request.params[:'openid.mode']
    params_with_path = params.reject { |key, value| request.path_parameters[key] }
    params_with_path.delete(:format)

    response = consumer.complete(params_with_path, "#{request.protocol}#{request.host_with_port}" + request.path)
    case response.status.to_s
    when 'success'
      sreg_response = ::OpenID::SReg::Response.from_success_response(response)
      result = on_success!(response, sreg_response)
      Rails.logger.info "\n\n#{result.inspect}\n\n"
      result
    when 'failure'
      on_failure!(response)
    when  'setup_needed'
      on_setup_needed!(response)
    when 'cancel'
      on_cancel!(response)
    end
  elsif identity_url = params[:identity_url]
    begin
      openid_request = consumer.begin(identity_url)
      openid_reg = ::OpenID::SReg::Request.new
      openid_reg.request_fields(required_reg_fields)
      openid_request.add_extension(openid_reg)
      customize_openid_request!(openid_request)
      redirect!(openid_request.redirect_url([request.protocol, request.host_with_port].join, openid_callback_url))
    rescue ::OpenID::OpenIDError => e
      request.session.authentication.errors.clear!
      request.session.authentication.errors.add(:openid, 'The OpenID verification failed')
      nil
    end
  end
end