From a6dfdbe9dfb0b1baad64558a643e3f51124d4fcd Mon Sep 17 00:00:00 2001 From: Andrew Dahl Date: Sun, 16 Dec 2012 06:48:15 -0600 Subject: [PATCH] Modified account controller to handle OpenID Attribute Exchange (AX) This is more meant to be an example as to what needs to be fixed in Redmine for Google/Yahoo/etc AX requests as SReg requests have been depricated. It's not the cleanest, but it's definitely functional. Auto-registration works with Google and Yahoo for sure. Registration for other providers hasn't been tested. After registration, it should continue working as normal. If using AX, usernames are generated by grabbing the first half of a user's e-mail address. --- app/controllers/account_controller.rb | 41 +++++++++++++++++++++++++++++---- 1 files changed, 36 insertions(+), 5 deletions(-) diff --git a/app/controllers/account_controller.rb b/app/controllers/account_controller.rb index 70ab851..92848d9 100644 --- a/app/controllers/account_controller.rb +++ b/app/controllers/account_controller.rb @@ -169,17 +169,48 @@ class AccountController < ApplicationController end def open_id_authenticate(openid_url) - authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => signin_url, :method => :post) do |result, identity_url, registration| + authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email, #Original + "http://axschema.org/namePerson/first", #Google + "http://axschema.org/namePerson/last", #Google + "http://schema.openid.net/contact/email", #Google + "http://axschema.org/namePerson", #Yahoo + "http://axschema.org/contact/email"], #Yahoo + :return_to => signin_url, :method => :post) do |result, identity_url, registration| if result.successful? user = User.find_or_initialize_by_identity_url(identity_url) if user.new_record? # Self-registration off redirect_to(home_url) && return unless Setting.self_registration? - # Create on the fly - user.login = registration['nickname'] unless registration['nickname'].nil? - user.mail = registration['email'] unless registration['email'].nil? - user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil? + # The open_id_authentication plugin returns registration as an OpenID::SReg::Response + # The problem here is that most OpenID providres don't handle SReg anymore (e.g. Google) + # So, we need to get an OpenID::AX::FetchResponse + axreg = OpenID::AX::FetchResponse.from_success_response(request.env[Rack::OpenID::RESPONSE]) + + # The previous call to get the FetchResponse object will be nil of nothing was returned from the provider + # At the very least, it's one way to know we should fall back to SReg or use AX + if axreg.nil? + # Create on the fly + user.login = registration['nickname'] unless registration['nickname'].nil? + user.mail = registration['email'] unless registration['email'].nil? + user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil? + else # Use AX (Attribute Exchange) + if axreg.get_single("http://schema.openid.net/contact/email").nil? + user.mail = axreg.get_single("http://axschema.org/contact/email") unless axreg.get_single("http://axschema.org/contact/email").nil? + else + user.mail = axreg.get_single("http://schema.openid.net/contact/email") + end + + user.login = user.mail.split("@")[0] unless user.mail.nil? + + if axreg.get_single("http://axschema.org/namePerson").nil? + user.firstname = axreg.get_single("http://axschema.org/namePerson/first") unless axreg.get_single("http://axschema.org/namePerson/first").nil? + user.lastname = axreg.get_single("http://axschema.org/namePerson/last") unless axreg.get_single("http://axschema.org/namePerson/last").nil? + else + user.firstname, user.lastname = axreg.get_single("http://axschema.org/namePerson").split(' ') + end + end + user.random_password user.register -- 1.7.1