Class: Merb::Authentication::Strategies::Basic::OpenID

Inherits:
Merb::Authentication::Strategy
  • Object
show all
Defined in:
lib/merb-auth-more/strategies/basic/openid.rb

Instance Method Summary collapse

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.



66
67
# File 'lib/merb-auth-more/strategies/basic/openid.rb', line 66

def customize_openid_request!(openid_request)
end

#find_user_by_identity_url(url) ⇒ Object

Overwrite this to support an ORM other than DataMapper



126
127
128
# File 'lib/merb-auth-more/strategies/basic/openid.rb', line 126

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

#on_cancel!(response) ⇒ Object



111
112
113
114
115
# File 'lib/merb-auth-more/strategies/basic/openid.rb', line 111

def on_cancel!(response)
  request.session.authentication.errors.clear!
  request.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



95
96
97
98
99
# File 'lib/merb-auth-more/strategies/basic/openid.rb', line 95

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



103
104
105
106
107
# File 'lib/merb-auth-more/strategies/basic/openid.rb', line 103

def on_setup_needed!(response)
  request.session.authentication.errors.clear!
  request.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



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/merb-auth-more/strategies/basic/openid.rb', line 80

def on_success!(response, sreg_response)
  if user = find_user_by_identity_url(response.identity_url)
    user
  else
    request.session[:'openid.url'] = response.identity_url
    required_reg_fields.each do |f|
      session[:"openid.#{f}"] = sreg_response.data[f] if sreg_response.data[f]
    end if sreg_response
    redirect!(Merb::Router.url(: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.



73
74
75
# File 'lib/merb-auth-more/strategies/basic/openid.rb', line 73

def openid_callback_url
  "#{request.protocol}://#{request.host}#{Merb::Router.url(:openid)}"
end

#openid_storeObject

Overwrite this method to set your store



133
134
135
# File 'lib/merb-auth-more/strategies/basic/openid.rb', line 133

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

#required_reg_fieldsObject



119
120
121
# File 'lib/merb-auth-more/strategies/basic/openid.rb', line 119

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

#run!Object



28
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
# File 'lib/merb-auth-more/strategies/basic/openid.rb', line 28

def run!
  if request.params[:'openid.mode']
    response = consumer.complete(request.send(:query_params), "#{request.protocol}://#{request.host}" + request.path)
    case response.status.to_s
    when 'success'
      sreg_response = ::OpenID::SReg::Response.from_success_response(response)
      result = on_success!(response, sreg_response)
      Merb.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[:openid_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}", 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