Project

General

Profile

Defect #11778 » redmine-openid_ax_requests_fix_google_yahoo_registration-DrewDahl-11778.patch

Andrew Dahl, 2012-12-16 14:01

View differences:

app/controllers/account_controller.rb
169 169
  end
170 170

  
171 171
  def open_id_authenticate(openid_url)
172
    authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => signin_url, :method => :post) do |result, identity_url, registration|
172
    authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email, #Original
173
							"http://axschema.org/namePerson/first", #Google
174
                                                        "http://axschema.org/namePerson/last", #Google
175
                                                        "http://schema.openid.net/contact/email", #Google
176
                                                        "http://axschema.org/namePerson", #Yahoo
177
                                                        "http://axschema.org/contact/email"], #Yahoo
178
                                          :return_to => signin_url, :method => :post) do |result, identity_url, registration|
173 179
      if result.successful?
174 180
        user = User.find_or_initialize_by_identity_url(identity_url)
175 181
        if user.new_record?
176 182
          # Self-registration off
177 183
          redirect_to(home_url) && return unless Setting.self_registration?
178 184

  
179
          # Create on the fly
180
          user.login = registration['nickname'] unless registration['nickname'].nil?
181
          user.mail = registration['email'] unless registration['email'].nil?
182
          user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil?
185
          # The open_id_authentication plugin returns registration as an OpenID::SReg::Response
186
          # The problem here is that most OpenID providres don't handle SReg anymore (e.g. Google)
187
          # So, we need to get an OpenID::AX::FetchResponse
188
          axreg = OpenID::AX::FetchResponse.from_success_response(request.env[Rack::OpenID::RESPONSE])
189

  
190
          # The previous call to get the FetchResponse object will be nil of nothing was returned from the provider
191
          # At the very least, it's one way to know we should fall back to SReg or use AX
192
          if axreg.nil?
193
            # Create on the fly
194
            user.login = registration['nickname'] unless registration['nickname'].nil?
195
            user.mail = registration['email'] unless registration['email'].nil?
196
            user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil?
197
          else # Use AX (Attribute Exchange)
198
            if axreg.get_single("http://schema.openid.net/contact/email").nil?
199
              user.mail = axreg.get_single("http://axschema.org/contact/email") unless axreg.get_single("http://axschema.org/contact/email").nil?
200
            else
201
              user.mail = axreg.get_single("http://schema.openid.net/contact/email")
202
            end
203

  
204
            user.login = user.mail.split("@")[0] unless user.mail.nil?
205

  
206
            if axreg.get_single("http://axschema.org/namePerson").nil?
207
              user.firstname = axreg.get_single("http://axschema.org/namePerson/first") unless axreg.get_single("http://axschema.org/namePerson/first").nil?
208
              user.lastname =  axreg.get_single("http://axschema.org/namePerson/last") unless axreg.get_single("http://axschema.org/namePerson/last").nil? 
209
            else
210
              user.firstname, user.lastname = axreg.get_single("http://axschema.org/namePerson").split(' ')
211
            end
212
          end
213

  
183 214
          user.random_password
184 215
          user.register
185 216

  
(1-1/2)