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.



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

def customize_openid_request!(openid_request)
end

#find_user_by_identity_url(url) ⇒ Object

Overwrite this to support an ORM other than DataMapper



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

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

#on_cancel!(response) ⇒ Object



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

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



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

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



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

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



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

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.



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

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

#openid_storeObject

Overwrite this method to set your store



141
142
143
# File 'lib/merb-auth-more/strategies/basic/openid.rb', line 141

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

#optional_reg_fieldsObject



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

def optional_reg_fields
  ['fullname']
end

#required_reg_fieldsObject



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

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

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, true)
      openid_reg.request_fields(optional_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