Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
524 views
in Technique[技术] by (71.8m points)

authentication - Retrieve OpenID AX attributes from Google / Yahoo in Rails

I'm using the rails plugin open_id_authentication in my app. This works for MyOpenID, however authenticating with Google I can't get the email address as part of the required attributes.

From what I understand, Google ignores sreg attribute requests, and only listens to the AX schema for email address.

Here's my code:

     def open_id_authentication(openid_url)

       #google only responds to AX for email, so we must provide that also
       authenticate_with_open_id(openid_url, :required => [:nickname, :email, 'http://axschema.org/contact/email']) do |result, identity_url, registration|
        if result.successful?    
         @user = User.find_or_initialize_by_identity_url(identity_url)
         if @user.new_record?            
             unless registration['email'] || registration['http://axschema.org/contact/email']          
                 failed_login "Your OpenID provider didn't send us an email address."
                 return
              end

          #some providers (like Google) won't send a nick name.  We'll use email instead for those
          nick = registration['nickname']
          nick |= registration['email']
          nick |= registration['http://axschema.org/contact/email']

          email = registration['email'];
          email |= registration['http://axschema.org/contact/email']

          @user.login = nick
          @user.email = email
          @user.save(false)
     end
     self.current_user = @user
     successful_login
    else
       failed_login result.message
    end
   end

My understanding is that I submit the email address (both sreg and AX) as required and I should be able to pull them out of the registration instance that is passed along with the response.

When I log in with Google the email address is passed back as 't'.

Am I handling this incorrectly? How can I get the user's email address from Google? Will I have to jump through any other hoops to support Yahoo?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I ended up solving this one myself. It wasn't easy finding official docs on which AX schema URLs are supported.

Here's what I found:

Google will respond only to email address using the AX schema: http://schema.openid.net/contact/email

Yahoo will respond to alias & email using these AX schemas:

http://axschema.org/namePerson/friendly
http://axschema.org/contact/email

So I need to request basically all of the known AX schema URLs for email address and hope the provider sends it. /shrug


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...